2 * Copyright (c) 2015 Cisco Systems, Inc. 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.yang.parser.rfc7950.namespace;
10 import com.google.common.collect.ImmutableBiMap;
11 import com.google.common.collect.ImmutableMap;
12 import java.util.HashMap;
14 import java.util.Map.Entry;
15 import java.util.Optional;
16 import javax.xml.namespace.NamespaceContext;
17 import org.opendaylight.yangtools.yang.common.QNameModule;
18 import org.opendaylight.yangtools.yang.common.YangNamespaceContext;
19 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleStatement;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
21 import org.opendaylight.yangtools.yang.parser.spi.source.BelongsToPrefixToModuleName;
22 import org.opendaylight.yangtools.yang.parser.spi.source.ImportPrefixToModuleCtx;
23 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName;
24 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNameToModuleQName;
27 * A {@link NamespaceContext} implementation based on the set of imports and local module namespace.
29 // TODO: this is a useful utility, so it may be useful to expose it either in this package, or yang.parser.spi.source
30 final class StmtNamespaceContext implements YangNamespaceContext {
31 private static final long serialVersionUID = 1L;
33 private final ImmutableBiMap<QNameModule, String> moduleToPrefix;
34 private final ImmutableMap<String, QNameModule> prefixToModule;
36 StmtNamespaceContext(final StmtContext<?, ?, ?> ctx) {
37 // QNameModule -> prefix mappings
38 final Map<QNameModule, String> qnameToPrefix = ctx.getAllFromNamespace(ModuleQNameToPrefix.class);
39 this.moduleToPrefix = qnameToPrefix == null ? ImmutableBiMap.of() : ImmutableBiMap.copyOf(qnameToPrefix);
41 // Additional mappings
42 final Map<String, QNameModule> additional = new HashMap<>();
43 final Map<String, StmtContext<?, ?, ?>> imports = ctx.getAllFromNamespace(ImportPrefixToModuleCtx.class);
44 if (imports != null) {
45 for (Entry<String, StmtContext<?, ?, ?>> entry : imports.entrySet()) {
46 if (!moduleToPrefix.containsValue(entry.getKey())) {
47 QNameModule qnameModule = ctx.getFromNamespace(ModuleCtxToModuleQName.class, entry.getValue());
48 if (qnameModule == null && ctx.producesDeclared(SubmoduleStatement.class)) {
49 final String moduleName = ctx.getFromNamespace(BelongsToPrefixToModuleName.class,
51 qnameModule = ctx.getFromNamespace(ModuleNameToModuleQName.class, moduleName);
54 if (qnameModule != null) {
55 additional.put(entry.getKey(), qnameModule);
60 if (ctx.producesDeclared(SubmoduleStatement.class)) {
61 final Map<String, String> belongsTo = ctx.getAllFromNamespace(BelongsToPrefixToModuleName.class);
62 if (belongsTo != null) {
63 for (Entry<String, String> entry : belongsTo.entrySet()) {
64 final QNameModule module = ctx.getFromNamespace(ModuleNameToModuleQName.class, entry.getValue());
65 if (module != null && !additional.containsKey(entry.getKey())) {
66 additional.put(entry.getKey(), module);
72 this.prefixToModule = ImmutableMap.copyOf(additional);
76 public Optional<String> findPrefixForNamespace(final QNameModule namespace) {
77 return Optional.ofNullable(moduleToPrefix.get(namespace));
81 public Optional<QNameModule> findNamespaceForPrefix(final String prefix) {
82 final QNameModule normal = moduleToPrefix.inverse().get(prefix);
83 return normal != null ? Optional.of(normal) : Optional.ofNullable(prefixToModule.get(prefix));