blob: 290d4c372a9ae194e6c8c45c7789f10605db1b22 [file] [log] [blame]
Shankara-Huaweid5823ab2016-11-22 10:14:52 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Shankara-Huaweid5823ab2016-11-22 10:14:52 +05303 *
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.Element;
20import org.slf4j.Logger;
21import org.slf4j.LoggerFactory;
22
23import java.util.Iterator;
24
25import static org.onosproject.yms.app.ych.defaultcodecs.xml.XmlNodeType.OBJECT_NODE;
26import static org.onosproject.yms.app.ych.defaultcodecs.xml.XmlNodeType.TEXT_NODE;
27
28/**
29 * Represents implementation of codec xml walker.
30 */
31class DefaultXmlCodecWalker implements XmlWalker {
32 private final Logger log = LoggerFactory.getLogger(getClass());
33
34 @Override
35 public void walk(XmlListener listener, Element element,
36 Element rootElement) {
37 try {
Vidyashree Ramac8489702016-12-19 09:38:31 +053038 Element newElement = element.createCopy();
39 newElement.remove(element.getNamespace());
40
41 listener.enterXmlElement(element, getElementType(newElement),
Shankara-Huaweid5823ab2016-11-22 10:14:52 +053042 rootElement);
43
44 if (element.hasContent() && !element.isTextOnly()) {
45 for (Iterator i = element.elementIterator(); i.hasNext();) {
46 Element childElement = (Element) i.next();
47 walk(listener, childElement, rootElement);
48 }
49 }
50
51 listener.exitXmlElement(element, getElementType(element),
52 rootElement);
53 } catch (Exception e) {
54 log.error("Exception occurred when walk xml element: {}", element);
55 }
56 }
57
58 /**
59 * Determine the type of an element.
60 *
61 * @param element to be analysed
62 * @return type of the element
63 */
64 private XmlNodeType getElementType(Element element) {
65 return element.hasContent() && element.isTextOnly() ?
66 TEXT_NODE : OBJECT_NODE;
67 }
68}