Changes for checkstyle 8.32
[policy/apex-pdp.git] / model / basic-model / src / main / java / org / onap / policy / apex / model / basicmodel / handling / ApexModelStringWriter.java
1 /*
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.model.basicmodel.handling;
23
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;
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)
62                     throws ApexException {
63         Assertions.argumentNotNull(concept, "concept may not be null");
64
65         if (jsonFlag) {
66             return writeJsonString(concept, rootConceptClass);
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         } catch (final Exception e) {
94             LOGGER.warn("error writing XML string", e);
95             throw new ApexException("error writing XML string", e);
96         }
97
98         LOGGER.debug("ran writeXMLString");
99         return baOutputStream.toString();
100     }
101
102     /**
103      * Write a concept to a JSON string.
104      *
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
109      */
110     public String writeJsonString(final C concept, final Class<C> rootConceptClass) throws ApexException {
111         LOGGER.debug("running writeJSONString . . .");
112
113         final ApexModelWriter<C> conceptWriter = new ApexModelWriter<>(rootConceptClass);
114         conceptWriter.setJsonOutput(true);
115         conceptWriter.setValidateFlag(validateFlag);
116
117         final ByteArrayOutputStream baOutputStream = new ByteArrayOutputStream();
118         try {
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);
124         }
125
126         LOGGER.debug("ran writeJSONString");
127         return baOutputStream.toString();
128     }
129
130     /**
131      * Checks if is validate flag.
132      *
133      * @return true, if checks if is validate flag
134      */
135     public boolean isValidateFlag() {
136         return validateFlag;
137     }
138
139     /**
140      * Sets the validate flag.
141      *
142      * @param validateFlag the validate flag
143      */
144     public void setValidateFlag(final boolean validateFlag) {
145         this.validateFlag = validateFlag;
146     }
147 }