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