2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019-2021 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;
25 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
26 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
27 import org.onap.policy.common.utils.validation.Assertions;
28 import org.slf4j.ext.XLogger;
29 import org.slf4j.ext.XLoggerFactory;
32 * This class writes an Apex concept to a string.
34 * @author Liam Fallon (liam.fallon@ericsson.com)
35 * @param <C> the type of Apex concept to write to a string, must be a sub class of {@link AxConcept}
37 public class ApexModelStringWriter<C extends AxConcept> {
38 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexModelStringWriter.class);
40 // Should concepts being written to files be valid
41 private boolean validateFlag;
44 * Constructor, set the validation flag.
46 * @param validateFlag Should validation be performed prior to output
48 public ApexModelStringWriter(final boolean validateFlag) {
49 this.validateFlag = validateFlag;
53 * Write a concept to a string.
55 * @param concept The concept to write
56 * @param rootConceptClass The concept class
57 * @param jsonFlag writes JSON if true, and a generic string if false
58 * @return The string with the concept
59 * @throws ApexException thrown on errors
61 public String writeString(final C concept, final Class<C> rootConceptClass, final boolean jsonFlag)
62 throws ApexException {
63 Assertions.argumentNotNull(concept, "concept may not be null");
66 return writeJsonString(concept, rootConceptClass);
68 return concept.toString();
73 * Write a concept to an XML string.
75 * @param concept The concept to write
76 * @param rootConceptClass The concept class
77 * @return The string with the concept
78 * @throws ApexException thrown on errors
80 public String writeXmlString(final C concept, final Class<C> rootConceptClass) throws ApexException {
81 LOGGER.debug("running writeXMLString . . .");
83 final ApexModelWriter<C> conceptWriter = new ApexModelWriter<>(rootConceptClass);
84 conceptWriter.setValidateFlag(validateFlag);
85 conceptWriter.getCDataFieldSet().add("description");
86 conceptWriter.getCDataFieldSet().add("logic");
87 conceptWriter.getCDataFieldSet().add("uiLogic");
89 try (var baOutputStream = new ByteArrayOutputStream()) {
90 conceptWriter.write(concept, baOutputStream);
91 return baOutputStream.toString();
92 } catch (final Exception e) {
93 LOGGER.warn("error writing XML string", e);
94 throw new ApexException("error writing XML string", e);
100 * Write a concept to a JSON string.
102 * @param concept The concept to write
103 * @param rootConceptClass The concept class
104 * @return The string with the concept
105 * @throws ApexException thrown on errors
107 public String writeJsonString(final C concept, final Class<C> rootConceptClass) throws ApexException {
108 LOGGER.debug("running writeJSONString . . .");
110 final ApexModelWriter<C> conceptWriter = new ApexModelWriter<>(rootConceptClass);
111 conceptWriter.setJsonOutput(true);
112 conceptWriter.setValidateFlag(validateFlag);
114 try (var baOutputStream = new ByteArrayOutputStream()) {
115 conceptWriter.write(concept, baOutputStream);
116 return baOutputStream.toString();
117 } catch (final Exception e) {
118 LOGGER.warn("error writing JSON string", e);
119 throw new ApexException("error writing JSON string", e);
125 * Checks if is validate flag.
127 * @return true, if checks if is validate flag
129 public boolean isValidateFlag() {
134 * Sets the validate flag.
136 * @param validateFlag the validate flag
138 public void setValidateFlag(final boolean validateFlag) {
139 this.validateFlag = validateFlag;