2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
7 * Modifications Copyright © 2018 IBM.
8 * ================================================================================
9 * Copyright (C) 2018 Samsung Electronics. All rights
11 * ================================================================================
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
16 * http://www.apache.org/licenses/LICENSE-2.0
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 * ============LICENSE_END=========================================================
26 package org.onap.ccsdk.sli.plugins.sshapicall.model;
28 import static com.google.common.base.Preconditions.checkNotNull;
30 import java.io.ByteArrayInputStream;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.util.HashMap;
34 import java.util.HashSet;
38 import javax.xml.parsers.ParserConfigurationException;
39 import javax.xml.parsers.SAXParser;
40 import javax.xml.parsers.SAXParserFactory;
42 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.xml.sax.Attributes;
46 import org.xml.sax.SAXException;
47 import org.xml.sax.helpers.DefaultHandler;
49 public final class XmlParser {
51 private static final Logger log = LoggerFactory.getLogger(XmlParser.class);
54 // Preventing instantiation of the same.
57 public static Map<String, String> convertToProperties(String s, Set<String> listNameList)
58 throws SvcLogicException {
60 checkNotNull(s, "Input should not be null.");
62 Handler handler = new Handler(listNameList);
64 SAXParserFactory factory = SAXParserFactory.newInstance();
65 SAXParser saxParser = factory.newSAXParser();
66 InputStream in = new ByteArrayInputStream(s.getBytes());
67 saxParser.parse(in, handler);
68 } catch (ParserConfigurationException | IOException | SAXException | NumberFormatException e) {
69 throw new SvcLogicException("Unable to convert XML to properties" + e.getLocalizedMessage(), e);
71 return handler.getProperties();
74 private static class Handler extends DefaultHandler {
76 private Set<String> listNameList;
78 private Map<String, String> properties = new HashMap<>();
80 StringBuilder currentName = new StringBuilder();
81 StringBuilder currentValue = new StringBuilder();
83 public Handler(Set<String> listNameList) {
85 this.listNameList = listNameList;
86 if (this.listNameList == null)
87 this.listNameList = new HashSet<>();
90 public Map<String, String> getProperties() {
95 public void startElement(String uri, String localName, String qName, Attributes attributes)
97 super.startElement(uri, localName, qName, attributes);
99 String name = localName;
100 if (name == null || name.trim().length() == 0)
102 int i2 = name.indexOf(':');
104 name = name.substring(i2 + 1);
106 if (currentName.length() > 0)
107 currentName.append(Character.toString('.'));
108 currentName.append(name);
110 String listName = removeIndexes(currentName.toString());
112 if (listNameList.contains(listName)) {
113 String n = currentName.toString() + "_length";
114 int len = getInt(properties, n);
115 properties.put(n, String.valueOf(len + 1));
116 currentName.append("[").append(len).append("]");
121 public void endElement(String uri, String localName, String qName) throws SAXException {
122 super.endElement(uri, localName, qName);
124 String name = localName;
125 if (name == null || name.trim().length() == 0)
127 int i2 = name.indexOf(':');
129 name = name.substring(i2 + 1);
131 String s = currentValue.toString().trim();
132 if (s.length() > 0) {
133 properties.put(currentName.toString(), s);
135 log.info("Added property: {} : {}", currentName, s);
136 currentValue = new StringBuilder();
139 int i1 = currentName.lastIndexOf("." + name);
141 currentName = new StringBuilder();
143 currentName = new StringBuilder(currentName.substring(0, i1));
147 public void characters(char[] ch, int start, int length) throws SAXException {
148 super.characters(ch, start, length);
150 String value = new String(ch, start, length);
151 currentValue.append(value);
154 private static int getInt(Map<String, String> mm, String name) {
155 String s = mm.get(name);
158 return Integer.parseInt(s);
161 private String removeIndexes(String currentName) {
162 StringBuilder b = new StringBuilder();
164 for (int i = 0; i < currentName.length(); i++) {
165 char c = currentName.charAt(i);
171 b.append(Character.toString(c));