Adjust test suite parser update to conform with API changes
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / StmtNamespaceContext.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Verify.verifyNotNull;
12 import static java.util.Objects.requireNonNull;
13
14 import com.google.common.collect.BiMap;
15 import com.google.common.collect.ImmutableBiMap;
16 import com.google.common.collect.Iterators;
17 import java.util.Iterator;
18 import javax.xml.namespace.NamespaceContext;
19 import org.opendaylight.yangtools.yang.common.QNameModule;
20 import org.opendaylight.yangtools.yang.parser.rfc7950.namespace.URIStringToImportPrefix;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
23
24 /**
25  * A {@link NamespaceContext} implementation based on the set of imports and local module namespace.
26  */
27 // TODO: this is a useful utility, so it may be useful to expose it either in this package, or yang.parser.spi.source.
28 final class StmtNamespaceContext implements NamespaceContext {
29     private final StmtContext<?, ?, ?> ctx;
30     private final BiMap<String, String> uriToPrefix;
31
32     private String localNamespaceURI;
33
34     private StmtNamespaceContext(final StmtContext<?, ?, ?> ctx) {
35         this(ctx, ImmutableBiMap.of());
36     }
37
38     private StmtNamespaceContext(final StmtContext<?, ?, ?> ctx, final BiMap<String, String> uriToPrefix) {
39         this.ctx = requireNonNull(ctx);
40         this.uriToPrefix = ImmutableBiMap.copyOf(requireNonNull(uriToPrefix));
41     }
42
43     public static NamespaceContext create(final StmtContext<?, ?, ?> ctx) {
44         return new StmtNamespaceContext(ctx);
45     }
46
47     public static NamespaceContext create(final StmtContext<?, ?, ?> ctx, final BiMap<String, String> uriToPrefix) {
48         return new StmtNamespaceContext(ctx, uriToPrefix);
49     }
50
51     private String localNamespaceURI() {
52         if (localNamespaceURI == null) {
53             localNamespaceURI = verifyNotNull(ctx.getPublicDefinition().getStatementName().getNamespace().toString(),
54                 "Local namespace URI not found in %s", ctx);
55         }
56         return localNamespaceURI;
57     }
58
59     @Override
60     public String getNamespaceURI(final String prefix) {
61         // API-mandated by NamespaceContext
62         checkArgument(prefix != null);
63
64         final String uri = uriToPrefix.inverse().get(prefix);
65         if (uri != null) {
66             return uri;
67         }
68
69         if (prefix.isEmpty()) {
70             return localNamespaceURI();
71         }
72
73         final QNameModule module = StmtContextUtils.getModuleQNameByPrefix(ctx, prefix);
74         return module == null ? null : module.getNamespace().toString();
75     }
76
77     @Override
78     public String getPrefix(final String namespaceURI) {
79         // API-mandated by NamespaceContext
80         checkArgument(namespaceURI != null);
81
82         final String prefix = uriToPrefix.get(namespaceURI);
83         if (prefix != null) {
84             return prefix;
85         }
86
87         if (localNamespaceURI().equals(namespaceURI)) {
88             return "";
89         }
90         return ctx.getFromNamespace(URIStringToImportPrefix.class, namespaceURI);
91     }
92
93     @Override
94     public Iterator<String> getPrefixes(final String namespaceURI) {
95         // Ensures underlying map remains constant
96         return Iterators.unmodifiableIterator(Iterators.concat(
97                 ctx.getAllFromNamespace(URIStringToImportPrefix.class).values().iterator(),
98                 uriToPrefix.values().iterator()));
99     }
100 }