2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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=========================================================
21 package org.openecomp.aai.workarounds;
23 import com.google.common.base.CaseFormat;
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
30 import javax.xml.XMLConstants;
31 import javax.xml.parsers.DocumentBuilder;
32 import javax.xml.parsers.DocumentBuilderFactory;
33 import javax.xml.parsers.ParserConfigurationException;
34 import javax.xml.transform.Transformer;
35 import javax.xml.transform.TransformerException;
36 import javax.xml.transform.TransformerFactory;
37 import javax.xml.transform.dom.DOMSource;
38 import javax.xml.transform.stream.StreamResult;
39 import javax.xml.xpath.XPath;
40 import javax.xml.xpath.XPathConstants;
41 import javax.xml.xpath.XPathExpression;
42 import javax.xml.xpath.XPathExpressionException;
43 import javax.xml.xpath.XPathFactory;
44 import org.w3c.dom.Document;
45 import org.w3c.dom.Element;
46 import org.w3c.dom.Node;
47 import org.w3c.dom.NodeList;
48 import org.xml.sax.SAXException;
50 public class ModifyOXMProperties {
52 private static String[] versions = new String[]{"v8"};
57 * @param args the arguments
58 * @throws SAXException the SAX exception
59 * @throws IOException Signals that an I/O exception has occurred.
60 * @throws ParserConfigurationException the parser configuration exception
61 * @throws XPathExpressionException the x path expression exception
62 * @throws TransformerException the transformer exception
64 public static void main(String[] args)
65 throws SAXException, IOException, ParserConfigurationException, XPathExpressionException, TransformerException {
67 for (String version : versions) {
75 * @param version the version
76 * @throws SAXException the SAX exception
77 * @throws IOException Signals that an I/O exception has occurred.
78 * @throws ParserConfigurationException the parser configuration exception
79 * @throws XPathExpressionException the x path expression exception
80 * @throws TransformerException the transformer exception
82 private static void process(String version)
83 throws SAXException, IOException, ParserConfigurationException, XPathExpressionException, TransformerException {
85 String filepath = "bundleconfig-local/etc/oxm/aai_oxm_" + version + ".xml";
86 DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
87 docFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
88 DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
89 Document doc = docBuilder.parse(filepath);
90 XPath xpath = XPathFactory.newInstance().newXPath();
91 //Find namespaces - restrict to inventory
92 //XPathExpression expr = xpath.compile("//java-type[java-attributes[count(xml-element[contains(@type, 'aai.openecomp.org')])=count(./xml-element)][count(xml-element) > 1]]/xml-root-element");
93 XPathExpression expr = xpath.compile("//java-type[@name='Inventory']/java-attributes/xml-element/@name");
94 Object result = expr.evaluate(doc, XPathConstants.NODESET);
95 NodeList nodes = (NodeList) result;
96 List<String> itemsUnderInventory = new ArrayList<>();
97 for (int i = 0; i < nodes.getLength(); i++) {
98 itemsUnderInventory.add(nodes.item(i).getTextContent());
100 Map<String, String> namespaces = new HashMap<>();
102 itemsUnderInventory.remove("Search");
103 for (String item : itemsUnderInventory) {
104 expr = xpath.compile("//java-type[xml-root-element/@name='" + item + "']");
105 result = expr.evaluate(doc, XPathConstants.NODESET);
106 nodes = (NodeList) result;
107 handleItemsUnderInventory(nodes, namespaces, xpath, item);
111 expr = xpath.compile(
112 "//java-type[java-attributes[count(xml-element) = 1]/xml-element[contains(@type, 'aai.openecomp.org')]]/xml-root-element");
114 result = expr.evaluate(doc, XPathConstants.NODESET);
115 nodes = (NodeList) result;
116 List<String> children = new ArrayList<>();
117 for (int i = 0; i < nodes.getLength(); i++) {
118 XPathExpression expr2 = xpath
119 .compile("../java-attributes/xml-element[1]/@type[contains(.,'aai.openecomp.org')]");
120 Object result2 = expr2.evaluate(nodes.item(i), XPathConstants.NODESET);
122 NodeList node2 = (NodeList) result2;
123 String[] temp = node2.item(0).getTextContent().split("\\.");
124 String containerName = nodes.item(i).getAttributes().getNamedItem("name").getTextContent();
125 String childrenTuple = containerName + "," + temp[temp.length - 1];
126 if (namespaces.containsKey(CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, containerName))) {
128 "," + namespaces.get(CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, containerName));
130 children.add(childrenTuple);
133 //match types up with plurals
134 handleChildrenNodes(children, xpath, doc);
136 filepath = "bundleconfig-local/etc/oxm/aai_oxm_" + version + ".xml";
137 TransformerFactory transformerFactory = TransformerFactory.newInstance();
138 transformerFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
139 Transformer transformer = transformerFactory.newTransformer();
140 DOMSource source = new DOMSource(doc);
141 StreamResult file = new StreamResult(new File(filepath));
142 transformer.transform(source, file);
145 private static void handleItemsUnderInventory(NodeList nodes, Map<String, String> namespaces, XPath xpath,
146 String item) throws XPathExpressionException {
148 for (int i = 0; i < nodes.getLength(); i++) {
149 String a = "java-attributes/xml-element/@type[contains(.,'aai.openecomp.org')]";
150 XPathExpression expr2 = xpath.compile(a);
151 Object result2 = expr2.evaluate(nodes.item(i), XPathConstants.NODESET);
153 NodeList node2 = (NodeList) result2;
154 for (int j = 0; j < node2.getLength(); j++) {
155 temp = node2.item(j).getTextContent().split("\\.");
156 namespaces.put(temp[temp.length - 1], item);
161 private static void handleChildrenNodes(List<String> children, XPath xpath, Document doc)
162 throws XPathExpressionException {
164 XPathExpression expr;
167 for (String s : children) {
168 split = s.split(",");
169 expr = xpath.compile("//java-type[@name='" + split[1] + "']/xml-properties");
170 result = expr.evaluate(doc, XPathConstants.NODESET);
171 nodes = (NodeList) result;
172 if (nodes.getLength() > 0) {
174 if (!hasChild(nodes.item(0), "name", "container")) {
176 Element property = doc.createElement("xml-property");
177 property.setAttribute("name", "container");
178 property.setAttribute("value", split[0]);
179 nodes.item(0).appendChild(property);
182 if (split.length == 3) {
184 if (!hasChild(nodes.item(0), "name", "namespace")) {
185 property = doc.createElement("xml-property");
186 property.setAttribute("name", "namespace");
187 property.setAttribute("value", split[2]);
188 nodes.item(0).appendChild(property);
198 * @param node the node
199 * @param name the name
200 * @param value the value
201 * @return true, if successful
203 private static boolean hasChild(Node node, String name, String value) {
204 boolean result = false;
205 NodeList list = node.getChildNodes();
207 for (int i = 0; i < list.getLength(); i++) {
209 if (list.item(i).hasAttributes()) {
210 temp = list.item(i).getAttributes().getNamedItem(name);
213 if (temp != null && temp.getTextContent().equals(value)) {