blob: 6b6ba3865d2e7970b6e63e5c3b206a44a79792f2 [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
72 if (nodeType == OBJECT_NODE) {
73 if (ydtExtBuilder != null) {
74 ydtExtBuilder.addChild(element.getName(), nameSpace, opType);
75 }
76 } else if (nodeType == TEXT_NODE) {
77
78 if (ydtExtBuilder != null) {
79 ydtExtBuilder.addLeaf(element.getName(), nameSpace,
80 element.getText());
81 }
82 }
83 }
84
85 @Override
86 public void exitXmlElement(Element element, XmlNodeType nodeType,
87 Element rootElement) {
88 if (element.equals(rootElement)) {
89 return;
90 }
91
92 if (ydtExtBuilder != null) {
93 ydtExtBuilder.traverseToParent();
94 }
95 }
96}