*/
package org.opendaylight.yangtools.yang.data.api.schema.stream;
+import static com.google.common.base.Verify.verify;
+import static java.util.Objects.requireNonNull;
import static org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter.UNKNOWN_SIZE;
import com.google.common.annotations.Beta;
-import com.google.common.base.Optional;
-import com.google.common.base.Preconditions;
-import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
-
import java.io.Closeable;
import java.io.Flushable;
import java.io.IOException;
import java.util.Collection;
+import java.util.Optional;
import java.util.Set;
-
import javax.xml.stream.XMLStreamReader;
-
+import javax.xml.transform.dom.DOMSource;
+import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
-import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
+import org.opendaylight.yangtools.yang.data.api.schema.AnydataNode;
+import org.opendaylight.yangtools.yang.data.api.schema.AnyxmlNode;
import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.api.schema.OrderedLeafSetNode;
import org.opendaylight.yangtools.yang.data.api.schema.OrderedMapNode;
import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
+import org.opendaylight.yangtools.yang.data.api.schema.YangModeledAnyXmlNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
*/
@Beta
public class NormalizedNodeWriter implements Closeable, Flushable {
- private final NormalizedNodeStreamWriter writer;
+ private static final Logger LOG = LoggerFactory.getLogger(NormalizedNodeWriter.class);
- private NormalizedNodeWriter(final NormalizedNodeStreamWriter writer) {
- this.writer = Preconditions.checkNotNull(writer);
+ private final @NonNull NormalizedNodeStreamWriter writer;
+
+ protected NormalizedNodeWriter(final NormalizedNodeStreamWriter writer) {
+ this.writer = requireNonNull(writer);
}
protected final NormalizedNodeStreamWriter getWriter() {
}
/**
- * Create a new writer backed by a {@link NormalizedNodeStreamWriter}. Unlike the simple {@link #forStreamWriter(NormalizedNodeStreamWriter)}
- * method, this allows the caller to switch off RFC6020 XML compliance, providing better
- * throughput. The reason is that the XML mapping rules in RFC6020 require the encoding
- * to emit leaf nodes which participate in a list's key first and in the order in which
- * they are defined in the key. For JSON, this requirement is completely relaxed and leaves
- * can be ordered in any way we see fit. The former requires a bit of work: first a lookup
- * for each key and then for each emitted node we need to check whether it was already
- * emitted.
+ * Create a new writer backed by a {@link NormalizedNodeStreamWriter}. Unlike the simple
+ * {@link #forStreamWriter(NormalizedNodeStreamWriter)} method, this allows the caller to switch off RFC6020 XML
+ * compliance, providing better throughput. The reason is that the XML mapping rules in RFC6020 require
+ * the encoding to emit leaf nodes which participate in a list's key first and in the order in which they are
+ * defined in the key. For JSON, this requirement is completely relaxed and leaves can be ordered in any way we
+ * see fit. The former requires a bit of work: first a lookup for each key and then for each emitted node we need
+ * to check whether it was already emitted.
*
* @param writer Back-end writer
* @param orderKeyLeaves whether the returned instance should be RFC6020 XML compliant.
* @return A new instance.
*/
- public static NormalizedNodeWriter forStreamWriter(final NormalizedNodeStreamWriter writer, final boolean orderKeyLeaves) {
- if (orderKeyLeaves) {
- return new OrderedNormalizedNodeWriter(writer);
- } else {
- return new NormalizedNodeWriter(writer);
- }
+ public static NormalizedNodeWriter forStreamWriter(final NormalizedNodeStreamWriter writer,
+ final boolean orderKeyLeaves) {
+ return orderKeyLeaves ? new OrderedNormalizedNodeWriter(writer) : new NormalizedNodeWriter(writer);
}
/**
* events to the encapsulated {@link NormalizedNodeStreamWriter}.
*
* @param node Node
- * @return
+ * @return NormalizedNodeWriter this
* @throws IOException when thrown from the backing writer.
*/
- public final NormalizedNodeWriter write(final NormalizedNode<?, ?> node) throws IOException {
+ public NormalizedNodeWriter write(final NormalizedNode<?, ?> node) throws IOException {
if (wasProcessedAsCompositeNode(node)) {
return this;
}
* @param children Child nodes
* @return Best estimate of the collection size required to hold all the children.
*/
- static final int childSizeHint(final Iterable<?> children) {
- return (children instanceof Collection) ? ((Collection<?>) children).size() : UNKNOWN_SIZE;
+ protected static int childSizeHint(final Iterable<?> children) {
+ return children instanceof Collection ? ((Collection<?>) children).size() : UNKNOWN_SIZE;
}
- private boolean wasProcessAsSimpleNode(final NormalizedNode<?, ?> node) throws IOException {
+ protected boolean wasProcessAsSimpleNode(final NormalizedNode<?, ?> node) throws IOException {
if (node instanceof LeafSetEntryNode) {
final LeafSetEntryNode<?> nodeAsLeafList = (LeafSetEntryNode<?>)node;
- writer.leafSetEntryNode(nodeAsLeafList.getValue());
+ writer.startLeafSetEntryNode(nodeAsLeafList.getIdentifier());
+ writer.scalarValue(nodeAsLeafList.getValue());
+ writer.endNode();
return true;
} else if (node instanceof LeafNode) {
final LeafNode<?> nodeAsLeaf = (LeafNode<?>)node;
- writer.leafNode(nodeAsLeaf.getIdentifier(), nodeAsLeaf.getValue());
- return true;
- } else if (node instanceof AnyXmlNode) {
- final AnyXmlNode anyXmlNode = (AnyXmlNode)node;
- writer.anyxmlNode(anyXmlNode.getIdentifier(), anyXmlNode.getValue());
+ writer.startLeafNode(nodeAsLeaf.getIdentifier());
+ writer.scalarValue(nodeAsLeaf.getValue());
+ writer.endNode();
return true;
+ } else if (node instanceof AnyxmlNode) {
+ final AnyxmlNode<?> anyxmlNode = (AnyxmlNode<?>)node;
+ final Class<?> model = anyxmlNode.getValueObjectModel();
+ if (writer.startAnyxmlNode(anyxmlNode.getIdentifier(), model)) {
+ final Object value = node.getValue();
+ if (DOMSource.class.isAssignableFrom(model)) {
+ verify(value instanceof DOMSource, "Inconsistent anyxml node %s", anyxmlNode);
+ writer.domSourceValue((DOMSource) value);
+ } else {
+ writer.scalarValue(value);
+ }
+ writer.endNode();
+ return true;
+ }
+
+ LOG.debug("Ignoring unhandled anyxml node {}", anyxmlNode);
+ } else if (node instanceof AnydataNode) {
+ final AnydataNode<?> anydata = (AnydataNode<?>) node;
+ final Class<?> model = anydata.getValueObjectModel();
+ if (writer.startAnydataNode(anydata.getIdentifier(), model)) {
+ writer.scalarValue(anydata.getValue());
+ writer.endNode();
+ return true;
+ }
+
+ LOG.debug("Writer {} does not support anydata in form of {}", writer, model);
}
return false;
* @return True
* @throws IOException when the writer reports it
*/
- protected final boolean writeChildren(final Iterable<? extends NormalizedNode<?, ?>> children) throws IOException {
- for (NormalizedNode<?, ?> child : children) {
+ protected boolean writeChildren(final Iterable<? extends NormalizedNode<?, ?>> children) throws IOException {
+ for (final NormalizedNode<?, ?> child : children) {
write(child);
}
return writeChildren(node.getValue());
}
- private boolean wasProcessedAsCompositeNode(final NormalizedNode<?, ?> node) throws IOException {
+ protected boolean wasProcessedAsCompositeNode(final NormalizedNode<?, ?> node) throws IOException {
if (node instanceof ContainerNode) {
final ContainerNode n = (ContainerNode) node;
writer.startContainerNode(n.getIdentifier(), childSizeHint(n.getValue()));
return writeChildren(n.getValue());
}
+ if (node instanceof YangModeledAnyXmlNode) {
+ final YangModeledAnyXmlNode n = (YangModeledAnyXmlNode) node;
+ writer.startYangModeledAnyXmlNode(n.getIdentifier(), childSizeHint(n.getValue()));
+ return writeChildren(n.getValue());
+ }
if (node instanceof MapEntryNode) {
return writeMapEntryNode((MapEntryNode) node);
}
writer.startMapNode(n.getIdentifier(), childSizeHint(n.getValue()));
return writeChildren(n.getValue());
}
+ if (node instanceof OrderedLeafSetNode) {
+ final LeafSetNode<?> n = (LeafSetNode<?>) node;
+ writer.startOrderedLeafSet(n.getIdentifier(), childSizeHint(n.getValue()));
+ return writeChildren(n.getValue());
+ }
if (node instanceof LeafSetNode) {
- //covers also OrderedLeafSetNode for which doesn't exist start* method
final LeafSetNode<?> n = (LeafSetNode<?>) node;
writer.startLeafSet(n.getIdentifier(), childSizeHint(n.getValue()));
return writeChildren(n.getValue());
@Override
protected boolean writeMapEntryNode(final MapEntryNode node) throws IOException {
- getWriter().startMapEntryNode(node.getIdentifier(), childSizeHint(node.getValue()));
+ final NormalizedNodeStreamWriter nnWriter = getWriter();
+ nnWriter.startMapEntryNode(node.getIdentifier(), childSizeHint(node.getValue()));
- final Set<QName> qnames = node.getIdentifier().getKeyValues().keySet();
+ final Set<QName> qnames = node.getIdentifier().keySet();
// Write out all the key children
- for (QName qname : qnames) {
+ for (final QName qname : qnames) {
final Optional<? extends NormalizedNode<?, ?>> child = node.getChild(new NodeIdentifier(qname));
if (child.isPresent()) {
write(child.get());
}
// Write all the rest
- return writeChildren(Iterables.filter(node.getValue(), new Predicate<NormalizedNode<?, ?>>() {
- @Override
- public boolean apply(final NormalizedNode<?, ?> input) {
- if (input instanceof AugmentationNode) {
- return true;
- }
- if (!qnames.contains(input.getNodeType())) {
- return true;
- }
-
- LOG.debug("Skipping key child {}", input);
- return false;
+ return writeChildren(Iterables.filter(node.getValue(), input -> {
+ if (input instanceof AugmentationNode) {
+ return true;
}
+ if (!qnames.contains(input.getNodeType())) {
+ return true;
+ }
+
+ LOG.debug("Skipping key child {}", input);
+ return false;
}));
}
}