blob: cd9fc4f3a6b2b6dd1ff6abda833756789a500c74 [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.onosproject.yms.app.ydt.YdtExtendedContext;
21import org.onosproject.yms.ydt.YdtContext;
22import org.onosproject.yms.ydt.YdtContextOperationType;
23
24import java.util.Stack;
25
26import static org.onosproject.yms.app.ych.defaultcodecs.netconf.NetconfCodecConstants.OPERATION;
27import 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 */
33public 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 Condonb0720e72017-01-10 12:29:02 +000084 newElement.addAttribute("nc:" + OPERATION,
Shankara-Huaweid5823ab2016-11-22 10:14:52 +053085 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}