package org.opendaylight.yangtools.yang.data.codec.gson;
import static java.util.Objects.requireNonNull;
-import static org.opendaylight.yangtools.yang.data.codec.gson.JsonParserStream.ANYXML_ARRAY_ELEMENT_ID;
import static org.w3c.dom.Node.ELEMENT_NODE;
import static org.w3c.dom.Node.TEXT_NODE;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
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.DataNodeContainer;
import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
import org.opendaylight.yangtools.yang.model.api.SchemaNode;
import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
+import org.w3c.dom.Text;
/**
* This implementation will create JSON output as output stream.
*/
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 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
* Warning suppressed due to static final constant which triggers a warning
* for the call to schema.isPresenceContainer().
*/
- @SuppressWarnings("unused")
@Override
public final void startContainerNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
final SchemaNode schema = tracker.startContainerNode(name);
-
- // FIXME this code ignores presence for containers
- // but datastore does as well and it needs be fixed first (2399)
- context = new JSONStreamWriterNamedObjectContext(context, name, DEFAULT_EMIT_EMPTY_CONTAINERS);
+ final boolean isPresence = schema instanceof ContainerSchemaNode
+ ? ((ContainerSchemaNode) schema).isPresenceContainer() : DEFAULT_EMIT_EMPTY_CONTAINERS;
+ context = new JSONStreamWriterNamedObjectContext(context, name, isPresence);
}
@Override
@Override
public final void anyxmlNode(final NodeIdentifier name, final Object value) throws IOException {
- @SuppressWarnings("unused")
- final AnyXmlSchemaNode schema = tracker.anyxmlNode(name);
// FIXME: should have a codec based on this :)
+ tracker.anyxmlNode(name);
context.emittingChild(codecs.getSchemaContext(), writer);
context.writeChildJsonIdentifier(codecs.getSchemaContext(), writer, name.getNodeType());
}
private void writeAnyXmlValue(final DOMSource anyXmlValue) throws IOException {
- final Node documentNode = anyXmlValue.getNode();
- final Node firstChild = documentNode.getFirstChild();
- if (ELEMENT_NODE == firstChild.getNodeType() && !ANYXML_ARRAY_ELEMENT_ID.equals(firstChild.getNodeName())) {
- writer.beginObject();
- traverseAnyXmlValue(documentNode);
- writer.endObject();
+ writeXmlNode(anyXmlValue.getNode());
+ }
+
+ private void writeXmlNode(final Node node) throws IOException {
+ if (isArrayElement(node)) {
+ writeArrayContent(node);
+ return;
+ }
+ final Element firstChildElement = getFirstChildElement(node);
+ if (firstChildElement == null) {
+ writeXmlValue(node);
} else {
- traverseAnyXmlValue(documentNode);
+ writeObjectContent(firstChildElement);
}
}
- private void traverseAnyXmlValue(final Node node) throws IOException {
- final NodeList children = node.getChildNodes();
- boolean inArray = false;
+ private void writeArrayContent(final Node node) throws IOException {
+ writer.beginArray();
+ handleArray(node);
+ writer.endArray();
+ }
- for (int i = 0, length = children.getLength(); i < length; i++) {
- final Node childNode = children.item(i);
- boolean inObject = false;
+ private void writeObjectContent(final Element firstChildElement) throws IOException {
+ writer.beginObject();
+ writeObject(firstChildElement);
+ writer.endObject();
+ }
- if (ELEMENT_NODE == childNode.getNodeType()) {
- final Node firstChild = childNode.getFirstChild();
- // beginning of an array
- if (ANYXML_ARRAY_ELEMENT_ID.equals(childNode.getNodeName()) && !inArray) {
- writer.beginArray();
- inArray = true;
- // object at the beginning of the array
- if (isJsonObjectInArray(childNode, firstChild)) {
- writer.beginObject();
- inObject = true;
- }
- // object in the array
- } else if (isJsonObjectInArray(childNode, firstChild)) {
- writer.beginObject();
- inObject = true;
- // object
- } else if (isJsonObject(firstChild)) {
- writer.name(childNode.getNodeName());
- writer.beginObject();
- inObject = true;
- // name
- } else if (!inArray) {
- writer.name(childNode.getNodeName());
+ private static boolean isArrayElement(final Node node) {
+ if (ELEMENT_NODE == node.getNodeType()) {
+ final String nodeName = node.getNodeName();
+ for (Node nextNode = node.getNextSibling(); nextNode != null; nextNode = nextNode.getNextSibling()) {
+ if (ELEMENT_NODE == nextNode.getNodeType() && nodeName.equals(nextNode.getNodeName())) {
+ return true;
}
}
+ }
+ return false;
+ }
- // text value, i.e. a number, string, boolean or null
- if (TEXT_NODE == childNode.getNodeType()) {
- final String childNodeText = childNode.getNodeValue();
- if (NUMBER_PATTERN.matcher(childNodeText).matches()) {
- writer.value(parseNumber(childNodeText));
- } else if ("true".equals(childNodeText) || "false".equals(childNodeText)) {
- writer.value(Boolean.parseBoolean(childNodeText));
- } else if ("null".equals(childNodeText)) {
- writer.nullValue();
+ private void handleArray(final Node node) throws IOException {
+ final Element parentNode = (Element)node.getParentNode();
+ final NodeList elementsList = parentNode.getElementsByTagName(node.getNodeName());
+ for (int i = 0, length = elementsList.getLength(); i < length; i++) {
+ final Node arrayElement = elementsList.item(i);
+ final Element parent = (Element)arrayElement.getParentNode();
+ if (parentNode.isSameNode(parent)) {
+ final Element firstChildElement = getFirstChildElement(arrayElement);
+ if (firstChildElement != null) {
+ writeObjectContent(firstChildElement);
} else {
- writer.value(childNodeText);
+ // It may be scalar
+ writeXmlValue(arrayElement);
}
+ }
+ }
+ }
- return;
+ private void writeObject(Node node) throws IOException {
+ String previousNodeName = "";
+ while (node != null) {
+ if (ELEMENT_NODE == node.getNodeType()) {
+ if (!node.getNodeName().equals(previousNodeName)) {
+ previousNodeName = node.getNodeName();
+ writer.name(node.getNodeName());
+ writeXmlNode(node);
+ }
}
+ node = node.getNextSibling();
+ }
+ }
+
+ private void writeXmlValue(final Node node) throws IOException {
+ Text firstChild = getFirstChildText(node);
+ String childNodeText = firstChild != null ? firstChild.getWholeText() : "";
+ childNodeText = childNodeText != null ? childNodeText.trim() : "";
- traverseAnyXmlValue(childNode);
+ if (NUMBER_PATTERN.matcher(childNodeText).matches()) {
+ writer.value(parseNumber(childNodeText));
+ return;
+ }
+ switch (childNodeText) {
+ case "null":
+ writer.nullValue();
+ break;
+ case "false":
+ writer.value(false);
+ break;
+ case "true":
+ writer.value(true);
+ break;
+ default:
+ writer.value(childNodeText);
+ }
+ }
- if (inObject) {
- writer.endObject();
+ private static Element getFirstChildElement(final Node node) {
+ final NodeList children = node.getChildNodes();
+ for (int i = 0, length = children.getLength(); i < length; i++) {
+ final Node childNode = children.item(i);
+ if (ELEMENT_NODE == childNode.getNodeType()) {
+ return (Element) childNode;
}
}
+ return null;
+ }
- if (inArray) {
- writer.endArray();
+ private static Text getFirstChildText(final Node node) {
+ final NodeList children = node.getChildNodes();
+ for (int i = 0, length = children.getLength(); i < length; i++) {
+ final Node childNode = children.item(i);
+ if (TEXT_NODE == childNode.getNodeType()) {
+ return (Text) childNode;
+ }
}
+ return null;
}
// json numbers are 64 bit wide floating point numbers - in java terms it is either long or double
return Double.valueOf(numberText);
}
-
- private static boolean isJsonObject(final Node firstChild) {
- return !ANYXML_ARRAY_ELEMENT_ID.equals(firstChild.getNodeName()) && TEXT_NODE != firstChild.getNodeType();
- }
-
- private static boolean isJsonObjectInArray(final Node node, final Node firstChild) {
- return ANYXML_ARRAY_ELEMENT_ID.equals(node.getNodeName())
- && !ANYXML_ARRAY_ELEMENT_ID.equals(firstChild.getNodeName())
- && TEXT_NODE != firstChild.getNodeType();
- }
}