7a32437627000c6b41b68dab2d63387a56b217a9
[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 ONAP Intellectual Property. All rights
6  *                                              reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.ccsdk.sli.plugins.restapicall;
23
24 import java.io.ByteArrayInputStream;
25 import java.io.InputStream;
26 import java.util.HashMap;
27 import java.util.HashSet;
28 import java.util.Map;
29 import java.util.Set;
30
31 import javax.xml.parsers.SAXParser;
32 import javax.xml.parsers.SAXParserFactory;
33
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.xml.sax.Attributes;
37 import org.xml.sax.SAXException;
38 import org.xml.sax.helpers.DefaultHandler;
39
40 public class XmlParser {
41
42         private static final Logger log = LoggerFactory.getLogger(XmlParser.class);
43
44         public static Map<String, String> convertToProperties(String s, Set<String> listNameList) {
45                 Handler handler = new Handler(listNameList);
46                 try {
47                         SAXParserFactory factory = SAXParserFactory.newInstance();
48                         SAXParser saxParser = factory.newSAXParser();
49                         InputStream in = new ByteArrayInputStream(s.getBytes());
50                         saxParser.parse(in, handler);
51                 } catch (Exception e) {
52                         e.printStackTrace();
53                 }
54
55                 return handler.getProperties();
56         }
57
58         private static class Handler extends DefaultHandler {
59
60                 private Set<String> listNameList;
61
62                 private Map<String, String> properties = new HashMap<>();
63
64                 public Map<String, String> getProperties() {
65                         return properties;
66                 }
67
68                 public Handler(Set<String> listNameList) {
69                         super();
70                         this.listNameList = listNameList;
71                         if (this.listNameList == null)
72                                 this.listNameList = new HashSet<String>();
73                 }
74
75                 String currentName = "";
76                 String currentValue = "";
77
78                 @Override
79                 public void startElement(String uri, String localName, String qName, Attributes attributes)
80                         throws SAXException {
81                         super.startElement(uri, localName, qName, attributes);
82
83                         String name = localName;
84                         if (name == null || name.trim().length() == 0)
85                                 name = qName;
86                         int i2 = name.indexOf(':');
87                         if (i2 >= 0)
88                                 name = name.substring(i2 + 1);
89
90                         if (currentName.length() > 0)
91                                 currentName += '.';
92                         currentName += name;
93
94                         String listName = removeIndexes(currentName);
95
96                         if (listNameList.contains(listName)) {
97                                 int len = getInt(properties, currentName + "_length");
98                                 properties.put(currentName + "_length", String.valueOf(len + 1));
99                                 currentName += "[" + len + "]";
100                         }
101                 }
102
103                 @Override
104                 public void endElement(String uri, String localName, String qName) throws SAXException {
105                         super.endElement(uri, localName, qName);
106
107                         String name = localName;
108                         if (name == null || name.trim().length() == 0)
109                                 name = qName;
110                         int i2 = name.indexOf(':');
111                         if (i2 >= 0)
112                                 name = name.substring(i2 + 1);
113
114                         if (currentValue.trim().length() > 0) {
115                                 currentValue = currentValue.trim();
116                                 properties.put(currentName, currentValue);
117
118                                 log.info("Added property: " + currentName + ": " + currentValue);
119
120                                 currentValue = "";
121                         }
122
123                         int i1 = currentName.lastIndexOf("." + name);
124                         if (i1 <= 0)
125                                 currentName = "";
126                         else
127                                 currentName = currentName.substring(0, i1);
128                 }
129
130                 @Override
131                 public void characters(char[] ch, int start, int length) throws SAXException {
132                         super.characters(ch, start, length);
133
134                         String value = new String(ch, start, length);
135                         currentValue += value;
136                 }
137
138                 private static int getInt(Map<String, String> mm, String name) {
139                         String s = mm.get(name);
140                         if (s == null)
141                                 return 0;
142                         return Integer.parseInt(s);
143                 }
144
145                 private String removeIndexes(String currentName) {
146                         String s = "";
147                         boolean add = true;
148                         for (int i = 0; i < currentName.length(); i++) {
149                                 char c = currentName.charAt(i);
150                                 if (c == '[')
151                                         add = false;
152                                 else if (c == ']')
153                                         add = true;
154                                 else if (add)
155                                         s += c;
156                         }
157                         return s;
158                 }
159         }
160 }