2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2018 Samsung Electronics. All rights
9 * ================================================================================
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 * ============LICENSE_END=========================================================
24 package org.onap.ccsdk.sli.plugins.sshapicall.model;
26 import static com.google.common.base.Preconditions.checkNotNull;
28 import java.io.ByteArrayInputStream;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.util.HashMap;
32 import java.util.HashSet;
36 import javax.xml.parsers.ParserConfigurationException;
37 import javax.xml.parsers.SAXParser;
38 import javax.xml.parsers.SAXParserFactory;
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;
47 public final class XmlParser {
49 private static final Logger log = LoggerFactory.getLogger(XmlParser.class);
52 // Preventing instantiation of the same.
55 public static Map<String, String> convertToProperties(String s, Set<String> listNameList)
56 throws SvcLogicException {
58 checkNotNull(s, "Input should not be null.");
60 Handler handler = new Handler(listNameList);
62 SAXParserFactory factory = SAXParserFactory.newInstance();
63 SAXParser saxParser = factory.newSAXParser();
64 InputStream in = new ByteArrayInputStream(s.getBytes());
65 saxParser.parse(in, handler);
66 } catch (ParserConfigurationException | IOException | SAXException | NumberFormatException e) {
67 throw new SvcLogicException("Unable to convert XML to properties" + e.getLocalizedMessage(), e);
69 return handler.getProperties();
72 private static class Handler extends DefaultHandler {
74 private Set<String> listNameList;
76 private Map<String, String> properties = new HashMap<>();
78 public Map<String, String> getProperties() {
82 public Handler(Set<String> listNameList) {
84 this.listNameList = listNameList;
85 if (this.listNameList == null)
86 this.listNameList = new HashSet<>();
89 StringBuilder currentName = new StringBuilder();
90 StringBuilder currentValue = new StringBuilder();
93 public void startElement(String uri, String localName, String qName, Attributes attributes)
95 super.startElement(uri, localName, qName, attributes);
97 String name = localName;
98 if (name == null || name.trim().length() == 0)
100 int i2 = name.indexOf(':');
102 name = name.substring(i2 + 1);
104 if (currentName.length() > 0)
105 currentName.append(Character.toString('.'));
106 currentName.append(name);
108 String listName = removeIndexes(currentName.toString());
110 if (listNameList.contains(listName)) {
111 String n = currentName.toString() + "_length";
112 int len = getInt(properties, n);
113 properties.put(n, String.valueOf(len + 1));
114 currentName.append("[").append(len).append("]");
119 public void endElement(String uri, String localName, String qName) throws SAXException {
120 super.endElement(uri, localName, qName);
122 String name = localName;
123 if (name == null || name.trim().length() == 0)
125 int i2 = name.indexOf(':');
127 name = name.substring(i2 + 1);
129 String s = currentValue.toString().trim();
130 if (s.length() > 0) {
131 properties.put(currentName.toString(), s);
133 log.info("Added property: {} : {}", currentName, s);
134 currentValue = new StringBuilder();
137 int i1 = currentName.lastIndexOf("." + name);
139 currentName = new StringBuilder();
141 currentName = new StringBuilder(currentName.substring(0, i1));
145 public void characters(char[] ch, int start, int length) throws SAXException {
146 super.characters(ch, start, length);
148 String value = new String(ch, start, length);
149 currentValue.append(value);
152 private static int getInt(Map<String, String> mm, String name) {
153 String s = mm.get(name);
156 return Integer.parseInt(s);
159 private String removeIndexes(String currentName) {
160 StringBuilder b = new StringBuilder();
162 for (int i = 0; i < currentName.length(); i++) {
163 char c = currentName.charAt(i);
169 b.append(Character.toString(c));