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