3d2e50b383394a9bc3f015c0daf85d1878af4ad4
[policy/apex-pdp.git] /
1 /*
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
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
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.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.model.basicmodel.handling;
22
23 import java.io.ByteArrayOutputStream;
24
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;
30
31 /**
32  * This class writes an Apex concept to a string.
33  *
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}
36  */
37 public class ApexModelStringWriter<C extends AxConcept> {
38     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexModelStringWriter.class);
39
40     // Should concepts being written to files be valid
41     private boolean validateFlag;
42
43     /**
44      * Constructor, set the validation flag.
45      *
46      * @param validateFlag Should validation be performed prior to output
47      */
48     public ApexModelStringWriter(final boolean validateFlag) {
49         this.validateFlag = validateFlag;
50     }
51
52     /**
53      * Write a concept to a string.
54      *
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
60      */
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");
63         
64         if (jsonFlag) {
65             return writeJSONString(concept, rootConceptClass);
66         }
67         else {
68             return concept.toString();
69         }
70     }
71
72     /**
73      * Write a concept to an XML string.
74      *
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
79      */
80     public String writeXMLString(final C concept, final Class<C> rootConceptClass) throws ApexException {
81         LOGGER.debug("running writeXMLString . . .");
82
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");
88
89         final ByteArrayOutputStream baOutputStream = new ByteArrayOutputStream();
90         try {
91             conceptWriter.write(concept, baOutputStream);
92             baOutputStream.close();
93         }
94         catch (final Exception e) {
95             LOGGER.warn("error writing XML string", e);
96             throw new ApexException("error writing XML string", e);
97         }
98
99         LOGGER.debug("ran writeXMLString");
100         return baOutputStream.toString();
101     }
102
103     /**
104      * Write a concept to a JSON string.
105      *
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
110      */
111     public String writeJSONString(final C concept, final Class<C> rootConceptClass) throws ApexException {
112         LOGGER.debug("running writeJSONString . . .");
113
114         final ApexModelWriter<C> conceptWriter = new ApexModelWriter<>(rootConceptClass);
115         conceptWriter.setJsonOutput(true);
116         conceptWriter.setValidateFlag(validateFlag);
117
118         final ByteArrayOutputStream baOutputStream = new ByteArrayOutputStream();
119         try {
120             conceptWriter.write(concept, baOutputStream);
121             baOutputStream.close();
122         }
123         catch (final Exception e) {
124             LOGGER.warn("error writing JSON string", e);
125             throw new ApexException("error writing JSON string", e);
126         }
127
128         LOGGER.debug("ran writeJSONString");
129         return baOutputStream.toString();
130     }
131
132     /**
133      * Checks if is validate flag.
134      *
135      * @return true, if checks if is validate flag
136      */
137     public boolean isValidateFlag() {
138         return validateFlag;
139     }
140
141     /**
142      * Sets the validate flag.
143      *
144      * @param validateFlag the validate flag
145      */
146     public void setValidateFlag(final boolean validateFlag) {
147         this.validateFlag = validateFlag;
148     }
149 }