Search in sources :

Example 1 with CompositeNodeDataWithSchema

use of org.opendaylight.yangtools.yang.data.util.CompositeNodeDataWithSchema in project yangtools by opendaylight.

the class XmlParserStream method read.

private void read(final XMLStreamReader in, final AbstractNodeDataWithSchema<?> parent, final String rootElement) throws XMLStreamException {
    if (!in.hasNext()) {
        return;
    }
    if (parent instanceof LeafNodeDataWithSchema || parent instanceof LeafListEntryNodeDataWithSchema) {
        parent.setAttributes(getElementAttributes(in));
        setValue((SimpleNodeDataWithSchema<?>) parent, in.getElementText().trim(), in.getNamespaceContext());
        if (isNextEndDocument(in)) {
            return;
        }
        if (!isAtElement(in)) {
            in.nextTag();
        }
        return;
    }
    if (parent instanceof ListEntryNodeDataWithSchema || parent instanceof ContainerNodeDataWithSchema) {
        parent.setAttributes(getElementAttributes(in));
    }
    if (parent instanceof LeafListNodeDataWithSchema || parent instanceof ListNodeDataWithSchema) {
        String xmlElementName = in.getLocalName();
        while (xmlElementName.equals(parent.getSchema().getQName().getLocalName())) {
            read(in, newEntryNode(parent), rootElement);
            if (in.getEventType() == XMLStreamConstants.END_DOCUMENT || in.getEventType() == XMLStreamConstants.END_ELEMENT) {
                break;
            }
            xmlElementName = in.getLocalName();
        }
        return;
    }
    if (parent instanceof AnyXmlNodeDataWithSchema) {
        setValue((AnyXmlNodeDataWithSchema) parent, readAnyXmlValue(in), in.getNamespaceContext());
        if (isNextEndDocument(in)) {
            return;
        }
        if (!isAtElement(in)) {
            in.nextTag();
        }
        return;
    }
    if (parent instanceof AnydataNodeDataWithSchema) {
        final AnydataNodeDataWithSchema anydata = (AnydataNodeDataWithSchema) parent;
        anydata.setObjectModel(DOMSourceAnydata.class);
        anydata.setAttributes(getElementAttributes(in));
        setValue(anydata, readAnyXmlValue(in), in.getNamespaceContext());
        if (isNextEndDocument(in)) {
            return;
        }
        if (!isAtElement(in)) {
            in.nextTag();
        }
        return;
    }
    switch(in.nextTag()) {
        case XMLStreamConstants.START_ELEMENT:
            // FIXME: 7.0.0: why do we even need this tracker? either document it or remove it.
            // it looks like it is a crude duplicate finder, which should really be handled via
            // ChildReusePolicy.REJECT
            final Set<Entry<String, String>> namesakes = new HashSet<>();
            while (in.hasNext()) {
                final String xmlElementName = in.getLocalName();
                final DataSchemaNode parentSchema = parent.getSchema();
                final String parentSchemaName = parentSchema.getQName().getLocalName();
                if (parentSchemaName.equals(xmlElementName) && in.getEventType() == XMLStreamConstants.END_ELEMENT) {
                    if (isNextEndDocument(in)) {
                        break;
                    }
                    if (!isAtElement(in)) {
                        in.nextTag();
                    }
                    break;
                }
                if (in.isEndElement() && rootElement.equals(xmlElementName)) {
                    break;
                }
                final String elementNS = in.getNamespaceURI();
                final boolean added = namesakes.add(new SimpleImmutableEntry<>(elementNS, xmlElementName));
                final XMLNamespace nsUri;
                try {
                    nsUri = rawXmlNamespace(elementNS).getNamespace();
                } catch (IllegalArgumentException e) {
                    throw new XMLStreamException("Failed to convert namespace " + xmlElementName, in.getLocation(), e);
                }
                final Deque<DataSchemaNode> childDataSchemaNodes = ParserStreamUtils.findSchemaNodeByNameAndNamespace(parentSchema, xmlElementName, nsUri);
                if (!childDataSchemaNodes.isEmpty()) {
                    final boolean elementList = isElementList(childDataSchemaNodes);
                    if (!added && !elementList) {
                        throw new XMLStreamException(String.format("Duplicate element \"%s\" in namespace \"%s\" with parent \"%s\" in XML input", xmlElementName, elementNS, parentSchema), in.getLocation());
                    }
                    // We have a match, proceed with it
                    final QName qname = childDataSchemaNodes.peekLast().getQName();
                    final AbstractNodeDataWithSchema<?> child = ((CompositeNodeDataWithSchema<?>) parent).addChild(childDataSchemaNodes, elementList ? ChildReusePolicy.REUSE : ChildReusePolicy.NOOP);
                    stack.enterDataTree(qname);
                    read(in, child, rootElement);
                    stack.exit();
                    continue;
                }
                if (parent instanceof AbstractMountPointDataWithSchema) {
                    // Parent can potentially hold a mount point, let's see if there is a label present
                    final Optional<MountPointSchemaNode> optMount;
                    if (parentSchema instanceof ContainerSchemaNode) {
                        optMount = MountPointSchemaNode.streamAll((ContainerSchemaNode) parentSchema).findFirst();
                    } else if (parentSchema instanceof ListSchemaNode) {
                        optMount = MountPointSchemaNode.streamAll((ListSchemaNode) parentSchema).findFirst();
                    } else {
                        throw new XMLStreamException("Unhandled mount-aware schema " + parentSchema, in.getLocation());
                    }
                    if (optMount.isPresent()) {
                        final MountPointIdentifier mountId = MountPointIdentifier.of(optMount.get().getQName());
                        LOG.debug("Assuming node {} and namespace {} belongs to mount point {}", xmlElementName, nsUri, mountId);
                        final Optional<MountPointContextFactory> optFactory = codecs.mountPointContext().findMountPoint(mountId);
                        if (optFactory.isPresent()) {
                            final MountPointData mountData = ((AbstractMountPointDataWithSchema<?>) parent).getMountPointData(mountId, optFactory.get());
                            addMountPointChild(mountData, nsUri, xmlElementName, new DOMSource(readAnyXmlValue(in).getDocumentElement()));
                            continue;
                        }
                        LOG.debug("Mount point {} not attached", mountId);
                    }
                }
                // We have not handled the node -- let's decide what to do about that
                if (strictParsing) {
                    throw new XMLStreamException(String.format("Schema for node with name %s and namespace %s does not exist in parent %s", xmlElementName, elementNS, parentSchema), in.getLocation());
                }
                LOG.debug("Skipping unknown node ns=\"{}\" localName=\"{}\" in parent {}", elementNS, xmlElementName, parentSchema);
                skipUnknownNode(in);
            }
            break;
        case XMLStreamConstants.END_ELEMENT:
            if (isNextEndDocument(in)) {
                break;
            }
            if (!isAtElement(in)) {
                in.nextTag();
            }
            break;
        default:
            break;
    }
}
Also used : LeafListNodeDataWithSchema(org.opendaylight.yangtools.yang.data.util.LeafListNodeDataWithSchema) ListNodeDataWithSchema(org.opendaylight.yangtools.yang.data.util.ListNodeDataWithSchema) DOMSource(javax.xml.transform.dom.DOMSource) ContainerNodeDataWithSchema(org.opendaylight.yangtools.yang.data.util.ContainerNodeDataWithSchema) MountPointData(org.opendaylight.yangtools.yang.data.util.MountPointData) AbstractMountPointDataWithSchema(org.opendaylight.yangtools.yang.data.util.AbstractMountPointDataWithSchema) Entry(java.util.Map.Entry) SimpleImmutableEntry(java.util.AbstractMap.SimpleImmutableEntry) ContainerSchemaNode(org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode) XMLNamespace(org.opendaylight.yangtools.yang.common.XMLNamespace) HashSet(java.util.HashSet) MountPointContextFactory(org.opendaylight.yangtools.rfc8528.data.api.MountPointContextFactory) TypedDataSchemaNode(org.opendaylight.yangtools.yang.model.api.TypedDataSchemaNode) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) AnydataNodeDataWithSchema(org.opendaylight.yangtools.yang.data.util.AnydataNodeDataWithSchema) QName(org.opendaylight.yangtools.yang.common.QName) LeafNodeDataWithSchema(org.opendaylight.yangtools.yang.data.util.LeafNodeDataWithSchema) MountPointSchemaNode(org.opendaylight.yangtools.rfc8528.model.api.MountPointSchemaNode) LeafListEntryNodeDataWithSchema(org.opendaylight.yangtools.yang.data.util.LeafListEntryNodeDataWithSchema) LeafListNodeDataWithSchema(org.opendaylight.yangtools.yang.data.util.LeafListNodeDataWithSchema) CompositeNodeDataWithSchema(org.opendaylight.yangtools.yang.data.util.CompositeNodeDataWithSchema) XMLStreamException(javax.xml.stream.XMLStreamException) ListEntryNodeDataWithSchema(org.opendaylight.yangtools.yang.data.util.ListEntryNodeDataWithSchema) LeafListEntryNodeDataWithSchema(org.opendaylight.yangtools.yang.data.util.LeafListEntryNodeDataWithSchema) ListSchemaNode(org.opendaylight.yangtools.yang.model.api.ListSchemaNode) LeafListSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode) MountPointIdentifier(org.opendaylight.yangtools.rfc8528.data.api.MountPointIdentifier) AnyXmlNodeDataWithSchema(org.opendaylight.yangtools.yang.data.util.AnyXmlNodeDataWithSchema)

