Search in sources :

Example 1 with ArgumentDefinition

use of org.opendaylight.yangtools.yang.model.api.meta.ArgumentDefinition in project yangtools by opendaylight.

the class UnrecognizedStatementSupport method statementDefinitionOverrideOf.

@Override
public UnrecognizedStatementSupport statementDefinitionOverrideOf(final StatementDefinition childDef) {
    /*
         * This code wraps statements encountered inside an extension so they do not get confused with regular
         * statements.
         */
    final QName baseQName = getStatementName();
    final QName statementName = QName.create(baseQName, childDef.getStatementName().getLocalName());
    final ModelDefinedStatementDefinition def;
    final Optional<ArgumentDefinition> optArgDef = childDef.getArgumentDefinition();
    if (optArgDef.isPresent()) {
        final ArgumentDefinition argDef = optArgDef.get();
        def = new ModelDefinedStatementDefinition(statementName, argDef.getArgumentName(), argDef.isYinElement());
    } else {
        def = new ModelDefinedStatementDefinition(statementName);
    }
    return new UnrecognizedStatementSupport(def, config);
}
Also used : QName(org.opendaylight.yangtools.yang.common.QName) ArgumentDefinition(org.opendaylight.yangtools.yang.model.api.meta.ArgumentDefinition)

Example 2 with ArgumentDefinition

use of org.opendaylight.yangtools.yang.model.api.meta.ArgumentDefinition in project yangtools by opendaylight.

the class YinStatementStreamSource method processElement.

private static boolean processElement(final int childId, final Element element, final StatementWriter writer, final QNameToStatementDefinition stmtDef) {
    final Optional<? extends ResumedStatement> optResumed = writer.resumeStatement(childId);
    final StatementSourceReference ref;
    final QName argName;
    final boolean allAttrs;
    final boolean allElements;
    if (optResumed.isPresent()) {
        final ResumedStatement resumed = optResumed.get();
        if (resumed.isFullyDefined()) {
            return true;
        }
        final StatementDefinition def = resumed.getDefinition();
        ref = resumed.getSourceReference();
        final Optional<ArgumentDefinition> optArgDef = def.getArgumentDefinition();
        if (optArgDef.isPresent()) {
            final ArgumentDefinition argDef = optArgDef.get();
            argName = argDef.getArgumentName();
            allAttrs = argDef.isYinElement();
            allElements = !allAttrs;
        } else {
            argName = null;
            allAttrs = false;
            allElements = false;
        }
    } else {
        ref = extractRef(element);
        final StatementDefinition def = getValidDefinition(element, writer, stmtDef, ref);
        if (def == null) {
            LOG.debug("Skipping element {}", element);
            return false;
        }
        final String argValue;
        final Optional<ArgumentDefinition> optArgDef = def.getArgumentDefinition();
        if (optArgDef.isPresent()) {
            final ArgumentDefinition argDef = optArgDef.get();
            argName = argDef.getArgumentName();
            allAttrs = argDef.isYinElement();
            allElements = !allAttrs;
            argValue = getArgValue(element, argName, allAttrs);
            SourceException.throwIfNull(argValue, ref, "Statement {} is missing mandatory argument %s", def.getStatementName(), argName);
        } else {
            argName = null;
            argValue = null;
            allAttrs = false;
            allElements = false;
        }
        writer.startStatement(childId, def.getStatementName(), argValue, ref);
    }
    // Child counter
    int childCounter = 0;
    boolean fullyDefined = true;
    // First process any statements defined as attributes. We need to skip argument, if present
    final NamedNodeMap attributes = element.getAttributes();
    if (attributes != null) {
        for (int i = 0, len = attributes.getLength(); i < len; ++i) {
            final Attr attr = (Attr) attributes.item(i);
            if ((allAttrs || !isArgument(argName, attr)) && !processAttribute(childCounter++, attr, writer, stmtDef, ref)) {
                fullyDefined = false;
            }
        }
    }
    // Now process child elements, if present
    final NodeList children = element.getChildNodes();
    for (int i = 0, len = children.getLength(); i < len; ++i) {
        final Node child = children.item(i);
        if (child.getNodeType() == Node.ELEMENT_NODE && (allElements || !isArgument(argName, child)) && !processElement(childCounter++, (Element) child, writer, stmtDef)) {
            fullyDefined = false;
        }
    }
    writer.storeStatement(childCounter, fullyDefined);
    writer.endStatement(ref);
    return fullyDefined;
}
Also used : StatementSourceReference(org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference) NamedNodeMap(org.w3c.dom.NamedNodeMap) QName(org.opendaylight.yangtools.yang.common.QName) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ArgumentDefinition(org.opendaylight.yangtools.yang.model.api.meta.ArgumentDefinition) Attr(org.w3c.dom.Attr) QNameToStatementDefinition(org.opendaylight.yangtools.yang.parser.spi.source.QNameToStatementDefinition) StatementDefinition(org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition) ResumedStatement(org.opendaylight.yangtools.yang.parser.spi.source.StatementWriter.ResumedStatement)

Example 3 with ArgumentDefinition

use of org.opendaylight.yangtools.yang.model.api.meta.ArgumentDefinition in project yangtools by opendaylight.

the class YinXMLEventReader method addStatement.

private void addStatement(final DeclaredStatement<?> statement) {
    final StatementDefinition def = statement.statementDefinition();
    final QName name = def.getStatementName();
    final Optional<ArgumentDefinition> optArgDef = def.getArgumentDefinition();
    if (optArgDef.isPresent()) {
        final ArgumentDefinition argDef = optArgDef.get();
        final QName argName = argDef.getArgumentName();
        if (argDef.isYinElement()) {
            events.addAll(Arrays.asList(startElement(name), startElement(argName), eventFactory.createCharacters(statement.rawArgument()), endElement(argName)));
        } else {
            final Entry<String, String> ns = namespaceContext.prefixAndNamespaceFor(name.getModule());
            events.add(eventFactory.createStartElement(ns.getKey(), ns.getValue(), name.getLocalName(), singletonIterator(attribute(argName, statement.rawArgument())), emptyIterator(), namespaceContext));
        }
    } else {
        // No attributes: just emit a start
        events.add(startElement(name));
    }
    stack.push(new OpenElement(name, statement.declaredSubstatements().iterator()));
}
Also used : StatementDefinition(org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition) QName(org.opendaylight.yangtools.yang.common.QName) ArgumentDefinition(org.opendaylight.yangtools.yang.model.api.meta.ArgumentDefinition)

Aggregations

QName (org.opendaylight.yangtools.yang.common.QName)3 ArgumentDefinition (org.opendaylight.yangtools.yang.model.api.meta.ArgumentDefinition)3 StatementDefinition (org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition)2 QNameToStatementDefinition (org.opendaylight.yangtools.yang.parser.spi.source.QNameToStatementDefinition)1 StatementSourceReference (org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference)1 ResumedStatement (org.opendaylight.yangtools.yang.parser.spi.source.StatementWriter.ResumedStatement)1 Attr (org.w3c.dom.Attr)1 NamedNodeMap (org.w3c.dom.NamedNodeMap)1 Node (org.w3c.dom.Node)1 NodeList (org.w3c.dom.NodeList)1