2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019-2022 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.model.basicmodel.handling;
24 import java.io.ByteArrayOutputStream;
27 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
28 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
29 import org.onap.policy.common.utils.validation.Assertions;
30 import org.slf4j.ext.XLogger;
31 import org.slf4j.ext.XLoggerFactory;
34 * This class writes an Apex concept to a string.
36 * @author Liam Fallon (liam.fallon@ericsson.com)
37 * @param <C> the type of Apex concept to write to a string, must be a sub class of {@link AxConcept}
41 public class ApexModelStringWriter<C extends AxConcept> {
42 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexModelStringWriter.class);
44 // Should concepts being written to files be valid
45 private boolean validate;
48 * Constructor, set the validation flag.
50 * @param validate Should validation be performed prior to output
52 public ApexModelStringWriter(final boolean validate) {
53 this.validate = validate;
57 * Write a concept to a string.
59 * @param concept The concept to write
60 * @param rootConceptClass The concept class
61 * @return The string with the concept
62 * @throws ApexException thrown on errors
64 public String writeString(final C concept, final Class<C> rootConceptClass)
65 throws ApexException {
66 Assertions.argumentNotNull(concept, "concept may not be null");
68 return writeJsonString(concept, rootConceptClass);
72 * Write a concept to a JSON string.
74 * @param concept The concept to write
75 * @param rootConceptClass The concept class
76 * @return The string with the concept
77 * @throws ApexException thrown on errors
79 public String writeJsonString(final C concept, final Class<C> rootConceptClass) throws ApexException {
80 LOGGER.debug("running writeJSONString . . .");
82 final ApexModelWriter<C> conceptWriter = new ApexModelWriter<>(rootConceptClass);
83 conceptWriter.setValidate(validate);
85 try (var baOutputStream = new ByteArrayOutputStream()) {
86 conceptWriter.write(concept, baOutputStream);
87 return baOutputStream.toString();
88 } catch (final Exception e) {
89 LOGGER.warn("error writing JSON string", e);
90 throw new ApexException("error writing JSON string", e);