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;
44 private static final int VALUE_NONE = 0;
45 private static final int VALUE_DESCRIPTION = 1;
46 private static final int VALUE_INDEXED_PROPS = 2;
47 private static final int VALUE_CONTAINER = 3;
48 private static final int VALUE_REQUIRES = 4;
50 public XSDElement(Element xmlElementElement, String maxOccurs) {
52 this.xmlElementElement = xmlElementElement;
53 this.maxOccurs = maxOccurs;
56 public XSDElement(Element xmlElementElement) {
58 this.xmlElementElement = xmlElementElement;
59 this.maxOccurs = null;
62 public String name() {
63 return this.getAttribute("name");
66 public Vector<String> getAddTypes(String version) {
67 String apiVersionFmt = "." + version + ".";
68 NamedNodeMap attributes = this.getAttributes();
69 Vector<String> addTypeV = new Vector<String>(); // vector of 1
70 String addType = null;
72 for ( int j = 0; j < attributes.getLength(); ++j ) {
73 Attr attr = (Attr) attributes.item(j);
74 String attrName = attr.getNodeName();
76 String attrValue = attr.getNodeValue();
77 if ( attrName.equals("type")) {
78 if ( attrValue.contains(apiVersionFmt) ) {
79 addType = attrValue.substring(attrValue.lastIndexOf('.')+1);
80 if ( addTypeV == null )
81 addTypeV = new Vector<String>();
82 addTypeV.add(addType);
90 public String getRequiresProperty() {
91 String elementAlsoRequiresProperty = null;
92 NodeList xmlPropNodes = this.getElementsByTagName("xml-properties");
94 for ( int i = 0; i < xmlPropNodes.getLength(); ++i ) {
95 Element xmlPropElement = (Element)xmlPropNodes.item(i);
96 if (! xmlPropElement.getParentNode().getAttributes().getNamedItem("name").getNodeValue().equals(this.xmlElementElement.getAttribute("name"))){
99 NodeList childNodes = xmlPropElement.getElementsByTagName("xml-property");
101 for ( int j = 0; j < childNodes.getLength(); ++j ) {
102 Element childElement = (Element)childNodes.item(j);
104 int useValue = VALUE_NONE;
105 NamedNodeMap attributes = childElement.getAttributes();
106 for ( int k = 0; k < attributes.getLength(); ++k ) {
107 Attr attr = (Attr) attributes.item(k);
108 String attrName = attr.getNodeName();
109 String attrValue = attr.getNodeValue();
110 if ( attrName == null || attrValue == null )
112 if ( attrName.equals("name") && attrValue.equals("requires")) {
113 useValue = VALUE_REQUIRES;
115 if ( useValue == VALUE_REQUIRES && attrName.equals("value")) {
116 elementAlsoRequiresProperty = attrValue;
121 return elementAlsoRequiresProperty;
124 public String getPathDescriptionProperty() {
125 String pathDescriptionProperty = null;
126 NodeList xmlPropNodes = this.getElementsByTagName("xml-properties");
128 for ( int i = 0; i < xmlPropNodes.getLength(); ++i ) {
129 Element xmlPropElement = (Element)xmlPropNodes.item(i);
130 if (! xmlPropElement.getParentNode().getAttributes().getNamedItem("name").getNodeValue().equals(this.xmlElementElement.getAttribute("name"))){
133 // This stopped working, replaced with above - should figure out why...
134 // if ( !xmlPropElement.getParentNode().isSameNode(this.xmlElementElement))
136 NodeList childNodes = xmlPropElement.getElementsByTagName("xml-property");
138 for ( int j = 0; j < childNodes.getLength(); ++j ) {
139 Element childElement = (Element)childNodes.item(j);
141 int useValue = VALUE_NONE;
142 NamedNodeMap attributes = childElement.getAttributes();
143 for ( int k = 0; k < attributes.getLength(); ++k ) {
144 Attr attr = (Attr) attributes.item(k);
145 String attrName = attr.getNodeName();
146 String attrValue = attr.getNodeValue();
147 if ( attrName == null || attrValue == null )
149 if ( attrName.equals("name") && attrValue.equals("description")) {
150 useValue = VALUE_DESCRIPTION;
152 if ( useValue == VALUE_DESCRIPTION && attrName.equals("value")) {
153 pathDescriptionProperty = attrValue;
158 return pathDescriptionProperty;
160 public Vector<String> getIndexedProps() {
161 Vector<String> indexedProps = new Vector<String>();
162 NodeList xmlPropNodes = this.getElementsByTagName("xml-properties");
164 for ( int i = 0; i < xmlPropNodes.getLength(); ++i ) {
165 Element xmlPropElement = (Element)xmlPropNodes.item(i);
166 if ( !xmlPropElement.getParentNode().isSameNode(this.xmlElementElement))
168 NodeList childNodes = xmlPropElement.getElementsByTagName("xml-property");
169 for ( int j = 0; j < childNodes.getLength(); ++j ) {
170 Element childElement = (Element)childNodes.item(j);
172 int useValue = VALUE_NONE;
173 NamedNodeMap attributes = childElement.getAttributes();
174 for ( int k = 0; k < attributes.getLength(); ++k ) {
175 Attr attr = (Attr) attributes.item(k);
176 String attrName = attr.getNodeName();
177 String attrValue = attr.getNodeValue();
178 if ( attrName == null || attrValue == null )
180 if ( attrValue.equals("indexedProps")) {
181 useValue = VALUE_INDEXED_PROPS;
183 if ( useValue == VALUE_INDEXED_PROPS && attrName.equals("value")) {
184 indexedProps = getIndexedProps( attrValue );
192 private static Vector<String> getIndexedProps( String attrValue )
194 if ( attrValue == null )
196 StringTokenizer st = new StringTokenizer( attrValue, ",");
197 if ( st.countTokens() == 0 )
199 Vector<String> result = new Vector<String>();
200 while ( st.hasMoreTokens()) {
201 result.add(st.nextToken());
206 public String getContainerProperty() {
207 NodeList xmlPropNodes = this.getElementsByTagName("xml-properties");
208 String container = null;
209 for ( int i = 0; i < xmlPropNodes.getLength(); ++i ) {
210 Element xmlPropElement = (Element)xmlPropNodes.item(i);
211 if ( !xmlPropElement.getParentNode().isSameNode(this.xmlElementElement))
213 NodeList childNodes = xmlPropElement.getElementsByTagName("xml-property");
214 for ( int j = 0; j < childNodes.getLength(); ++j ) {
215 Element childElement = (Element)childNodes.item(j);
217 int useValue = VALUE_NONE;
218 NamedNodeMap attributes = childElement.getAttributes();
219 for ( int k = 0; k < attributes.getLength(); ++k ) {
220 Attr attr = (Attr) attributes.item(k);
221 String attrName = attr.getNodeName();
222 String attrValue = attr.getNodeValue();
223 if ( attrName == null || attrValue == null )
225 if ( useValue == VALUE_CONTAINER && attrName.equals("value")) {
226 container = attrValue;
228 if ( attrValue.equals("container")) {
229 useValue = VALUE_CONTAINER;
237 public String getQueryParamYAML() {
238 StringBuffer sbParameter = new StringBuffer();
239 sbParameter.append((" - name: " + this.getAttribute("name") + "\n"));
240 sbParameter.append((" in: query\n"));
241 if ( this.getAttribute("description") != null && this.getAttribute("description").length() > 0 )
242 sbParameter.append((" description: " + this.getAttribute("description") + "\n"));
244 sbParameter.append((" description:\n"));
245 sbParameter.append((" required: false\n"));
246 if ( ("java.lang.String").equals(this.getAttribute("type")))
247 sbParameter.append(" type: string\n");
248 if ( ("java.lang.Long").equals(this.getAttribute("type"))) {
249 sbParameter.append(" type: integer\n");
250 sbParameter.append(" format: int64\n");
252 if ( ("java.lang.Integer").equals(this.getAttribute("type"))) {
253 sbParameter.append(" type: integer\n");
254 sbParameter.append(" format: int32\n");
256 if ( ("java.lang.Boolean").equals(this.getAttribute("type"))) {
257 sbParameter.append(" type: boolean\n");
259 return sbParameter.toString();
262 public String getPathParamYAML(String elementDescription) {
263 StringBuffer sbParameter = new StringBuffer();
264 sbParameter.append((" - name: " + this.getAttribute("name") + "\n"));
265 sbParameter.append((" in: path\n"));
266 if ( elementDescription != null && elementDescription.length() > 0 )
267 sbParameter.append((" description: " + elementDescription + "\n"));
268 sbParameter.append((" required: true\n"));
269 if ( ("java.lang.String").equals(this.getAttribute("type")))
270 sbParameter.append(" type: string\n");
271 if ( ("java.lang.Long").equals(this.getAttribute("type"))) {
272 sbParameter.append(" type: integer\n");
273 sbParameter.append(" format: int64\n");
275 if ( ("java.lang.Integer").equals(this.getAttribute("type"))) {
276 sbParameter.append(" type: integer\n");
277 sbParameter.append(" format: int32\n");
279 if ( ("java.lang.Boolean").equals(this.getAttribute("type"))) {
280 sbParameter.append(" type: boolean\n");
282 if(StringUtils.isNotBlank(this.getAttribute("name"))) {
283 sbParameter.append(" example: "+"__"+this.getAttribute("name").toUpperCase()+"__"+"\n");
285 return sbParameter.toString();
288 public String getHTMLElement(SchemaVersion v, boolean useAnnotation, HTMLfromOXM driver) {
289 StringBuffer sbElement = new StringBuffer();
290 String elementName = this.getAttribute("name");
291 String elementType = this.getAttribute("type");
292 String elementContainerType = this.getAttribute("container-type");
293 String elementIsRequired = this.getAttribute("required");
294 String addType = elementType.contains("." + v.toString() + ".") ? elementType.substring(elementType.lastIndexOf('.')+1) : null;
296 if ( addType != null ) {
297 sbElement.append(" <xs:element ref=\"tns:" + driver.getXmlRootElementName(addType)+"\"");
299 sbElement.append(" <xs:element name=\"" + elementName +"\"");
301 if ( elementType.equals("java.lang.String"))
302 sbElement.append(" type=\"xs:string\"");
303 if ( elementType.equals("java.lang.Long"))
304 sbElement.append(" type=\"xs:unsignedInt\"");
305 if ( elementType.equals("java.lang.Integer"))
306 sbElement.append(" type=\"xs:int\"");
307 if ( elementType.equals("java.lang.Boolean"))
308 sbElement.append(" type=\"xs:boolean\"");
309 if ( addType != null || elementType.startsWith("java.lang.") ) {
310 sbElement.append(" minOccurs=\"0\"");
312 if ( elementContainerType != null && elementContainerType.equals("java.util.ArrayList")) {
313 sbElement.append(" maxOccurs=\"" + maxOccurs + "\"");
316 String annotation = new XSDElement(xmlElementElement, maxOccurs).getHTMLAnnotation("field", " ");
317 sbElement.append(StringUtils.isNotEmpty(annotation) ? ">\n" : "");
318 sbElement.append(annotation);
319 sbElement.append(StringUtils.isNotEmpty(annotation) ? " </xs:element>\n" : "/>\n" );
321 sbElement.append("/>\n");
323 return this.getHTMLElementWrapper(sbElement.toString(), v, useAnnotation);
324 // return sbElement.toString();
327 public String getHTMLElementWrapper(String unwrappedElement, SchemaVersion v, boolean useAnnotation) {
329 NodeList childNodes = this.getElementsByTagName("xml-element-wrapper");
331 String xmlElementWrapper = null;
332 if ( childNodes.getLength() > 0 ) {
333 Element childElement = (Element)childNodes.item(0);
335 xmlElementWrapper = childElement == null ? null : childElement.getAttribute("name");
337 if(xmlElementWrapper == null)
338 return unwrappedElement;
340 StringBuffer sbElement = new StringBuffer();
341 sbElement.append(" <xs:element name=\"" + xmlElementWrapper +"\"");
342 String elementType = xmlElementElement.getAttribute("type");
343 String elementIsRequired = this.getAttribute("required");
344 String addType = elementType.contains("." + v.toString() + ".") ? elementType.substring(elementType.lastIndexOf('.')+1) : null;
346 if ( elementIsRequired == null || !elementIsRequired.equals("true")||addType != null) {
347 sbElement.append(" minOccurs=\"0\"");
349 sbElement.append(">\n");
350 sbElement.append(" <xs:complexType>\n");
352 XSDElement javaTypeElement = new XSDElement((Element)this.getParentNode(), maxOccurs);
353 sbElement.append(javaTypeElement.getHTMLAnnotation("class", " "));
355 sbElement.append(" <xs:sequence>\n");
356 sbElement.append(" ");
357 sbElement.append(unwrappedElement);
358 sbElement.append(" </xs:sequence>\n");
359 sbElement.append(" </xs:complexType>\n");
360 sbElement.append(" </xs:element>\n");
361 return sbElement.toString();
364 public String getHTMLAnnotation(String target, String indentation) {
365 StringBuffer sb = new StringBuffer();
366 List<String> metadata = new ArrayList<>();
367 if("true".equals(this.getAttribute("xml-key")) ) {
368 metadata.add("isKey=true");
371 NodeList xmlPropTags = this.getElementsByTagName("xml-properties");
372 Element xmlPropElement = null;
373 for ( int i = 0; i < xmlPropTags.getLength(); ++i ) {
374 xmlPropElement = (Element)xmlPropTags.item(i);
375 if (! xmlPropElement.getParentNode().getAttributes().getNamedItem("name").getNodeValue().equals(this.xmlElementElement.getAttribute("name")))
380 if(xmlPropElement != null) {
381 NodeList xmlProperties = xmlPropElement.getElementsByTagName("xml-property");
382 for (int i = 0; i < xmlProperties.getLength(); i++) {
383 Element item = (Element)xmlProperties.item(i);
384 String name = item.getAttribute("name");
385 String value = item.getAttribute("value");
386 if (name.equals("abstract")) {
388 } else if (name.equals("extends")) {
389 name = "extendsFrom";
391 metadata.add(name + "=\"" + value.replaceAll("&", "&") + "\"");
394 if(metadata.size() == 0) {
397 sb.append(indentation +"<xs:annotation>\r\n");
399 indentation + " <xs:appinfo>\r\n" +
400 indentation + " <annox:annotate target=\""+target+"\">@org.onap.aai.annotations.Metadata(" + Joiner.on(",").join(metadata) + ")</annox:annotate>\r\n" +
401 indentation + " </xs:appinfo>\r\n");
402 sb.append(indentation +"</xs:annotation>\r\n");
403 return sb.toString();
406 public String getTypePropertyYAML() {
407 StringBuffer sbProperties = new StringBuffer();
408 sbProperties.append(" " + this.getAttribute("name") + ":\n");
409 sbProperties.append(" type: ");
411 if ( ("java.lang.String").equals(this.getAttribute("type")))
412 sbProperties.append("string\n");
413 else if ( ("java.lang.Long").equals(this.getAttribute("type"))) {
414 sbProperties.append("integer\n");
415 sbProperties.append(" format: int64\n");
417 else if ( ("java.lang.Integer").equals(this.getAttribute("type"))){
418 sbProperties.append("integer\n");
419 sbProperties.append(" format: int32\n");
421 else if ( ("java.lang.Boolean").equals(this.getAttribute("type")))
422 sbProperties.append("boolean\n");
423 String attrDescription = this.getPathDescriptionProperty();
424 if ( attrDescription != null && attrDescription.length() > 0 )
425 sbProperties.append(" description: " + attrDescription + "\n");
426 String elementAlsoRequiresProperty=this.getRequiresProperty();
427 if ( StringUtils.isNotEmpty(elementAlsoRequiresProperty) )
428 sbProperties.append(" also requires: " + elementAlsoRequiresProperty + "\n");
429 return sbProperties.toString();
432 public boolean isStandardType()
434 switch ( this.getAttribute("type") ) {
435 case "java.lang.String":
436 case "java.lang.Long":
437 case "java.lang.Integer":
438 case"java.lang.Boolean":
445 public String getNodeName() {
446 return xmlElementElement.getNodeName();
450 public String getNodeValue() throws DOMException {
451 return xmlElementElement.getNodeValue();
455 public void setNodeValue(String nodeValue) throws DOMException {
456 xmlElementElement.setNodeValue(nodeValue);
460 public short getNodeType() {
461 return xmlElementElement.getNodeType();
465 public Node getParentNode() {
466 return xmlElementElement.getParentNode();
470 public NodeList getChildNodes() {
471 return xmlElementElement.getChildNodes();
475 public Node getFirstChild() {
476 return xmlElementElement.getFirstChild();
480 public Node getLastChild() {
481 return xmlElementElement.getLastChild();
485 public Node getPreviousSibling() {
486 return xmlElementElement.getPreviousSibling();
490 public Node getNextSibling() {
491 return xmlElementElement.getNextSibling();
495 public NamedNodeMap getAttributes() {
496 return xmlElementElement.getAttributes();
500 public Document getOwnerDocument() {
501 return xmlElementElement.getOwnerDocument();
505 public Node insertBefore(Node newChild, Node refChild) throws DOMException {
506 return xmlElementElement.insertBefore(newChild, refChild);
510 public Node replaceChild(Node newChild, Node oldChild) throws DOMException {
511 return xmlElementElement.replaceChild(newChild, oldChild);
515 public Node removeChild(Node oldChild) throws DOMException {
516 return xmlElementElement.removeChild(oldChild);
520 public Node appendChild(Node newChild) throws DOMException {
521 return xmlElementElement.appendChild(newChild);
525 public boolean hasChildNodes() {
526 return xmlElementElement.hasChildNodes();
530 public Node cloneNode(boolean deep) {
531 return xmlElementElement.cloneNode(deep);
535 public void normalize() {
536 xmlElementElement.normalize();
540 public boolean isSupported(String feature, String version) {
541 return xmlElementElement.isSupported(feature, version);
545 public String getNamespaceURI() {
546 return xmlElementElement.getNamespaceURI();
550 public String getPrefix() {
551 return xmlElementElement.getPrefix();
555 public void setPrefix(String prefix) throws DOMException {
556 xmlElementElement.setPrefix(prefix);
560 public String getLocalName() {
562 return xmlElementElement.getLocalName();
566 public boolean hasAttributes() {
567 return xmlElementElement.hasAttributes();
571 public String getBaseURI() {
572 return xmlElementElement.getBaseURI();
576 public short compareDocumentPosition(Node other) throws DOMException {
577 return xmlElementElement.compareDocumentPosition(other);
581 public String getTextContent() throws DOMException {
582 return xmlElementElement.getTextContent();
586 public void setTextContent(String textContent) throws DOMException {
587 xmlElementElement.setTextContent(textContent);
591 public boolean isSameNode(Node other) {
592 return xmlElementElement.isSameNode(other);
596 public String lookupPrefix(String namespaceURI) {
597 return xmlElementElement.lookupPrefix(namespaceURI);
601 public boolean isDefaultNamespace(String namespaceURI) {
602 return xmlElementElement.isDefaultNamespace(namespaceURI);
606 public String lookupNamespaceURI(String prefix) {
607 return xmlElementElement.lookupNamespaceURI(prefix);
611 public boolean isEqualNode(Node arg) {
612 return xmlElementElement.isEqualNode(arg);
616 public Object getFeature(String feature, String version) {
617 return xmlElementElement.getFeature(feature, version);
621 public Object setUserData(String key, Object data, UserDataHandler handler) {
622 return xmlElementElement.setUserData(key, data, handler);
626 public Object getUserData(String key) {
627 return xmlElementElement.getUserData(key);
631 public String getTagName() {
632 return xmlElementElement.getTagName();
636 public String getAttribute(String name) {
637 return xmlElementElement.getAttribute(name);
641 public void setAttribute(String name, String value) throws DOMException {
642 xmlElementElement.setAttribute(name, value);
646 public void removeAttribute(String name) throws DOMException {
647 xmlElementElement.removeAttribute(name);
651 public Attr getAttributeNode(String name) {
652 return xmlElementElement.getAttributeNode(name);
656 public Attr setAttributeNode(Attr newAttr) throws DOMException {
657 return xmlElementElement.setAttributeNode(newAttr);
661 public Attr removeAttributeNode(Attr oldAttr) throws DOMException {
662 return xmlElementElement.removeAttributeNode(oldAttr);
666 public NodeList getElementsByTagName(String name) {
667 return xmlElementElement.getElementsByTagName(name);
671 public String getAttributeNS(String namespaceURI, String localName) throws DOMException {
672 return xmlElementElement.getAttributeNS(namespaceURI, localName);
676 public void setAttributeNS(String namespaceURI, String qualifiedName, String value) throws DOMException {
677 xmlElementElement.setAttributeNS(namespaceURI, qualifiedName, value);
682 public void removeAttributeNS(String namespaceURI, String localName) throws DOMException {
683 xmlElementElement.removeAttributeNS(namespaceURI, localName);
687 public Attr getAttributeNodeNS(String namespaceURI, String localName) throws DOMException {
688 return xmlElementElement.getAttributeNodeNS(namespaceURI, localName);
692 public Attr setAttributeNodeNS(Attr newAttr) throws DOMException {
693 return xmlElementElement.setAttributeNodeNS(newAttr);
697 public NodeList getElementsByTagNameNS(String namespaceURI, String localName) throws DOMException {
698 return xmlElementElement.getElementsByTagNameNS(namespaceURI, localName);
702 public boolean hasAttribute(String name) {
703 return xmlElementElement.hasAttribute(name);
707 public boolean hasAttributeNS(String namespaceURI, String localName) throws DOMException {
708 return xmlElementElement.hasAttributeNS(namespaceURI, localName);
712 public TypeInfo getSchemaTypeInfo() {
713 return xmlElementElement.getSchemaTypeInfo();
717 public void setIdAttribute(String name, boolean isId) throws DOMException {
718 xmlElementElement.setIdAttribute(name, isId);
723 public void setIdAttributeNS(String namespaceURI, String localName, boolean isId) throws DOMException {
724 xmlElementElement.setIdAttributeNS(namespaceURI, localName, isId);
728 public void setIdAttributeNode(Attr idAttr, boolean isId) throws DOMException {
729 xmlElementElement.setIdAttributeNode(idAttr, isId);