Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame^] | 2 | * Copyright 2016-present Open Networking Foundation |
Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.yms.app.ych.defaultcodecs.xml; |
| 18 | |
| 19 | import org.dom4j.Attribute; |
| 20 | import org.dom4j.Element; |
| 21 | import org.onosproject.yms.app.ydt.YdtExtendedBuilder; |
| 22 | import org.onosproject.yms.ydt.YdtContextOperationType; |
| 23 | |
| 24 | import java.util.Iterator; |
| 25 | |
| 26 | import static org.onosproject.yms.app.ych.defaultcodecs.netconf.NetconfCodecConstants.OPERATION; |
| 27 | import static org.onosproject.yms.app.ych.defaultcodecs.xml.XmlNodeType.OBJECT_NODE; |
| 28 | import static org.onosproject.yms.app.ych.defaultcodecs.xml.XmlNodeType.TEXT_NODE; |
| 29 | |
| 30 | /** |
| 31 | * Default implementation of codec xml listener. |
| 32 | */ |
| 33 | class XmlCodecListener implements XmlListener { |
| 34 | |
| 35 | /** |
| 36 | * YANG data tree builder object. |
| 37 | */ |
| 38 | private YdtExtendedBuilder ydtExtBuilder; |
| 39 | |
Vidyashree Rama | c848970 | 2016-12-19 09:38:31 +0530 | [diff] [blame] | 40 | private String prevNodeNamespace; |
| 41 | |
Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame] | 42 | /** |
| 43 | * Sets the YANG data tree builder object. |
| 44 | * |
| 45 | * @param ydtBuilder YANG data tree builder object |
| 46 | */ |
| 47 | void setYdtExtBuilder(YdtExtendedBuilder ydtBuilder) { |
| 48 | ydtExtBuilder = ydtBuilder; |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public void enterXmlElement(Element element, XmlNodeType nodeType, |
| 53 | Element rootElement) { |
| 54 | if (element.equals(rootElement)) { |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | YdtContextOperationType opType = null; |
| 59 | |
| 60 | for (Iterator iter = element.attributeIterator(); iter.hasNext();) { |
| 61 | Attribute attr = (Attribute) iter.next(); |
| 62 | if (attr.getName().equals(OPERATION)) { |
| 63 | opType = |
| 64 | YdtContextOperationType.valueOf(attr.getValue() |
| 65 | .toUpperCase()); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | String nameSpace = null; |
| 70 | if (element.getNamespace() != null) { |
| 71 | nameSpace = element.getNamespace().getURI(); |
| 72 | } |
| 73 | |
Vidyashree Rama | c848970 | 2016-12-19 09:38:31 +0530 | [diff] [blame] | 74 | /* |
| 75 | * When new module has to be added, and if curnode has reference of |
Sean Condon | fe6ceba | 2017-02-26 16:40:03 +0000 | [diff] [blame] | 76 | * previous module, then we need to traverse back to parent(logical |
| 77 | * root node). |
Vidyashree Rama | c848970 | 2016-12-19 09:38:31 +0530 | [diff] [blame] | 78 | */ |
| 79 | if (ydtExtBuilder.getRootNode() == ydtExtBuilder.getCurNode() |
| 80 | .getParent() && prevNodeNamespace != null && |
| 81 | !prevNodeNamespace.equals(nameSpace)) { |
| 82 | ydtExtBuilder.traverseToParent(); |
| 83 | } |
| 84 | |
Sean Condon | fe6ceba | 2017-02-26 16:40:03 +0000 | [diff] [blame] | 85 | if (nodeType == OBJECT_NODE && |
| 86 | (element.content() == null || element.content().isEmpty())) { |
| 87 | if (ydtExtBuilder != null) { |
| 88 | if (ydtExtBuilder.getCurNode() == ydtExtBuilder.getRootNode()) { |
| 89 | ydtExtBuilder.addChild(null, nameSpace, opType); |
| 90 | } |
| 91 | ydtExtBuilder.addNode(element.getName(), nameSpace); |
| 92 | } |
| 93 | } else if (nodeType == OBJECT_NODE) { |
Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame] | 94 | if (ydtExtBuilder != null) { |
Vidyashree Rama | c848970 | 2016-12-19 09:38:31 +0530 | [diff] [blame] | 95 | if (ydtExtBuilder.getCurNode() == ydtExtBuilder.getRootNode()) { |
| 96 | ydtExtBuilder.addChild(null, nameSpace, opType); |
| 97 | } |
Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame] | 98 | ydtExtBuilder.addChild(element.getName(), nameSpace, opType); |
| 99 | } |
| 100 | } else if (nodeType == TEXT_NODE) { |
Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame] | 101 | if (ydtExtBuilder != null) { |
Vidyashree Rama | c848970 | 2016-12-19 09:38:31 +0530 | [diff] [blame] | 102 | if (ydtExtBuilder.getCurNode() == ydtExtBuilder.getRootNode()) { |
| 103 | ydtExtBuilder.addChild(null, nameSpace, opType); |
| 104 | } |
Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame] | 105 | ydtExtBuilder.addLeaf(element.getName(), nameSpace, |
| 106 | element.getText()); |
| 107 | } |
| 108 | } |
Vidyashree Rama | c848970 | 2016-12-19 09:38:31 +0530 | [diff] [blame] | 109 | |
| 110 | if (nameSpace != null) { |
| 111 | prevNodeNamespace = nameSpace; |
| 112 | } |
Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public void exitXmlElement(Element element, XmlNodeType nodeType, |
| 117 | Element rootElement) { |
| 118 | if (element.equals(rootElement)) { |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | if (ydtExtBuilder != null) { |
| 123 | ydtExtBuilder.traverseToParent(); |
| 124 | } |
| 125 | } |
| 126 | } |