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