Revert "Added w3c Document to yang-data-api codec with schema support" 30/4230/2
authorTony Tkacik <ttkacik@cisco.com>
Tue, 14 Jan 2014 16:00:17 +0000 (16:00 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Tue, 14 Jan 2014 16:00:33 +0000 (16:00 +0000)
This reverts commit 5a53f88da0e878808be2698827842038aefbaa71.

Change-Id: Ic14f16f81b69294d49c0b34d96a98618f5906558
Signed-off-by: Tony Tkacik <ttkacik@cisco.com>
yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/ImmutableCompositeNode.java
yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/codec/xml/XmlDocumentUtils.java
yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/LeafListSchemaNode.java
yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/LeafSchemaNode.java
yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/SimpleValueSchemaNode.java [deleted file]
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/builder/impl/LeafListSchemaNodeBuilder.java

index f88c9a9320b772f2c6fabb123809c12d617c8246..d423506959ebc1d98623739fd218bf302b4bf751 100644 (file)
@@ -77,9 +77,10 @@ public final class ImmutableCompositeNode extends AbstractNodeTO<List<Node<?>>>
      * @param parent
      *            use null to create top composite node (without parent)
      * @param value
+     * @param modifyAction
      */
-    public ImmutableCompositeNode(QName qname, List<Node<?>> value) {
-        super(qname, null, ImmutableList.copyOf(value));
+    public ImmutableCompositeNode(QName qname, List<Node<?>> value, ModifyAction modifyAction) {
+        super(qname, null, value, modifyAction);
         init();
     }
 
@@ -253,8 +254,6 @@ public final class ImmutableCompositeNode extends AbstractNodeTO<List<Node<?>>>
     public Collection<List<Node<?>>> values() {
         return nodeMap.values();
     }
-    
-    
 
     // Serialization related
 
index b066afd517a7586f87f13c2565c96683cae18deb..ad639b74c86811144f7228af29fd93664c1e8fd5 100644 (file)
@@ -1,9 +1,5 @@
 package org.opendaylight.yangtools.yang.data.impl.codec.xml;
 
-import static com.google.common.base.Preconditions.checkState;
-
-import java.net.URI;
-import java.util.List;
 import java.util.Set;
 
 import javax.activation.UnsupportedDataTypeException;
@@ -15,34 +11,15 @@ import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
 import org.opendaylight.yangtools.yang.data.api.Node;
 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
-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;
-import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
-import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.SchemaNode;
-import org.opendaylight.yangtools.yang.model.api.SimpleValueSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.*;
 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 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 {
 
@@ -79,60 +56,10 @@ public class XmlDocumentUtils {
         }
     }
 
-    public static Node<?> toDomNode(Element xmlElement, Optional<DataSchemaNode> schema,
-            Optional<XmlCodecProvider> codecProvider) {
-        if (schema.isPresent()) {
-            return toNodeWithSchema(xmlElement, schema.get(), codecProvider.or(DEFAULT_XML_VALUE_CODEC_PROVIDER));
-        }
-        return toDomNode(xmlElement);
-    }
-
-    public static CompositeNode fromElement(Element xmlElement) {
-        CompositeNodeBuilder<ImmutableCompositeNode> node = ImmutableCompositeNode.builder();
-        node.setQName(qNameFromElement(xmlElement));
-
-        return node.toInstance();
-    }
-
-    private static QName qNameFromElement(Element xmlElement) {
-        String namespace = xmlElement.getNamespaceURI();
-        String localName = xmlElement.getLocalName();
-        return QName.create(namespace != null ? URI.create(namespace) : null, null, localName);
-    }
-
-    private static Node<?> toNodeWithSchema(Element xmlElement, DataSchemaNode schema, XmlCodecProvider codecProvider) {
-        checkQName(xmlElement, schema.getQName());
-        if (schema instanceof DataNodeContainer) {
-            return toCompositeNodeWithSchema(xmlElement, schema.getQName(), (DataNodeContainer) schema, codecProvider);
-        } else if (schema instanceof SimpleValueSchemaNode) {
-            return toSimpleNodeWithType(xmlElement, (SimpleValueSchemaNode) schema, codecProvider);
-        }
-        return null;
-    }
-
-    private static Node<?> toSimpleNodeWithType(Element xmlElement, SimpleValueSchemaNode schema,
-            XmlCodecProvider codecProvider) {
-        TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> codec = codecProvider.codecFor(schema.getType());
-        String text = xmlElement.getTextContent();
-        Object value = codec.deserialize(text);
-        return new SimpleNodeTOImpl<Object>(schema.getQName(), null, value);
-    }
-
-    private static Node<?> toCompositeNodeWithSchema(Element xmlElement, QName qName, DataNodeContainer schema,
-            XmlCodecProvider codecProvider) {
-        List<Node<?>> values = toDomNodes(xmlElement, Optional.fromNullable(schema.getChildNodes()));
-        return new ImmutableCompositeNode(qName, values );
-    }
-
-    private static void checkQName(Element xmlElement, QName qName) {
-        checkState(Objects.equal(xmlElement.getNamespaceURI(), qName.getNamespace().toString()));
-        checkState(qName.getLocalName().equals(xmlElement.getLocalName()));
-    }
-
     private static Element createXmlRootElement(Document doc, Node<?> data, SchemaNode schema,
             XmlCodecProvider codecProvider) throws UnsupportedDataTypeException {
         QName dataType = data.getNodeType();
-        Element itemEl = createElementFor(doc, dataType);
+        Element itemEl = doc.createElementNS(dataType.getNamespace().toString(), dataType.getLocalName());
         if (data instanceof SimpleNode<?>) {
             if (schema instanceof LeafListSchemaNode) {
                 writeValueByType(itemEl, (SimpleNode<?>) data, ((LeafListSchemaNode) schema).getType(),
@@ -165,14 +92,6 @@ public class XmlDocumentUtils {
         return itemEl;
     }
 
-    private static Element createElementFor(Document doc, QName dataType) {
-        if (dataType.getNamespace() != null) {
-            return doc.createElementNS(dataType.getNamespace().toString(), dataType.getLocalName());
-        } else {
-            return doc.createElementNS(null, dataType.getLocalName());
-        }
-    }
-
     public static void writeValueByType(Element element, SimpleNode<?> node, TypeDefinition<?> type,
             DataSchemaNode schema, XmlCodecProvider codecProvider) {
 
@@ -188,7 +107,8 @@ public class XmlDocumentUtils {
                 element.setAttribute("xmlns:" + prefix, value.getNamespace().toString());
                 element.setTextContent(prefix + ":" + value.getLocalName());
             } else {
-                logger.debug("Value of {}:{} is not instance of QName but is {}", baseType.getQName().getNamespace(), //
+                logger.debug("Value of {}:{} is not instance of QName but is {}",
+                        baseType.getQName().getNamespace(), //
                         baseType.getQName().getLocalName(), //
                         node.getValue().getClass());
                 element.setTextContent(String.valueOf(node.getValue()));
@@ -200,7 +120,7 @@ public class XmlDocumentUtils {
                     element.setTextContent(value);
                 } catch (ClassCastException e) {
                     element.setTextContent(String.valueOf(node.getValue()));
-                    logger.error("Provided node did not have type required by mapping. Using stream instead. {}", e);
+                    logger.error("Provided node did not have type required by mapping. Using stream instead. {}",e);
                 }
             }
         }
@@ -232,90 +152,8 @@ public class XmlDocumentUtils {
         return null;
     }
 
-    private static final Optional<DataSchemaNode> findFirstSchema(QName qname, Set<DataSchemaNode> dataSchemaNode) {
-        if (dataSchemaNode != null && !dataSchemaNode.isEmpty() && qname != null) {
-            for (DataSchemaNode dsn : dataSchemaNode) {
-                if (qname.isEqualWithoutRevision(dsn.getQName())) {
-                    return Optional.<DataSchemaNode> of(dsn);
-                } else if (dsn instanceof ChoiceNode) {
-                    for (ChoiceCaseNode choiceCase : ((ChoiceNode) dsn).getCases()) {
-                        Optional<DataSchemaNode> foundDsn = findFirstSchema(qname, choiceCase.getChildNodes());
-                        if (foundDsn != null) {
-                            return foundDsn;
-                        }
-                    }
-                }
-            }
-        }
-        return Optional.absent();
-    }
-
     public static final XmlCodecProvider defaultValueCodecProvider() {
         return DEFAULT_XML_VALUE_CODEC_PROVIDER;
     }
 
-    public static Node<?> toDomNode(Document doc) {
-        return toDomNode(doc.getDocumentElement());
-    }
-
-    private static Node<?> toDomNode(Element element) {
-        QName qname = qNameFromElement(element);
-
-        ImmutableList.Builder<Node<?>> values = ImmutableList.<Node<?>> builder();
-        NodeList nodes = element.getChildNodes();
-        boolean isSimpleObject = true;
-        String value = null;
-        for (int i = 0; i < nodes.getLength(); i++) {
-            org.w3c.dom.Node child = nodes.item(i);
-            if (child instanceof Element) {
-                isSimpleObject = false;
-                values.add(toDomNode((Element) child));
-            }
-            if (isSimpleObject && child instanceof org.w3c.dom.Text) {
-                value = element.getTextContent();
-                if (!Strings.isNullOrEmpty(value)) {
-                    isSimpleObject = true;
-                }
-            }
-        }
-        if (isSimpleObject) {
-            return new SimpleNodeTOImpl<>(qname, null, value);
-        }
-        return new ImmutableCompositeNode(qname, values.build());
-    }
-
-    public static List<Node<?>> toDomNodes(final Element element, final Optional<Set<DataSchemaNode>> context) {
-        return forEachChild(element.getChildNodes(), new Function<Element, Optional<Node<?>>>() {
-
-            @Override
-            public Optional<Node<?>> apply(Element input) {
-                if (context.isPresent()) {
-                    QName partialQName = qNameFromElement(input);
-                    Optional<DataSchemaNode> schemaNode = findFirstSchema(partialQName, context.get());
-                    if (schemaNode.isPresent()) {
-                        return Optional.<Node<?>> fromNullable(//
-                                toNodeWithSchema(input, schemaNode.get(), DEFAULT_XML_VALUE_CODEC_PROVIDER));
-                    }
-                }
-                return Optional.<Node<?>> fromNullable(toDomNode(element));
-            }
-
-        });
-
-    }
-
-    private static final <T> List<T> forEachChild(NodeList nodes, Function<Element, Optional<T>> forBody) {
-        ImmutableList.Builder<T> ret = ImmutableList.<T> builder();
-        for (int i = 0; i < nodes.getLength(); i++) {
-            org.w3c.dom.Node child = nodes.item(i);
-            if(child instanceof Element) {
-                Optional<T> result = forBody.apply((Element) child);
-                if(result.isPresent()) {
-                    ret.add(result.get());
-                }
-            }
-        }
-        return ret.build();
-    }
-
 }
index a8607ca010825094b60e4594b548f35366a9f5f7..b6c2a6ec42102e7faf4dd75f9313ab06b134efc4 100644 (file)
@@ -10,7 +10,7 @@ package org.opendaylight.yangtools.yang.model.api;
 /**
  * Interface describing YANG 'leaf-list' statement.
  */
-public interface LeafListSchemaNode extends SimpleValueSchemaNode {
+public interface LeafListSchemaNode extends DataSchemaNode {
 
     /**
      * Returns type of the instance which implements <code>DataSchemaNode</code>
index da1f7f56dfeff24bdf45ece67ef8e517a175a83a..37b2ef0744c0f5170400c6d74da11e7d2fd439f9 100644 (file)
@@ -20,6 +20,33 @@ package org.opendaylight.yangtools.yang.model.api;
  * The 'leaf' statement is used to define a leaf node in the schema tree.
  * </p>
  */
-public interface LeafSchemaNode extends SimpleValueSchemaNode {
+public interface LeafSchemaNode extends DataSchemaNode {
+
+    /**
+     * Returns the YANG <code>type</code> of the instance of the type
+     * <code>LeafSchemaNode</code>.
+     * 
+     * @return type definition which represents the value of the YANG
+     *         <code>type</code> substatement for <code>leaf</code> statement
+     */
+    TypeDefinition<?> getType();
+
+    /**
+     * Returns the default value of YANG <code>leaf</code>.
+     * 
+     * @return string with the value of the argument of YANG
+     *         <code>default</code> substatement of the <code>leaf</code>
+     *         statement
+     */
+    String getDefault();
+
+    /**
+     * Returns the units in which are the values of the <code>leaf</code>
+     * presented.
+     * 
+     * @return string with the value of the argument of YANG <code>units</code>
+     *         substatement of the <code>leaf</code> statement
+     */
+    String getUnits();
 
 }
diff --git a/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/SimpleValueSchemaNode.java b/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/SimpleValueSchemaNode.java
deleted file mode 100644 (file)
index ae80917..0000000
+++ /dev/null
@@ -1,32 +0,0 @@
-package org.opendaylight.yangtools.yang.model.api;
-
-public interface SimpleValueSchemaNode extends DataSchemaNode {
-    
-    /**
-     * Returns the YANG <code>type</code> of the instance of the type
-     * <code>LeafSchemaNode</code>.
-     * 
-     * @return type definition which represents the value of the YANG
-     *         <code>type</code> substatement for <code>leaf</code> statement
-     */
-    TypeDefinition<?> getType();
-
-    /**
-     * Returns the default value of YANG <code>leaf</code>.
-     * 
-     * @return string with the value of the argument of YANG
-     *         <code>default</code> substatement of the <code>leaf</code>
-     *         statement
-     */
-    String getDefault();
-
-    /**
-     * Returns the units in which are the values of the <code>leaf</code>
-     * presented.
-     * 
-     * @return string with the value of the argument of YANG <code>units</code>
-     *         substatement of the <code>leaf</code> statement
-     */
-    String getUnits();
-
-}
index bb86908691b764a81461f8e9f70e73f469ed478d..ea7bbd9772746f115759b0ddd5d5ebe588f4147e 100644 (file)
@@ -291,19 +291,6 @@ public final class LeafListSchemaNodeBuilder extends AbstractTypeAwareBuilder im
         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
             return Collections.unmodifiableList(unknownNodes);
         }
-        
-        
-        @Override
-        public String getDefault() {
-            // TODO Auto-generated method stub
-            return null;
-        }
-        
-        @Override
-        public String getUnits() {
-            // TODO Auto-generated method stub
-            return null;
-        }
 
         private void addUnknownSchemaNodes(List<UnknownSchemaNode> unknownNodes) {
             if (unknownNodes != null) {