1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.xmlhammer.gui.xpath;
24
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.Iterator;
28 import java.util.List;
29
30 import javax.xml.XMLConstants;
31 import javax.xml.namespace.NamespaceContext;
32
33 /***
34 * Implementation of the a Namespace Context as a HashMap.
35 *
36 * @version $Revision: 1.3 $, $Date: 2006/05/13 10:25:24 $
37 * @author Edwin Dankert <edankert@gmail.com>
38 */
39
40 public class NamespaceContextMap extends HashMap<String,String> implements NamespaceContext {
41
42 private static final long serialVersionUID = 3257568403886650425L;
43
44 public NamespaceContextMap() {
45 put(XMLConstants.XML_NS_PREFIX, XMLConstants.XML_NS_URI);
46 put(XMLConstants.XMLNS_ATTRIBUTE, XMLConstants.XMLNS_ATTRIBUTE_NS_URI);
47 }
48
49 /***
50 * Get Namespace URI bound to a prefix in the current scope.
51 *
52 * @param prefix the namespace prefix.
53 * @return the URI found for the prefix.
54 *
55 * @see javax.xml.namespace.NamespaceContext#getNamespaceURI(java.lang.String)
56 */
57 public String getNamespaceURI(String prefix) {
58 if (prefix == null) {
59 throw new IllegalArgumentException("Prefix cannot be null!");
60 }
61
62 String uri = get(prefix);
63
64 if (uri != null) {
65 return uri;
66 }
67
68 return XMLConstants.NULL_NS_URI;
69 }
70
71 /***
72 * Return the prefix bound to the namespace uri, null if no prefix could be found.
73 *
74 * @param namespaceURI the namespace URI.
75 * @return the prefix found for the URI.
76 *
77 * @see javax.xml.namespace.NamespaceContext#getPrefix(java.lang.String)
78 */
79 public String getPrefix(String namespaceURI) {
80 if ( namespaceURI == null) {
81 throw new IllegalArgumentException("Namespace URI cannot be null.");
82 }
83
84 for (String prefix : keySet()) {
85 if (get(prefix).equals(namespaceURI)) {
86 return prefix;
87 }
88 }
89
90 return null;
91 }
92
93 /***
94 * Return the list of prefixes bound to the namespace uri, null if no prefix
95 * could be found.
96 *
97 * @param namespaceURI the namespace URI.
98 * @return the prefixs bound to the URI.
99 *
100 * @see javax.xml.namespace.NamespaceContext#getPrefix(java.lang.String)
101 */
102 public Iterator getPrefixes(String namespaceURI) {
103 if ( namespaceURI == null) {
104 throw new IllegalArgumentException("Namespace URI cannot be null.");
105 }
106
107 List<String> prefixes = new ArrayList<String>();
108
109 for ( String prefix : keySet()) {
110 if (get(prefix).equals(namespaceURI)) {
111 prefixes.add( prefix);
112 }
113 }
114
115 return prefixes.iterator();
116 }
117 }