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;
38 import javax.xml.XMLConstants;
40 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.xml.sax.Attributes;
44 import org.xml.sax.SAXException;
45 import org.xml.sax.helpers.DefaultHandler;
46 import org.xml.sax.XMLReader;
48 public final class XmlParser {
50 private static final Logger log = LoggerFactory.getLogger(XmlParser.class);
53 // Preventing instantiation of the same.
56 public static Map<String, String> convertToProperties(String s, Set<String> listNameList)
57 throws SvcLogicException {
59 checkNotNull(s, "Input should not be null.");
61 Handler handler = new Handler(listNameList);
63 SAXParserFactory spf = SAXParserFactory.newInstance();
64 spf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
65 spf.setFeature("http://xml.org/sax/features/external-general-entities", false);
66 spf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
67 spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
68 InputStream in = new ByteArrayInputStream(s.getBytes());
69 SAXParser saxParser = spf.newSAXParser();
70 saxParser.parse(in, handler);
72 } catch (ParserConfigurationException | IOException | SAXException | NumberFormatException e) {
73 throw new SvcLogicException("Unable to convert XML to properties" + e.getLocalizedMessage(), e);
75 return handler.getProperties();
78 private static class Handler extends DefaultHandler {
80 private Set<String> listNameList;
82 private Map<String, String> properties = new HashMap<>();
84 StringBuilder currentName = new StringBuilder();
85 StringBuilder currentValue = new StringBuilder();
87 public Handler(Set<String> listNameList) {
89 this.listNameList = listNameList;
90 if (this.listNameList == null)
91 this.listNameList = new HashSet<>();
94 public Map<String, String> getProperties() {
99 public void startElement(String uri, String localName, String qName, Attributes attributes)
100 throws SAXException {
101 super.startElement(uri, localName, qName, attributes);
103 String name = localName;
104 if (name == null || name.trim().length() == 0)
106 int i2 = name.indexOf(':');
108 name = name.substring(i2 + 1);
110 if (currentName.length() > 0)
111 currentName.append(Character.toString('.'));
112 currentName.append(name);
114 String listName = removeIndexes(currentName.toString());
116 if (listNameList.contains(listName)) {
117 String n = currentName.toString() + "_length";
118 int len = getInt(properties, n);
119 properties.put(n, String.valueOf(len + 1));
120 currentName.append("[").append(len).append("]");
125 public void endElement(String uri, String localName, String qName) throws SAXException {
126 super.endElement(uri, localName, qName);
128 String name = localName;
129 if (name == null || name.trim().length() == 0)
131 int i2 = name.indexOf(':');
133 name = name.substring(i2 + 1);
135 String s = currentValue.toString().trim();
136 if (s.length() > 0) {
137 properties.put(currentName.toString(), s);
139 log.info("Added property: {} : {}", currentName, s);
140 currentValue = new StringBuilder();
143 int i1 = currentName.lastIndexOf("." + name);
145 currentName = new StringBuilder();
147 currentName = new StringBuilder(currentName.substring(0, i1));
151 public void characters(char[] ch, int start, int length) throws SAXException {
152 super.characters(ch, start, length);
154 String value = new String(ch, start, length);
155 currentValue.append(value);
158 private static int getInt(Map<String, String> mm, String name) {
159 String s = mm.get(name);
162 return Integer.parseInt(s);
165 private String removeIndexes(String currentName) {
166 StringBuilder b = new StringBuilder();
168 for (int i = 0; i < currentName.length(); i++) {
169 char c = currentName.charAt(i);
175 b.append(Character.toString(c));