2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.model.basicmodel.handling;
23 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.apex.model.utilities.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) throws ApexException {
62 Assertions.argumentNotNull(concept, "concept may not be null");
65 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 final ByteArrayOutputStream baOutputStream = new ByteArrayOutputStream();
91 conceptWriter.write(concept, baOutputStream);
92 baOutputStream.close();
94 catch (final Exception e) {
95 LOGGER.warn("error writing XML string", e);
96 throw new ApexException("error writing XML string", e);
99 LOGGER.debug("ran writeXMLString");
100 return baOutputStream.toString();
104 * Write a concept to a JSON string.
106 * @param concept The concept to write
107 * @param rootConceptClass The concept class
108 * @return The string with the concept
109 * @throws ApexException thrown on errors
111 public String writeJSONString(final C concept, final Class<C> rootConceptClass) throws ApexException {
112 LOGGER.debug("running writeJSONString . . .");
114 final ApexModelWriter<C> conceptWriter = new ApexModelWriter<>(rootConceptClass);
115 conceptWriter.setJsonOutput(true);
116 conceptWriter.setValidateFlag(validateFlag);
118 final ByteArrayOutputStream baOutputStream = new ByteArrayOutputStream();
120 conceptWriter.write(concept, baOutputStream);
121 baOutputStream.close();
123 catch (final Exception e) {
124 LOGGER.warn("error writing JSON string", e);
125 throw new ApexException("error writing JSON string", e);
128 LOGGER.debug("ran writeJSONString");
129 return baOutputStream.toString();
133 * Checks if is validate flag.
135 * @return true, if checks if is validate flag
137 public boolean isValidateFlag() {
142 * Sets the validate flag.
144 * @param validateFlag the validate flag
146 public void setValidateFlag(final boolean validateFlag) {
147 this.validateFlag = validateFlag;