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;
47 * Representation of XML implementation of properties node listener.
49 public class PropertiesNodeXmlListener implements PropertiesNodeListener {
52 * XML data from the element.
54 private String xmlData;
57 * Root element of the XML document.
59 private Element rootElement;
62 * Writer to write the XML.
64 private Writer writer;
67 * XML element stack to store the elements.
69 private final Stack<Element> elementStack = new Stack<>();
72 * Creates the properties node XML listener.
74 public PropertiesNodeXmlListener() {
78 public void start(PropertiesNode node) {
83 public void end(PropertiesNode node) throws SvcLogicException {
84 xmlData = UTF_HEADER + xmlData;
85 writer = getXmlWriter(xmlData, "4");
89 public void enterPropertiesNode(PropertiesNode node)
90 throws SvcLogicException {
91 Element element = null;
92 String ns = getNodeNamespace(node);
93 switch (node.nodeType()) {
94 case MULTI_INSTANCE_LEAF_HOLDER_NODE:
95 case MULTI_INSTANCE_HOLDER_NODE:
98 case SINGLE_INSTANCE_NODE:
99 case MULTI_INSTANCE_NODE:
100 element = addElement(ns, node);
103 case MULTI_INSTANCE_LEAF_NODE:
104 case SINGLE_INSTANCE_LEAF_NODE:
105 element = addElement(ns, node);
106 setValueWithNs(element, (LeafNode) node);
110 throw new SvcLogicException(format(
111 NODE_TYPE_ERR, node.nodeType().toString()));
113 if (element != null) {
114 if (elementStack.isEmpty()) {
115 rootElement = element;
117 elementStack.push(element);
122 public void exitPropertiesNode(PropertiesNode node)
123 throws SvcLogicException {
124 walkAugmentationNode(node);
125 switch (node.nodeType()) {
126 case MULTI_INSTANCE_LEAF_HOLDER_NODE:
127 case MULTI_INSTANCE_HOLDER_NODE:
130 case SINGLE_INSTANCE_NODE:
131 case MULTI_INSTANCE_NODE:
132 case MULTI_INSTANCE_LEAF_NODE:
133 case SINGLE_INSTANCE_LEAF_NODE:
134 if (!elementStack.isEmpty() &&
135 elementStack.peek().equals(rootElement)) {
136 xmlData = rootElement.asXML();
143 throw new SvcLogicException(format(
144 NODE_TYPE_ERR, node.nodeType().toString()));
149 * Returns the writer.
153 public Writer getWriter() {
158 * Adds an XML element to the stack with namespace if present. If the
159 * stack is empty it creates new document and adds element else adds to
160 * the parent element.
162 * @param ns namespace of the element
163 * @param node properties node
164 * @return new added element
166 private Element addElement(String ns, PropertiesNode node) {
168 if (elementStack.isEmpty()) {
169 Document doc = DocumentHelper.createDocument();
171 element = doc.addElement(node.name(), ns);
173 element = doc.addElement(node.name());
176 element = elementStack.peek();
178 element = element.addElement(node.name(), ns);
180 element = element.addElement(node.name());
188 * Returns the abstract XML namespace to be used in XML data format from
189 * the properties node.
191 * @param node properties node
192 * @return abstract XML namespace
194 private String getNodeNamespace(PropertiesNode node) {
195 PropertiesNode parent = node.parent();
196 if (parent instanceof RootNode || !parent.namespace().moduleName()
197 .equals(node.namespace().moduleName())) {
198 return node.namespace().moduleNs().toString();
204 * Sets the value to the element for a leaf node and adds the value
205 * namespace if required.
207 * @param element XML element
208 * @param node leaf properties node
210 private void setValueWithNs(Element element, LeafNode node) {
211 Namespace valNs = node.valueNs();
212 URI modNs = (valNs == null) ? null : valNs.moduleNs();
213 String val = node.value();
215 element.addNamespace(XML_PREFIX, modNs.toString());
216 element.setText(XML_PREFIX + ":" + val);
218 element.setText(val);
223 * Gets all the augmentation of the given node and walks through it.
225 * @param node properties node
226 * @throws SvcLogicException when walking the properties node fails
228 private void walkAugmentationNode(PropertiesNode node)
229 throws SvcLogicException {
230 for (Map.Entry<Object, Collection<PropertiesNode>>
231 augToChild : node.augmentations().asMap().entrySet()) {
232 Collection<PropertiesNode> augChild = augToChild.getValue();
233 if (!augChild.isEmpty()) {
234 DefaultPropertiesNodeWalker walker = new
235 DefaultPropertiesNodeWalker();
236 for (PropertiesNode p : augChild) {
237 enterPropertiesNode(p);
238 walker.walkChildNode(this, p);
239 exitPropertiesNode(p);