Removed unused dependency.
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / codec / xml / XmlDocumentUtils.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 package org.opendaylight.yangtools.yang.data.impl.codec.xml;
9
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import java.net.URI;
13 import java.util.Map;
14 import java.util.Map.Entry;
15 import javax.xml.parsers.DocumentBuilder;
16 import javax.xml.parsers.DocumentBuilderFactory;
17 import javax.xml.parsers.ParserConfigurationException;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.data.api.AttributesContainer;
20 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
22 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
23 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.w3c.dom.Attr;
29 import org.w3c.dom.Document;
30 import org.w3c.dom.Element;
31
32 public final class XmlDocumentUtils {
33     public static final QName OPERATION_ATTRIBUTE_QNAME = QName.create(SchemaContext.NAME, "operation");
34     private static final Logger LOG = LoggerFactory.getLogger(XmlDocumentUtils.class);
35
36     private XmlDocumentUtils() {
37         throw new UnsupportedOperationException("Utility class should not be instantiated");
38     }
39
40     public static Document getDocument() {
41         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
42         Document doc = null;
43         try {
44             DocumentBuilder bob = dbf.newDocumentBuilder();
45             doc = bob.newDocument();
46         } catch (ParserConfigurationException e) {
47             throw new RuntimeException(e);
48         }
49         return doc;
50     }
51
52     private static Element createElementFor(final Document doc, final QName qname, final Object obj) {
53         final Element ret;
54         if (qname.getNamespace() != null) {
55             ret = doc.createElementNS(qname.getNamespace().toString(), qname.getLocalName());
56         } else {
57             ret = doc.createElementNS(null, qname.getLocalName());
58         }
59
60         if (obj instanceof AttributesContainer) {
61             final Map<QName, String> attrs = ((AttributesContainer)obj).getAttributes();
62
63             if (attrs != null) {
64                 for (Entry<QName, String> attribute : attrs.entrySet()) {
65                     ret.setAttributeNS(attribute.getKey().getNamespace().toString(), attribute.getKey().getLocalName(),
66                             attribute.getValue());
67                 }
68             }
69         }
70
71         return ret;
72     }
73
74     public static Element createElementFor(final Document doc, final NormalizedNode<?, ?> data) {
75         return createElementFor(doc, data.getNodeType(), data);
76     }
77
78     public static QName qNameFromElement(final Element xmlElement) {
79         String namespace = xmlElement.getNamespaceURI();
80         String localName = xmlElement.getLocalName();
81         return QName.create(namespace != null ? URI.create(namespace) : null, null, localName);
82     }
83
84     public static Optional<ModifyAction> getModifyOperationFromAttributes(final Element xmlElement) {
85         Attr attributeNodeNS = xmlElement.getAttributeNodeNS(OPERATION_ATTRIBUTE_QNAME.getNamespace().toString(), OPERATION_ATTRIBUTE_QNAME.getLocalName());
86         if(attributeNodeNS == null) {
87             return Optional.absent();
88         }
89
90         ModifyAction action = ModifyAction.fromXmlValue(attributeNodeNS.getValue());
91         Preconditions.checkArgument(action.isOnElementPermitted(), "Unexpected operation %s on %s", action, xmlElement);
92
93         return Optional.of(action);
94     }
95
96     public static Optional<DataSchemaNode> findFirstSchema(final QName qname, final Iterable<DataSchemaNode> dataSchemaNode) {
97         if (dataSchemaNode != null && qname != null) {
98             for (DataSchemaNode dsn : dataSchemaNode) {
99                 if (qname.isEqualWithoutRevision(dsn.getQName())) {
100                     return Optional.<DataSchemaNode> of(dsn);
101                 } else if (dsn instanceof ChoiceSchemaNode) {
102                     for (ChoiceCaseNode choiceCase : ((ChoiceSchemaNode) dsn).getCases()) {
103                         Optional<DataSchemaNode> foundDsn = findFirstSchema(qname, choiceCase.getChildNodes());
104                         if (foundDsn != null && foundDsn.isPresent()) {
105                             return foundDsn;
106                         }
107                     }
108                 }
109             }
110         }
111         return Optional.absent();
112     }
113
114     public static XmlCodecProvider defaultValueCodecProvider() {
115         return XmlUtils.DEFAULT_XML_CODEC_PROVIDER;
116     }
117 }