RestapiCallNode URL Formatting Fix
[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
39 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.xml.sax.Attributes;
43 import org.xml.sax.SAXException;
44 import org.xml.sax.helpers.DefaultHandler;
45
46 public final class XmlParser {
47
48     private static final Logger log = LoggerFactory.getLogger(XmlParser.class);
49
50     private XmlParser() {
51         // Preventing instantiation of the same.
52     }
53
54     public static Map<String, String> convertToProperties(String s, Set<String> listNameList)
55         throws SvcLogicException {
56
57         checkNotNull(s, "Input should not be null.");
58
59         Handler handler = new Handler(listNameList);
60         try {
61             SAXParserFactory factory = SAXParserFactory.newInstance();
62             SAXParser saxParser = factory.newSAXParser();
63             InputStream in = new ByteArrayInputStream(s.getBytes());
64             saxParser.parse(in, handler);
65         } catch (ParserConfigurationException | IOException | SAXException | NumberFormatException e) {
66             throw new SvcLogicException("Unable to convert XML to properties" + e.getLocalizedMessage(), e);
67         }
68         return handler.getProperties();
69     }
70
71     private static class Handler extends DefaultHandler {
72
73         private Set<String> listNameList;
74
75         private Map<String, String> properties = new HashMap<>();
76
77         StringBuilder currentName = new StringBuilder();
78         StringBuilder currentValue = new StringBuilder();
79
80         public Handler(Set<String> listNameList) {
81             super();
82             this.listNameList = listNameList;
83             if (this.listNameList == null)
84                 this.listNameList = new HashSet<>();
85         }
86
87         public Map<String, String> getProperties() {
88             return properties;
89         }
90
91         @Override
92         public void startElement(String uri, String localName, String qName, Attributes attributes)
93                 throws SAXException {
94             super.startElement(uri, localName, qName, attributes);
95
96             String name = localName;
97             if (name == null || name.trim().length() == 0)
98                 name = qName;
99             int i2 = name.indexOf(':');
100             if (i2 >= 0)
101                 name = name.substring(i2 + 1);
102
103             if (currentName.length() > 0)
104                 currentName.append(Character.toString('.'));
105             currentName.append(name);
106
107             String listName = removeIndexes(currentName.toString());
108
109             if (listNameList.contains(listName)) {
110                 String n = currentName.toString() + "_length";
111                 int len = getInt(properties, n);
112                 properties.put(n, String.valueOf(len + 1));
113                 currentName.append("[").append(len).append("]");
114             }
115         }
116
117         @Override
118         public void endElement(String uri, String localName, String qName) throws SAXException {
119             super.endElement(uri, localName, qName);
120
121             String name = localName;
122             if (name == null || name.trim().length() == 0)
123                 name = qName;
124             int i2 = name.indexOf(':');
125             if (i2 >= 0)
126                 name = name.substring(i2 + 1);
127
128             String s = currentValue.toString().trim();
129             if (s.length() > 0) {
130                 properties.put(currentName.toString(), s);
131
132                 log.info("Added property: {} : {}", currentName, s);
133                 currentValue = new StringBuilder();
134             }
135
136             int i1 = currentName.lastIndexOf("." + name);
137             if (i1 <= 0)
138                 currentName = new StringBuilder();
139             else
140                 currentName = new StringBuilder(currentName.substring(0, i1));
141         }
142
143         @Override
144         public void characters(char[] ch, int start, int length) throws SAXException {
145             super.characters(ch, start, length);
146
147             String value = new String(ch, start, length);
148             currentValue.append(value);
149         }
150
151         private static int getInt(Map<String, String> mm, String name) {
152             String s = mm.get(name);
153             if (s == null)
154                 return 0;
155             return Integer.parseInt(s);
156         }
157
158         private String removeIndexes(String currentName) {
159             StringBuilder b = new StringBuilder();
160             boolean add = true;
161             for (int i = 0; i < currentName.length(); i++) {
162                 char c = currentName.charAt(i);
163                 if (c == '[')
164                     add = false;
165                 else if (c == ']')
166                     add = true;
167                 else if (add)
168                     b.append(Character.toString(c));
169             }
170             return b.toString();
171         }
172     }
173 }