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.schemagen.genxsd;
22 import com.google.common.base.Joiner;
23 import org.apache.commons.lang3.StringUtils;
24 import org.onap.aai.setup.SchemaVersion;
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;
35 private static final int VALUE_NONE = 0;
36 private static final int VALUE_DESCRIPTION = 1;
37 private static final int VALUE_INDEXED_PROPS = 2;
38 private static final int VALUE_CONTAINER = 3;
39 private static final int VALUE_REQUIRES = 4;
41 public XSDElement(Element xmlElementElement, String maxOccurs) {
43 this.xmlElementElement = xmlElementElement;
44 this.maxOccurs = maxOccurs;
47 public XSDElement(Element xmlElementElement) {
49 this.xmlElementElement = xmlElementElement;
50 this.maxOccurs = null;
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 if ( pathDescriptionProperty != null ) {
150 //suppress non-printable characters in a description
151 String replaceDescription = pathDescriptionProperty.replaceAll("[^\\p{ASCII}]", "");
152 return replaceDescription;
154 return pathDescriptionProperty;
156 public Vector<String> getIndexedProps() {
157 Vector<String> indexedProps = new Vector<String>();
158 NodeList xmlPropNodes = this.getElementsByTagName("xml-properties");
160 for ( int i = 0; i < xmlPropNodes.getLength(); ++i ) {
161 Element xmlPropElement = (Element)xmlPropNodes.item(i);
162 if ( !xmlPropElement.getParentNode().isSameNode(this.xmlElementElement))
164 NodeList childNodes = xmlPropElement.getElementsByTagName("xml-property");
165 for ( int j = 0; j < childNodes.getLength(); ++j ) {
166 Element childElement = (Element)childNodes.item(j);
168 int useValue = VALUE_NONE;
169 NamedNodeMap attributes = childElement.getAttributes();
170 for ( int k = 0; k < attributes.getLength(); ++k ) {
171 Attr attr = (Attr) attributes.item(k);
172 String attrName = attr.getNodeName();
173 String attrValue = attr.getNodeValue();
174 if ( attrName == null || attrValue == null )
176 if ( attrValue.equals("indexedProps")) {
177 useValue = VALUE_INDEXED_PROPS;
179 if ( useValue == VALUE_INDEXED_PROPS && attrName.equals("value")) {
180 indexedProps = getIndexedProps( attrValue );
188 private static Vector<String> getIndexedProps( String attrValue )
190 if ( attrValue == null )
192 StringTokenizer st = new StringTokenizer( attrValue, ",");
193 if ( st.countTokens() == 0 )
195 Vector<String> result = new Vector<String>();
196 while ( st.hasMoreTokens()) {
197 result.add(st.nextToken());
202 public String getContainerProperty() {
203 NodeList xmlPropNodes = this.getElementsByTagName("xml-properties");
204 String container = null;
205 for ( int i = 0; i < xmlPropNodes.getLength(); ++i ) {
206 Element xmlPropElement = (Element)xmlPropNodes.item(i);
207 if ( !xmlPropElement.getParentNode().isSameNode(this.xmlElementElement))
209 NodeList childNodes = xmlPropElement.getElementsByTagName("xml-property");
210 for ( int j = 0; j < childNodes.getLength(); ++j ) {
211 Element childElement = (Element)childNodes.item(j);
213 int useValue = VALUE_NONE;
214 NamedNodeMap attributes = childElement.getAttributes();
215 for ( int k = 0; k < attributes.getLength(); ++k ) {
216 Attr attr = (Attr) attributes.item(k);
217 String attrName = attr.getNodeName();
218 String attrValue = attr.getNodeValue();
219 if ( attrName == null || attrValue == null )
221 if ( useValue == VALUE_CONTAINER && attrName.equals("value")) {
222 container = attrValue;
224 if ( attrValue.equals("container")) {
225 useValue = VALUE_CONTAINER;
233 public String getQueryParamYAML() {
234 StringBuffer sbParameter = new StringBuffer();
235 sbParameter.append((" - name: " + this.getAttribute("name") + "\n"));
236 sbParameter.append((" in: query\n"));
237 if ( this.getAttribute("description") != null && this.getAttribute("description").length() > 0 )
238 sbParameter.append((" description: " + this.getAttribute("description") + "\n"));
240 sbParameter.append((" description: n/a\n"));
241 sbParameter.append((" required: false\n"));
242 if ( ("java.lang.String").equals(this.getAttribute("type")))
243 sbParameter.append(" type: string\n");
244 if ( ("java.lang.Long").equals(this.getAttribute("type"))) {
245 sbParameter.append(" type: integer\n");
246 sbParameter.append(" format: int64\n");
248 if ( ("java.lang.Integer").equals(this.getAttribute("type"))) {
249 sbParameter.append(" type: integer\n");
250 sbParameter.append(" format: int32\n");
252 if ( ("java.lang.Boolean").equals(this.getAttribute("type"))) {
253 sbParameter.append(" type: boolean\n");
255 return sbParameter.toString();
258 public String getPathParamYAML(String elementDescription) {
259 return getPathParamYAML(elementDescription, null);
262 public String getPathParamYAML(String elementDescription, String overrideName) {
263 // updated to allow caller to provide parameter name to use in API
264 StringBuffer sbParameter = new StringBuffer();
265 if ( overrideName == null ) {
266 overrideName = this.getAttribute("name");
268 sbParameter.append((" - name: " + overrideName + "\n"));
269 sbParameter.append((" in: path\n"));
270 if ( elementDescription != null && elementDescription.length() > 0 )
271 sbParameter.append((" description: " + elementDescription + "\n"));
272 sbParameter.append((" required: true\n"));
273 if ( ("java.lang.String").equals(this.getAttribute("type")))
274 sbParameter.append(" type: string\n");
275 if ( ("java.lang.Long").equals(this.getAttribute("type"))) {
276 sbParameter.append(" type: integer\n");
277 sbParameter.append(" format: int64\n");
279 if ( ("java.lang.Integer").equals(this.getAttribute("type"))) {
280 sbParameter.append(" type: integer\n");
281 sbParameter.append(" format: int32\n");
283 if ( ("java.lang.Boolean").equals(this.getAttribute("type"))) {
284 sbParameter.append(" type: boolean\n");
286 if(StringUtils.isNotBlank(this.getAttribute("name"))) {
287 sbParameter.append(" example: "+"__"+this.getAttribute("name").toUpperCase()+"__"+"\n");
289 return sbParameter.toString();
292 public String getHTMLElement(SchemaVersion v, boolean useAnnotation, HTMLfromOXM driver) {
293 StringBuffer sbElement = new StringBuffer();
294 String elementName = this.getAttribute("name");
295 String elementType = this.getAttribute("type");
296 String elementContainerType = this.getAttribute("container-type");
297 String elementIsRequired = this.getAttribute("required");
298 String addType = elementType.contains("." + v.toString() + ".") ? elementType.substring(elementType.lastIndexOf('.')+1) : null;
300 if ( addType != null ) {
301 sbElement.append(" <xs:element ref=\"tns:" + driver.getXmlRootElementName(addType)+"\"");
303 sbElement.append(" <xs:element name=\"" + elementName +"\"");
305 if ( elementType.equals("java.lang.String"))
306 sbElement.append(" type=\"xs:string\"");
307 if ( elementType.equals("java.lang.Long"))
308 sbElement.append(" type=\"xs:unsignedInt\"");
309 if ( elementType.equals("java.lang.Integer"))
310 sbElement.append(" type=\"xs:int\"");
311 if ( elementType.equals("java.lang.Boolean"))
312 sbElement.append(" type=\"xs:boolean\"");
313 if ( addType != null || elementType.startsWith("java.lang.") ) {
314 sbElement.append(" minOccurs=\"0\"");
316 if ( elementContainerType != null && elementContainerType.equals("java.util.ArrayList")) {
317 sbElement.append(" maxOccurs=\"" + maxOccurs + "\"");
320 String annotation = new XSDElement(xmlElementElement, maxOccurs).getHTMLAnnotation("field", " ");
321 sbElement.append(StringUtils.isNotEmpty(annotation) ? ">" + OxmFileProcessor.LINE_SEPARATOR : "");
322 sbElement.append(annotation);
323 sbElement.append(StringUtils.isNotEmpty(annotation) ? " </xs:element>" + OxmFileProcessor.LINE_SEPARATOR : "/>" + OxmFileProcessor.LINE_SEPARATOR );
325 sbElement.append("/>" + OxmFileProcessor.LINE_SEPARATOR);
327 return this.getHTMLElementWrapper(sbElement.toString(), v, useAnnotation);
328 // return sbElement.toString();
331 public String getHTMLElementWrapper(String unwrappedElement, SchemaVersion v, boolean useAnnotation) {
333 NodeList childNodes = this.getElementsByTagName("xml-element-wrapper");
335 String xmlElementWrapper = null;
336 if ( childNodes.getLength() > 0 ) {
337 Element childElement = (Element)childNodes.item(0);
339 xmlElementWrapper = childElement == null ? null : childElement.getAttribute("name");
341 if(xmlElementWrapper == null)
342 return unwrappedElement;
344 StringBuffer sbElement = new StringBuffer();
345 sbElement.append(" <xs:element name=\"" + xmlElementWrapper +"\"");
346 String elementType = xmlElementElement.getAttribute("type");
347 String elementIsRequired = this.getAttribute("required");
348 String addType = elementType.contains("." + v.toString() + ".") ? elementType.substring(elementType.lastIndexOf('.')+1) : null;
350 if ( elementIsRequired == null || !elementIsRequired.equals("true")||addType != null) {
351 sbElement.append(" minOccurs=\"0\"");
353 sbElement.append(">" + OxmFileProcessor.LINE_SEPARATOR);
354 sbElement.append(" <xs:complexType>" + OxmFileProcessor.LINE_SEPARATOR);
356 XSDElement javaTypeElement = new XSDElement((Element)this.getParentNode(), maxOccurs);
357 sbElement.append(javaTypeElement.getHTMLAnnotation("class", " "));
359 sbElement.append(" <xs:sequence>" + OxmFileProcessor.LINE_SEPARATOR);
360 sbElement.append(" ");
361 sbElement.append(unwrappedElement);
362 sbElement.append(" </xs:sequence>" + OxmFileProcessor.LINE_SEPARATOR);
363 sbElement.append(" </xs:complexType>" + OxmFileProcessor.LINE_SEPARATOR);
364 sbElement.append(" </xs:element>" + OxmFileProcessor.LINE_SEPARATOR);
365 return sbElement.toString();
368 public String getHTMLAnnotation(String target, String indentation) {
369 StringBuffer sb = new StringBuffer();
370 List<String> metadata = new ArrayList<>();
371 if("true".equals(this.getAttribute("xml-key")) ) {
372 metadata.add("isKey=true");
375 NodeList xmlPropTags = this.getElementsByTagName("xml-properties");
376 Element xmlPropElement = null;
377 for ( int i = 0; i < xmlPropTags.getLength(); ++i ) {
378 xmlPropElement = (Element)xmlPropTags.item(i);
379 if (! xmlPropElement.getParentNode().getAttributes().getNamedItem("name").getNodeValue().equals(this.xmlElementElement.getAttribute("name")))
384 if(xmlPropElement != null) {
385 NodeList xmlProperties = xmlPropElement.getElementsByTagName("xml-property");
386 for (int i = 0; i < xmlProperties.getLength(); i++) {
387 Element item = (Element)xmlProperties.item(i);
388 String name = item.getAttribute("name");
389 String value = item.getAttribute("value");
390 if (name.equals("abstract")) {
392 } else if (name.equals("extends")) {
393 name = "extendsFrom";
395 metadata.add(name + "=\"" + value.replaceAll("&", "&") + "\"");
398 if(metadata.size() == 0) {
401 sb.append(indentation +"<xs:annotation>" + OxmFileProcessor.LINE_SEPARATOR);
403 indentation + " <xs:appinfo>" + OxmFileProcessor.LINE_SEPARATOR +
404 indentation + " <annox:annotate target=\""+target+"\">@org.onap.aai.annotations.Metadata(" + Joiner.on(",").join(metadata) + ")</annox:annotate>" + OxmFileProcessor.LINE_SEPARATOR +
405 indentation + " </xs:appinfo>" + OxmFileProcessor.LINE_SEPARATOR);
406 sb.append(indentation +"</xs:annotation>" + OxmFileProcessor.LINE_SEPARATOR);
407 return sb.toString();
410 public String getTypePropertyYAML() {
411 StringBuffer sbProperties = new StringBuffer();
412 sbProperties.append(" " + this.getAttribute("name") + ":\n");
413 sbProperties.append(" type: ");
415 if ( ("java.lang.String").equals(this.getAttribute("type")))
416 sbProperties.append("string\n");
417 else if ( ("java.lang.Long").equals(this.getAttribute("type"))) {
418 sbProperties.append("integer\n");
419 sbProperties.append(" format: int64\n");
421 else if ( ("java.lang.Integer").equals(this.getAttribute("type"))){
422 sbProperties.append("integer\n");
423 sbProperties.append(" format: int32\n");
425 else if ( ("java.lang.Boolean").equals(this.getAttribute("type")))
426 sbProperties.append("boolean\n");
427 String attrDescription = this.getPathDescriptionProperty();
428 if ( attrDescription != null && attrDescription.length() > 0 )
429 sbProperties.append(" description: " + attrDescription + "\n");
430 String elementAlsoRequiresProperty=this.getRequiresProperty();
431 if ( StringUtils.isNotEmpty(elementAlsoRequiresProperty) )
432 sbProperties.append(" also requires: " + elementAlsoRequiresProperty + "\n");
433 return sbProperties.toString();
436 public boolean isStandardType()
438 switch ( this.getAttribute("type") ) {
439 case "java.lang.String":
440 case "java.lang.Long":
441 case "java.lang.Integer":
442 case"java.lang.Boolean":
449 public String getNodeName() {
450 return xmlElementElement.getNodeName();
454 public String getNodeValue() throws DOMException {
455 return xmlElementElement.getNodeValue();
459 public void setNodeValue(String nodeValue) throws DOMException {
460 xmlElementElement.setNodeValue(nodeValue);
464 public short getNodeType() {
465 return xmlElementElement.getNodeType();
469 public Node getParentNode() {
470 return xmlElementElement.getParentNode();
474 public NodeList getChildNodes() {
475 return xmlElementElement.getChildNodes();
479 public Node getFirstChild() {
480 return xmlElementElement.getFirstChild();
484 public Node getLastChild() {
485 return xmlElementElement.getLastChild();
489 public Node getPreviousSibling() {
490 return xmlElementElement.getPreviousSibling();
494 public Node getNextSibling() {
495 return xmlElementElement.getNextSibling();
499 public NamedNodeMap getAttributes() {
500 return xmlElementElement.getAttributes();
504 public Document getOwnerDocument() {
505 return xmlElementElement.getOwnerDocument();
509 public Node insertBefore(Node newChild, Node refChild) throws DOMException {
510 return xmlElementElement.insertBefore(newChild, refChild);
514 public Node replaceChild(Node newChild, Node oldChild) throws DOMException {
515 return xmlElementElement.replaceChild(newChild, oldChild);
519 public Node removeChild(Node oldChild) throws DOMException {
520 return xmlElementElement.removeChild(oldChild);
524 public Node appendChild(Node newChild) throws DOMException {
525 return xmlElementElement.appendChild(newChild);
529 public boolean hasChildNodes() {
530 return xmlElementElement.hasChildNodes();
534 public Node cloneNode(boolean deep) {
535 return xmlElementElement.cloneNode(deep);
539 public void normalize() {
540 xmlElementElement.normalize();
544 public boolean isSupported(String feature, String version) {
545 return xmlElementElement.isSupported(feature, version);
549 public String getNamespaceURI() {
550 return xmlElementElement.getNamespaceURI();
554 public String getPrefix() {
555 return xmlElementElement.getPrefix();
559 public void setPrefix(String prefix) throws DOMException {
560 xmlElementElement.setPrefix(prefix);
564 public String getLocalName() {
566 return xmlElementElement.getLocalName();
570 public boolean hasAttributes() {
571 return xmlElementElement.hasAttributes();
575 public String getBaseURI() {
576 return xmlElementElement.getBaseURI();
580 public short compareDocumentPosition(Node other) throws DOMException {
581 return xmlElementElement.compareDocumentPosition(other);
585 public String getTextContent() throws DOMException {
586 return xmlElementElement.getTextContent();
590 public void setTextContent(String textContent) throws DOMException {
591 xmlElementElement.setTextContent(textContent);
595 public boolean isSameNode(Node other) {
596 return xmlElementElement.isSameNode(other);
600 public String lookupPrefix(String namespaceURI) {
601 return xmlElementElement.lookupPrefix(namespaceURI);
605 public boolean isDefaultNamespace(String namespaceURI) {
606 return xmlElementElement.isDefaultNamespace(namespaceURI);
610 public String lookupNamespaceURI(String prefix) {
611 return xmlElementElement.lookupNamespaceURI(prefix);
615 public boolean isEqualNode(Node arg) {
616 return xmlElementElement.isEqualNode(arg);
620 public Object getFeature(String feature, String version) {
621 return xmlElementElement.getFeature(feature, version);
625 public Object setUserData(String key, Object data, UserDataHandler handler) {
626 return xmlElementElement.setUserData(key, data, handler);
630 public Object getUserData(String key) {
631 return xmlElementElement.getUserData(key);
635 public String getTagName() {
636 return xmlElementElement.getTagName();
640 public String getAttribute(String name) {
641 return xmlElementElement.getAttribute(name);
645 public void setAttribute(String name, String value) throws DOMException {
646 xmlElementElement.setAttribute(name, value);
650 public void removeAttribute(String name) throws DOMException {
651 xmlElementElement.removeAttribute(name);
655 public Attr getAttributeNode(String name) {
656 return xmlElementElement.getAttributeNode(name);
660 public Attr setAttributeNode(Attr newAttr) throws DOMException {
661 return xmlElementElement.setAttributeNode(newAttr);
665 public Attr removeAttributeNode(Attr oldAttr) throws DOMException {
666 return xmlElementElement.removeAttributeNode(oldAttr);
670 public NodeList getElementsByTagName(String name) {
671 return xmlElementElement.getElementsByTagName(name);
675 public String getAttributeNS(String namespaceURI, String localName) throws DOMException {
676 return xmlElementElement.getAttributeNS(namespaceURI, localName);
680 public void setAttributeNS(String namespaceURI, String qualifiedName, String value) throws DOMException {
681 xmlElementElement.setAttributeNS(namespaceURI, qualifiedName, value);
686 public void removeAttributeNS(String namespaceURI, String localName) throws DOMException {
687 xmlElementElement.removeAttributeNS(namespaceURI, localName);
691 public Attr getAttributeNodeNS(String namespaceURI, String localName) throws DOMException {
692 return xmlElementElement.getAttributeNodeNS(namespaceURI, localName);
696 public Attr setAttributeNodeNS(Attr newAttr) throws DOMException {
697 return xmlElementElement.setAttributeNodeNS(newAttr);
701 public NodeList getElementsByTagNameNS(String namespaceURI, String localName) throws DOMException {
702 return xmlElementElement.getElementsByTagNameNS(namespaceURI, localName);
706 public boolean hasAttribute(String name) {
707 return xmlElementElement.hasAttribute(name);
711 public boolean hasAttributeNS(String namespaceURI, String localName) throws DOMException {
712 return xmlElementElement.hasAttributeNS(namespaceURI, localName);
716 public TypeInfo getSchemaTypeInfo() {
717 return xmlElementElement.getSchemaTypeInfo();
721 public void setIdAttribute(String name, boolean isId) throws DOMException {
722 xmlElementElement.setIdAttribute(name, isId);
727 public void setIdAttributeNS(String namespaceURI, String localName, boolean isId) throws DOMException {
728 xmlElementElement.setIdAttributeNS(namespaceURI, localName, isId);
732 public void setIdAttributeNode(Attr idAttr, boolean isId) throws DOMException {
733 xmlElementElement.setIdAttributeNode(idAttr, isId);