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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.model.basicmodel.handling;
24 import java.io.IOException;
25 import java.io.PrintStream;
26 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;
34 import org.slf4j.ext.XLogger;
35 import org.slf4j.ext.XLoggerFactory;
38 * This class generates the XML model schema from the given Apex concept classes.
40 * @author Liam Fallon (liam.fallon@ericsson.com)
42 public class ApexSchemaGenerator {
43 // Get a reference to the logger
44 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexSchemaGenerator.class);
47 * A Main method to allow schema generation from the command line or from maven or scripts.
49 * @param args the command line arguments, usage is {@code ApexSchemaGenerator apex-root-class [schema-file-name]}
51 public static void main(final String[] args) {
52 PrintStream printStream = null;
54 if (args.length == 1) {
55 printStream = System.out;
56 } else if (args.length == 2) {
57 final File schemaFile = new File(args[1]);
60 schemaFile.getParentFile().mkdirs();
61 printStream = new PrintStream(schemaFile);
62 } catch (final Exception e) {
63 LOGGER.error("error on Apex schema output", e);
67 LOGGER.error("usage: ApexSchemaGenerator apex-root-class [schema-file-name]");
72 final String schema = new ApexSchemaGenerator().generate(args[0]);
75 printStream.println(schema);
81 * Generates the XML schema (XSD) for the Apex model described using JAXB annotations.
83 * @param rootClassName the name of the root class for schema generation
86 public String generate(final String rootClassName) {
87 JAXBContext jaxbContext;
89 jaxbContext = JAXBContext.newInstance(Class.forName(rootClassName));
90 } catch (final ClassNotFoundException e) {
91 LOGGER.error("could not create JAXB context, root class " + rootClassName + " not found", e);
93 } catch (final JAXBException e) {
94 LOGGER.error("could not create JAXB context", e);
98 final ApexSchemaOutputResolver sor = new ApexSchemaOutputResolver();
100 jaxbContext.generateSchema(sor);
101 } catch (final IOException e) {
102 LOGGER.error("error generating the Apex schema (XSD) file", e);
106 String schemaString = sor.getSchema();
107 schemaString = fixForUnqualifiedBug(schemaString);
113 * There is a bug in schema generation that does not specify the elements from Java Maps as being unqualified. This
114 * method "hacks" those elements in the schema to fix this, the elements being {@code entry}, {@code key}, and
117 * @param schemaString The schema in which elements should be fixed
120 private String fixForUnqualifiedBug(final String schemaString) {
121 // Fix the "entry" element
122 String newSchemaString = schemaString.replaceAll(
123 "<xs:element name=\"entry\" minOccurs=\"0\" maxOccurs=\"unbounded\">",
124 "<xs:element name=\"entry\" minOccurs=\"0\" maxOccurs=\"unbounded\" form=\"unqualified\">");
126 // Fix the "key" element
127 newSchemaString = newSchemaString.replaceAll("<xs:element name=\"key\"",
128 "<xs:element name=\"key\" form=\"unqualified\"");
130 // Fix the "value" element
131 newSchemaString = newSchemaString.replaceAll("<xs:element name=\"value\"",
132 "<xs:element name=\"value\" form=\"unqualified\"");
134 return newSchemaString;
138 * This inner class is used to receive the output of schema generation from the JAXB schema generator.
140 private class ApexSchemaOutputResolver extends SchemaOutputResolver {
141 private final StringWriter stringWriter = new StringWriter();
147 public Result createOutput(final String namespaceUri, final String suggestedFileName) throws IOException {
148 final StreamResult result = new StreamResult(stringWriter);
149 result.setSystemId(suggestedFileName);
154 * Get the schema from the string writer.
156 * @return the schema generated by JAXB
158 public String getSchema() {
159 return stringWriter.toString();