2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 Huawei Technologies Co., Ltd. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.ccsdk.sli.plugins.yangserializers.dfserializer;
23 import org.dom4j.Element;
24 import org.dom4j.Namespace;
25 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
27 import java.util.List;
29 import static java.lang.String.format;
30 import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DfSerializerUtil.NODE_TYPE_ERR;
33 * Representation of default implementation of XML listener.
35 public class DefaultXmlListener implements XmlListener {
38 * Serializer helper to convert to properties node.
40 private SerializerHelper serializerHelper;
43 * Creates an instance of default XML listener with its serializer helper.
45 * @param serializerHelper serializer helper
47 public DefaultXmlListener(SerializerHelper serializerHelper) {
48 this.serializerHelper = serializerHelper;
52 public void enterXmlElement(Element element, XmlNodeType nodeType)
53 throws SvcLogicException {
56 serializerHelper.addNode(element.getName(),
57 element.getNamespace().getURI(),
58 element.getText(), null, null);
62 List cont = element.content();
63 if (cont != null && cont.size() == 2 &&
64 isValueNsForLeaf(cont, element)) {
67 serializerHelper.addNode(element.getName(),
68 element.getNamespace().getURI(),
73 throw new SvcLogicException(format(NODE_TYPE_ERR,
74 nodeType.toString()));
79 * Returns true if element has value namespace and adds the node to
80 * property tree; false otherwise.
82 * @param cont content of the element
83 * @param element element
84 * @return true if element has value namespace; false otherwise
85 * @throws SvcLogicException
87 private boolean isValueNsForLeaf(List cont, Element element)
88 throws SvcLogicException {
89 for (Object c : cont) {
90 if (c instanceof Namespace) {
91 String value = element.getText();
93 String[] val = value.split(":");
94 String valPrefix = val[0];
95 String actVal = val[1];
96 if (valPrefix != null && actVal != null &&
97 valPrefix.equals(((Namespace) c).getPrefix())) {
98 serializerHelper.addNode(
100 element.getNamespace().getURI(),
102 ((Namespace) c).getURI(), null);
112 public void exitXmlElement(Element element) throws SvcLogicException {
113 serializerHelper.exitNode();
117 public SerializerHelper serializerHelper() {
118 return serializerHelper;