Bug 714 - Fixed creating DOM Document's element with namespace
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / mapping / attributes / toxml / SimpleAttributeWritingStrategy.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.util.Util;
12 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
13 import org.w3c.dom.Document;
14 import org.w3c.dom.Element;
15
16 import com.google.common.base.Optional;
17
18 public class SimpleAttributeWritingStrategy implements AttributeWritingStrategy {
19
20     private final Document document;
21     private final String key;
22
23     /**
24      * @param document
25      * @param key
26      */
27     public SimpleAttributeWritingStrategy(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         value = preprocess(value);
35         Util.checkType(value, String.class);
36         Element innerNode = createElement(document, key, (String) value, Optional.of(namespace));
37         parentElement.appendChild(innerNode);
38     }
39
40     protected Element createElement(Document document, String key, String value, Optional<String> namespace) {
41         Element typeElement = XmlUtil.createElement(document, key, namespace);
42
43         typeElement.appendChild(document.createTextNode(value));
44         return typeElement;
45     }
46
47     protected Object preprocess(Object value) {
48         return value;
49     }
50
51
52 }