Rename XmlStreamUtils 03/53303/5
authorRobert Varga <rovarga@cisco.com>
Tue, 14 Mar 2017 20:35:42 +0000 (21:35 +0100)
committerRobert Varga <rovarga@cisco.com>
Fri, 17 Mar 2017 09:31:02 +0000 (10:31 +0100)
The utilities come in two flavors, schema-aware and schema-less,
so split them out into two separate classes. This flushes out the fact
the schema-less version cannot serialize instance identifiers, so mark
that explicitly.

Change-Id: I59b0f63ba80b6cb44570df19bef2e20675755d01
Signed-off-by: Robert Varga <rovarga@cisco.com>
yang/yang-data-codec-xml/src/main/java/org/opendaylight/yangtools/yang/data/codec/xml/SchemaAwareXMLStreamNormalizedNodeStreamWriter.java
yang/yang-data-codec-xml/src/main/java/org/opendaylight/yangtools/yang/data/codec/xml/SchemaAwareXMLStreamWriterUtils.java [new file with mode: 0644]
yang/yang-data-codec-xml/src/main/java/org/opendaylight/yangtools/yang/data/codec/xml/SchemalessXMLStreamWriterUtils.java [new file with mode: 0644]
yang/yang-data-codec-xml/src/main/java/org/opendaylight/yangtools/yang/data/codec/xml/XMLStreamWriterUtils.java [moved from yang/yang-data-codec-xml/src/main/java/org/opendaylight/yangtools/yang/data/codec/xml/XmlStreamUtils.java with 58% similarity]
yang/yang-data-codec-xml/src/test/java/org/opendaylight/yangtools/yang/data/codec/xml/XmlStreamUtilsTest.java

