*/
package org.opendaylight.yangtools.yang.data.codec.gson;
+import static com.google.common.base.Preconditions.checkState;
+import static com.google.common.base.Verify.verify;
import static java.util.Objects.requireNonNull;
import static org.w3c.dom.Node.ELEMENT_NODE;
import static org.w3c.dom.Node.TEXT_NODE;
+import com.google.common.collect.ClassToInstanceMap;
+import com.google.common.collect.ImmutableClassToInstanceMap;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
-import java.net.URI;
+import java.util.List;
import java.util.regex.Pattern;
-import javax.annotation.RegEx;
import javax.xml.transform.dom.DOMSource;
-import org.opendaylight.yangtools.yang.common.QName;
+import org.checkerframework.checker.regex.qual.Regex;
+import org.opendaylight.yangtools.rfc8528.data.api.MountPointContext;
+import org.opendaylight.yangtools.rfc8528.data.api.MountPointIdentifier;
+import org.opendaylight.yangtools.rfc8528.data.api.StreamWriterMountPointExtension;
+import org.opendaylight.yangtools.yang.common.XMLNamespace;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedAnydata;
import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
+import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriterExtension;
import org.opendaylight.yangtools.yang.data.impl.codec.SchemaTracker;
+import org.opendaylight.yangtools.yang.model.api.AnydataSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.AnyxmlSchemaNode;
import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
+import org.opendaylight.yangtools.yang.model.api.EffectiveStatementInference;
import org.opendaylight.yangtools.yang.model.api.SchemaNode;
import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+import org.opendaylight.yangtools.yang.model.api.TypedDataSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
* <p>
* Values of leaf and leaf-list are NOT translated according to codecs.
*/
-public abstract class JSONNormalizedNodeStreamWriter implements NormalizedNodeStreamWriter {
+public abstract class JSONNormalizedNodeStreamWriter implements NormalizedNodeStreamWriter,
+ StreamWriterMountPointExtension {
private static final class Exclusive extends JSONNormalizedNodeStreamWriter {
- Exclusive(final JSONCodecFactory codecFactory, final SchemaPath path, final JsonWriter writer,
+ Exclusive(final JSONCodecFactory codecFactory, final SchemaTracker tracker, final JsonWriter writer,
final JSONStreamWriterRootContext rootContext) {
- super(codecFactory, path, writer, rootContext);
+ super(codecFactory, tracker, writer, rootContext);
}
@Override
}
private static final class Nested extends JSONNormalizedNodeStreamWriter {
- Nested(final JSONCodecFactory codecFactory, final SchemaPath path, final JsonWriter writer,
+ Nested(final JSONCodecFactory codecFactory, final SchemaTracker tracker, final JsonWriter writer,
final JSONStreamWriterRootContext rootContext) {
- super(codecFactory, path, writer, rootContext);
+ super(codecFactory, tracker, writer, rootContext);
}
@Override
*/
private static final boolean DEFAULT_EMIT_EMPTY_CONTAINERS = true;
- @RegEx
+ @Regex
private static final String NUMBER_STRING = "-?\\d+(\\.\\d+)?";
private static final Pattern NUMBER_PATTERN = Pattern.compile(NUMBER_STRING);
- @RegEx
+ @Regex
private static final String NOT_DECIMAL_NUMBER_STRING = "-?\\d+";
private static final Pattern NOT_DECIMAL_NUMBER_PATTERN = Pattern.compile(NOT_DECIMAL_NUMBER_STRING);
private final JsonWriter writer;
private JSONStreamWriterContext context;
- JSONNormalizedNodeStreamWriter(final JSONCodecFactory codecFactory, final SchemaPath path, final JsonWriter writer,
- final JSONStreamWriterRootContext rootContext) {
+ JSONNormalizedNodeStreamWriter(final JSONCodecFactory codecFactory, final SchemaTracker tracker,
+ final JsonWriter writer, final JSONStreamWriterRootContext rootContext) {
this.writer = requireNonNull(writer);
this.codecs = requireNonNull(codecFactory);
- this.tracker = SchemaTracker.create(codecFactory.getSchemaContext(), path);
+ this.tracker = requireNonNull(tracker);
this.context = requireNonNull(rootContext);
}
* @return A stream writer instance
*/
public static NormalizedNodeStreamWriter createExclusiveWriter(final JSONCodecFactory codecFactory,
- final SchemaPath path, final URI initialNs, final JsonWriter jsonWriter) {
- return new Exclusive(codecFactory, path, jsonWriter, new JSONStreamWriterExclusiveRootContext(initialNs));
+ final SchemaPath path, final XMLNamespace initialNs, final JsonWriter jsonWriter) {
+ return new Exclusive(codecFactory, SchemaTracker.create(codecFactory.getEffectiveModelContext(), path),
+ jsonWriter, new JSONStreamWriterExclusiveRootContext(initialNs));
+ }
+
+ /**
+ * Create a new stream writer, which writes to the specified output stream.
+ *
+ * <p>
+ * The codec factory can be reused between multiple writers.
+ *
+ * <p>
+ * Returned writer is exclusive user of JsonWriter, which means it will start
+ * top-level JSON element and ends it.
+ *
+ * <p>
+ * This instance of writer can be used only to emit one top level element,
+ * otherwise it will produce incorrect JSON. Closing this instance will close
+ * the writer too.
+ *
+ * @param codecFactory JSON codec factory
+ * @param path Schema Path
+ * @param initialNs Initial namespace
+ * @param jsonWriter JsonWriter
+ * @return A stream writer instance
+ */
+ public static NormalizedNodeStreamWriter createExclusiveWriter(final JSONCodecFactory codecFactory,
+ final Absolute path, final XMLNamespace initialNs, final JsonWriter jsonWriter) {
+ return new Exclusive(codecFactory, SchemaTracker.create(codecFactory.getEffectiveModelContext(), path),
+ jsonWriter, new JSONStreamWriterExclusiveRootContext(initialNs));
+ }
+
+ /**
+ * Create a new stream writer, which writes to the specified output stream.
+ *
+ * <p>
+ * The codec factory can be reused between multiple writers.
+ *
+ * <p>
+ * Returned writer is exclusive user of JsonWriter, which means it will start
+ * top-level JSON element and ends it.
+ *
+ * <p>
+ * This instance of writer can be used only to emit one top level element,
+ * otherwise it will produce incorrect JSON. Closing this instance will close
+ * the writer too.
+ *
+ * @param codecFactory JSON codec factory
+ * @param rootNode Root node
+ * @param initialNs Initial namespace
+ * @param jsonWriter JsonWriter
+ * @return A stream writer instance
+ */
+ public static NormalizedNodeStreamWriter createExclusiveWriter(final JSONCodecFactory codecFactory,
+ final DataNodeContainer rootNode, final XMLNamespace initialNs, final JsonWriter jsonWriter) {
+ return new Exclusive(codecFactory, SchemaTracker.create(rootNode), jsonWriter,
+ new JSONStreamWriterExclusiveRootContext(initialNs));
}
/**
* @return A stream writer instance
*/
public static NormalizedNodeStreamWriter createNestedWriter(final JSONCodecFactory codecFactory,
- final SchemaPath path, final URI initialNs, final JsonWriter jsonWriter) {
- return new Nested(codecFactory, path, jsonWriter, new JSONStreamWriterSharedRootContext(initialNs));
+ final SchemaPath path, final XMLNamespace initialNs, final JsonWriter jsonWriter) {
+ return new Nested(codecFactory, SchemaTracker.create(codecFactory.getEffectiveModelContext(), path), jsonWriter,
+ new JSONStreamWriterSharedRootContext(initialNs));
+ }
+
+ /**
+ * Create a new stream writer, which writes to the specified output stream.
+ *
+ * <p>
+ * The codec factory can be reused between multiple writers.
+ *
+ * <p>
+ * Returned writer can be used emit multiple top level element,
+ * but does not start / close parent JSON object, which must be done
+ * by user providing {@code jsonWriter} instance in order for
+ * JSON to be valid. Closing this instance <strong>will not</strong>
+ * close the wrapped writer; the caller must take care of that.
+ *
+ * @param codecFactory JSON codec factory
+ * @param path Schema Path
+ * @param initialNs Initial namespace
+ * @param jsonWriter JsonWriter
+ * @return A stream writer instance
+ */
+ public static NormalizedNodeStreamWriter createNestedWriter(final JSONCodecFactory codecFactory,
+ final Absolute path, final XMLNamespace initialNs, final JsonWriter jsonWriter) {
+ return new Nested(codecFactory, SchemaTracker.create(codecFactory.getEffectiveModelContext(), path), jsonWriter,
+ new JSONStreamWriterSharedRootContext(initialNs));
+ }
+
+ /**
+ * Create a new stream writer, which writes to the specified output stream.
+ *
+ * <p>
+ * The codec factory can be reused between multiple writers.
+ *
+ * <p>
+ * Returned writer can be used emit multiple top level element,
+ * but does not start / close parent JSON object, which must be done
+ * by user providing {@code jsonWriter} instance in order for
+ * JSON to be valid. Closing this instance <strong>will not</strong>
+ * close the wrapped writer; the caller must take care of that.
+ *
+ * @param codecFactory JSON codec factory
+ * @param rootNode Root node
+ * @param initialNs Initial namespace
+ * @param jsonWriter JsonWriter
+ * @return A stream writer instance
+ */
+ public static NormalizedNodeStreamWriter createNestedWriter(final JSONCodecFactory codecFactory,
+ final DataNodeContainer rootNode, final XMLNamespace initialNs, final JsonWriter jsonWriter) {
+ return new Nested(codecFactory, SchemaTracker.create(rootNode), jsonWriter,
+ new JSONStreamWriterSharedRootContext(initialNs));
+ }
+
+ @Override
+ public ClassToInstanceMap<NormalizedNodeStreamWriterExtension> getExtensions() {
+ return ImmutableClassToInstanceMap.of(StreamWriterMountPointExtension.class, this);
}
@Override
- public final void leafNode(final NodeIdentifier name, final Object value) throws IOException {
- final LeafSchemaNode schema = tracker.leafNode(name);
- final JSONCodec<?> codec = codecs.codecFor(schema);
- context.emittingChild(codecs.getSchemaContext(), writer);
- context.writeChildJsonIdentifier(codecs.getSchemaContext(), writer, name.getNodeType());
- writeValue(value, codec);
+ public void startLeafNode(final NodeIdentifier name) throws IOException {
+ tracker.startLeafNode(name);
+ context.emittingChild(codecs.getEffectiveModelContext(), writer);
+ context.writeChildJsonIdentifier(codecs.getEffectiveModelContext(), writer, name.getNodeType());
}
@Override
}
@Override
- public final void leafSetEntryNode(final QName name, final Object value) throws IOException {
- final LeafListSchemaNode schema = tracker.leafSetEntryNode(name);
- final JSONCodec<?> codec = codecs.codecFor(schema);
- context.emittingChild(codecs.getSchemaContext(), writer);
- writeValue(value, codec);
+ public void startLeafSetEntryNode(final NodeWithValue<?> name) throws IOException {
+ tracker.startLeafSetEntryNode(name);
+ context.emittingChild(codecs.getEffectiveModelContext(), writer);
}
@Override
}
@Override
- public final void anyxmlNode(final NodeIdentifier name, final Object value) throws IOException {
- // FIXME: should have a codec based on this :)
- tracker.anyxmlNode(name);
+ public final boolean startAnydataNode(final NodeIdentifier name, final Class<?> objectModel) throws IOException {
+ if (NormalizedAnydata.class.isAssignableFrom(objectModel)) {
+ tracker.startAnydataNode(name);
+ context.emittingChild(codecs.getEffectiveModelContext(), writer);
+ context.writeChildJsonIdentifier(codecs.getEffectiveModelContext(), writer, name.getNodeType());
+ return true;
+ }
- context.emittingChild(codecs.getSchemaContext(), writer);
- context.writeChildJsonIdentifier(codecs.getSchemaContext(), writer, name.getNodeType());
+ return false;
+ }
- writeAnyXmlValue((DOMSource) value);
+ @Override
+ public final NormalizedNodeStreamWriter startMountPoint(final MountPointIdentifier mountId,
+ final MountPointContext mountCtx) throws IOException {
+ final EffectiveModelContext ctx = mountCtx.getEffectiveModelContext();
+ return new Nested(codecs.rebaseTo(ctx), SchemaTracker.create(ctx), writer,
+ new JSONStreamWriterSharedRootContext(context.getNamespace()));
}
@Override
- public final void startYangModeledAnyXmlNode(final NodeIdentifier name, final int childSizeHint)
- throws IOException {
- tracker.startYangModeledAnyXmlNode(name);
- context = new JSONStreamWriterNamedObjectContext(context, name, true);
+ public final boolean startAnyxmlNode(final NodeIdentifier name, final Class<?> objectModel) throws IOException {
+ if (DOMSource.class.isAssignableFrom(objectModel)) {
+ tracker.startAnyxmlNode(name);
+ context.emittingChild(codecs.getEffectiveModelContext(), writer);
+ context.writeChildJsonIdentifier(codecs.getEffectiveModelContext(), writer, name.getNodeType());
+ return true;
+ }
+ return false;
}
@Override
public final void endNode() throws IOException {
tracker.endNode();
- context = context.endNode(codecs.getSchemaContext(), writer);
-
- if (context instanceof JSONStreamWriterRootContext) {
- context.emitEnd(writer);
- }
+ context = context.endNode(codecs.getEffectiveModelContext(), writer);
}
@Override
}
final void closeWriter() throws IOException {
+ if (!(context instanceof JSONStreamWriterRootContext)) {
+ throw new IOException("Unexpected root context " + context);
+ }
+
+ context.endNode(codecs.getEffectiveModelContext(), writer);
writer.close();
}
+ @Override
+ public void scalarValue(final Object value) throws IOException {
+ final Object current = tracker.getParent();
+ if (current instanceof TypedDataSchemaNode) {
+ writeValue(value, codecs.codecFor((TypedDataSchemaNode) current));
+ } else if (current instanceof AnydataSchemaNode) {
+ writeAnydataValue(value);
+ } else {
+ throw new IllegalStateException(String.format("Cannot emit scalar %s for %s", value, current));
+ }
+ }
+
+ @Override
+ public void domSourceValue(final DOMSource value) throws IOException {
+ final Object current = tracker.getParent();
+ checkState(current instanceof AnyxmlSchemaNode, "Cannot emit DOMSource %s for %s", value, current);
+ // FIXME: should have a codec based on this :)
+ writeAnyXmlValue(value);
+ }
+
@SuppressWarnings("unchecked")
private void writeValue(final Object value, final JSONCodec<?> codec) throws IOException {
((JSONCodec<Object>) codec).writeValue(writer, value);
}
+ private void writeAnydataValue(final Object value) throws IOException {
+ if (value instanceof NormalizedAnydata) {
+ writeNormalizedAnydata((NormalizedAnydata) value);
+ } else {
+ throw new IllegalStateException("Unexpected anydata value " + value);
+ }
+ }
+
+ private void writeNormalizedAnydata(final NormalizedAnydata anydata) throws IOException {
+ final EffectiveStatementInference inference = anydata.getInference();
+ final List<? extends EffectiveStatement<?, ?>> path = inference.statementPath();
+ final DataNodeContainer parent;
+ if (path.size() > 1) {
+ final EffectiveStatement<?, ?> stmt = path.get(path.size() - 2);
+ verify(stmt instanceof DataNodeContainer, "Unexpected statement %s", stmt);
+ parent = (DataNodeContainer) stmt;
+ } else {
+ parent = inference.getEffectiveModelContext();
+ }
+
+ anydata.writeTo(JSONNormalizedNodeStreamWriter.createNestedWriter(
+ codecs.rebaseTo(inference.getEffectiveModelContext()), parent, context.getNamespace(), writer));
+ }
+
private void writeAnyXmlValue(final DOMSource anyXmlValue) throws IOException {
writeXmlNode(anyXmlValue.getNode());
}