2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020 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;
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;
37 * This class generates the XML model schema from the given Apex concept classes.
39 * @author Liam Fallon (liam.fallon@ericsson.com)
41 public class ApexSchemaGenerator {
42 // Get a reference to the logger
43 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexSchemaGenerator.class);
46 * A Main method to allow schema generation from the command line or from maven or scripts.
48 * @param args the command line arguments, usage is {@code ApexSchemaGenerator apex-root-class [schema-file-name]}
50 public static void main(final String[] args) {
51 PrintStream printStream = null;
53 if (args.length == 1) {
54 printStream = System.out;
55 } else if (args.length == 2) {
56 final File schemaFile = new File(args[1]);
59 schemaFile.getParentFile().mkdirs();
60 printStream = new PrintStream(schemaFile);
61 } catch (final Exception e) {
62 LOGGER.error("error on Apex schema output", e);
66 LOGGER.error("usage: ApexSchemaGenerator apex-root-class [schema-file-name]");
71 final String schema = new ApexSchemaGenerator().generate(args[0]);
74 printStream.println(schema);
80 * Generates the XML schema (XSD) for the Apex model described using JAXB annotations.
82 * @param rootClassName the name of the root class for schema generation
85 public String generate(final String rootClassName) {
86 JAXBContext jaxbContext;
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);
92 } catch (final JAXBException e) {
93 LOGGER.error("could not create JAXB context", e);
97 final ApexSchemaOutputResolver sor = new ApexSchemaOutputResolver();
99 jaxbContext.generateSchema(sor);
100 } catch (final IOException e) {
101 LOGGER.error("error generating the Apex schema (XSD) file", e);
105 String schemaString = sor.getSchema();
106 schemaString = fixForUnqualifiedBug(schemaString);
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
116 * @param schemaString The schema in which elements should be fixed
119 private String fixForUnqualifiedBug(final String schemaString) {
120 // Fix the "entry" element
121 String newSchemaString = schemaString.replace(
122 "<xs:element name=\"entry\" minOccurs=\"0\" maxOccurs=\"unbounded\">",
123 "<xs:element name=\"entry\" minOccurs=\"0\" maxOccurs=\"unbounded\" form=\"unqualified\">");
125 // Fix the "key" element
126 newSchemaString = newSchemaString.replace("<xs:element name=\"key\"",
127 "<xs:element name=\"key\" form=\"unqualified\"");
129 // Fix the "value" element
130 newSchemaString = newSchemaString.replace("<xs:element name=\"value\"",
131 "<xs:element name=\"value\" form=\"unqualified\"");
133 return newSchemaString;
137 * This inner class is used to receive the output of schema generation from the JAXB schema generator.
139 private class ApexSchemaOutputResolver extends SchemaOutputResolver {
140 private final StringWriter stringWriter = new StringWriter();
146 public Result createOutput(final String namespaceUri, final String suggestedFileName) throws IOException {
147 final StreamResult result = new StreamResult(stringWriter);
148 result.setSystemId(suggestedFileName);
153 * Get the schema from the string writer.
155 * @return the schema generated by JAXB
157 public String getSchema() {
158 return stringWriter.toString();