2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.impl.codec.xml;
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
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.w3c.dom.Attr;
27 import org.w3c.dom.Document;
28 import org.w3c.dom.Element;
30 public final class XmlDocumentUtils {
31 public static final QName OPERATION_ATTRIBUTE_QNAME = QName.create(SchemaContext.NAME, "operation");
33 private XmlDocumentUtils() {
34 throw new UnsupportedOperationException("Utility class should not be instantiated");
37 public static Document getDocument() {
38 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
41 DocumentBuilder bob = dbf.newDocumentBuilder();
42 doc = bob.newDocument();
43 } catch (ParserConfigurationException e) {
44 throw new RuntimeException(e);
49 private static Element createElementFor(final Document doc, final QName qname, final Object obj) {
51 if (qname.getNamespace() != null) {
52 ret = doc.createElementNS(qname.getNamespace().toString(), qname.getLocalName());
54 ret = doc.createElementNS(null, qname.getLocalName());
57 if (obj instanceof AttributesContainer) {
58 final Map<QName, String> attrs = ((AttributesContainer)obj).getAttributes();
61 for (Entry<QName, String> attribute : attrs.entrySet()) {
62 ret.setAttributeNS(attribute.getKey().getNamespace().toString(), attribute.getKey().getLocalName(),
63 attribute.getValue());
71 public static Element createElementFor(final Document doc, final NormalizedNode<?, ?> data) {
72 return createElementFor(doc, data.getNodeType(), data);
75 public static QName qNameFromElement(final Element xmlElement) {
76 String namespace = xmlElement.getNamespaceURI();
77 String localName = xmlElement.getLocalName();
78 return QName.create(namespace != null ? URI.create(namespace) : null, null, localName);
81 public static Optional<ModifyAction> getModifyOperationFromAttributes(final Element xmlElement) {
82 Attr attributeNodeNS = xmlElement.getAttributeNodeNS(OPERATION_ATTRIBUTE_QNAME.getNamespace().toString(), OPERATION_ATTRIBUTE_QNAME.getLocalName());
83 if(attributeNodeNS == null) {
84 return Optional.absent();
87 ModifyAction action = ModifyAction.fromXmlValue(attributeNodeNS.getValue());
88 Preconditions.checkArgument(action.isOnElementPermitted(), "Unexpected operation %s on %s", action, xmlElement);
90 return Optional.of(action);
93 public static Optional<DataSchemaNode> findFirstSchema(final QName qname, final Iterable<DataSchemaNode> dataSchemaNode) {
94 if (dataSchemaNode != null && qname != null) {
95 for (DataSchemaNode dsn : dataSchemaNode) {
96 if (qname.isEqualWithoutRevision(dsn.getQName())) {
97 return Optional.of(dsn);
98 } else if (dsn instanceof ChoiceSchemaNode) {
99 for (ChoiceCaseNode choiceCase : ((ChoiceSchemaNode) dsn).getCases()) {
100 Optional<DataSchemaNode> foundDsn = findFirstSchema(qname, choiceCase.getChildNodes());
101 if (foundDsn != null && foundDsn.isPresent()) {
108 return Optional.absent();
111 public static XmlCodecProvider defaultValueCodecProvider() {
112 return XmlUtils.DEFAULT_XML_CODEC_PROVIDER;