Java Code Examples for org.opendaylight.yangtools.yang.common.QName#getModule()

The following examples show how to use org.opendaylight.yangtools.yang.common.QName#getModule() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JSONStreamWriterContext.java    From yangtools with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Write a child JSON node identifier, optionally prefixing it with the module name corresponding to its namespace.
 *
 * @param schema Schema context
 * @param writer Output writer
 * @param qname Namespace/name tuple
 * @throws IOException when the writer reports it
 */
final void writeChildJsonIdentifier(final SchemaContext schema, final JsonWriter writer, final QName qname)
        throws IOException {

    final StringBuilder sb = new StringBuilder();
    // Prepend module name if namespaces do not match
    final QNameModule module = qname.getModule();
    if (!module.getNamespace().equals(getNamespace())) {
        final Optional<String> modules = schema.findModule(module).map(Module::getName);
        checkArgument(modules.isPresent(), "Could not find module for namespace {}", module);
        sb.append(modules.get()).append(':');
    }
    sb.append(qname.getLocalName());

    writer.name(sb.toString());
}
 
Example 2
Source File: AbstractMagnesiumDataOutput.java    From yangtools with Eclipse Public License 1.0 6 votes vote down vote up
private void encodeQName(final @NonNull QName qname) throws IOException {
    final Integer prev = qnameCodeMap.put(qname, qnameCodeMap.size());
    if (prev != null) {
        throw new IOException("Internal coding error: attempted to re-encode " + qname + "%s already encoded as "
                + prev);
    }

    final QNameModule module = qname.getModule();
    final Integer code = moduleCodeMap.get(module);
    if (code == null) {
        moduleCodeMap.put(module, moduleCodeMap.size());
        encodeString(module.getNamespace().toString());
        final Optional<Revision> rev = module.getRevision();
        if (rev.isPresent()) {
            encodeString(rev.get().toString());
        } else {
            output.writeByte(MagnesiumValue.STRING_EMPTY);
        }
    } else {
        writeModuleRef(code);
    }
    encodeString(qname.getLocalName());
}
 
Example 3
Source File: SchemaNodeIdentifier.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
private static List<?> simplifyQNames(final List<QName> qnames) {
    final List<Object> ret = new ArrayList<>(qnames.size());

    QNameModule prev = null;
    for (QName qname : qnames) {
        final QNameModule module = qname.getModule();
        ret.add(module.equals(prev) ? qname.getLocalName() : qname);
        prev = module;
    }

    return ret;
}
 
Example 4
Source File: AbstractStringInstanceIdentifierCodec.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected final String serializeImpl(final YangInstanceIdentifier data) {
    final StringBuilder sb = new StringBuilder();
    DataSchemaContextNode<?> current = getDataContextTree().getRoot();
    QNameModule lastModule = null;
    for (PathArgument arg : data.getPathArguments()) {
        current = current.getChild(arg);
        checkArgument(current != null, "Invalid input %s: schema for argument %s (after %s) not found", data, arg,
                sb);

        if (current.isMixin()) {
            /*
             * XML/YANG instance identifier does not have concept
             * of augmentation identifier, or list as whole which
             * identifies a mixin (same as the parent element),
             * so we can safely ignore it if it is part of path
             * (since child node) is identified in same fashion.
             */
            continue;
        }

        final QName qname = arg.getNodeType();
        sb.append('/');
        appendQName(sb, qname, lastModule);
        lastModule = qname.getModule();

        if (arg instanceof NodeIdentifierWithPredicates) {
            for (Entry<QName, Object> entry : ((NodeIdentifierWithPredicates) arg).entrySet()) {
                appendQName(sb.append('['), entry.getKey(), lastModule);
                sb.append("='").append(String.valueOf(entry.getValue())).append("']");
            }
        } else if (arg instanceof NodeWithValue) {
            sb.append("[.='").append(((NodeWithValue<?>) arg).getValue()).append("']");
        }
    }
    return sb.toString();
}
 
Example 5
Source File: XpathStringParsingPathArgumentBuilder.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
private PathArgument computeNextArgument() {
    checkValid(SLASH == currentChar(), "Identifier must start with '/'.");
    skipCurrentChar();
    checkValid(!allCharactersConsumed(), "Identifier cannot end with '/'.");
    final QName name = nextQName();
    // Memoize module
    lastModule = name.getModule();
    if (allCharactersConsumed() || SLASH == currentChar()) {
        return computeIdentifier(name);
    }

    checkValid(PRECONDITION_START == currentChar(), "Last element must be identifier, predicate or '/'");
    return computeIdentifierWithPredicate(name);
}
 
Example 6
Source File: LeafRefPathParserImpl.java    From yangtools with Eclipse Public License 1.0 4 votes vote down vote up
private static QNameWithPredicate adaptChildStep(final QNameStep step, final QNameModule localModule) {
    final QName qname = resolve(step.getQName(), localModule);
    final Set<YangExpr> predicates = step.getPredicates();
    if (predicates.isEmpty()) {
        return new SimpleQNameWithPredicate(qname);
    }

    final QNameWithPredicateBuilder builder = new QNameWithPredicateBuilder(qname.getModule(),
        qname.getLocalName());

    for (YangExpr pred : predicates) {
        final QNamePredicateBuilder predBuilder = new QNamePredicateBuilder();

        if (pred instanceof YangBinaryExpr) {
            final YangBinaryExpr eqPred = (YangBinaryExpr) pred;
            checkState(eqPred.getOperator() == YangBinaryOperator.EQUALS);

            final YangExpr left = eqPred.getLeftExpr();
            checkState(left instanceof YangQNameExpr, "Unsupported left expression %s", left);
            predBuilder.setIdentifier(resolve(((YangQNameExpr) left).getQName(), localModule));

            final YangExpr right = eqPred.getRightExpr();
            if (right instanceof YangPathExpr) {
                final YangPathExpr rightPath = (YangPathExpr) right;
                final YangExpr filter = rightPath.getFilterExpr();
                if (filter instanceof YangFunctionCallExpr) {
                    checkState(YangFunction.CURRENT.getIdentifier().equals(
                        ((YangFunctionCallExpr) filter).getName()));
                } else {
                    throw new IllegalStateException("Unhandled filter " + filter);
                }

                final Relative location = rightPath.getLocationPath()
                        .orElseThrow(() -> new IllegalStateException("Missing locationPath in " + rightPath));
                predBuilder.setPathKeyExpression(LeafRefPath.create(
                    createPathSteps(localModule, location.getSteps()), false));
            } else {
                throw new UnsupportedOperationException("Not implemented for " + right);
            }
        }

        builder.addQNamePredicate(predBuilder.build());
    }

    return builder.build();
}