2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2024 Ericsson
4 * Modifications Copyright (C) 2024 OpenInfra Foundation Europe
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
21 package org.oran.smo.yangtools.parser.model;
23 import java.util.HashMap;
25 import java.util.Objects;
27 import org.oran.smo.yangtools.parser.PrefixResolver;
28 import org.oran.smo.yangtools.parser.model.schema.ModuleAndNamespaceResolver;
31 * A resolver that can handle prefixes, module names, and namespaces, and map
32 * back and forth between these. Namespaces are of importance when it comes to
33 * data; specifically, YANG data over NETCONF is XML encoded, requiring namespaces.
35 * However, this class should not be used for handling of data, as it does not
36 * support setting the default namespace.
38 * @author Mark Hollmann
40 public class ModulePrefixResolver extends PrefixResolver {
42 private final ModuleAndNamespaceResolver globalNamespaceResolver;
44 private final Map<String, ModuleIdentity> prefixToModulename = new HashMap<>();
46 public ModulePrefixResolver(final ModuleAndNamespaceResolver globalNamespaceResolver) {
47 this.globalNamespaceResolver = globalNamespaceResolver;
51 * ================ Here is all the module stuff =====================
55 * Records a mapping between a prefix and a module name. This mapping has typically
56 * been extracted from the header of a YAM.
58 public void addModuleMapping(final String prefix, final ModuleIdentity moduleIdentity) {
59 prefixToModulename.put(Objects.requireNonNull(prefix), Objects.requireNonNull(moduleIdentity));
63 * Records the default mapping for this prefix resolver, i.e. the module that is mapped
66 public void setDefaultModuleMapping(final ModuleIdentity moduleIdentity) {
67 prefixToModulename.put(PrefixResolver.NO_PREFIX, Objects.requireNonNull(moduleIdentity));
71 * Returns the module representing the prefix. May return null if the prefix is unknown.
73 public ModuleIdentity getModuleForPrefix(final String prefix) {
75 return getDefaultModuleIdentity();
77 return prefixToModulename.get(prefix);
81 * Returns the module representing the default prefix. May be null if unknown.
83 public ModuleIdentity getDefaultModuleIdentity() {
84 return prefixToModulename.get(PrefixResolver.NO_PREFIX);
88 * ================ Here is all the namespace stuff =====================
92 public void setDefaultNamespaceUri(final String namespaceUri) {
93 throw new UnsupportedOperationException();
97 public void addMapping(final String prefix, final String namespaceUri) {
98 throw new UnsupportedOperationException();
102 * Returns the default namespace URI for this module. May return null.
105 public String getDefaultNamespaceUri() {
106 final ModuleIdentity moduleIdentityForPrefix = getDefaultModuleIdentity();
107 return moduleIdentityForPrefix == null ?
109 globalNamespaceResolver.getNamespaceForModule(moduleIdentityForPrefix.getModuleName());
113 * Returns the namespace URI for the given prefix or null if no mapping exists for the prefix.
116 public String resolveNamespaceUri(final String prefix) {
117 final ModuleIdentity moduleIdentityForPrefix = getModuleForPrefix(prefix);
118 return moduleIdentityForPrefix == null ?
120 globalNamespaceResolver.getNamespaceForModule(moduleIdentityForPrefix.getModuleName());
124 * Returns the name of the module for the given namespace.
126 public String resolveModuleName(final String namespace) {
127 return globalNamespaceResolver.getModuleForNamespace(namespace);
131 public String toString() {
132 return "Mappings: " + prefixToModulename.toString();