--- /dev/null
+/*
+ * 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);
+ }
+}
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;
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;
* 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
* @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.
* @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());
}
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 {
}
}
- 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();
}
}
- 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;
}
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();
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();
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) {