Move netconf{-client,impl,util} to protocol/
[netconf.git] / protocol / netconf-util / src / main / java / org / opendaylight / netconf / util / xml / HardcodedNamespaceResolver.java
1 /*
2  * Copyright (c) 2013 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.netconf.util.xml;
9
10 import com.google.common.collect.ImmutableMap;
11 import java.util.Collections;
12 import java.util.Iterator;
13 import java.util.Map;
14 import javax.xml.namespace.NamespaceContext;
15
16 // http://www.ibm.com/developerworks/library/x-nmspccontext/
17 public class HardcodedNamespaceResolver implements NamespaceContext {
18     private final Map<String/* prefix */, String/* namespace */> prefixesToNamespaces;
19
20     public HardcodedNamespaceResolver(final String prefix, final String namespace) {
21         this(ImmutableMap.of(prefix, namespace));
22     }
23
24     public HardcodedNamespaceResolver(final Map<String, String> prefixesToNamespaces) {
25         this.prefixesToNamespaces = Collections.unmodifiableMap(prefixesToNamespaces);
26     }
27
28     /**
29      * Returns the URI for all prefixes needed. Wherever possible it
30      * uses {@code XMLConstants}.
31      *
32      * @param prefix    prefix
33      * @return uri      uniform resource identifier
34      */
35     @Override
36     public String getNamespaceURI(final String prefix) {
37         if (prefixesToNamespaces.containsKey(prefix)) {
38             return prefixesToNamespaces.get(prefix);
39         }
40
41         throw new IllegalStateException("Prefix mapping not found for " + prefix);
42     }
43
44     @Override
45     public String getPrefix(final String namespaceURI) {
46         // Not needed in this context.
47         return null;
48     }
49
50     @Override
51     public Iterator<String> getPrefixes(final String namespaceURI) {
52         // Not needed in this context.
53         return null;
54     }
55 }