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.Element; |
| 20 | import org.onosproject.yms.app.ydt.YdtExtendedContext; |
| 21 | import org.onosproject.yms.ydt.YdtContext; |
| 22 | import org.onosproject.yms.ydt.YdtContextOperationType; |
| 23 | |
| 24 | import java.util.Stack; |
| 25 | |
| 26 | import static org.onosproject.yms.app.ych.defaultcodecs.netconf.NetconfCodecConstants.OPERATION; |
| 27 | import static org.onosproject.yms.ydt.YdtContextOperationType.NONE; |
| 28 | |
| 29 | /** |
| 30 | * Represents an codec handler to process the xml content and add |
| 31 | * element to the stack. |
| 32 | */ |
| 33 | public abstract class XmlCodecHandler { |
| 34 | |
| 35 | /** |
| 36 | * Sets the namespace and tag name in element tree maintained in stack. |
| 37 | * |
| 38 | * @param ydtContext YDT context |
| 39 | * @param elementStack element tree stack |
| 40 | */ |
| 41 | void processXmlContext(YdtContext ydtContext, |
| 42 | Stack<Element> elementStack) { |
| 43 | |
| 44 | Element newElement = updateNameAndNamespace(ydtContext, |
| 45 | elementStack.peek()); |
| 46 | elementStack.push(newElement); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Returns the new element name by updating tag name and namespace. |
| 51 | * |
| 52 | * @param ydtContext YDT context node |
| 53 | * @param xmlElement element in the stack used for adding new element |
| 54 | * @return new element name by updating tag name and namespace |
| 55 | */ |
| 56 | Element updateNameAndNamespace(YdtContext ydtContext, |
| 57 | Element xmlElement) { |
| 58 | String nameSpace = null; |
| 59 | if (ydtContext.getNamespace() != null) { |
| 60 | nameSpace = ydtContext.getNamespace(); |
| 61 | } |
| 62 | |
| 63 | String parentNameSpace = null; |
| 64 | if (ydtContext.getParent() != null) { |
| 65 | parentNameSpace = ydtContext.getParent().getNamespace(); |
| 66 | } |
| 67 | |
| 68 | Element newElement; |
| 69 | if (nameSpace != null) { |
| 70 | newElement = xmlElement.addElement(ydtContext.getName(), |
| 71 | nameSpace); |
| 72 | } else { |
| 73 | if (parentNameSpace != null) { |
| 74 | newElement = xmlElement.addElement(ydtContext.getName(), |
| 75 | parentNameSpace); |
| 76 | } else { |
| 77 | newElement = xmlElement.addElement(ydtContext.getName()); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | YdtContextOperationType opType = ((YdtExtendedContext) ydtContext) |
| 82 | .getYdtContextOperationType(); |
| 83 | if (opType != null && opType != NONE) { |
Sean Condon | b0720e7 | 2017-01-10 12:29:02 +0000 | [diff] [blame] | 84 | newElement.addAttribute("nc:" + OPERATION, |
Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame] | 85 | opType.toString().toLowerCase()); |
| 86 | } |
| 87 | |
| 88 | return newElement; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Sets the leaf value in the current element maintained in stack. |
| 93 | * Default behaviour is to do nothing. |
| 94 | * |
| 95 | * @param ydtContext YDT context node |
| 96 | * @param domElementStack current element node in the stack |
| 97 | */ |
| 98 | public void setXmlValue(YdtContext ydtContext, |
| 99 | Stack<Element> domElementStack) { |
| 100 | } |
| 101 | } |