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