2 * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.odlext.parser;
11 import java.util.Optional;
12 import org.opendaylight.yangtools.odlext.model.api.AnyxmlSchemaLocationStatement;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
15 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
16 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
17 import org.opendaylight.yangtools.yang.model.api.stmt.AnyxmlEffectiveStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.AnyxmlStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
20 import org.opendaylight.yangtools.yang.parser.rfc7950.namespace.SchemaNodeIdentifierBuildNamespace;
21 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.anyxml.AnyxmlStatementSupport;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.ForwardingStatementSupport;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupport;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
27 public final class AnyxmlStatementSupportOverride
28 extends ForwardingStatementSupport<QName, AnyxmlStatement, AnyxmlEffectiveStatement> {
29 private static final AnyxmlStatementSupportOverride INSTANCE = new AnyxmlStatementSupportOverride();
31 private final StatementSupport<QName, AnyxmlStatement, AnyxmlEffectiveStatement> delegate =
32 new AnyxmlStatementSupport();
34 private AnyxmlStatementSupportOverride() {
38 public static AnyxmlStatementSupportOverride getInstance() {
43 protected StatementSupport<QName, AnyxmlStatement, AnyxmlEffectiveStatement> delegate() {
48 public Class<? extends EffectiveStatement<?, ?>> getEffectiveRepresentationClass() {
49 // FIXME: this is not entirely accurate?
50 return delegate.getEffectiveRepresentationClass();
54 public AnyxmlEffectiveStatement createEffective(
55 final StmtContext<QName, AnyxmlStatement, AnyxmlEffectiveStatement> ctx) {
56 final AnyxmlEffectiveStatement delegateStatement = delegate.createEffective(ctx);
57 final Map<StatementDefinition, Mutable<SchemaNodeIdentifier, AnyxmlSchemaLocationStatement,
58 EffectiveStatement<SchemaNodeIdentifier, AnyxmlSchemaLocationStatement>>> schemaLocations =
59 ctx.getAllFromCurrentStmtCtxNamespace(AnyxmlSchemaLocationNamespace.class);
60 if (schemaLocations != null && !schemaLocations.isEmpty()) {
61 final SchemaNodeIdentifier anyXmlSchemaNodeIdentifier = schemaLocations.values().iterator().next()
62 .getStatementArgument();
63 final Optional<ContainerSchemaNode> anyXmlSchema = getAnyXmlSchema(ctx, anyXmlSchemaNodeIdentifier);
64 if (anyXmlSchema.isPresent()) {
65 return new YangModeledAnyxmlEffectiveStatementImpl(delegateStatement, anyXmlSchema.get());
69 return delegateStatement;
72 private static Optional<ContainerSchemaNode> getAnyXmlSchema(
73 final StmtContext<QName, AnyxmlStatement, AnyxmlEffectiveStatement> ctx,
74 final SchemaNodeIdentifier contentSchemaPath) {
75 return SchemaNodeIdentifierBuildNamespace.findNode(ctx.getRoot(), contentSchemaPath)
76 .map(StmtContext::buildEffective)
77 .filter(ContainerSchemaNode.class::isInstance).map(ContainerSchemaNode.class::cast);