Degrade DataNodeContainer.getChildNodes() from Set to Collection
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / codec / xml / XmlDocumentUtils.java
index a3a2a86e886c5ebcdb2b3e3bc8af1bd81e857332..262a48a93385579c74061d497ea11c0abcdcb18a 100644 (file)
@@ -9,6 +9,13 @@ package org.opendaylight.yangtools.yang.data.impl.codec.xml;
 
 import static com.google.common.base.Preconditions.checkState;
 
+import com.google.common.base.Function;
+import com.google.common.base.Objects;
+import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
+import com.google.common.collect.ImmutableList;
+
 import java.net.URI;
 import java.util.ArrayList;
 import java.util.List;
@@ -34,7 +41,6 @@ import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode;
 import org.opendaylight.yangtools.yang.data.impl.SimpleNodeTOImpl;
 import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
-import org.opendaylight.yangtools.yang.data.impl.util.CompositeNodeBuilder;
 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
@@ -55,13 +61,6 @@ import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.NodeList;
 
-import com.google.common.base.Function;
-import com.google.common.base.Objects;
-import com.google.common.base.Optional;
-import com.google.common.base.Preconditions;
-import com.google.common.base.Strings;
-import com.google.common.collect.ImmutableList;
-
 public class XmlDocumentUtils {
     private static class ElementWithSchemaContext {
         Element element;
@@ -84,12 +83,6 @@ public class XmlDocumentUtils {
     public static final QName OPERATION_ATTRIBUTE_QNAME = QName.create(URI.create("urn:ietf:params:xml:ns:netconf:base:1.0"), null, "operation");
     private static final Logger logger = LoggerFactory.getLogger(XmlDocumentUtils.class);
     private static final XMLOutputFactory FACTORY = XMLOutputFactory.newFactory();
-    private static final XmlCodecProvider DEFAULT_XML_VALUE_CODEC_PROVIDER = new XmlCodecProvider() {
-        @Override
-        public TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> codecFor(final TypeDefinition<?> baseType) {
-            return TypeDefinitionAwareCodec.from(baseType);
-        }
-    };
 
     /**
      * Converts Data DOM structure to XML Document for specified XML Codec Provider and corresponding
@@ -112,8 +105,7 @@ public class XmlDocumentUtils {
             throw new UnsupportedDataTypeException("Schema can be ContainerSchemaNode or ListSchemaNode. Other types are not supported yet.");
         }
 
-        final DOMResult result = new DOMResult();
-        result.setNode(getDocument());
+        final DOMResult result = new DOMResult(getDocument());
         try {
             final XMLStreamWriter writer = FACTORY.createXMLStreamWriter(result);
             XmlStreamUtils.create(codecProvider).writeDocument(writer, data, (SchemaNode)schema);
@@ -148,7 +140,7 @@ public class XmlDocumentUtils {
      * @throws UnsupportedDataTypeException
      */
     public static Document toDocument(final CompositeNode data, final XmlCodecProvider codecProvider) {
-        final DOMResult result = new DOMResult();
+        final DOMResult result = new DOMResult(getDocument());
         try {
             final XMLStreamWriter writer = FACTORY.createXMLStreamWriter(result);
             XmlStreamUtils.create(codecProvider).writeDocument(writer, data);
@@ -193,18 +185,11 @@ public class XmlDocumentUtils {
     public static Node<?> toDomNode(final Element xmlElement, final Optional<DataSchemaNode> schema,
             final Optional<XmlCodecProvider> codecProvider) {
         if (schema.isPresent()) {
-            return toNodeWithSchema(xmlElement, schema.get(), codecProvider.or(DEFAULT_XML_VALUE_CODEC_PROVIDER));
+            return toNodeWithSchema(xmlElement, schema.get(), codecProvider.or(XmlUtils.DEFAULT_XML_CODEC_PROVIDER));
         }
         return toDomNode(xmlElement);
     }
 
-    public static CompositeNode fromElement(final Element xmlElement) {
-        CompositeNodeBuilder<ImmutableCompositeNode> node = ImmutableCompositeNode.builder();
-        node.setQName(qNameFromElement(xmlElement));
-
-        return node.toInstance();
-    }
-
     public static QName qNameFromElement(final Element xmlElement) {
         String namespace = xmlElement.getNamespaceURI();
         String localName = xmlElement.getLocalName();
@@ -293,8 +278,8 @@ public class XmlDocumentUtils {
         checkState(qName.getLocalName().equals(xmlElement.getLocalName()));
     }
 
-    public static final Optional<DataSchemaNode> findFirstSchema(final QName qname, final Set<DataSchemaNode> dataSchemaNode) {
-        if (dataSchemaNode != null && !dataSchemaNode.isEmpty() && qname != null) {
+    public static final Optional<DataSchemaNode> findFirstSchema(final QName qname, final Iterable<DataSchemaNode> dataSchemaNode) {
+        if (dataSchemaNode != null && qname != null) {
             for (DataSchemaNode dsn : dataSchemaNode) {
                 if (qname.isEqualWithoutRevision(dsn.getQName())) {
                     return Optional.<DataSchemaNode> of(dsn);
@@ -341,7 +326,7 @@ public class XmlDocumentUtils {
         return ImmutableCompositeNode.create(qname, values.build());
     }
 
-    public static List<Node<?>> toDomNodes(final Element element, final Optional<Set<DataSchemaNode>> context,final SchemaContext schemaCtx) {
+    public static List<Node<?>> toDomNodes(final Element element, final Optional<? extends Iterable<DataSchemaNode>> context, final SchemaContext schemaCtx) {
         return forEachChild(element.getChildNodes(),schemaCtx, new Function<ElementWithSchemaContext, Optional<Node<?>>>() {
 
             @Override
@@ -350,8 +335,8 @@ public class XmlDocumentUtils {
                     QName partialQName = qNameFromElement(input.getElement());
                     Optional<DataSchemaNode> schemaNode = findFirstSchema(partialQName, context.get());
                     if (schemaNode.isPresent()) {
-                        return Optional.<Node<?>> fromNullable(//
-                                toNodeWithSchema(input.getElement(), schemaNode.get(), DEFAULT_XML_VALUE_CODEC_PROVIDER,input.getSchemaContext()));
+                        return Optional.<Node<?>> fromNullable(
+                                toNodeWithSchema(input.getElement(), schemaNode.get(), XmlUtils.DEFAULT_XML_CODEC_PROVIDER, input.getSchemaContext()));
                     }
                 }
                 return Optional.<Node<?>> fromNullable(toDomNode(input.getElement()));
@@ -361,8 +346,8 @@ public class XmlDocumentUtils {
 
     }
 
-    public static List<Node<?>> toDomNodes(final Element element, final Optional<Set<DataSchemaNode>> context) {
-        return toDomNodes(element,context,null);
+    public static List<Node<?>> toDomNodes(final Element element, final Optional<? extends Iterable<DataSchemaNode>> context) {
+        return toDomNodes(element, context, null);
     }
 
     /**
@@ -398,9 +383,9 @@ public class XmlDocumentUtils {
                     final Optional<NotificationDefinition> notificationDef = findNotification(partialQName,
                             notifications.get());
                     if (notificationDef.isPresent()) {
-                        final Set<DataSchemaNode> dataNodes = notificationDef.get().getChildNodes();
+                        final Iterable<DataSchemaNode> dataNodes = notificationDef.get().getChildNodes();
                         final List<Node<?>> domNodes = toDomNodes(childElement,
-                                Optional.<Set<DataSchemaNode>> fromNullable(dataNodes),schemaCtx);
+                                Optional.<Iterable<DataSchemaNode>> fromNullable(dataNodes),schemaCtx);
                         return ImmutableCompositeNode.create(notificationDef.get().getQName(), domNodes);
                     }
                 }
@@ -446,6 +431,6 @@ public class XmlDocumentUtils {
     }
 
     public static final XmlCodecProvider defaultValueCodecProvider() {
-        return DEFAULT_XML_VALUE_CODEC_PROVIDER;
+        return XmlUtils.DEFAULT_XML_CODEC_PROVIDER;
     }
 }