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;
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 final ByteArrayOutputStream baOutputStream = new ByteArrayOutputStream();
91 conceptWriter.write(concept, baOutputStream);
92 baOutputStream.close();
93 } catch (final Exception e) {
94 LOGGER.warn("error writing XML string", e);
95 throw new ApexException("error writing XML string", e);
98 LOGGER.debug("ran writeXMLString");
99 return baOutputStream.toString();
103 * Write a concept to a JSON string.
105 * @param concept The concept to write
106 * @param rootConceptClass The concept class
107 * @return The string with the concept
108 * @throws ApexException thrown on errors
110 public String writeJsonString(final C concept, final Class<C> rootConceptClass) throws ApexException {
111 LOGGER.debug("running writeJSONString . . .");
113 final ApexModelWriter<C> conceptWriter = new ApexModelWriter<>(rootConceptClass);
114 conceptWriter.setJsonOutput(true);
115 conceptWriter.setValidateFlag(validateFlag);
117 final ByteArrayOutputStream baOutputStream = new ByteArrayOutputStream();
119 conceptWriter.write(concept, baOutputStream);
120 baOutputStream.close();
121 } catch (final Exception e) {
122 LOGGER.warn("error writing JSON string", e);
123 throw new ApexException("error writing JSON string", e);
126 LOGGER.debug("ran writeJSONString");
127 return baOutputStream.toString();
131 * Checks if is validate flag.
133 * @return true, if checks if is validate flag
135 public boolean isValidateFlag() {
140 * Sets the validate flag.
142 * @param validateFlag the validate flag
144 public void setValidateFlag(final boolean validateFlag) {
145 this.validateFlag = validateFlag;