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 com.google.common.base.Joiner;
23 import org.apache.commons.lang3.StringUtils;
24 import org.onap.aai.introspection.Version;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.StringTokenizer;
30 import java.util.Vector;
32 public class XSDElement implements Element {
33 Element xmlElementElement;
34 private static final int VALUE_NONE = 0;
35 private static final int VALUE_DESCRIPTION = 1;
36 private static final int VALUE_INDEXED_PROPS = 2;
37 private static final int VALUE_CONTAINER = 3;
39 public XSDElement(Element xmlElementElement) {
41 this.xmlElementElement = xmlElementElement;
43 public String name() {
44 return this.getAttribute("name");
47 public Vector<String> getAddTypes(String version) {
48 String apiVersionFmt = "." + version + ".";
49 NamedNodeMap attributes = this.getAttributes();
50 Vector<String> addTypeV = new Vector<String>(); // vector of 1
51 String addType = null;
53 for ( int j = 0; j < attributes.getLength(); ++j ) {
54 Attr attr = (Attr) attributes.item(j);
55 String attrName = attr.getNodeName();
57 String attrValue = attr.getNodeValue();
58 if ( attrName.equals("type")) {
59 if ( attrValue.contains(apiVersionFmt) ) {
60 addType = attrValue.substring(attrValue.lastIndexOf('.')+1);
61 if ( addTypeV == null )
62 addTypeV = new Vector<String>();
63 addTypeV.add(addType);
71 public String getPathDescriptionProperty() {
72 String pathDescriptionProperty = null;
73 NodeList xmlPropNodes = this.getElementsByTagName("xml-properties");
75 for ( int i = 0; i < xmlPropNodes.getLength(); ++i ) {
76 Element xmlPropElement = (Element)xmlPropNodes.item(i);
77 if (! xmlPropElement.getParentNode().getAttributes().getNamedItem("name").getNodeValue().equals(this.xmlElementElement.getAttribute("name"))){
80 // This stopped working, replaced with above - should figure out why...
81 // if ( !xmlPropElement.getParentNode().isSameNode(this.xmlElementElement))
83 NodeList childNodes = xmlPropElement.getElementsByTagName("xml-property");
85 for ( int j = 0; j < childNodes.getLength(); ++j ) {
86 Element childElement = (Element)childNodes.item(j);
88 int useValue = VALUE_NONE;
89 NamedNodeMap attributes = childElement.getAttributes();
90 for ( int k = 0; k < attributes.getLength(); ++k ) {
91 Attr attr = (Attr) attributes.item(k);
92 String attrName = attr.getNodeName();
93 String attrValue = attr.getNodeValue();
94 if ( attrName == null || attrValue == null )
96 if ( attrName.equals("name") && attrValue.equals("description")) {
97 useValue = VALUE_DESCRIPTION;
99 if ( useValue == VALUE_DESCRIPTION && attrName.equals("value")) {
100 pathDescriptionProperty = attrValue;
105 return pathDescriptionProperty;
107 public Vector<String> getIndexedProps() {
108 Vector<String> indexedProps = new Vector<String>();
109 NodeList xmlPropNodes = this.getElementsByTagName("xml-properties");
111 for ( int i = 0; i < xmlPropNodes.getLength(); ++i ) {
112 Element xmlPropElement = (Element)xmlPropNodes.item(i);
113 if ( !xmlPropElement.getParentNode().isSameNode(this.xmlElementElement))
115 NodeList childNodes = xmlPropElement.getElementsByTagName("xml-property");
116 for ( int j = 0; j < childNodes.getLength(); ++j ) {
117 Element childElement = (Element)childNodes.item(j);
119 int useValue = VALUE_NONE;
120 NamedNodeMap attributes = childElement.getAttributes();
121 for ( int k = 0; k < attributes.getLength(); ++k ) {
122 Attr attr = (Attr) attributes.item(k);
123 String attrName = attr.getNodeName();
124 String attrValue = attr.getNodeValue();
125 if ( attrName == null || attrValue == null )
127 if ( attrValue.equals("indexedProps")) {
128 useValue = VALUE_INDEXED_PROPS;
130 if ( useValue == VALUE_INDEXED_PROPS && attrName.equals("value")) {
131 indexedProps = getIndexedProps( attrValue );
139 private static Vector<String> getIndexedProps( String attrValue )
141 if ( attrValue == null )
143 StringTokenizer st = new StringTokenizer( attrValue, ",");
144 if ( st.countTokens() == 0 )
146 Vector<String> result = new Vector<String>();
147 while ( st.hasMoreTokens()) {
148 result.add(st.nextToken());
153 public String getContainerProperty() {
154 NodeList xmlPropNodes = this.getElementsByTagName("xml-properties");
155 String container = null;
156 for ( int i = 0; i < xmlPropNodes.getLength(); ++i ) {
157 Element xmlPropElement = (Element)xmlPropNodes.item(i);
158 if ( !xmlPropElement.getParentNode().isSameNode(this.xmlElementElement))
160 NodeList childNodes = xmlPropElement.getElementsByTagName("xml-property");
161 for ( int j = 0; j < childNodes.getLength(); ++j ) {
162 Element childElement = (Element)childNodes.item(j);
164 int useValue = VALUE_NONE;
165 NamedNodeMap attributes = childElement.getAttributes();
166 for ( int k = 0; k < attributes.getLength(); ++k ) {
167 Attr attr = (Attr) attributes.item(k);
168 String attrName = attr.getNodeName();
169 String attrValue = attr.getNodeValue();
170 if ( attrName == null || attrValue == null )
172 if ( useValue == VALUE_CONTAINER && attrName.equals("value")) {
173 container = attrValue;
175 if ( attrValue.equals("container")) {
176 useValue = VALUE_CONTAINER;
184 public String getQueryParamYAML() {
185 StringBuffer sbParameter = new StringBuffer();
186 sbParameter.append((" - name: " + this.getAttribute("name") + "\n"));
187 sbParameter.append((" in: query\n"));
188 if ( this.getAttribute("description") != null && this.getAttribute("description").length() > 0 )
189 sbParameter.append((" description: " + this.getAttribute("description") + "\n"));
191 sbParameter.append((" description:\n"));
192 sbParameter.append((" required: false\n"));
193 if ( ("java.lang.String").equals(this.getAttribute("type")))
194 sbParameter.append(" type: string\n");
195 if ( ("java.lang.Long").equals(this.getAttribute("type"))) {
196 sbParameter.append(" type: integer\n");
197 sbParameter.append(" format: int64\n");
199 if ( ("java.lang.Integer").equals(this.getAttribute("type"))) {
200 sbParameter.append(" type: integer\n");
201 sbParameter.append(" format: int32\n");
203 if ( ("java.lang.Boolean").equals(this.getAttribute("type"))) {
204 sbParameter.append(" type: boolean\n");
206 return sbParameter.toString();
209 public String getPathParamYAML(String elementDescription) {
210 StringBuffer sbParameter = new StringBuffer();
211 sbParameter.append((" - name: " + this.getAttribute("name") + "\n"));
212 sbParameter.append((" in: path\n"));
213 if ( elementDescription != null && elementDescription.length() > 0 )
214 sbParameter.append((" description: " + elementDescription + "\n"));
215 sbParameter.append((" required: true\n"));
216 if ( ("java.lang.String").equals(this.getAttribute("type")))
217 sbParameter.append(" type: string\n");
218 if ( ("java.lang.Long").equals(this.getAttribute("type"))) {
219 sbParameter.append(" type: integer\n");
220 sbParameter.append(" format: int64\n");
222 if ( ("java.lang.Integer").equals(this.getAttribute("type"))) {
223 sbParameter.append(" type: integer\n");
224 sbParameter.append(" format: int32\n");
226 if ( ("java.lang.Boolean").equals(this.getAttribute("type"))) {
227 sbParameter.append(" type: boolean\n");
229 if(StringUtils.isNotBlank(this.getAttribute("name"))) {
230 sbParameter.append(" example: "+"__"+this.getAttribute("name").toUpperCase()+"__"+"\n");
232 return sbParameter.toString();
235 public String getHTMLElement(Version v, boolean useAnnotation, HTMLfromOXM driver) {
236 StringBuffer sbElement = new StringBuffer();
237 String elementName = this.getAttribute("name");
238 String elementType = this.getAttribute("type");
239 String elementContainerType = this.getAttribute("container-type");
240 String elementIsRequired = this.getAttribute("required");
241 String addType = elementType.contains("." + v.name() + ".") ? elementType.substring(elementType.lastIndexOf('.')+1) : null;
243 if ( addType != null ) {
244 sbElement.append(" <xs:element ref=\"tns:" + driver.getXmlRootElementName(addType)+"\"");
246 sbElement.append(" <xs:element name=\"" + elementName +"\"");
248 if ( elementType.equals("java.lang.String"))
249 sbElement.append(" type=\"xs:string\"");
250 if ( elementType.equals("java.lang.Long"))
251 sbElement.append(" type=\"xs:unsignedInt\"");
252 if ( elementType.equals("java.lang.Integer"))
253 sbElement.append(" type=\"xs:int\"");
254 if ( elementType.equals("java.lang.Boolean"))
255 sbElement.append(" type=\"xs:boolean\"");
256 if ( elementIsRequired == null || !elementIsRequired.equals("true")||addType != null) {
257 sbElement.append(" minOccurs=\"0\"");
259 if ( elementContainerType != null && elementContainerType.equals("java.util.ArrayList")) {
260 sbElement.append(" maxOccurs=\"unbounded\"");
263 String annotation = new XSDElement(xmlElementElement).getHTMLAnnotation("field", " ");
264 sbElement.append(StringUtils.isNotEmpty(annotation) ? ">\n" : "");
265 sbElement.append(annotation);
266 sbElement.append(StringUtils.isNotEmpty(annotation) ? " </xs:element>\n" : "/>\n" );
268 sbElement.append("/>\n");
270 return this.getHTMLElementWrapper(sbElement.toString(), v, useAnnotation);
271 // return sbElement.toString();
274 public String getHTMLElementWrapper(String unwrappedElement, Version v, boolean useAnnotation) {
276 NodeList childNodes = this.getElementsByTagName("xml-element-wrapper");
278 String xmlElementWrapper = null;
279 if ( childNodes.getLength() > 0 ) {
280 Element childElement = (Element)childNodes.item(0);
282 xmlElementWrapper = childElement == null ? null : childElement.getAttribute("name");
284 if(xmlElementWrapper == null)
285 return unwrappedElement;
287 StringBuffer sbElement = new StringBuffer();
288 sbElement.append(" <xs:element name=\"" + xmlElementWrapper +"\"");
289 String elementType = xmlElementElement.getAttribute("type");
290 String elementIsRequired = this.getAttribute("required");
291 String addType = elementType.contains("." + v.name() + ".") ? elementType.substring(elementType.lastIndexOf('.')+1) : null;
293 if ( elementIsRequired == null || !elementIsRequired.equals("true")||addType != null) {
294 sbElement.append(" minOccurs=\"0\"");
296 sbElement.append(">\n");
297 sbElement.append(" <xs:complexType>\n");
299 XSDElement javaTypeElement = new XSDElement((Element)this.getParentNode());
300 sbElement.append(javaTypeElement.getHTMLAnnotation("class", " "));
302 sbElement.append(" <xs:sequence>\n");
303 sbElement.append(" ");
304 sbElement.append(unwrappedElement);
305 sbElement.append(" </xs:sequence>\n");
306 sbElement.append(" </xs:complexType>\n");
307 sbElement.append(" </xs:element>\n");
308 return sbElement.toString();
311 public String getHTMLAnnotation(String target, String indentation) {
312 StringBuffer sb = new StringBuffer();
313 List<String> metadata = new ArrayList<>();
314 if("true".equals(this.getAttribute("xml-key")) ) {
315 metadata.add("isKey=true");
318 NodeList xmlPropTags = this.getElementsByTagName("xml-properties");
319 Element xmlPropElement = null;
320 for ( int i = 0; i < xmlPropTags.getLength(); ++i ) {
321 xmlPropElement = (Element)xmlPropTags.item(i);
322 if (! xmlPropElement.getParentNode().getAttributes().getNamedItem("name").getNodeValue().equals(this.xmlElementElement.getAttribute("name")))
327 if(xmlPropElement != null) {
328 NodeList xmlProperties = xmlPropElement.getElementsByTagName("xml-property");
329 for (int i = 0; i < xmlProperties.getLength(); i++) {
330 Element item = (Element)xmlProperties.item(i);
331 String name = item.getAttribute("name");
332 String value = item.getAttribute("value");
333 if (name.equals("abstract")) {
335 } else if (name.equals("extends")) {
336 name = "extendsFrom";
338 metadata.add(name + "=\"" + value.replaceAll("&", "&") + "\"");
341 if(metadata.size() == 0) {
344 sb.append(indentation +"<xs:annotation>\r\n");
346 indentation + " <xs:appinfo>\r\n" +
347 indentation + " <annox:annotate target=\""+target+"\">@org.onap.aai.annotations.Metadata(" + Joiner.on(",").join(metadata) + ")</annox:annotate>\r\n" +
348 indentation + " </xs:appinfo>\r\n");
349 sb.append(indentation +"</xs:annotation>\r\n");
350 return sb.toString();
353 public String getTypePropertyYAML() {
354 StringBuffer sbProperties = new StringBuffer();
355 sbProperties.append(" " + this.getAttribute("name") + ":\n");
356 sbProperties.append(" type: ");
358 if ( ("java.lang.String").equals(this.getAttribute("type")))
359 sbProperties.append("string\n");
360 else if ( ("java.lang.Long").equals(this.getAttribute("type"))) {
361 sbProperties.append("integer\n");
362 sbProperties.append(" format: int64\n");
364 else if ( ("java.lang.Integer").equals(this.getAttribute("type"))){
365 sbProperties.append("integer\n");
366 sbProperties.append(" format: int32\n");
368 else if ( ("java.lang.Boolean").equals(this.getAttribute("type")))
369 sbProperties.append("boolean\n");
370 String attrDescription = this.getPathDescriptionProperty();
371 if ( attrDescription != null && attrDescription.length() > 0 )
372 sbProperties.append(" description: " + attrDescription + "\n");
373 return sbProperties.toString();
376 public boolean isStandardType()
378 switch ( this.getAttribute("type") ) {
379 case "java.lang.String":
380 case "java.lang.Long":
381 case "java.lang.Integer":
382 case"java.lang.Boolean":
389 public String getNodeName() {
390 return xmlElementElement.getNodeName();
394 public String getNodeValue() throws DOMException {
395 return xmlElementElement.getNodeValue();
399 public void setNodeValue(String nodeValue) throws DOMException {
400 xmlElementElement.setNodeValue(nodeValue);
404 public short getNodeType() {
405 return xmlElementElement.getNodeType();
409 public Node getParentNode() {
410 return xmlElementElement.getParentNode();
414 public NodeList getChildNodes() {
415 return xmlElementElement.getChildNodes();
419 public Node getFirstChild() {
420 return xmlElementElement.getFirstChild();
424 public Node getLastChild() {
425 return xmlElementElement.getLastChild();
429 public Node getPreviousSibling() {
430 return xmlElementElement.getPreviousSibling();
434 public Node getNextSibling() {
435 return xmlElementElement.getNextSibling();
439 public NamedNodeMap getAttributes() {
440 return xmlElementElement.getAttributes();
444 public Document getOwnerDocument() {
445 return xmlElementElement.getOwnerDocument();
449 public Node insertBefore(Node newChild, Node refChild) throws DOMException {
450 return xmlElementElement.insertBefore(newChild, refChild);
454 public Node replaceChild(Node newChild, Node oldChild) throws DOMException {
455 return xmlElementElement.replaceChild(newChild, oldChild);
459 public Node removeChild(Node oldChild) throws DOMException {
460 return xmlElementElement.removeChild(oldChild);
464 public Node appendChild(Node newChild) throws DOMException {
465 return xmlElementElement.appendChild(newChild);
469 public boolean hasChildNodes() {
470 return xmlElementElement.hasChildNodes();
474 public Node cloneNode(boolean deep) {
475 return xmlElementElement.cloneNode(deep);
479 public void normalize() {
480 xmlElementElement.normalize();
484 public boolean isSupported(String feature, String version) {
485 return xmlElementElement.isSupported(feature, version);
489 public String getNamespaceURI() {
490 return xmlElementElement.getNamespaceURI();
494 public String getPrefix() {
495 return xmlElementElement.getPrefix();
499 public void setPrefix(String prefix) throws DOMException {
500 xmlElementElement.setPrefix(prefix);
504 public String getLocalName() {
506 return xmlElementElement.getLocalName();
510 public boolean hasAttributes() {
511 return xmlElementElement.hasAttributes();
515 public String getBaseURI() {
516 return xmlElementElement.getBaseURI();
520 public short compareDocumentPosition(Node other) throws DOMException {
521 return xmlElementElement.compareDocumentPosition(other);
525 public String getTextContent() throws DOMException {
526 return xmlElementElement.getTextContent();
530 public void setTextContent(String textContent) throws DOMException {
531 xmlElementElement.setTextContent(textContent);
535 public boolean isSameNode(Node other) {
536 return xmlElementElement.isSameNode(other);
540 public String lookupPrefix(String namespaceURI) {
541 return xmlElementElement.lookupPrefix(namespaceURI);
545 public boolean isDefaultNamespace(String namespaceURI) {
546 return xmlElementElement.isDefaultNamespace(namespaceURI);
550 public String lookupNamespaceURI(String prefix) {
551 return xmlElementElement.lookupNamespaceURI(prefix);
555 public boolean isEqualNode(Node arg) {
556 return xmlElementElement.isEqualNode(arg);
560 public Object getFeature(String feature, String version) {
561 return xmlElementElement.getFeature(feature, version);
565 public Object setUserData(String key, Object data, UserDataHandler handler) {
566 return xmlElementElement.setUserData(key, data, handler);
570 public Object getUserData(String key) {
571 return xmlElementElement.getUserData(key);
575 public String getTagName() {
576 return xmlElementElement.getTagName();
580 public String getAttribute(String name) {
581 return xmlElementElement.getAttribute(name);
585 public void setAttribute(String name, String value) throws DOMException {
586 xmlElementElement.setAttribute(name, value);
590 public void removeAttribute(String name) throws DOMException {
591 xmlElementElement.removeAttribute(name);
595 public Attr getAttributeNode(String name) {
596 return xmlElementElement.getAttributeNode(name);
600 public Attr setAttributeNode(Attr newAttr) throws DOMException {
601 return xmlElementElement.setAttributeNode(newAttr);
605 public Attr removeAttributeNode(Attr oldAttr) throws DOMException {
606 return xmlElementElement.removeAttributeNode(oldAttr);
610 public NodeList getElementsByTagName(String name) {
611 return xmlElementElement.getElementsByTagName(name);
615 public String getAttributeNS(String namespaceURI, String localName) throws DOMException {
616 return xmlElementElement.getAttributeNS(namespaceURI, localName);
620 public void setAttributeNS(String namespaceURI, String qualifiedName, String value) throws DOMException {
621 xmlElementElement.setAttributeNS(namespaceURI, qualifiedName, value);
626 public void removeAttributeNS(String namespaceURI, String localName) throws DOMException {
627 xmlElementElement.removeAttributeNS(namespaceURI, localName);
631 public Attr getAttributeNodeNS(String namespaceURI, String localName) throws DOMException {
632 return xmlElementElement.getAttributeNodeNS(namespaceURI, localName);
636 public Attr setAttributeNodeNS(Attr newAttr) throws DOMException {
637 return xmlElementElement.setAttributeNodeNS(newAttr);
641 public NodeList getElementsByTagNameNS(String namespaceURI, String localName) throws DOMException {
642 return xmlElementElement.getElementsByTagNameNS(namespaceURI, localName);
646 public boolean hasAttribute(String name) {
647 return xmlElementElement.hasAttribute(name);
651 public boolean hasAttributeNS(String namespaceURI, String localName) throws DOMException {
652 return xmlElementElement.hasAttributeNS(namespaceURI, localName);
656 public TypeInfo getSchemaTypeInfo() {
657 return xmlElementElement.getSchemaTypeInfo();
661 public void setIdAttribute(String name, boolean isId) throws DOMException {
662 xmlElementElement.setIdAttribute(name, isId);
667 public void setIdAttributeNS(String namespaceURI, String localName, boolean isId) throws DOMException {
668 xmlElementElement.setIdAttributeNS(namespaceURI, localName, isId);
672 public void setIdAttributeNode(Attr idAttr, boolean isId) throws DOMException {
673 xmlElementElement.setIdAttributeNode(idAttr, isId);