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 java.util.ArrayList;
23 import java.util.List;
24 import java.util.StringTokenizer;
25 import java.util.Vector;
27 import org.apache.commons.lang3.StringUtils;
28 import org.onap.aai.setup.SchemaVersion;
29 import org.w3c.dom.Attr;
30 import org.w3c.dom.DOMException;
31 import org.w3c.dom.Document;
32 import org.w3c.dom.Element;
33 import org.w3c.dom.NamedNodeMap;
34 import org.w3c.dom.Node;
35 import org.w3c.dom.NodeList;
36 import org.w3c.dom.TypeInfo;
37 import org.w3c.dom.UserDataHandler;
39 import com.google.common.base.Joiner;
41 public class XSDElement implements Element {
42 Element xmlElementElement;
43 private static final int VALUE_NONE = 0;
44 private static final int VALUE_DESCRIPTION = 1;
45 private static final int VALUE_INDEXED_PROPS = 2;
46 private static final int VALUE_CONTAINER = 3;
47 private static final int VALUE_REQUIRES = 4;
49 public XSDElement(Element xmlElementElement) {
51 this.xmlElementElement = xmlElementElement;
53 public String name() {
54 return this.getAttribute("name");
57 public Vector<String> getAddTypes(String version) {
58 String apiVersionFmt = "." + version + ".";
59 NamedNodeMap attributes = this.getAttributes();
60 Vector<String> addTypeV = new Vector<String>(); // vector of 1
61 String addType = null;
63 for ( int j = 0; j < attributes.getLength(); ++j ) {
64 Attr attr = (Attr) attributes.item(j);
65 String attrName = attr.getNodeName();
67 String attrValue = attr.getNodeValue();
68 if ( attrName.equals("type")) {
69 if ( attrValue.contains(apiVersionFmt) ) {
70 addType = attrValue.substring(attrValue.lastIndexOf('.')+1);
71 if ( addTypeV == null )
72 addTypeV = new Vector<String>();
73 addTypeV.add(addType);
81 public String getRequiresProperty() {
82 String elementAlsoRequiresProperty = null;
83 NodeList xmlPropNodes = this.getElementsByTagName("xml-properties");
85 for ( int i = 0; i < xmlPropNodes.getLength(); ++i ) {
86 Element xmlPropElement = (Element)xmlPropNodes.item(i);
87 if (! xmlPropElement.getParentNode().getAttributes().getNamedItem("name").getNodeValue().equals(this.xmlElementElement.getAttribute("name"))){
90 NodeList childNodes = xmlPropElement.getElementsByTagName("xml-property");
92 for ( int j = 0; j < childNodes.getLength(); ++j ) {
93 Element childElement = (Element)childNodes.item(j);
95 int useValue = VALUE_NONE;
96 NamedNodeMap attributes = childElement.getAttributes();
97 for ( int k = 0; k < attributes.getLength(); ++k ) {
98 Attr attr = (Attr) attributes.item(k);
99 String attrName = attr.getNodeName();
100 String attrValue = attr.getNodeValue();
101 if ( attrName == null || attrValue == null )
103 if ( attrName.equals("name") && attrValue.equals("requires")) {
104 useValue = VALUE_REQUIRES;
106 if ( useValue == VALUE_REQUIRES && attrName.equals("value")) {
107 elementAlsoRequiresProperty = attrValue;
112 return elementAlsoRequiresProperty;
115 public String getPathDescriptionProperty() {
116 String pathDescriptionProperty = null;
117 NodeList xmlPropNodes = this.getElementsByTagName("xml-properties");
119 for ( int i = 0; i < xmlPropNodes.getLength(); ++i ) {
120 Element xmlPropElement = (Element)xmlPropNodes.item(i);
121 if (! xmlPropElement.getParentNode().getAttributes().getNamedItem("name").getNodeValue().equals(this.xmlElementElement.getAttribute("name"))){
124 // This stopped working, replaced with above - should figure out why...
125 // if ( !xmlPropElement.getParentNode().isSameNode(this.xmlElementElement))
127 NodeList childNodes = xmlPropElement.getElementsByTagName("xml-property");
129 for ( int j = 0; j < childNodes.getLength(); ++j ) {
130 Element childElement = (Element)childNodes.item(j);
132 int useValue = VALUE_NONE;
133 NamedNodeMap attributes = childElement.getAttributes();
134 for ( int k = 0; k < attributes.getLength(); ++k ) {
135 Attr attr = (Attr) attributes.item(k);
136 String attrName = attr.getNodeName();
137 String attrValue = attr.getNodeValue();
138 if ( attrName == null || attrValue == null )
140 if ( attrName.equals("name") && attrValue.equals("description")) {
141 useValue = VALUE_DESCRIPTION;
143 if ( useValue == VALUE_DESCRIPTION && attrName.equals("value")) {
144 pathDescriptionProperty = attrValue;
149 return pathDescriptionProperty;
151 public Vector<String> getIndexedProps() {
152 Vector<String> indexedProps = new Vector<String>();
153 NodeList xmlPropNodes = this.getElementsByTagName("xml-properties");
155 for ( int i = 0; i < xmlPropNodes.getLength(); ++i ) {
156 Element xmlPropElement = (Element)xmlPropNodes.item(i);
157 if ( !xmlPropElement.getParentNode().isSameNode(this.xmlElementElement))
159 NodeList childNodes = xmlPropElement.getElementsByTagName("xml-property");
160 for ( int j = 0; j < childNodes.getLength(); ++j ) {
161 Element childElement = (Element)childNodes.item(j);
163 int useValue = VALUE_NONE;
164 NamedNodeMap attributes = childElement.getAttributes();
165 for ( int k = 0; k < attributes.getLength(); ++k ) {
166 Attr attr = (Attr) attributes.item(k);
167 String attrName = attr.getNodeName();
168 String attrValue = attr.getNodeValue();
169 if ( attrName == null || attrValue == null )
171 if ( attrValue.equals("indexedProps")) {
172 useValue = VALUE_INDEXED_PROPS;
174 if ( useValue == VALUE_INDEXED_PROPS && attrName.equals("value")) {
175 indexedProps = getIndexedProps( attrValue );
183 private static Vector<String> getIndexedProps( String attrValue )
185 if ( attrValue == null )
187 StringTokenizer st = new StringTokenizer( attrValue, ",");
188 if ( st.countTokens() == 0 )
190 Vector<String> result = new Vector<String>();
191 while ( st.hasMoreTokens()) {
192 result.add(st.nextToken());
197 public String getContainerProperty() {
198 NodeList xmlPropNodes = this.getElementsByTagName("xml-properties");
199 String container = null;
200 for ( int i = 0; i < xmlPropNodes.getLength(); ++i ) {
201 Element xmlPropElement = (Element)xmlPropNodes.item(i);
202 if ( !xmlPropElement.getParentNode().isSameNode(this.xmlElementElement))
204 NodeList childNodes = xmlPropElement.getElementsByTagName("xml-property");
205 for ( int j = 0; j < childNodes.getLength(); ++j ) {
206 Element childElement = (Element)childNodes.item(j);
208 int useValue = VALUE_NONE;
209 NamedNodeMap attributes = childElement.getAttributes();
210 for ( int k = 0; k < attributes.getLength(); ++k ) {
211 Attr attr = (Attr) attributes.item(k);
212 String attrName = attr.getNodeName();
213 String attrValue = attr.getNodeValue();
214 if ( attrName == null || attrValue == null )
216 if ( useValue == VALUE_CONTAINER && attrName.equals("value")) {
217 container = attrValue;
219 if ( attrValue.equals("container")) {
220 useValue = VALUE_CONTAINER;
228 public String getQueryParamYAML() {
229 StringBuffer sbParameter = new StringBuffer();
230 sbParameter.append((" - name: " + this.getAttribute("name") + "\n"));
231 sbParameter.append((" in: query\n"));
232 if ( this.getAttribute("description") != null && this.getAttribute("description").length() > 0 )
233 sbParameter.append((" description: " + this.getAttribute("description") + "\n"));
235 sbParameter.append((" description:\n"));
236 sbParameter.append((" required: false\n"));
237 if ( ("java.lang.String").equals(this.getAttribute("type")))
238 sbParameter.append(" type: string\n");
239 if ( ("java.lang.Long").equals(this.getAttribute("type"))) {
240 sbParameter.append(" type: integer\n");
241 sbParameter.append(" format: int64\n");
243 if ( ("java.lang.Integer").equals(this.getAttribute("type"))) {
244 sbParameter.append(" type: integer\n");
245 sbParameter.append(" format: int32\n");
247 if ( ("java.lang.Boolean").equals(this.getAttribute("type"))) {
248 sbParameter.append(" type: boolean\n");
250 return sbParameter.toString();
253 public String getPathParamYAML(String elementDescription) {
254 StringBuffer sbParameter = new StringBuffer();
255 sbParameter.append((" - name: " + this.getAttribute("name") + "\n"));
256 sbParameter.append((" in: path\n"));
257 if ( elementDescription != null && elementDescription.length() > 0 )
258 sbParameter.append((" description: " + elementDescription + "\n"));
259 sbParameter.append((" required: true\n"));
260 if ( ("java.lang.String").equals(this.getAttribute("type")))
261 sbParameter.append(" type: string\n");
262 if ( ("java.lang.Long").equals(this.getAttribute("type"))) {
263 sbParameter.append(" type: integer\n");
264 sbParameter.append(" format: int64\n");
266 if ( ("java.lang.Integer").equals(this.getAttribute("type"))) {
267 sbParameter.append(" type: integer\n");
268 sbParameter.append(" format: int32\n");
270 if ( ("java.lang.Boolean").equals(this.getAttribute("type"))) {
271 sbParameter.append(" type: boolean\n");
273 if(StringUtils.isNotBlank(this.getAttribute("name"))) {
274 sbParameter.append(" example: "+"__"+this.getAttribute("name").toUpperCase()+"__"+"\n");
276 return sbParameter.toString();
279 public String getHTMLElement(SchemaVersion v, boolean useAnnotation, HTMLfromOXM driver) {
280 StringBuffer sbElement = new StringBuffer();
281 String elementName = this.getAttribute("name");
282 String elementType = this.getAttribute("type");
283 String elementContainerType = this.getAttribute("container-type");
284 String elementIsRequired = this.getAttribute("required");
285 String addType = elementType.contains("." + v.toString() + ".") ? elementType.substring(elementType.lastIndexOf('.')+1) : null;
287 if ( addType != null ) {
288 sbElement.append(" <xs:element ref=\"tns:" + driver.getXmlRootElementName(addType)+"\"");
290 sbElement.append(" <xs:element name=\"" + elementName +"\"");
292 if ( elementType.equals("java.lang.String"))
293 sbElement.append(" type=\"xs:string\"");
294 if ( elementType.equals("java.lang.Long"))
295 sbElement.append(" type=\"xs:unsignedInt\"");
296 if ( elementType.equals("java.lang.Integer"))
297 sbElement.append(" type=\"xs:int\"");
298 if ( elementType.equals("java.lang.Boolean"))
299 sbElement.append(" type=\"xs:boolean\"");
300 if ( elementIsRequired == null || !elementIsRequired.equals("true")||addType != null) {
301 sbElement.append(" minOccurs=\"0\"");
303 if ( elementContainerType != null && elementContainerType.equals("java.util.ArrayList")) {
304 sbElement.append(" maxOccurs=\"5000\"");
307 String annotation = new XSDElement(xmlElementElement).getHTMLAnnotation("field", " ");
308 sbElement.append(StringUtils.isNotEmpty(annotation) ? ">\n" : "");
309 sbElement.append(annotation);
310 sbElement.append(StringUtils.isNotEmpty(annotation) ? " </xs:element>\n" : "/>\n" );
312 sbElement.append("/>\n");
314 return this.getHTMLElementWrapper(sbElement.toString(), v, useAnnotation);
315 // return sbElement.toString();
318 public String getHTMLElementWrapper(String unwrappedElement, SchemaVersion v, boolean useAnnotation) {
320 NodeList childNodes = this.getElementsByTagName("xml-element-wrapper");
322 String xmlElementWrapper = null;
323 if ( childNodes.getLength() > 0 ) {
324 Element childElement = (Element)childNodes.item(0);
326 xmlElementWrapper = childElement == null ? null : childElement.getAttribute("name");
328 if(xmlElementWrapper == null)
329 return unwrappedElement;
331 StringBuffer sbElement = new StringBuffer();
332 sbElement.append(" <xs:element name=\"" + xmlElementWrapper +"\"");
333 String elementType = xmlElementElement.getAttribute("type");
334 String elementIsRequired = this.getAttribute("required");
335 String addType = elementType.contains("." + v.toString() + ".") ? elementType.substring(elementType.lastIndexOf('.')+1) : null;
337 if ( elementIsRequired == null || !elementIsRequired.equals("true")||addType != null) {
338 sbElement.append(" minOccurs=\"0\"");
340 sbElement.append(">\n");
341 sbElement.append(" <xs:complexType>\n");
343 XSDElement javaTypeElement = new XSDElement((Element)this.getParentNode());
344 sbElement.append(javaTypeElement.getHTMLAnnotation("class", " "));
346 sbElement.append(" <xs:sequence>\n");
347 sbElement.append(" ");
348 sbElement.append(unwrappedElement);
349 sbElement.append(" </xs:sequence>\n");
350 sbElement.append(" </xs:complexType>\n");
351 sbElement.append(" </xs:element>\n");
352 return sbElement.toString();
355 public String getHTMLAnnotation(String target, String indentation) {
356 StringBuffer sb = new StringBuffer();
357 List<String> metadata = new ArrayList<>();
358 if("true".equals(this.getAttribute("xml-key")) ) {
359 metadata.add("isKey=true");
362 NodeList xmlPropTags = this.getElementsByTagName("xml-properties");
363 Element xmlPropElement = null;
364 for ( int i = 0; i < xmlPropTags.getLength(); ++i ) {
365 xmlPropElement = (Element)xmlPropTags.item(i);
366 if (! xmlPropElement.getParentNode().getAttributes().getNamedItem("name").getNodeValue().equals(this.xmlElementElement.getAttribute("name")))
371 if(xmlPropElement != null) {
372 NodeList xmlProperties = xmlPropElement.getElementsByTagName("xml-property");
373 for (int i = 0; i < xmlProperties.getLength(); i++) {
374 Element item = (Element)xmlProperties.item(i);
375 String name = item.getAttribute("name");
376 String value = item.getAttribute("value");
377 if (name.equals("abstract")) {
379 } else if (name.equals("extends")) {
380 name = "extendsFrom";
382 metadata.add(name + "=\"" + value.replaceAll("&", "&") + "\"");
385 if(metadata.size() == 0) {
388 sb.append(indentation +"<xs:annotation>\r\n");
390 indentation + " <xs:appinfo>\r\n" +
391 indentation + " <annox:annotate target=\""+target+"\">@org.onap.aai.annotations.Metadata(" + Joiner.on(",").join(metadata) + ")</annox:annotate>\r\n" +
392 indentation + " </xs:appinfo>\r\n");
393 sb.append(indentation +"</xs:annotation>\r\n");
394 return sb.toString();
397 public String getTypePropertyYAML() {
398 StringBuffer sbProperties = new StringBuffer();
399 sbProperties.append(" " + this.getAttribute("name") + ":\n");
400 sbProperties.append(" type: ");
402 if ( ("java.lang.String").equals(this.getAttribute("type")))
403 sbProperties.append("string\n");
404 else if ( ("java.lang.Long").equals(this.getAttribute("type"))) {
405 sbProperties.append("integer\n");
406 sbProperties.append(" format: int64\n");
408 else if ( ("java.lang.Integer").equals(this.getAttribute("type"))){
409 sbProperties.append("integer\n");
410 sbProperties.append(" format: int32\n");
412 else if ( ("java.lang.Boolean").equals(this.getAttribute("type")))
413 sbProperties.append("boolean\n");
414 String attrDescription = this.getPathDescriptionProperty();
415 if ( attrDescription != null && attrDescription.length() > 0 )
416 sbProperties.append(" description: " + attrDescription + "\n");
417 String elementAlsoRequiresProperty=this.getRequiresProperty();
418 if ( StringUtils.isNotEmpty(elementAlsoRequiresProperty) )
419 sbProperties.append(" also requires: " + elementAlsoRequiresProperty + "\n");
420 return sbProperties.toString();
423 public boolean isStandardType()
425 switch ( this.getAttribute("type") ) {
426 case "java.lang.String":
427 case "java.lang.Long":
428 case "java.lang.Integer":
429 case"java.lang.Boolean":
436 public String getNodeName() {
437 return xmlElementElement.getNodeName();
441 public String getNodeValue() throws DOMException {
442 return xmlElementElement.getNodeValue();
446 public void setNodeValue(String nodeValue) throws DOMException {
447 xmlElementElement.setNodeValue(nodeValue);
451 public short getNodeType() {
452 return xmlElementElement.getNodeType();
456 public Node getParentNode() {
457 return xmlElementElement.getParentNode();
461 public NodeList getChildNodes() {
462 return xmlElementElement.getChildNodes();
466 public Node getFirstChild() {
467 return xmlElementElement.getFirstChild();
471 public Node getLastChild() {
472 return xmlElementElement.getLastChild();
476 public Node getPreviousSibling() {
477 return xmlElementElement.getPreviousSibling();
481 public Node getNextSibling() {
482 return xmlElementElement.getNextSibling();
486 public NamedNodeMap getAttributes() {
487 return xmlElementElement.getAttributes();
491 public Document getOwnerDocument() {
492 return xmlElementElement.getOwnerDocument();
496 public Node insertBefore(Node newChild, Node refChild) throws DOMException {
497 return xmlElementElement.insertBefore(newChild, refChild);
501 public Node replaceChild(Node newChild, Node oldChild) throws DOMException {
502 return xmlElementElement.replaceChild(newChild, oldChild);
506 public Node removeChild(Node oldChild) throws DOMException {
507 return xmlElementElement.removeChild(oldChild);
511 public Node appendChild(Node newChild) throws DOMException {
512 return xmlElementElement.appendChild(newChild);
516 public boolean hasChildNodes() {
517 return xmlElementElement.hasChildNodes();
521 public Node cloneNode(boolean deep) {
522 return xmlElementElement.cloneNode(deep);
526 public void normalize() {
527 xmlElementElement.normalize();
531 public boolean isSupported(String feature, String version) {
532 return xmlElementElement.isSupported(feature, version);
536 public String getNamespaceURI() {
537 return xmlElementElement.getNamespaceURI();
541 public String getPrefix() {
542 return xmlElementElement.getPrefix();
546 public void setPrefix(String prefix) throws DOMException {
547 xmlElementElement.setPrefix(prefix);
551 public String getLocalName() {
553 return xmlElementElement.getLocalName();
557 public boolean hasAttributes() {
558 return xmlElementElement.hasAttributes();
562 public String getBaseURI() {
563 return xmlElementElement.getBaseURI();
567 public short compareDocumentPosition(Node other) throws DOMException {
568 return xmlElementElement.compareDocumentPosition(other);
572 public String getTextContent() throws DOMException {
573 return xmlElementElement.getTextContent();
577 public void setTextContent(String textContent) throws DOMException {
578 xmlElementElement.setTextContent(textContent);
582 public boolean isSameNode(Node other) {
583 return xmlElementElement.isSameNode(other);
587 public String lookupPrefix(String namespaceURI) {
588 return xmlElementElement.lookupPrefix(namespaceURI);
592 public boolean isDefaultNamespace(String namespaceURI) {
593 return xmlElementElement.isDefaultNamespace(namespaceURI);
597 public String lookupNamespaceURI(String prefix) {
598 return xmlElementElement.lookupNamespaceURI(prefix);
602 public boolean isEqualNode(Node arg) {
603 return xmlElementElement.isEqualNode(arg);
607 public Object getFeature(String feature, String version) {
608 return xmlElementElement.getFeature(feature, version);
612 public Object setUserData(String key, Object data, UserDataHandler handler) {
613 return xmlElementElement.setUserData(key, data, handler);
617 public Object getUserData(String key) {
618 return xmlElementElement.getUserData(key);
622 public String getTagName() {
623 return xmlElementElement.getTagName();
627 public String getAttribute(String name) {
628 return xmlElementElement.getAttribute(name);
632 public void setAttribute(String name, String value) throws DOMException {
633 xmlElementElement.setAttribute(name, value);
637 public void removeAttribute(String name) throws DOMException {
638 xmlElementElement.removeAttribute(name);
642 public Attr getAttributeNode(String name) {
643 return xmlElementElement.getAttributeNode(name);
647 public Attr setAttributeNode(Attr newAttr) throws DOMException {
648 return xmlElementElement.setAttributeNode(newAttr);
652 public Attr removeAttributeNode(Attr oldAttr) throws DOMException {
653 return xmlElementElement.removeAttributeNode(oldAttr);
657 public NodeList getElementsByTagName(String name) {
658 return xmlElementElement.getElementsByTagName(name);
662 public String getAttributeNS(String namespaceURI, String localName) throws DOMException {
663 return xmlElementElement.getAttributeNS(namespaceURI, localName);
667 public void setAttributeNS(String namespaceURI, String qualifiedName, String value) throws DOMException {
668 xmlElementElement.setAttributeNS(namespaceURI, qualifiedName, value);
673 public void removeAttributeNS(String namespaceURI, String localName) throws DOMException {
674 xmlElementElement.removeAttributeNS(namespaceURI, localName);
678 public Attr getAttributeNodeNS(String namespaceURI, String localName) throws DOMException {
679 return xmlElementElement.getAttributeNodeNS(namespaceURI, localName);
683 public Attr setAttributeNodeNS(Attr newAttr) throws DOMException {
684 return xmlElementElement.setAttributeNodeNS(newAttr);
688 public NodeList getElementsByTagNameNS(String namespaceURI, String localName) throws DOMException {
689 return xmlElementElement.getElementsByTagNameNS(namespaceURI, localName);
693 public boolean hasAttribute(String name) {
694 return xmlElementElement.hasAttribute(name);
698 public boolean hasAttributeNS(String namespaceURI, String localName) throws DOMException {
699 return xmlElementElement.hasAttributeNS(namespaceURI, localName);
703 public TypeInfo getSchemaTypeInfo() {
704 return xmlElementElement.getSchemaTypeInfo();
708 public void setIdAttribute(String name, boolean isId) throws DOMException {
709 xmlElementElement.setIdAttribute(name, isId);
714 public void setIdAttributeNS(String namespaceURI, String localName, boolean isId) throws DOMException {
715 xmlElementElement.setIdAttributeNS(namespaceURI, localName, isId);
719 public void setIdAttributeNode(Attr idAttr, boolean isId) throws DOMException {
720 xmlElementElement.setIdAttributeNode(idAttr, isId);