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.data.api.schema.xpath;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.annotations.Beta;
14 import com.google.common.base.Converter;
15 import com.google.common.collect.ImmutableBiMap;
16 import com.google.common.collect.ImmutableBiMap.Builder;
17 import com.google.common.collect.Maps;
18 import java.util.Optional;
19 import javax.xml.xpath.XPathExpressionException;
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.opendaylight.yangtools.yang.common.QNameModule;
22 import org.opendaylight.yangtools.yang.model.api.Module;
23 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 * A set of utility functions for dealing with common types of namespace mappings.
31 public final class PrefixConverters {
32 private PrefixConverters() {
37 * Create a prefix {@link Converter} for {@link XPathExpressionException} defined in a particular YANG
38 * {@link Module} .Instantiation requires establishing how a module's imports are mapped to actual modules
39 * and their namespaces. This information is cached and used for improved lookups.
41 * @param ctx A SchemaContext
42 * @param module Module in which the XPath is defined
43 * @return A new Converter
45 public static @NonNull Converter<String, QNameModule> create(final SchemaContext ctx, final Module module) {
46 // Always check for null ctx
47 requireNonNull(ctx, "Schema context may not be null");
49 // Use immutable map builder for detection of duplicates (which should never occur)
50 final Builder<String, QNameModule> b = ImmutableBiMap.builder();
51 b.put(module.getPrefix(), module.getQNameModule());
53 for (ModuleImport i : module.getImports()) {
54 final Optional<Module> mod = ctx.findModule(i.getModuleName(), i.getRevision());
55 checkArgument(mod.isPresent(), "Unsatisfied import of %s by module %s", i, module);
57 b.put(i.getPrefix(), mod.get().getQNameModule());
60 return Maps.asConverter(b.build());