Example 2 with CompositeNodeDataWithSchema

use of org.opendaylight.yangtools.yang.data.util.CompositeNodeDataWithSchema in project yangtools by opendaylight.

the class JsonParserStream method parse.

public JsonParserStream parse(final JsonReader reader) {
    // code copied from gson's JsonParser and Stream classes
    final boolean readerLenient = reader.isLenient();
    reader.setLenient(true);
    boolean isEmpty = true;
    try {
        reader.peek();
        isEmpty = false;
        final CompositeNodeDataWithSchema<?> compositeNodeDataWithSchema = new CompositeNodeDataWithSchema<>(parentNode);
        read(reader, compositeNodeDataWithSchema);
        compositeNodeDataWithSchema.write(writer);
        return this;
    } catch (final EOFException e) {
        if (isEmpty) {
            return this;
        }
        // The stream ended prematurely so it is likely a syntax error.
        throw new JsonSyntaxException(e);
    } catch (final MalformedJsonException | NumberFormatException e) {
        throw new JsonSyntaxException(e);
    } catch (final IOException e) {
        throw new JsonIOException(e);
    } catch (StackOverflowError | OutOfMemoryError e) {
        throw new JsonParseException("Failed parsing JSON source: " + reader + " to Json", e);
    } finally {
        reader.setLenient(readerLenient);
    }
}
Also used : JsonIOException(com.google.gson.JsonIOException) IOException(java.io.IOException) JsonParseException(com.google.gson.JsonParseException) CompositeNodeDataWithSchema(org.opendaylight.yangtools.yang.data.util.CompositeNodeDataWithSchema) JsonSyntaxException(com.google.gson.JsonSyntaxException) JsonIOException(com.google.gson.JsonIOException) EOFException(java.io.EOFException) MalformedJsonException(com.google.gson.stream.MalformedJsonException)

