d5f34785b1a07a4281dae5594d5814b2cb65959f
[policy/apex-pdp.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020-2021 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.File;
25 import java.io.IOException;
26 import java.io.PrintStream;
27 import java.io.StringWriter;
28 import javax.xml.bind.JAXBContext;
29 import javax.xml.bind.JAXBException;
30 import javax.xml.bind.SchemaOutputResolver;
31 import javax.xml.transform.Result;
32 import javax.xml.transform.stream.StreamResult;
33 import org.slf4j.ext.XLogger;
34 import org.slf4j.ext.XLoggerFactory;
35
36 /**
37  * This class generates the XML model schema from the given Apex concept classes.
38  *
39  * @author Liam Fallon (liam.fallon@ericsson.com)
40  */
41 public class ApexSchemaGenerator {
42     // Get a reference to the logger
43     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexSchemaGenerator.class);
44
45     /**
46      * A Main method to allow schema generation from the command line or from maven or scripts.
47      *
48      * @param args the command line arguments, usage is {@code ApexSchemaGenerator apex-root-class [schema-file-name]}
49      */
50     public static void main(final String[] args) {
51         PrintStream printStream = null;
52
53         if (args.length == 1) {
54             printStream = System.out;
55         } else if (args.length == 2) {
56             final var schemaFile = new File(args[1]);
57
58             try {
59                 schemaFile.getParentFile().mkdirs();
60                 printStream = new PrintStream(schemaFile);
61             } catch (final Exception e) {
62                 LOGGER.error("error on Apex schema output", e);
63                 return;
64             }
65         } else {
66             LOGGER.error("usage: ApexSchemaGenerator apex-root-class [schema-file-name]");
67             return;
68         }
69
70         // Get the schema
71         final String schema = new ApexSchemaGenerator().generate(args[0]);
72
73         // Output the schema
74         printStream.println(schema);
75
76         printStream.close();
77     }
78
79     /**
80      * Generates the XML schema (XSD) for the Apex model described using JAXB annotations.
81      *
82      * @param rootClassName the name of the root class for schema generation
83      * @return The schema
84      */
85     public String generate(final String rootClassName) {
86         JAXBContext jaxbContext;
87         try {
88             jaxbContext = JAXBContext.newInstance(Class.forName(rootClassName));
89         } catch (final ClassNotFoundException e) {
90             LOGGER.error("could not create JAXB context, root class " + rootClassName + " not found", e);
91             return null;
92         } catch (final JAXBException e) {
93             LOGGER.error("could not create JAXB context", e);
94             return null;
95         }
96
97         final var sor = new ApexSchemaOutputResolver();
98         try {
99             jaxbContext.generateSchema(sor);
100         } catch (final IOException e) {
101             LOGGER.error("error generating the Apex schema (XSD) file", e);
102             return null;
103         }
104
105         var schemaString = sor.getSchema();
106         schemaString = fixForUnqualifiedBug(schemaString);
107
108         return schemaString;
109     }
110
111     /**
112      * There is a bug in schema generation that does not specify the elements from Java Maps as being unqualified. This
113      * method "hacks" those elements in the schema to fix this, the elements being {@code entry}, {@code key}, and
114      * {@code value}
115      *
116      * @param schemaString The schema in which elements should be fixed
117      * @return the string
118      */
119     private String fixForUnqualifiedBug(final String schemaString) {
120         // Fix the "entry" element
121         var newSchemaString = schemaString.replace(
122                         "<xs:element name=\"entry\" minOccurs=\"0\" maxOccurs=\"unbounded\">",
123                         "<xs:element name=\"entry\" minOccurs=\"0\" maxOccurs=\"unbounded\" form=\"unqualified\">");
124
125         // Fix the "key" element
126         newSchemaString = newSchemaString.replace("<xs:element name=\"key\"",
127                         "<xs:element name=\"key\" form=\"unqualified\"");
128
129         // Fix the "value" element
130         newSchemaString = newSchemaString.replace("<xs:element name=\"value\"",
131                         "<xs:element name=\"value\" form=\"unqualified\"");
132
133         return newSchemaString;
134     }
135
136     /**
137      * This inner class is used to receive the output of schema generation from the JAXB schema generator.
138      */
139     private class ApexSchemaOutputResolver extends SchemaOutputResolver {
140         private final StringWriter stringWriter = new StringWriter();
141
142         /**
143          * {@inheritDoc}.
144          */
145         @Override
146         public Result createOutput(final String namespaceUri, final String suggestedFileName) throws IOException {
147             final var result = new StreamResult(stringWriter);
148             result.setSystemId(suggestedFileName);
149             return result;
150         }
151
152         /**
153          * Get the schema from the string writer.
154          *
155          * @return the schema generated by JAXB
156          */
157         public String getSchema() {
158             return stringWriter.toString();
159         }
160     }
161 }