index b33743519eab250738eb7fcf2a2ce3c0d3e83f76..b625eaa4f12417a4b499ed46204ee2a8b0a0f2a7 100644 (file)
@@ -33,13 +33,13 @@ import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 
 final class SchemaAwareXMLStreamNormalizedNodeStreamWriter extends XMLStreamNormalizedNodeStreamWriter<SchemaNode> {
     private final SchemaTracker tracker;
-    private final XmlStreamUtils streamUtils;
+    private final XMLStreamWriterUtils streamUtils;
 
     private SchemaAwareXMLStreamNormalizedNodeStreamWriter(final XMLStreamWriter writer, final SchemaContext context,
                                                            final SchemaPath path) {
         super(writer);
         this.tracker = SchemaTracker.create(context, path);
-        this.streamUtils = XmlStreamUtils.create(context);
+        this.streamUtils = XMLStreamWriterUtils.create(context);
     }
 
     static NormalizedNodeStreamWriter newInstance(final XMLStreamWriter writer, final SchemaContext context,
diff --git a/yang/yang-data-codec-xml/src/main/java/org/opendaylight/yangtools/yang/data/codec/xml/SchemaAwareXMLStreamWriterUtils.java b/yang/yang-data-codec-xml/src/main/java/org/opendaylight/yangtools/yang/data/codec/xml/SchemaAwareXMLStreamWriterUtils.java
new file mode 100644 (file)
index 0000000..5f7f5bb
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ * Copyright (c) 2017 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.data.codec.xml;
+
+import com.google.common.base.Preconditions;
+import com.google.common.base.Verify;
+import java.net.URI;
+import java.util.Map.Entry;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.api.SchemaNode;
+import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
+import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
+
+final class SchemaAwareXMLStreamWriterUtils extends XMLStreamWriterUtils {
+    private final SchemaContext schemaContext;
+
+    SchemaAwareXMLStreamWriterUtils(final SchemaContext schemaContext) {
+        this.schemaContext = Preconditions.checkNotNull(schemaContext);
+    }
+
+    @Override
+    TypeDefinition<?> getBaseTypeForLeafRef(final SchemaNode schemaNode, final LeafrefTypeDefinition type) {
+        final TypeDefinition<?> ret = SchemaContextUtil.getBaseTypeForLeafRef(type, schemaContext, schemaNode);
+        return Verify.verifyNotNull(ret, "Unable to find base type for leafref node '%s'.", schemaNode.getPath());
+    }
+
+    @Override
+    void writeInstanceIdentifier(final XMLStreamWriter writer, final YangInstanceIdentifier value)
+            throws XMLStreamException {
+        RandomPrefixInstanceIdentifierSerializer iiCodec = new RandomPrefixInstanceIdentifierSerializer(schemaContext);
+        String serializedValue = iiCodec.serialize(value);
+
+        for (Entry<URI, String> e : iiCodec.getPrefixes()) {
+            writer.writeNamespace(e.getValue(), e.getKey().toString());
+        }
+
+        writer.writeCharacters(serializedValue);
+    }
+}
diff --git a/yang/yang-data-codec-xml/src/main/java/org/opendaylight/yangtools/yang/data/codec/xml/SchemalessXMLStreamWriterUtils.java b/yang/yang-data-codec-xml/src/main/java/org/opendaylight/yangtools/yang/data/codec/xml/SchemalessXMLStreamWriterUtils.java
new file mode 100644 (file)
index 0000000..c6c1e3f
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * Copyright (c) 2017 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.data.codec.xml;
+
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.model.api.SchemaNode;
+import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
+
+final class SchemalessXMLStreamWriterUtils extends XMLStreamWriterUtils {
+    static final SchemalessXMLStreamWriterUtils INSTANCE = new SchemalessXMLStreamWriterUtils();
+
+    private SchemalessXMLStreamWriterUtils() {
+        // Hidden on purpose
+    }
+
+    @Override
+    TypeDefinition<?> getBaseTypeForLeafRef(final SchemaNode schemaNode, final LeafrefTypeDefinition type) {
+        return type;
+    }
+
+    @Override
+    void writeInstanceIdentifier(final XMLStreamWriter writer, final YangInstanceIdentifier value)
+            throws XMLStreamException {
+        throw new UnsupportedOperationException("Schema context not present in " + this + ", cannot serialize "
+            + value);
+    }
+}
similarity index 58%
rename from yang/yang-data-codec-xml/src/main/java/org/opendaylight/yangtools/yang/data/codec/xml/XmlStreamUtils.java
rename to yang/yang-data-codec-xml/src/main/java/org/opendaylight/yangtools/yang/data/codec/xml/XMLStreamWriterUtils.java
index 28927d693ebcfcfa94b4c50972ae6b414a176a36..c9b78d14473f2f722e0e58604c4c4793a9d69460 100644 (file)
@@ -9,10 +9,7 @@
 package org.opendaylight.yangtools.yang.data.codec.xml;
 
 import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
-import com.google.common.base.Verify;
-import java.net.URI;
 import java.util.Map.Entry;
 import javax.annotation.Nonnull;
 import javax.xml.stream.XMLStreamException;
@@ -21,15 +18,13 @@ import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
-import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.TypedSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
-import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -37,16 +32,12 @@ import org.slf4j.LoggerFactory;
  * Utility class for bridging JAXP Stream and YANG Data APIs. Note that the definition of this class
  * by no means final and subject to change as more functionality is centralized here.
  */
-class XmlStreamUtils {
-    private static final Logger LOG = LoggerFactory.getLogger(XmlStreamUtils.class);
-    private final Optional<SchemaContext> schemaContext;
+abstract class XMLStreamWriterUtils {
+    private static final Logger LOG = LoggerFactory.getLogger(XMLStreamWriterUtils.class);
 
-    private XmlStreamUtils(final SchemaContext schemaContext) {
-        this.schemaContext = Optional.fromNullable(schemaContext);
-    }
-
-    static XmlStreamUtils create(final SchemaContext schemaContext) {
-        return new XmlStreamUtils(schemaContext);
+    static XMLStreamWriterUtils create(final SchemaContext schemaContext) {
+        return schemaContext == null ? SchemalessXMLStreamWriterUtils.INSTANCE
+                : new SchemaAwareXMLStreamWriterUtils(schemaContext);
     }
 
     @VisibleForTesting
@@ -65,44 +56,29 @@ class XmlStreamUtils {
      * @param writer XML Stream writer
      * @param schemaNode Schema node that describes the value
      * @param value data value
-     * @param parent optional parameter of a module QName owning the leaf definition
+     * @param parent module QName owning the leaf definition
      * @throws XMLStreamException if an encoding problem occurs
      */
     void writeValue(@Nonnull final XMLStreamWriter writer, @Nonnull final SchemaNode schemaNode,
-                           final Object value, final Optional<QNameModule> parent) throws XMLStreamException {
+            final Object value, final QNameModule parent) throws XMLStreamException {
         if (value == null) {
             LOG.debug("Value of {}:{} is null, not encoding it", schemaNode.getQName().getNamespace(),
                     schemaNode.getQName().getLocalName());
             return;
         }
 
-        Preconditions.checkArgument(schemaNode instanceof LeafSchemaNode || schemaNode instanceof LeafListSchemaNode,
+        Preconditions.checkArgument(schemaNode instanceof TypedSchemaNode,
                 "Unable to write value for node %s, only nodes of type: leaf and leaf-list can be written at this point",
                 schemaNode.getQName());
 
-        TypeDefinition<?> type = schemaNode instanceof LeafSchemaNode ?
-                ((LeafSchemaNode) schemaNode).getType():
-                ((LeafListSchemaNode) schemaNode).getType();
-
-        if (schemaContext.isPresent() && type instanceof LeafrefTypeDefinition) {
-            LeafrefTypeDefinition leafrefTypeDefinition = (LeafrefTypeDefinition) type;
-            type = SchemaContextUtil.getBaseTypeForLeafRef(leafrefTypeDefinition, schemaContext.get(), schemaNode);
-            Verify.verifyNotNull(type, "Unable to find base type for leafref node '%s'.", schemaNode.getPath());
+        TypeDefinition<?> type = ((TypedSchemaNode) schemaNode).getType();
+        if (type instanceof LeafrefTypeDefinition) {
+            type = getBaseTypeForLeafRef(schemaNode, (LeafrefTypeDefinition) type);
         }
 
         writeValue(writer, type, value, parent);
     }
 
-    void writeValue(@Nonnull final XMLStreamWriter writer, @Nonnull final SchemaNode schemaNode,
-                           final Object value) throws XMLStreamException {
-        writeValue(writer, schemaNode, value, Optional.absent());
-    }
-
-    void writeValue(@Nonnull final XMLStreamWriter writer, @Nonnull final SchemaNode schemaNode,
-                           final Object value, final QNameModule parent) throws XMLStreamException {
-        writeValue(writer, schemaNode, value, Optional.of(parent));
-    }
-
     /**
      * Write a value into a XML stream writer. This method assumes the start and end of element is
      * emitted by the caller.
@@ -113,8 +89,8 @@ class XmlStreamUtils {
      * @param parent optional parameter of a module QName owning the leaf definition
      * @throws XMLStreamException if an encoding problem occurs
      */
-    void writeValue(@Nonnull final XMLStreamWriter writer, @Nonnull final TypeDefinition<?> type,
-                           final Object value, final Optional<QNameModule> parent) throws XMLStreamException {
+    private void writeValue(@Nonnull final XMLStreamWriter writer, @Nonnull final TypeDefinition<?> type,
+            final Object value, final QNameModule parent) throws XMLStreamException {
         if (value == null) {
             LOG.debug("Value of {}:{} is null, not encoding it", type.getQName().getNamespace(),
                     type.getQName().getLocalName());
@@ -122,11 +98,7 @@ class XmlStreamUtils {
         }
 
         if (type instanceof IdentityrefTypeDefinition) {
-            if (parent.isPresent()) {
-                write(writer, (IdentityrefTypeDefinition) type, value, parent);
-            } else {
-                write(writer, (IdentityrefTypeDefinition) type, value, Optional.absent());
-            }
+            write(writer, (IdentityrefTypeDefinition) type, value, parent);
         } else if (type instanceof InstanceIdentifierTypeDefinition) {
             write(writer, (InstanceIdentifierTypeDefinition) type, value);
         } else {
@@ -148,25 +120,15 @@ class XmlStreamUtils {
         }
     }
 
-    void writeValue(@Nonnull final XMLStreamWriter writer, @Nonnull final TypeDefinition<?> type,
-                           final Object value, final QNameModule parent) throws XMLStreamException {
-        writeValue(writer, type, value, Optional.of(parent));
-    }
-
-    void writeValue(@Nonnull final XMLStreamWriter writer, @Nonnull final TypeDefinition<?> type,
-                           final Object value) throws XMLStreamException {
-        writeValue(writer, type, value, Optional.absent());
-    }
-
     @VisibleForTesting
     static void write(@Nonnull final XMLStreamWriter writer, @Nonnull final IdentityrefTypeDefinition type,
-                      @Nonnull final Object value, final Optional<QNameModule>  parent) throws XMLStreamException {
+                      @Nonnull final Object value, final QNameModule parent) throws XMLStreamException {
         if (value instanceof QName) {
             final QName qname = (QName) value;
             final String prefix = "x";
 
             //in case parent is present and same as element namespace write value without namespace
-            if (parent.isPresent() && qname.getNamespace().equals(parent.get().getNamespace())){
+            if (qname.getNamespace().equals(parent.getNamespace())){
                 writer.writeCharacters(qname.getLocalName());
             } else {
                 final String ns = qname.getNamespace().toString();
@@ -192,24 +154,8 @@ class XmlStreamUtils {
         }
     }
 
-    void writeInstanceIdentifier(final XMLStreamWriter writer, final YangInstanceIdentifier value)
-            throws XMLStreamException {
-        if (schemaContext.isPresent()) {
-            RandomPrefixInstanceIdentifierSerializer iiCodec =
-                    new RandomPrefixInstanceIdentifierSerializer(schemaContext.get());
-            String serializedValue = iiCodec.serialize(value);
-            writeNamespaceDeclarations(writer, iiCodec.getPrefixes());
-            writer.writeCharacters(serializedValue);
-        } else {
-            LOG.warn("Schema context not present in {}, serializing {} without schema.",this,value);
-            writeInstanceIdentifier(writer, value);
-        }
-    }
+    abstract TypeDefinition<?> getBaseTypeForLeafRef(SchemaNode schemaNode, LeafrefTypeDefinition type);
 
-    private static void writeNamespaceDeclarations(final XMLStreamWriter writer,
-                                               final Iterable<Entry<URI, String>> prefixes) throws XMLStreamException {
-        for (Entry<URI, String> e: prefixes) {
-            writer.writeNamespace(e.getValue(), e.getKey().toString());
-        }
-    }
+    abstract void writeInstanceIdentifier(XMLStreamWriter writer, YangInstanceIdentifier value)
+            throws XMLStreamException;
 }
index 7d32bf103cbc380ef6173b9caa3ed6cad8cc77d4..dc0c919098b1a827347dc05f89bf90de779f0be9 100644 (file)
@@ -77,8 +77,8 @@ public class XmlStreamUtilsTest {
         final Map.Entry<QName, String> attributeEntryNoPrefix = new AbstractMap.SimpleEntry<>(name, "value");
 
         final RandomPrefix randomPrefix = new RandomPrefix();
-        XmlStreamUtils.writeAttribute(writer, attributeEntry, randomPrefix);
-        XmlStreamUtils.writeAttribute(writer, attributeEntryNoPrefix, randomPrefix);
+        XMLStreamWriterUtils.writeAttribute(writer, attributeEntry, randomPrefix);
+        XMLStreamWriterUtils.writeAttribute(writer, attributeEntryNoPrefix, randomPrefix);
 
         writer.writeEndElement();
         writer.close();
@@ -109,11 +109,11 @@ public class XmlStreamUtilsTest {
 
         writer.writeStartElement("element");
         final QNameModule parent = QNameModule.create(URI.create("parent:uri"), new Date());
-        XmlStreamUtils.write(writer, null, QName.create(parent, "identity"), Optional.of(parent));
+        XMLStreamWriterUtils.write(writer, null, QName.create(parent, "identity"), parent);
         writer.writeEndElement();
 
         writer.writeStartElement("elementDifferent");
-        XmlStreamUtils.write(writer, null, QName.create("different:namespace", "identity"), Optional.of(parent));
+        XMLStreamWriterUtils.write(writer, null, QName.create("different:namespace", "identity"), parent);
         writer.writeEndElement();
 
         writer.close();
@@ -189,9 +189,8 @@ public class XmlStreamUtilsTest {
             final QName moduleQName = QName.create(namespace, revision, "module");
             final QNameModule module = QNameModule.create(moduleQName.getNamespace(), moduleQName.getRevision());
             return QName.create(module, localName);
-        } else {
-            return QName.create(namespace, revision, localName);
         }
+        return QName.create(namespace, revision, localName);
     }
 
     private LeafSchemaNode findSchemaNodeWithLeafrefType(final DataNodeContainer module, final String nodeName) {