Release version 1.1.0 of sli/plugins
[ccsdk/sli/plugins.git] / restapi-call-node / provider / src / main / java / org / onap / ccsdk / sli / plugins / restapicall / XmlParser.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                      reserved.
7  *      Modifications Copyright © 2018 IBM
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.ccsdk.sli.plugins.restapicall;
24
25 import static com.google.common.base.Preconditions.checkNotNull;
26
27 import java.io.ByteArrayInputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.util.HashMap;
31 import java.util.HashSet;
32 import java.util.Map;
33 import java.util.Set;
34
35 import javax.xml.parsers.ParserConfigurationException;
36 import javax.xml.parsers.SAXParser;
37 import javax.xml.parsers.SAXParserFactory;
38 import javax.xml.XMLConstants;
39
40 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.xml.sax.Attributes;
44 import org.xml.sax.SAXException;
45 import org.xml.sax.helpers.DefaultHandler;
46 import org.xml.sax.XMLReader;
47
48 public final class XmlParser {
49
50     private static final Logger log = LoggerFactory.getLogger(XmlParser.class);
51
52     private XmlParser() {
53         // Preventing instantiation of the same.
54     }
55
56     public static Map<String, String> convertToProperties(String s, Set<String> listNameList)
57         throws SvcLogicException {
58
59         checkNotNull(s, "Input should not be null.");
60
61         Handler handler = new Handler(listNameList);
62         try {
63             SAXParserFactory spf = SAXParserFactory.newInstance();
64             spf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
65             spf.setFeature("http://xml.org/sax/features/external-general-entities", false);
66             spf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
67             spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);  
68             InputStream in = new ByteArrayInputStream(s.getBytes());
69             SAXParser saxParser = spf.newSAXParser();
70             saxParser.parse(in, handler);
71             
72         } catch (ParserConfigurationException | IOException | SAXException | NumberFormatException e) {
73             throw new SvcLogicException("Unable to convert XML to properties" + e.getLocalizedMessage(), e);
74         }
75         return handler.getProperties();
76     }
77
78     private static class Handler extends DefaultHandler {
79
80         private Set<String> listNameList;
81
82         private Map<String, String> properties = new HashMap<>();
83
84         StringBuilder currentName = new StringBuilder();
85         StringBuilder currentValue = new StringBuilder();
86
87         public Handler(Set<String> listNameList) {
88             super();
89             this.listNameList = listNameList;
90             if (this.listNameList == null)
91                 this.listNameList = new HashSet<>();
92         }
93
94         public Map<String, String> getProperties() {
95             return properties;
96         }
97
98         @Override
99         public void startElement(String uri, String localName, String qName, Attributes attributes)
100                 throws SAXException {
101             super.startElement(uri, localName, qName, attributes);
102
103             String name = localName;
104             if (name == null || name.trim().length() == 0)
105                 name = qName;
106             int i2 = name.indexOf(':');
107             if (i2 >= 0)
108                 name = name.substring(i2 + 1);
109
110             if (currentName.length() > 0)
111                 currentName.append(Character.toString('.'));
112             currentName.append(name);
113
114             String listName = removeIndexes(currentName.toString());
115
116             if (listNameList.contains(listName)) {
117                 String n = currentName.toString() + "_length";
118                 int len = getInt(properties, n);
119                 properties.put(n, String.valueOf(len + 1));
120                 currentName.append("[").append(len).append("]");
121             }
122         }
123
124         @Override
125         public void endElement(String uri, String localName, String qName) throws SAXException {
126             super.endElement(uri, localName, qName);
127
128             String name = localName;
129             if (name == null || name.trim().length() == 0)
130                 name = qName;
131             int i2 = name.indexOf(':');
132             if (i2 >= 0)
133                 name = name.substring(i2 + 1);
134
135             String s = currentValue.toString().trim();
136             if (s.length() > 0) {
137                 properties.put(currentName.toString(), s);
138
139                 log.info("Added property: {} : {}", currentName, s);
140                 currentValue = new StringBuilder();
141             }
142
143             int i1 = currentName.lastIndexOf("." + name);
144             if (i1 <= 0)
145                 currentName = new StringBuilder();
146             else
147                 currentName = new StringBuilder(currentName.substring(0, i1));
148         }
149
150         @Override
151         public void characters(char[] ch, int start, int length) throws SAXException {
152             super.characters(ch, start, length);
153
154             String value = new String(ch, start, length);
155             currentValue.append(value);
156         }
157
158         private static int getInt(Map<String, String> mm, String name) {
159             String s = mm.get(name);
160             if (s == null)
161                 return 0;
162             return Integer.parseInt(s);
163         }
164
165         private String removeIndexes(String currentName) {
166             StringBuilder b = new StringBuilder();
167             boolean add = true;
168             for (int i = 0; i < currentName.length(); i++) {
169                 char c = currentName.charAt(i);
170                 if (c == '[')
171                     add = false;
172                 else if (c == ']')
173                     add = true;
174                 else if (add)
175                     b.append(Character.toString(c));
176             }
177             return b.toString();
178         }
179     }
180 }