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.Document;
24 import org.dom4j.DocumentHelper;
25 import org.dom4j.Element;
26 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
27 import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.DefaultPropertiesNodeWalker;
28 import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.LeafNode;
29 import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.Namespace;
30 import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.PropertiesNode;
31 import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.PropertiesNodeListener;
32 import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.RootNode;
34 import java.io.Writer;
36 import java.util.Collection;
38 import java.util.Stack;
40 import static java.lang.String.format;
41 import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DfSerializerUtil.NODE_TYPE_ERR;
42 import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DfSerializerUtil.UTF_HEADER;
43 import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DfSerializerUtil.XML_PREFIX;
44 import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DfSerializerUtil.getXmlWriter;
45 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.MULTI_INSTANCE_HOLDER_NODE;
46 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.MULTI_INSTANCE_LEAF_HOLDER_NODE;
49 * Representation of XML implementation of properties node listener.
51 public class PropertiesNodeXmlListener implements PropertiesNodeListener {
54 * XML data from the element.
56 private String xmlData;
59 * Root element of the XML document.
61 private Element rootElement;
64 * Writer to write the XML.
66 private Writer writer;
69 * XML element stack to store the elements.
71 private final Stack<Element> elementStack = new Stack<>();
74 * Creates the properties node XML listener.
76 public PropertiesNodeXmlListener() {
80 public void start(PropertiesNode node) {
85 public void end(PropertiesNode node) throws SvcLogicException {
86 xmlData = UTF_HEADER + xmlData;
87 writer = getXmlWriter(xmlData, "4");
91 public void enterPropertiesNode(PropertiesNode node)
92 throws SvcLogicException {
93 Element element = null;
94 String ns = getNodeNamespace(node);
95 switch (node.nodeType()) {
96 case MULTI_INSTANCE_LEAF_HOLDER_NODE:
97 case MULTI_INSTANCE_HOLDER_NODE:
100 case SINGLE_INSTANCE_NODE:
101 case MULTI_INSTANCE_NODE:
102 element = addElement(ns, node);
105 case MULTI_INSTANCE_LEAF_NODE:
106 case SINGLE_INSTANCE_LEAF_NODE:
107 element = addElement(ns, node);
108 setValueWithNs(element, (LeafNode) node);
112 throw new SvcLogicException(format(
113 NODE_TYPE_ERR, node.nodeType().toString()));
115 if (element != null) {
116 if (elementStack.isEmpty()) {
117 rootElement = element;
119 elementStack.push(element);
124 public void exitPropertiesNode(PropertiesNode node)
125 throws SvcLogicException {
126 walkAugmentationNode(node);
127 switch (node.nodeType()) {
128 case MULTI_INSTANCE_LEAF_HOLDER_NODE:
129 case MULTI_INSTANCE_HOLDER_NODE:
132 case SINGLE_INSTANCE_NODE:
133 case MULTI_INSTANCE_NODE:
134 case MULTI_INSTANCE_LEAF_NODE:
135 case SINGLE_INSTANCE_LEAF_NODE:
136 if (!elementStack.isEmpty() &&
137 elementStack.peek().equals(rootElement)) {
138 xmlData = rootElement.asXML();
145 throw new SvcLogicException(format(
146 NODE_TYPE_ERR, node.nodeType().toString()));
151 * Returns the writer.
155 public Writer getWriter() {
160 * Adds an XML element to the stack with namespace if present. If the
161 * stack is empty it creates new document and adds element else adds to
162 * the parent element.
164 * @param ns namespace of the element
165 * @param node properties node
166 * @return new added element
168 private Element addElement(String ns, PropertiesNode node) {
170 if (elementStack.isEmpty()) {
171 Document doc = DocumentHelper.createDocument();
173 element = doc.addElement(node.name(), ns);
175 element = doc.addElement(node.name());
178 element = elementStack.peek();
180 element = element.addElement(node.name(), ns);
182 element = element.addElement(node.name());
190 * Returns the abstract XML namespace to be used in XML data format from
191 * the properties node.
193 * @param node properties node
194 * @return abstract XML namespace
196 private String getNodeNamespace(PropertiesNode node) {
197 PropertiesNode parent = node.parent();
198 if (parent.nodeType() == MULTI_INSTANCE_HOLDER_NODE ||
199 parent.nodeType() == MULTI_INSTANCE_LEAF_HOLDER_NODE) {
200 parent = parent.parent();
202 if (parent instanceof RootNode || ! parent.namespace().moduleName()
203 .equals(node.namespace().moduleName())) {
204 return node.namespace().moduleNs().toString();
210 * Sets the value to the element for a leaf node and adds the value
211 * namespace if required.
213 * @param element XML element
214 * @param node leaf properties node
216 private void setValueWithNs(Element element, LeafNode node) {
217 Namespace valNs = node.valueNs();
218 URI modNs = (valNs == null) ? null : valNs.moduleNs();
219 String val = node.value();
221 element.addNamespace(XML_PREFIX, modNs.toString());
222 element.setText(XML_PREFIX + ":" + val);
224 element.setText(val);
229 * Gets all the augmentation of the given node and walks through it.
231 * @param node properties node
232 * @throws SvcLogicException when walking the properties node fails
234 private void walkAugmentationNode(PropertiesNode node)
235 throws SvcLogicException {
236 for (Map.Entry<Object, Collection<PropertiesNode>>
237 augToChild : node.augmentations().asMap().entrySet()) {
238 Collection<PropertiesNode> augChild = augToChild.getValue();
239 if (!augChild.isEmpty()) {
240 DefaultPropertiesNodeWalker walker = new
241 DefaultPropertiesNodeWalker();
242 for (PropertiesNode p : augChild) {
243 enterPropertiesNode(p);
244 walker.walkChildNode(this, p);
245 exitPropertiesNode(p);