import java.io.IOException;
import java.net.URI;
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.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.stream.NormalizedNodeStreamWriter;
import org.opendaylight.yangtools.yang.data.impl.codec.SchemaTracker;
+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.SchemaNode;
import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+import org.opendaylight.yangtools.yang.model.api.TypedDataSchemaNode;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
*/
public abstract class JSONNormalizedNodeStreamWriter implements NormalizedNodeStreamWriter {
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);
}
*/
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));
+ return new Exclusive(codecFactory, SchemaTracker.create(codecFactory.getSchemaContext(), 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 URI initialNs, final JsonWriter jsonWriter) {
+ return new Exclusive(codecFactory, SchemaTracker.create(rootNode), jsonWriter,
+ new JSONStreamWriterExclusiveRootContext(initialNs));
}
/**
*/
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));
+ return new Nested(codecFactory, SchemaTracker.create(codecFactory.getSchemaContext(), 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 URI initialNs, final JsonWriter jsonWriter) {
+ return new Nested(codecFactory, SchemaTracker.create(rootNode), jsonWriter,
+ new JSONStreamWriterSharedRootContext(initialNs));
}
@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);
+ public void startLeafNode(final NodeIdentifier name) throws IOException {
+ tracker.startLeafNode(name);
context.emittingChild(codecs.getSchemaContext(), writer);
context.writeChildJsonIdentifier(codecs.getSchemaContext(), writer, name.getNodeType());
- writeValue(value, codec);
}
@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);
+ public void startLeafSetEntryNode(final NodeWithValue<?> name) throws IOException {
+ tracker.startLeafSetEntryNode(name);
context.emittingChild(codecs.getSchemaContext(), writer);
- writeValue(value, codec);
}
@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 void startAnyxmlNode(final NodeIdentifier name) throws IOException {
+ tracker.startAnyxmlNode(name);
context.emittingChild(codecs.getSchemaContext(), writer);
context.writeChildJsonIdentifier(codecs.getSchemaContext(), writer, name.getNodeType());
-
- writeAnyXmlValue((DOMSource) value);
}
@Override
writer.close();
}
+ @Override
+ public void nodeValue(final Object value) throws IOException {
+ final Object current = tracker.getParent();
+ if (current instanceof TypedDataSchemaNode) {
+ final JSONCodec<?> codec = codecs.codecFor((TypedDataSchemaNode) current);
+ writeValue(value, codec);
+ } else if (current instanceof AnyXmlSchemaNode) {
+ // FIXME: should have a codec based on this :)
+ writeAnyXmlValue((DOMSource) value);
+ } else {
+ throw new IllegalStateException("Cannot emit scalar for " + current);
+ }
+ }
+
@SuppressWarnings("unchecked")
private void writeValue(final Object value, final JSONCodec<?> codec) throws IOException {
((JSONCodec<Object>) codec).writeValue(writer, value);