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