2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.onap.aai.util.genxsd;
22 import org.onap.aai.exceptions.AAIException;
23 import org.onap.aai.introspection.Version;
25 import org.xml.sax.InputSource;
26 import org.xml.sax.SAXException;
28 import javax.xml.XMLConstants;
29 import javax.xml.parsers.DocumentBuilder;
30 import javax.xml.parsers.DocumentBuilderFactory;
31 import javax.xml.parsers.ParserConfigurationException;
33 import java.io.IOException;
34 import java.io.StringReader;
35 import java.util.HashMap;
38 public abstract class OxmFileProcessor {
39 protected File oxmFile;
42 protected Document doc = null;
43 protected String apiVersion = null;
44 protected static int annotationsStartVersion = 9; // minimum version to support annotations in xsd
45 protected static int swaggerSupportStartsVersion = 7; // minimum version to support swagger documentation
47 protected String apiVersionFmt = null;
48 protected HashMap<String, String> generatedJavaType = new HashMap<String, String>();
49 protected HashMap<String, String> appliedPaths = new HashMap<String, String>();
50 protected NodeList javaTypeNodes = null;
51 protected static Map<String,String> javaTypeDefinitions = createJavaTypeDefinitions();
52 private static Map<String, String> createJavaTypeDefinitions()
54 StringBuffer aaiInternal = new StringBuffer();
55 StringBuffer nodes = new StringBuffer();
56 Map<String,String> javaTypeDefinitions = new HashMap<String, String>();
57 aaiInternal.append(" aai-internal:\n");
58 aaiInternal.append(" properties:\n");
59 aaiInternal.append(" property-name:\n");
60 aaiInternal.append(" type: string\n");
61 aaiInternal.append(" property-value:\n");
62 aaiInternal.append(" type: string\n");
63 // javaTypeDefinitions.put("aai-internal", aaiInternal.toString());
64 nodes.append(" nodes:\n");
65 nodes.append(" properties:\n");
66 nodes.append(" inventory-item-data:\n");
67 nodes.append(" type: array\n");
68 nodes.append(" items:\n");
69 nodes.append(" $ref: \"#/definitions/inventory-item-data\"\n");
70 javaTypeDefinitions.put("nodes", nodes.toString());
71 return javaTypeDefinitions;
75 public OxmFileProcessor(File oxmFile, Version v) {
77 this.oxmFile = oxmFile;
81 public OxmFileProcessor(String xml, Version v) {
85 protected void init() throws ParserConfigurationException, SAXException, IOException, AAIException {
86 DocumentBuilder dBuilder = null;
88 DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
89 dbFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
90 dBuilder = dbFactory.newDocumentBuilder();
91 } catch (ParserConfigurationException e) {
96 doc = dBuilder.parse(oxmFile);
98 InputSource isInput = new InputSource(new StringReader(xml));
99 doc = dBuilder.parse(isInput);
101 } catch (SAXException e) {
103 } catch (IOException e) {
107 NodeList bindingsNodes = doc.getElementsByTagName("xml-bindings");
108 Element bindingElement;
109 NodeList javaTypesNodes;
110 Element javaTypesElement;
112 if ( bindingsNodes == null || bindingsNodes.getLength() == 0 ) {
113 throw new AAIException("OXM file error: missing <binding-nodes> in " + oxmFile);
116 bindingElement = (Element) bindingsNodes.item(0);
117 javaTypesNodes = bindingElement.getElementsByTagName("java-types");
118 if ( javaTypesNodes.getLength() < 1 ) {
119 throw new AAIException("OXM file error: missing <binding-nodes><java-types> in " + oxmFile);
121 javaTypesElement = (Element) javaTypesNodes.item(0);
123 javaTypeNodes = javaTypesElement.getElementsByTagName("java-type");
124 if ( javaTypeNodes.getLength() < 1 ) {
125 throw new AAIException("OXM file error: missing <binding-nodes><java-types><java-type> in " + oxmFile );
128 public abstract String getDocumentHeader();
129 public abstract String process() throws AAIException;
131 public String getXMLRootElementName(Element javaTypeElement) {
132 String xmlRootElementName=null;
133 NamedNodeMap attributes;
135 NodeList valNodes = javaTypeElement.getElementsByTagName("xml-root-element");
136 Element valElement = (Element) valNodes.item(0);
137 attributes = valElement.getAttributes();
138 for ( int i = 0; i < attributes.getLength(); ++i ) {
139 Attr attr = (Attr) attributes.item(i);
140 String attrName = attr.getNodeName();
142 String attrValue = attr.getNodeValue();
143 if ( attrName.equals("name"))
144 xmlRootElementName = attrValue;
146 return xmlRootElementName;
149 public String getXmlRootElementName( String javaTypeName )
151 String attrName, attrValue;
153 Element javaTypeElement;
154 for ( int i = 0; i < javaTypeNodes.getLength(); ++ i ) {
155 javaTypeElement = (Element) javaTypeNodes.item(i);
156 NamedNodeMap attributes = javaTypeElement.getAttributes();
157 for ( int j = 0; j < attributes.getLength(); ++j ) {
158 attr = (Attr) attributes.item(j);
159 attrName = attr.getNodeName();
160 attrValue = attr.getNodeValue();
161 if ( attrName.equals("name") && attrValue.equals(javaTypeName)) {
162 NodeList valNodes = javaTypeElement.getElementsByTagName("xml-root-element");
163 Element valElement = (Element) valNodes.item(0);
164 attributes = valElement.getAttributes();
165 for ( int k = 0; k < attributes.getLength(); ++k ) {
166 attr = (Attr) attributes.item(k);
167 attrName = attr.getNodeName();
169 attrValue = attr.getNodeValue();
170 if ( attrName.equals("name"))
179 public Element getJavaTypeElementSwagger( String javaTypeName )
182 String attrName, attrValue;
184 Element javaTypeElement;
185 for ( int i = 0; i < javaTypeNodes.getLength(); ++ i ) {
186 javaTypeElement = (Element) javaTypeNodes.item(i);
187 NamedNodeMap attributes = javaTypeElement.getAttributes();
188 for ( int j = 0; j < attributes.getLength(); ++j ) {
189 attr = (Attr) attributes.item(j);
190 attrName = attr.getNodeName();
191 attrValue = attr.getNodeValue();
192 if ( attrName.equals("name") && attrValue.equals(javaTypeName))
193 return javaTypeElement;
196 return (Element) null;