Example 3 with CompositeNodeDataWithSchema

use of org.opendaylight.yangtools.yang.data.util.CompositeNodeDataWithSchema in project yangtools by opendaylight.

the class JsonParserStream method read.

private void read(final JsonReader in, AbstractNodeDataWithSchema<?> parent) throws IOException {
    switch(in.peek()) {
        case STRING:
        case NUMBER:
            setValue(parent, in.nextString());
            break;
        case BOOLEAN:
            setValue(parent, Boolean.toString(in.nextBoolean()));
            break;
        case NULL:
            in.nextNull();
            setValue(parent, null);
            break;
        case BEGIN_ARRAY:
            in.beginArray();
            while (in.hasNext()) {
                if (parent instanceof LeafNodeDataWithSchema) {
                    read(in, parent);
                } else {
                    final AbstractNodeDataWithSchema<?> newChild = newArrayEntry(parent);
                    read(in, newChild);
                }
            }
            in.endArray();
            return;
        case BEGIN_OBJECT:
            final Set<String> namesakes = new HashSet<>();
            in.beginObject();
            /*
                 * This allows parsing of incorrectly /as showcased/
                 * in testconf nesting of list items - eg.
                 * lists with one value are sometimes serialized
                 * without wrapping array.
                 *
                 */
            if (isArray(parent)) {
                parent = newArrayEntry(parent);
            }
            while (in.hasNext()) {
                final String jsonElementName = in.nextName();
                final DataSchemaNode parentSchema = parent.getSchema();
                final Entry<String, XMLNamespace> namespaceAndName = resolveNamespace(jsonElementName, parentSchema);
                final String localName = namespaceAndName.getKey();
                final XMLNamespace namespace = namespaceAndName.getValue();
                if (lenient && (localName == null || namespace == null)) {
                    LOG.debug("Schema node with name {} was not found under {}", localName, parentSchema.getQName());
                    in.skipValue();
                    continue;
                }
                addNamespace(namespace);
                if (!namesakes.add(jsonElementName)) {
                    throw new JsonSyntaxException("Duplicate name " + jsonElementName + " in JSON input.");
                }
                final Deque<DataSchemaNode> childDataSchemaNodes = ParserStreamUtils.findSchemaNodeByNameAndNamespace(parentSchema, localName, getCurrentNamespace());
                checkState(!childDataSchemaNodes.isEmpty(), "Schema for node with name %s and namespace %s does not exist at %s", localName, getCurrentNamespace(), parentSchema);
                final QName qname = childDataSchemaNodes.peekLast().getQName();
                final AbstractNodeDataWithSchema<?> newChild = ((CompositeNodeDataWithSchema<?>) parent).addChild(childDataSchemaNodes, ChildReusePolicy.NOOP);
                if (newChild instanceof AnyXmlNodeDataWithSchema) {
                    readAnyXmlValue(in, (AnyXmlNodeDataWithSchema) newChild, jsonElementName);
                } else {
                    stack.enterDataTree(qname);
                    read(in, newChild);
                    stack.exit();
                }
                removeNamespace();
            }
            in.endObject();
            return;
        default:
            break;
    }
}
Also used : TypedDataSchemaNode(org.opendaylight.yangtools.yang.model.api.TypedDataSchemaNode) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) QName(org.opendaylight.yangtools.yang.common.QName) LeafNodeDataWithSchema(org.opendaylight.yangtools.yang.data.util.LeafNodeDataWithSchema) JsonSyntaxException(com.google.gson.JsonSyntaxException) CompositeNodeDataWithSchema(org.opendaylight.yangtools.yang.data.util.CompositeNodeDataWithSchema) XMLNamespace(org.opendaylight.yangtools.yang.common.XMLNamespace) HashSet(java.util.HashSet) AnyXmlNodeDataWithSchema(org.opendaylight.yangtools.yang.data.util.AnyXmlNodeDataWithSchema)

