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 java.io.ByteArrayInputStream;
25 import java.io.InputStream;
26 import java.util.HashMap;
27 import java.util.HashSet;
31 import javax.xml.parsers.SAXParser;
32 import javax.xml.parsers.SAXParserFactory;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.xml.sax.Attributes;
37 import org.xml.sax.SAXException;
38 import org.xml.sax.helpers.DefaultHandler;
40 public class XmlParser {
42 private static final Logger log = LoggerFactory.getLogger(XmlParser.class);
44 public static Map<String, String> convertToProperties(String s, Set<String> listNameList) {
45 Handler handler = new Handler(listNameList);
47 SAXParserFactory factory = SAXParserFactory.newInstance();
48 SAXParser saxParser = factory.newSAXParser();
49 InputStream in = new ByteArrayInputStream(s.getBytes());
50 saxParser.parse(in, handler);
51 } catch (Exception e) {
55 return handler.getProperties();
58 private static class Handler extends DefaultHandler {
60 private Set<String> listNameList;
62 private Map<String, String> properties = new HashMap<>();
64 public Map<String, String> getProperties() {
68 public Handler(Set<String> listNameList) {
70 this.listNameList = listNameList;
71 if (this.listNameList == null)
72 this.listNameList = new HashSet<>();
75 String currentName = "";
76 String currentValue = "";
79 public void startElement(String uri, String localName, String qName, Attributes attributes)
81 super.startElement(uri, localName, qName, attributes);
83 String name = localName;
84 if (name == null || name.trim().length() == 0)
86 int i2 = name.indexOf(':');
88 name = name.substring(i2 + 1);
90 if (currentName.length() > 0)
91 currentName += Character.toString('.');
94 String listName = removeIndexes(currentName);
96 if (listNameList.contains(listName)) {
97 int len = getInt(properties, currentName + "_length");
98 properties.put(currentName + "_length", String.valueOf(len + 1));
99 currentName += "[" + len + "]";
104 public void endElement(String uri, String localName, String qName) throws SAXException {
105 super.endElement(uri, localName, qName);
107 String name = localName;
108 if (name == null || name.trim().length() == 0)
110 int i2 = name.indexOf(':');
112 name = name.substring(i2 + 1);
114 if (currentValue.trim().length() > 0) {
115 currentValue = currentValue.trim();
116 properties.put(currentName, currentValue);
118 log.info("Added property: " + currentName + ": " + currentValue);
123 int i1 = currentName.lastIndexOf("." + name);
127 currentName = currentName.substring(0, i1);
131 public void characters(char[] ch, int start, int length) throws SAXException {
132 super.characters(ch, start, length);
134 String value = new String(ch, start, length);
135 currentValue += value;
138 private static int getInt(Map<String, String> mm, String name) {
139 String s = mm.get(name);
142 return Integer.parseInt(s);
145 private String removeIndexes(String currentName) {
148 for (int i = 0; i < currentName.length(); i++) {
149 char c = currentName.charAt(i);
155 s += Character.toString(c);