2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019 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;
26 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
27 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
28 import org.onap.policy.common.utils.validation.Assertions;
29 import org.slf4j.ext.XLogger;
30 import org.slf4j.ext.XLoggerFactory;
33 * This class writes an Apex concept to a string.
35 * @author Liam Fallon (liam.fallon@ericsson.com)
36 * @param <C> the type of Apex concept to write to a string, must be a sub class of {@link AxConcept}
38 public class ApexModelStringWriter<C extends AxConcept> {
39 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexModelStringWriter.class);
41 // Should concepts being written to files be valid
42 private boolean validateFlag;
45 * Constructor, set the validation flag.
47 * @param validateFlag Should validation be performed prior to output
49 public ApexModelStringWriter(final boolean validateFlag) {
50 this.validateFlag = validateFlag;
54 * Write a concept to a string.
56 * @param concept The concept to write
57 * @param rootConceptClass The concept class
58 * @param jsonFlag writes JSON if true, and a generic string if false
59 * @return The string with the concept
60 * @throws ApexException thrown on errors
62 public String writeString(final C concept, final Class<C> rootConceptClass, final boolean jsonFlag)
63 throws ApexException {
64 Assertions.argumentNotNull(concept, "concept may not be null");
67 return writeJsonString(concept, rootConceptClass);
69 return concept.toString();
74 * Write a concept to an XML string.
76 * @param concept The concept to write
77 * @param rootConceptClass The concept class
78 * @return The string with the concept
79 * @throws ApexException thrown on errors
81 public String writeXmlString(final C concept, final Class<C> rootConceptClass) throws ApexException {
82 LOGGER.debug("running writeXMLString . . .");
84 final ApexModelWriter<C> conceptWriter = new ApexModelWriter<>(rootConceptClass);
85 conceptWriter.setValidateFlag(validateFlag);
86 conceptWriter.getCDataFieldSet().add("description");
87 conceptWriter.getCDataFieldSet().add("logic");
88 conceptWriter.getCDataFieldSet().add("uiLogic");
90 final ByteArrayOutputStream baOutputStream = new ByteArrayOutputStream();
92 conceptWriter.write(concept, baOutputStream);
93 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();
122 } catch (final Exception e) {
123 LOGGER.warn("error writing JSON string", e);
124 throw new ApexException("error writing JSON string", e);
127 LOGGER.debug("ran writeJSONString");
128 return baOutputStream.toString();
132 * Checks if is validate flag.
134 * @return true, if checks if is validate flag
136 public boolean isValidateFlag() {
141 * Sets the validate flag.
143 * @param validateFlag the validate flag
145 public void setValidateFlag(final boolean validateFlag) {
146 this.validateFlag = validateFlag;