8563b781e3403ba1e45c1bd06f15d2c1c536a51e
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / mapping / attributes / toxml / ObjectNameAttributeWritingStrategy.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.toxml;
10
11 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.mapping.ObjectNameAttributeMappingStrategy;
12 import org.opendaylight.controller.netconf.confignetconfconnector.util.Util;
13 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
14 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
15 import org.w3c.dom.Document;
16 import org.w3c.dom.Element;
17
18 public class ObjectNameAttributeWritingStrategy implements AttributeWritingStrategy {
19
20     private final Document document;
21     private final String key;
22
23     /**
24      * @param document
25      * @param key
26      */
27     public ObjectNameAttributeWritingStrategy(Document document, String key) {
28         this.document = document;
29         this.key = key;
30     }
31
32     @Override
33     public void writeElement(Element parentElement, String namespace, Object value) {
34         Util.checkType(value, ObjectNameAttributeMappingStrategy.MappedDependency.class);
35         Element innerNode = document.createElement(key);
36         XmlUtil.addNamespaceAttr(innerNode, namespace);
37
38         String moduleName = ((ObjectNameAttributeMappingStrategy.MappedDependency) value).getServiceName();
39         String refName = ((ObjectNameAttributeMappingStrategy.MappedDependency) value).getRefName();
40         String namespaceForType = ((ObjectNameAttributeMappingStrategy.MappedDependency) value).getNamespace();
41
42         Element typeElement = XmlUtil.createPrefixedTextElement(document, XmlNetconfConstants.TYPE_KEY, XmlNetconfConstants.PREFIX,
43                 moduleName);
44         XmlUtil.addPrefixedNamespaceAttr(typeElement, XmlNetconfConstants.PREFIX, namespaceForType);
45         innerNode.appendChild(typeElement);
46
47         final Element nameElement = XmlUtil.createTextElement(document, XmlNetconfConstants.NAME_KEY, refName);
48         innerNode.appendChild(nameElement);
49
50         parentElement.appendChild(innerNode);
51     }
52
53 }