2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. *
6 * Modifications Copyright © 2018 IBM.
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.prop;
24 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.xml.sax.Attributes;
28 import org.xml.sax.SAXException;
29 import org.xml.sax.helpers.DefaultHandler;
31 import javax.xml.parsers.ParserConfigurationException;
32 import javax.xml.parsers.SAXParser;
33 import javax.xml.parsers.SAXParserFactory;
34 import java.io.ByteArrayInputStream;
35 import java.io.IOException;
36 import java.io.InputStream;
37 import java.util.HashMap;
38 import java.util.HashSet;
42 import static com.google.common.base.Preconditions.checkNotNull;
44 public final class XmlParser {
46 private static final Logger log = LoggerFactory.getLogger(XmlParser.class);
49 // Preventing instantiation of the same.
52 public static Map<String, String> convertToProperties(String s, Set<String> listNameList)
53 throws SvcLogicException {
55 checkNotNull(s, "Input should not be null.");
57 Handler handler = new Handler(listNameList);
59 SAXParserFactory factory = SAXParserFactory.newInstance();
60 SAXParser saxParser = factory.newSAXParser();
61 InputStream in = new ByteArrayInputStream(s.getBytes());
62 saxParser.parse(in, handler);
63 } catch (ParserConfigurationException | IOException | SAXException | NumberFormatException e) {
64 throw new SvcLogicException("Unable to convert XML to properties" + e.getLocalizedMessage(), e);
66 return handler.getProperties();
69 private static class Handler extends DefaultHandler {
71 private Set<String> listNameList;
73 private Map<String, String> properties = new HashMap<>();
75 StringBuilder currentName = new StringBuilder();
76 StringBuilder currentValue = new StringBuilder();
78 public Handler(Set<String> listNameList) {
80 this.listNameList = listNameList;
81 if (this.listNameList == null)
82 this.listNameList = new HashSet<>();
85 public Map<String, String> getProperties() {
90 public void startElement(String uri, String localName, String qName, Attributes attributes)
92 super.startElement(uri, localName, qName, attributes);
94 String name = localName;
95 if (name == null || name.trim().length() == 0)
97 int i2 = name.indexOf(':');
99 name = name.substring(i2 + 1);
101 if (currentName.length() > 0)
102 currentName.append(Character.toString('.'));
103 currentName.append(name);
105 String listName = removeIndexes(currentName.toString());
107 if (listNameList.contains(listName)) {
108 String n = currentName.toString() + "_length";
109 int len = getInt(properties, n);
110 properties.put(n, String.valueOf(len + 1));
111 currentName.append("[").append(len).append("]");
116 public void endElement(String uri, String localName, String qName) throws SAXException {
117 super.endElement(uri, localName, qName);
119 String name = localName;
120 if (name == null || name.trim().length() == 0)
122 int i2 = name.indexOf(':');
124 name = name.substring(i2 + 1);
126 String s = currentValue.toString().trim();
127 if (s.length() > 0) {
128 properties.put(currentName.toString(), s);
130 log.info("Added property: {} : {}", currentName, s);
131 currentValue = new StringBuilder();
134 int i1 = currentName.lastIndexOf("." + name);
136 currentName = new StringBuilder();
138 currentName = new StringBuilder(currentName.substring(0, i1));
142 public void characters(char[] ch, int start, int length) throws SAXException {
143 super.characters(ch, start, length);
145 String value = new String(ch, start, length);
146 currentValue.append(value);
149 private static int getInt(Map<String, String> mm, String name) {
150 String s = mm.get(name);
153 return Integer.parseInt(s);
156 private String removeIndexes(String currentName) {
157 StringBuilder b = new StringBuilder();
159 for (int i = 0; i < currentName.length(); i++) {
160 char c = currentName.charAt(i);
166 b.append(Character.toString(c));