2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * Modifications Copyright © 2018 IBM
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.ccsdk.sli.plugins.restapicall;
25 import static com.google.common.base.Preconditions.checkNotNull;
27 import java.io.ByteArrayInputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.util.HashMap;
31 import java.util.HashSet;
35 import javax.xml.parsers.ParserConfigurationException;
36 import javax.xml.parsers.SAXParser;
37 import javax.xml.parsers.SAXParserFactory;
39 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.xml.sax.Attributes;
43 import org.xml.sax.SAXException;
44 import org.xml.sax.helpers.DefaultHandler;
46 public final class XmlParser {
48 private static final Logger log = LoggerFactory.getLogger(XmlParser.class);
51 // Preventing instantiation of the same.
54 public static Map<String, String> convertToProperties(String s, Set<String> listNameList)
55 throws SvcLogicException {
57 checkNotNull(s, "Input should not be null.");
59 Handler handler = new Handler(listNameList);
61 SAXParserFactory factory = SAXParserFactory.newInstance();
62 SAXParser saxParser = factory.newSAXParser();
63 InputStream in = new ByteArrayInputStream(s.getBytes());
64 saxParser.parse(in, handler);
65 } catch (ParserConfigurationException | IOException | SAXException | NumberFormatException e) {
66 throw new SvcLogicException("Unable to convert XML to properties" + e.getLocalizedMessage(), e);
68 return handler.getProperties();
71 private static class Handler extends DefaultHandler {
73 private Set<String> listNameList;
75 private Map<String, String> properties = new HashMap<>();
77 StringBuilder currentName = new StringBuilder();
78 StringBuilder currentValue = new StringBuilder();
80 public Handler(Set<String> listNameList) {
82 this.listNameList = listNameList;
83 if (this.listNameList == null)
84 this.listNameList = new HashSet<>();
87 public Map<String, String> getProperties() {
92 public void startElement(String uri, String localName, String qName, Attributes attributes)
94 super.startElement(uri, localName, qName, attributes);
96 String name = localName;
97 if (name == null || name.trim().length() == 0)
99 int i2 = name.indexOf(':');
101 name = name.substring(i2 + 1);
103 if (currentName.length() > 0)
104 currentName.append(Character.toString('.'));
105 currentName.append(name);
107 String listName = removeIndexes(currentName.toString());
109 if (listNameList.contains(listName)) {
110 String n = currentName.toString() + "_length";
111 int len = getInt(properties, n);
112 properties.put(n, String.valueOf(len + 1));
113 currentName.append("[").append(len).append("]");
118 public void endElement(String uri, String localName, String qName) throws SAXException {
119 super.endElement(uri, localName, qName);
121 String name = localName;
122 if (name == null || name.trim().length() == 0)
124 int i2 = name.indexOf(':');
126 name = name.substring(i2 + 1);
128 String s = currentValue.toString().trim();
129 if (s.length() > 0) {
130 properties.put(currentName.toString(), s);
132 log.info("Added property: {} : {}", currentName, s);
133 currentValue = new StringBuilder();
136 int i1 = currentName.lastIndexOf("." + name);
138 currentName = new StringBuilder();
140 currentName = new StringBuilder(currentName.substring(0, i1));
144 public void characters(char[] ch, int start, int length) throws SAXException {
145 super.characters(ch, start, length);
147 String value = new String(ch, start, length);
148 currentValue.append(value);
151 private static int getInt(Map<String, String> mm, String name) {
152 String s = mm.get(name);
155 return Integer.parseInt(s);
158 private String removeIndexes(String currentName) {
159 StringBuilder b = new StringBuilder();
161 for (int i = 0; i < currentName.length(); i++) {
162 char c = currentName.charAt(i);
168 b.append(Character.toString(c));