03abf44fd3131e1d2e27db840a7826a04cf241bc
[ccsdk/sli.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - CCSDK
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.ccsdk.sli.plugins.yangserializers.dfserializer;
22
23 import org.dom4j.Element;
24 import org.dom4j.Namespace;
25 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
26
27 import java.util.List;
28
29 import static java.lang.String.format;
30 import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DfSerializerUtil.NODE_TYPE_ERR;
31
32 /**
33  * Representation of default implementation of XML listener.
34  */
35 public class DefaultXmlListener implements XmlListener {
36
37     /**
38      * Serializer helper to convert to properties node.
39      */
40     private SerializerHelper serializerHelper;
41
42     /**
43      * Creates an instance of default XML listener with its serializer helper.
44      *
45      * @param serializerHelper serializer helper
46      */
47     public DefaultXmlListener(SerializerHelper serializerHelper) {
48         this.serializerHelper = serializerHelper;
49     }
50
51     @Override
52     public void enterXmlElement(Element element, XmlNodeType nodeType)
53             throws SvcLogicException {
54         switch (nodeType) {
55             case TEXT_NODE:
56                 serializerHelper.addNode(element.getName(),
57                                          element.getNamespace().getURI(),
58                                          element.getText(), null, null);
59                 break;
60
61             case OBJECT_NODE:
62                 List cont = element.content();
63                 if (cont != null && cont.size() == 2 &&
64                         isValueNsForLeaf(cont, element)) {
65                     return;
66                 }
67                 serializerHelper.addNode(element.getName(),
68                                          element.getNamespace().getURI(),
69                                          null, null, null);
70                 break;
71
72             default:
73                 throw new SvcLogicException(format(NODE_TYPE_ERR,
74                                                    nodeType.toString()));
75         }
76     }
77
78     /**
79      * Returns true if element has value namespace and adds the node to
80      * property tree; false otherwise.
81      *
82      * @param cont    content of the element
83      * @param element element
84      * @return true if element has value namespace; false otherwise
85      * @throws SvcLogicException
86      */
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();
92                 if (value != null) {
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(
99                                 element.getName(),
100                                 element.getNamespace().getURI(),
101                                 actVal,
102                                 ((Namespace) c).getURI(), null);
103                         return true;
104                     }
105                 }
106             }
107         }
108         return false;
109     }
110
111     @Override
112     public void exitXmlElement(Element element) throws SvcLogicException {
113         serializerHelper.exitNode();
114     }
115
116     @Override
117     public SerializerHelper serializerHelper() {
118         return serializerHelper;
119     }
120 }