blob: 1c6536a5b647317843363c7de0fbf6e5ec3212d1 [file] [log] [blame]
Shankara-Huaweid5823ab2016-11-22 10:14:52 +05301/*
2 * Copyright 2016-present Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.yms.app.ych.defaultcodecs.xml;
18
19import org.dom4j.Attribute;
20import org.dom4j.Element;
21import org.onosproject.yms.app.ydt.YdtExtendedBuilder;
22import org.onosproject.yms.ydt.YdtContextOperationType;
23
24import java.util.Iterator;
25
26import static org.onosproject.yms.app.ych.defaultcodecs.netconf.NetconfCodecConstants.OPERATION;
27import static org.onosproject.yms.app.ych.defaultcodecs.xml.XmlNodeType.OBJECT_NODE;
28import static org.onosproject.yms.app.ych.defaultcodecs.xml.XmlNodeType.TEXT_NODE;
29
30/**
31 * Default implementation of codec xml listener.
32 */
33class XmlCodecListener implements XmlListener {
34
35 /**
36 * YANG data tree builder object.
37 */
38 private YdtExtendedBuilder ydtExtBuilder;
39
40 /**
41 * Sets the YANG data tree builder object.
42 *
43 * @param ydtBuilder YANG data tree builder object
44 */
45 void setYdtExtBuilder(YdtExtendedBuilder ydtBuilder) {
46 ydtExtBuilder = ydtBuilder;
47 }
48
49 @Override
50 public void enterXmlElement(Element element, XmlNodeType nodeType,
51 Element rootElement) {
52 if (element.equals(rootElement)) {
53 return;
54 }
55
56 YdtContextOperationType opType = null;
57
58 for (Iterator iter = element.attributeIterator(); iter.hasNext();) {
59 Attribute attr = (Attribute) iter.next();
60 if (attr.getName().equals(OPERATION)) {
61 opType =
62 YdtContextOperationType.valueOf(attr.getValue()
63 .toUpperCase());
64 }
65 }
66
67 String nameSpace = null;
68 if (element.getNamespace() != null) {
69 nameSpace = element.getNamespace().getURI();
70 }
71
sonu guptaeff184b2016-11-24 12:43:49 +053072 if (nodeType == OBJECT_NODE && element.content() == null || element
73 .content().isEmpty()) {
74 nodeType = TEXT_NODE;
75 }
Shankara-Huaweid5823ab2016-11-22 10:14:52 +053076 if (nodeType == OBJECT_NODE) {
77 if (ydtExtBuilder != null) {
78 ydtExtBuilder.addChild(element.getName(), nameSpace, opType);
79 }
80 } else if (nodeType == TEXT_NODE) {
81
82 if (ydtExtBuilder != null) {
83 ydtExtBuilder.addLeaf(element.getName(), nameSpace,
84 element.getText());
85 }
86 }
87 }
88
89 @Override
90 public void exitXmlElement(Element element, XmlNodeType nodeType,
91 Element rootElement) {
92 if (element.equals(rootElement)) {
93 return;
94 }
95
96 if (ydtExtBuilder != null) {
97 ydtExtBuilder.traverseToParent();
98 }
99 }
100}