import com.google.common.annotations.Beta;
import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import java.net.URI;
+import java.util.Collection;
+import java.util.Collections;
import java.util.Map;
import java.util.Map.Entry;
import javax.annotation.Nonnull;
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.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.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;
public class XmlStreamUtils {
private static final Logger LOG = LoggerFactory.getLogger(XmlStreamUtils.class);
private final XmlCodecProvider codecProvider;
+ private final Optional<SchemaContext> schemaContext;
protected XmlStreamUtils(final XmlCodecProvider codecProvider) {
+ this(codecProvider, null);
+ }
+
+ private XmlStreamUtils(XmlCodecProvider codecProvider, SchemaContext schemaContext) {
this.codecProvider = Preconditions.checkNotNull(codecProvider);
+ this.schemaContext = Optional.fromNullable(schemaContext);
}
/**
}
/**
- * Short-hand for {@link #writeDataDocument(XMLStreamWriter, CompositeNode, SchemaNode, XmlCodecProvider)} with
+ * Short-hand for {@link #writeDocument(XMLStreamWriter, CompositeNode, SchemaNode)})} with
* null SchemaNode.
*
* @param writer XML Stream writer
* @param data data node
- * @param schema corresponding schema node, may be null
* @throws XMLStreamException if an encoding problem occurs
*/
public void writeDocument(final XMLStreamWriter writer, final CompositeNode data) throws XMLStreamException {
if (data instanceof SimpleNode<?>) {
// Simple node
- if (schema instanceof LeafListSchemaNode) {
- writeValue(writer, ((LeafListSchemaNode) schema).getType(), data.getValue());
- } else if (schema instanceof LeafSchemaNode) {
- writeValue(writer, ((LeafSchemaNode) schema).getType(), data.getValue());
+ if (schema instanceof LeafListSchemaNode || schema instanceof LeafSchemaNode) {
+ writeValue(writer, schema, data.getValue());
} else {
Object value = data.getValue();
if (value != null) {
}
} else {
// CompositeNode
- for (Node<?> child : ((CompositeNode) data).getValue()) {
- DataSchemaNode childSchema = null;
- if (schema instanceof DataNodeContainer) {
- childSchema = SchemaUtils.findFirstSchema(child.getNodeType(), ((DataNodeContainer) schema).getChildNodes()).orNull();
- if (LOG.isDebugEnabled()) {
- if (childSchema == null) {
- LOG.debug("Probably the data node \"{}\" does not conform to schema", child == null ? "" : child.getNodeType().getLocalName());
- }
- }
+ final CompositeNode castedData = ((CompositeNode) data);
+ final DataNodeContainer castedSchema;
+ if (schema instanceof DataNodeContainer) {
+ castedSchema = (DataNodeContainer) schema;
+ } else {
+ castedSchema = null;
+ }
+ final Collection<QName> keyLeaves;
+ if (schema instanceof ListSchemaNode) {
+ keyLeaves = ((ListSchemaNode) schema).getKeyDefinition();
+
+ } else {
+ keyLeaves = Collections.emptyList();
+
+ }
+ for (QName key : keyLeaves) {
+ SimpleNode<?> keyLeaf = castedData.getFirstSimpleByName(key);
+ if(keyLeaf != null) {
+ writeChildElement(writer,keyLeaf,castedSchema);
}
+ }
- writeElement(writer, child, childSchema);
+ for (Node<?> child : castedData.getValue()) {
+ if(keyLeaves.contains(child.getNodeType())) {
+ // Skip key leaf which was written by previous for loop.
+ continue;
+ }
+ writeChildElement(writer,child,castedSchema);
}
}
}
+ private void writeChildElement(XMLStreamWriter writer, Node<?> child, DataNodeContainer parentSchema) throws XMLStreamException {
+ DataSchemaNode childSchema = null;
+ if (parentSchema != null) {
+ childSchema = SchemaUtils.findFirstSchema(child.getNodeType(), parentSchema.getChildNodes()).orNull();
+ if ((childSchema == null) && LOG.isDebugEnabled()) {
+ LOG.debug("Probably the data node \"{}\" does not conform to schema", child == null ? "" : child.getNodeType().getLocalName());
+ }
+ }
+ writeElement(writer, child, childSchema);
+ }
+
private static void writeAttributes(final XMLStreamWriter writer, final AttributesContainer data, final RandomPrefix randomPrefix) throws XMLStreamException {
for (Entry<QName, String> attribute : data.getAttributes().entrySet()) {
writeAttribute(writer, attribute, randomPrefix);
* emitted by the caller.
*
* @param writer XML Stream writer
- * @param type data type
+ * @param schemaNode Schema node that describes the value
+ * @param value data value
+ * @throws XMLStreamException if an encoding problem occurs
+ */
+ public void writeValue(final @Nonnull XMLStreamWriter writer, final @Nonnull SchemaNode schemaNode, final Object value) 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,
+ "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();
+
+ TypeDefinition<?> baseType = XmlUtils.resolveBaseTypeFrom(type);
+
+ if (schemaContext.isPresent() && baseType instanceof LeafrefTypeDefinition) {
+ LeafrefTypeDefinition leafrefTypeDefinition = (LeafrefTypeDefinition) baseType;
+ baseType = SchemaContextUtil.getBaseTypeForLeafRef(leafrefTypeDefinition, schemaContext.get(), schemaNode);
+ }
+
+ writeValue(writer, baseType, value);
+ }
+
+ /**
+ * Write a value into a XML stream writer. This method assumes the start and end of element is
+ * emitted by the caller.
+ *
+ * @param writer XML Stream writer
+ * @param type data type. In case of leaf ref this should be the type of leaf being referenced
* @param value data value
* @throws XMLStreamException if an encoding problem occurs
*/
LOG.debug("Value of {}:{} is null, not encoding it", type.getQName().getNamespace(), type.getQName().getLocalName());
return;
}
+ TypeDefinition<?> baseType = XmlUtils.resolveBaseTypeFrom(type);
- final TypeDefinition<?> baseType = XmlUtils.resolveBaseTypeFrom(type);
if (baseType instanceof IdentityrefTypeDefinition) {
write(writer, (IdentityrefTypeDefinition) baseType, value);
} else if (baseType instanceof InstanceIdentifierTypeDefinition) {
writer.writeCharacters(String.valueOf(value));
}
}
+
+ public static XmlStreamUtils create(XmlCodecProvider codecProvider, SchemaContext schemaContext) {
+ return new XmlStreamUtils(codecProvider, schemaContext);
+ }
}