2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.ccsdk.sli.plugins.restapicall;
24 import static com.google.common.base.Preconditions.checkNotNull;
26 import java.io.ByteArrayInputStream;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.util.HashMap;
30 import java.util.HashSet;
34 import javax.xml.parsers.ParserConfigurationException;
35 import javax.xml.parsers.SAXParser;
36 import javax.xml.parsers.SAXParserFactory;
38 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.xml.sax.Attributes;
42 import org.xml.sax.SAXException;
43 import org.xml.sax.helpers.DefaultHandler;
45 public final class XmlParser {
47 private static final Logger log = LoggerFactory.getLogger(XmlParser.class);
50 // Preventing instantiation of the same.
53 public static Map<String, String> convertToProperties(String s, Set<String> listNameList)
54 throws SvcLogicException {
56 checkNotNull(s, "Input should not be null.");
58 Handler handler = new Handler(listNameList);
60 SAXParserFactory factory = SAXParserFactory.newInstance();
61 SAXParser saxParser = factory.newSAXParser();
62 InputStream in = new ByteArrayInputStream(s.getBytes());
63 saxParser.parse(in, handler);
64 } catch (ParserConfigurationException | IOException | SAXException | NumberFormatException e) {
65 throw new SvcLogicException("Unable to convert XML to properties" + e.getLocalizedMessage(), e);
67 return handler.getProperties();
70 private static class Handler extends DefaultHandler {
72 private Set<String> listNameList;
74 private Map<String, String> properties = new HashMap<>();
76 public Map<String, String> getProperties() {
80 public Handler(Set<String> listNameList) {
82 this.listNameList = listNameList;
83 if (this.listNameList == null)
84 this.listNameList = new HashSet<>();
87 StringBuilder currentName = new StringBuilder();
88 StringBuilder currentValue = new StringBuilder();
91 public void startElement(String uri, String localName, String qName, Attributes attributes)
93 super.startElement(uri, localName, qName, attributes);
95 String name = localName;
96 if (name == null || name.trim().length() == 0)
98 int i2 = name.indexOf(':');
100 name = name.substring(i2 + 1);
102 if (currentName.length() > 0)
103 currentName.append(Character.toString('.'));
104 currentName.append(name);
106 String listName = removeIndexes(currentName.toString());
108 if (listNameList.contains(listName)) {
109 String n = currentName.toString() + "_length";
110 int len = getInt(properties, n);
111 properties.put(n, String.valueOf(len + 1));
112 currentName.append("[").append(len).append("]");
117 public void endElement(String uri, String localName, String qName) throws SAXException {
118 super.endElement(uri, localName, qName);
120 String name = localName;
121 if (name == null || name.trim().length() == 0)
123 int i2 = name.indexOf(':');
125 name = name.substring(i2 + 1);
127 String s = currentValue.toString().trim();
128 if (s.length() > 0) {
129 properties.put(currentName.toString(), s);
131 log.info("Added property: {} : {}", currentName, s);
132 currentValue = new StringBuilder();
135 int i1 = currentName.lastIndexOf("." + name);
137 currentName = new StringBuilder();
139 currentName = new StringBuilder(currentName.substring(0, i1));
143 public void characters(char[] ch, int start, int length) throws SAXException {
144 super.characters(ch, start, length);
146 String value = new String(ch, start, length);
147 currentValue.append(value);
150 private static int getInt(Map<String, String> mm, String name) {
151 String s = mm.get(name);
154 return Integer.parseInt(s);
157 private String removeIndexes(String currentName) {
158 StringBuilder b = new StringBuilder();
160 for (int i = 0; i < currentName.length(); i++) {
161 char c = currentName.charAt(i);
167 b.append(Character.toString(c));