Add plugin to check coverage
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / util / genxsd / OxmFileProcessor.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.onap.aai.util.genxsd;
21
22 import org.onap.aai.exceptions.AAIException;
23 import org.onap.aai.introspection.Version;
24 import org.w3c.dom.*;
25 import org.xml.sax.InputSource;
26 import org.xml.sax.SAXException;
27
28 import javax.xml.XMLConstants;
29 import javax.xml.parsers.DocumentBuilder;
30 import javax.xml.parsers.DocumentBuilderFactory;
31 import javax.xml.parsers.ParserConfigurationException;
32 import java.io.File;
33 import java.io.IOException;
34 import java.io.StringReader;
35 import java.util.HashMap;
36 import java.util.Map;
37
38 public abstract class OxmFileProcessor {        
39         protected File oxmFile;
40         protected String xml;
41         protected Version v;
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
46
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()
53     {
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;
72     }
73
74
75         public OxmFileProcessor(File oxmFile, Version v) {
76                 super();
77                 this.oxmFile = oxmFile;
78                 this.v = v;
79         }
80
81         public OxmFileProcessor(String xml, Version v) {
82                 this.xml = xml;
83                 this.v = v;
84         }
85         protected void init() throws ParserConfigurationException, SAXException, IOException, AAIException {
86                 DocumentBuilder dBuilder = null;
87                 try {   
88                     DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
89                     dbFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
90                     dBuilder = dbFactory.newDocumentBuilder();
91                 } catch (ParserConfigurationException e) {
92                         throw e;
93                 }
94                 try {   
95                     if ( xml == null ) {
96                         doc = dBuilder.parse(oxmFile);
97                     } else {
98                             InputSource isInput = new InputSource(new StringReader(xml));
99                             doc = dBuilder.parse(isInput);
100                     }
101                 } catch (SAXException e) {
102                         throw e;
103                 } catch (IOException e) {
104                         throw e;
105                 }
106                 
107             NodeList bindingsNodes = doc.getElementsByTagName("xml-bindings");
108                 Element bindingElement;
109                 NodeList javaTypesNodes;
110                 Element javaTypesElement;
111                 
112                 if ( bindingsNodes == null || bindingsNodes.getLength() == 0 ) {
113                         throw new AAIException("OXM file error: missing <binding-nodes> in " + oxmFile);
114                 }           
115                 
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);
120                 }
121                 javaTypesElement = (Element) javaTypesNodes.item(0);
122
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 );
126                 }
127         }
128         public abstract String getDocumentHeader();
129         public abstract String process() throws AAIException;
130         
131         public String getXMLRootElementName(Element javaTypeElement) {
132                 String xmlRootElementName=null;
133                 NamedNodeMap attributes;
134                 
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();
141
142             String attrValue = attr.getNodeValue();
143             if ( attrName.equals("name"))
144                 xmlRootElementName = attrValue;
145                 }
146                 return xmlRootElementName;
147         }
148         
149         public String getXmlRootElementName( String javaTypeName )
150         {
151                 String attrName, attrValue;
152                 Attr attr;
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();
168
169                             attrValue = attr.getNodeValue();
170                             if ( attrName.equals("name"))
171                                 return (attrValue);
172                                 }
173                     }
174                         }
175                 }
176                 return null;
177         }
178         
179         public Element getJavaTypeElementSwagger( String javaTypeName )
180         {
181                 
182                 String attrName, attrValue;
183                 Attr attr;
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;
194                         }
195                 }
196                 return (Element) null;
197         }
198
199 }