Aggregations

CompositeNodeDataWithSchema (org.opendaylight.yangtools.yang.data.util.CompositeNodeDataWithSchema)3 JsonSyntaxException (com.google.gson.JsonSyntaxException)2 HashSet (java.util.HashSet)2 QName (org.opendaylight.yangtools.yang.common.QName)2 XMLNamespace (org.opendaylight.yangtools.yang.common.XMLNamespace)2 AnyXmlNodeDataWithSchema (org.opendaylight.yangtools.yang.data.util.AnyXmlNodeDataWithSchema)2 LeafNodeDataWithSchema (org.opendaylight.yangtools.yang.data.util.LeafNodeDataWithSchema)2 DataSchemaNode (org.opendaylight.yangtools.yang.model.api.DataSchemaNode)2 TypedDataSchemaNode (org.opendaylight.yangtools.yang.model.api.TypedDataSchemaNode)2 JsonIOException (com.google.gson.JsonIOException)1 JsonParseException (com.google.gson.JsonParseException)1 MalformedJsonException (com.google.gson.stream.MalformedJsonException)1 EOFException (java.io.EOFException)1 IOException (java.io.IOException)1 SimpleImmutableEntry (java.util.AbstractMap.SimpleImmutableEntry)1 Entry (java.util.Map.Entry)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 DOMSource (javax.xml.transform.dom.DOMSource)1 MountPointContextFactory (org.opendaylight.yangtools.rfc8528.data.api.MountPointContextFactory)1 MountPointIdentifier (org.opendaylight.yangtools.rfc8528.data.api.MountPointIdentifier)1