import java.util.Date;
import java.util.Map;
import java.util.Map.Entry;
-import java.util.Set;
import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.ImportStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.ModuleStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.NamespaceStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.PrefixStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.RevisionDateStatement;
import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleIdentifierImpl;
import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
import org.opendaylight.yangtools.yang.parser.spi.source.ImpPrefixToModuleIdentifier;
-import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.ImportEffectiveStatementImpl;
public class ImportStatementDefinition
- extends
- AbstractStatementSupport<String, ImportStatement, EffectiveStatement<String, ImportStatement>> {
+ extends AbstractStatementSupport<String, ImportStatement, EffectiveStatement<String, ImportStatement>> {
public ImportStatementDefinition() {
super(Rfc6020Mapping.IMPORT);
@Override
public void onLinkageDeclared(
- final Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> stmt)
- throws SourceException {
+ final Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> stmt) {
final ModuleIdentifier impIdentifier = getImportedModuleIdentifier(stmt);
- ModelActionBuilder importAction = stmt
- .newInferenceAction(SOURCE_LINKAGE);
- final Prerequisite<StmtContext<?, ?, ?>> imported;
- final Prerequisite<Mutable<?, ?, ?>> linkageTarget;
- imported = importAction.requiresCtx(stmt, ModuleNamespace.class,
- impIdentifier, SOURCE_LINKAGE);
- linkageTarget = importAction.mutatesCtx(stmt.getRoot(), SOURCE_LINKAGE);
+ final ModelActionBuilder importAction = stmt.newInferenceAction(SOURCE_LINKAGE);
+ final Prerequisite<StmtContext<?, ?, ?>> imported = importAction.requiresCtx(stmt, ModuleNamespace.class,
+ impIdentifier, SOURCE_LINKAGE);
+ final Prerequisite<Mutable<?, ?, ?>> linkageTarget = importAction.mutatesCtx(stmt.getRoot(), SOURCE_LINKAGE);
importAction.apply(new InferenceAction() {
-
@Override
public void apply() throws InferenceException {
StmtContext<?, ?, ?> importedModule = null;
ModuleIdentifier importedModuleIdentifier = null;
if (impIdentifier.getRevision() == SimpleDateFormatUtil.DEFAULT_DATE_IMP) {
Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> recentModuleEntry = findRecentModule(
- impIdentifier,
- stmt.getAllFromNamespace(ModuleNamespace.class));
+ impIdentifier, stmt.getAllFromNamespace(ModuleNamespace.class));
if (recentModuleEntry != null) {
importedModuleIdentifier = recentModuleEntry.getKey();
importedModule = recentModuleEntry.getValue();
}
}
- if(importedModule == null || importedModuleIdentifier == null) {
+ if (importedModule == null || importedModuleIdentifier == null) {
importedModule = imported.get();
importedModuleIdentifier = impIdentifier;
}
- linkageTarget.get().addToNs(ImportedModuleContext.class,
- importedModuleIdentifier, importedModule);
- String impPrefix = firstAttributeOf(stmt.declaredSubstatements(),
- PrefixStatement.class);
- stmt.addToNs(ImpPrefixToModuleIdentifier.class, impPrefix,
- importedModuleIdentifier);
+ linkageTarget.get().addToNs(ImportedModuleContext.class, importedModuleIdentifier, importedModule);
+ String impPrefix = firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class);
+ stmt.addToNs(ImpPrefixToModuleIdentifier.class, impPrefix, importedModuleIdentifier);
+
+ final URI modNs = firstAttributeOf(importedModule.declaredSubstatements(), NamespaceStatement.class);
+ stmt.addToNs(URIStringToImpPrefix.class, modNs.toString(), impPrefix);
}
private Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> findRecentModule(
ModuleIdentifier recentModuleIdentifier = impIdentifier;
Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> recentModuleEntry = null;
- Set<Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>>> moduleEntrySet = allModules
- .entrySet();
- for (Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> moduleEntry : moduleEntrySet) {
- if (moduleEntry.getKey().getName()
- .equals(impIdentifier.getName())
- && moduleEntry
- .getKey()
- .getRevision()
- .compareTo(
- recentModuleIdentifier
- .getRevision()) > 0) {
+ for (Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> moduleEntry : allModules.entrySet()) {
+ if (moduleEntry.getKey().getName().equals(impIdentifier.getName())
+ && moduleEntry.getKey().getRevision().compareTo(recentModuleIdentifier.getRevision()) > 0) {
recentModuleIdentifier = moduleEntry.getKey();
recentModuleEntry = moduleEntry;
}
}
@Override
- public void prerequisiteFailed(
- final Collection<? extends Prerequisite<?>> failed)
- throws InferenceException {
+ public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
if (failed.contains(imported)) {
- throw new InferenceException(String.format(
- "Imported module [%s] was not found.",
- impIdentifier), stmt.getStatementSourceReference());
+ throw new InferenceException(String.format("Imported module [%s] was not found.", impIdentifier),
+ stmt.getStatementSourceReference());
}
}
});
}
- private static ModuleIdentifier getImportedModuleIdentifier(
- final Mutable<String, ImportStatement, ?> stmt) throws SourceException {
-
- String moduleName = stmt.getStatementArgument();
- Date revision = firstAttributeOf(stmt.declaredSubstatements(),
- RevisionDateStatement.class);
+ private static ModuleIdentifier getImportedModuleIdentifier(final Mutable<String, ImportStatement, ?> stmt) {
+ Date revision = firstAttributeOf(stmt.declaredSubstatements(), RevisionDateStatement.class);
if (revision == null) {
revision = SimpleDateFormatUtil.DEFAULT_DATE_IMP;
}
- return new ModuleIdentifierImpl(moduleName, Optional.<URI> absent(),
+ return new ModuleIdentifierImpl(stmt.getStatementArgument(), Optional.<URI> absent(),
Optional.<Date> of(revision));
}
--- /dev/null
+/*
+ * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
+
+import com.google.common.base.Preconditions;
+import com.google.common.base.Verify;
+import com.google.common.collect.Iterators;
+import java.util.Iterator;
+import javax.xml.namespace.NamespaceContext;
+import org.opendaylight.yangtools.yang.common.QNameModule;
+import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
+
+/**
+ * A {@link NamespaceContext} implementation based on the set of imports and local module namespace.
+ *
+ * TODO: this is a useful utility, so should probably move to yang.parser.spi.meta.
+ */
+final class StmtNamespaceContext implements NamespaceContext {
+ private final StmtContext<?, ?, ?> ctx;
+ private String localNamespaceURI;
+
+ private StmtNamespaceContext(final StmtContext<?, ?, ?> ctx) {
+ this.ctx = Preconditions.checkNotNull(ctx);
+ }
+
+ public static NamespaceContext create(final StmtContext<?, ?, ?> ctx) {
+ return new StmtNamespaceContext(ctx);
+ }
+
+ private String localNamespaceURI() {
+ if (localNamespaceURI == null) {
+ localNamespaceURI = Verify.verifyNotNull(
+ ctx.getPublicDefinition().getStatementName().getNamespace().toString(),
+ "Local namespace URI not found in %s", ctx);
+ }
+ return localNamespaceURI;
+ }
+
+ @Override
+ public String getNamespaceURI(final String prefix) {
+ // API-mandated by NamespaceContext
+ Preconditions.checkArgument(prefix != null);
+
+ if (prefix.isEmpty()) {
+ return localNamespaceURI();
+ }
+
+ final QNameModule module = Utils.getModuleQNameByPrefix(ctx, prefix);
+ return module == null ? null : module.getNamespace().toString();
+ }
+
+ @Override
+ public String getPrefix(final String namespaceURI) {
+ // API-mandated by NamespaceContext
+ Preconditions.checkArgument(namespaceURI != null);
+
+ if (localNamespaceURI().equals(namespaceURI)) {
+ return "";
+ }
+ return ctx.getFromNamespace(URIStringToImpPrefix.class, namespaceURI);
+ }
+
+ @Override
+ public Iterator<String> getPrefixes(final String namespaceURI) {
+ // Ensures underlying map remains constant
+ return Iterators.unmodifiableIterator(
+ ctx.getAllFromNamespace(URIStringToImpPrefix.class).values().iterator());
+ }
+}