Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.yms.app.ych.defaultcodecs.xml; |
| 18 | |
| 19 | import org.dom4j.Element; |
| 20 | import org.slf4j.Logger; |
| 21 | import org.slf4j.LoggerFactory; |
| 22 | |
| 23 | import java.util.Iterator; |
| 24 | |
| 25 | import static org.onosproject.yms.app.ych.defaultcodecs.xml.XmlNodeType.OBJECT_NODE; |
| 26 | import static org.onosproject.yms.app.ych.defaultcodecs.xml.XmlNodeType.TEXT_NODE; |
| 27 | |
| 28 | /** |
| 29 | * Represents implementation of codec xml walker. |
| 30 | */ |
| 31 | class 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 { |
| 38 | listener.enterXmlElement(element, getElementType(element), |
| 39 | rootElement); |
| 40 | |
| 41 | if (element.hasContent() && !element.isTextOnly()) { |
| 42 | for (Iterator i = element.elementIterator(); i.hasNext();) { |
| 43 | Element childElement = (Element) i.next(); |
| 44 | walk(listener, childElement, rootElement); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | listener.exitXmlElement(element, getElementType(element), |
| 49 | rootElement); |
| 50 | } catch (Exception e) { |
| 51 | log.error("Exception occurred when walk xml element: {}", element); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Determine the type of an element. |
| 57 | * |
| 58 | * @param element to be analysed |
| 59 | * @return type of the element |
| 60 | */ |
| 61 | private XmlNodeType getElementType(Element element) { |
| 62 | return element.hasContent() && element.isTextOnly() ? |
| 63 | TEXT_NODE : OBJECT_NODE; |
| 64 | } |
| 65 | } |