1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 package org.jaxen;
50
51 import java.io.Serializable;
52 import java.util.HashMap;
53 import java.util.Iterator;
54 import java.util.Map;
55
56 /**
57 * Provides mappings from namespace prefix to namespace URI to the XPath
58 * engine.
59 */
60 public class SimpleNamespaceContext implements NamespaceContext, Serializable
61 {
62
63 /**
64 *
65 */
66 private static final long serialVersionUID = -808928409643497762L;
67
68 private Map namespaces;
69
70 /**
71 * Creates a new empty namespace context.
72 */
73 public SimpleNamespaceContext()
74 {
75 this.namespaces = new HashMap();
76 }
77
78 /**
79 * Creates a new namespace context pre-populated with the specified bindings.
80 *
81 * @param namespaces the initial namespace bindings in scope. The keys in this
82 * must be strings containing the prefixes and the values are strings
83 * containing the namespace URIs.
84 *
85 * @throws NullPointerException if the argument is null
86 * @throws ClassCastException if any keys or values in the map are not strings
87 */
88 public SimpleNamespaceContext(Map namespaces)
89 {
90 Iterator entries = namespaces.entrySet().iterator();
91 while (entries.hasNext()) {
92 Map.Entry entry = (Map.Entry) entries.next();
93 if (! (entry.getKey() instanceof String)
94 || ! (entry.getValue() instanceof String)) {
95 throw new ClassCastException("Non-string namespace binding");
96 }
97 }
98 this.namespaces = new HashMap(namespaces);
99 }
100
101 /**
102 * Adds all the namespace declarations that are in scope on the given
103 * element. In the case of an XSLT stylesheet, this would be the element
104 * that has the XPath expression in one of its attributes; e.g.
105 * <code><xsl:if test="condition/xpath/expression"></code>.
106 *
107 * @param nav the navigator for use in conjunction with
108 * <code>element</code>
109 * @param element the element to copy the namespaces from
110 * @throws UnsupportedAxisException if the navigator does not support the
111 * namespace axis
112 */
113 public void addElementNamespaces( Navigator nav, Object element )
114 throws UnsupportedAxisException
115 {
116 Iterator namespaceAxis = nav.getNamespaceAxisIterator( element );
117
118 while ( namespaceAxis.hasNext() ) {
119 Object namespace = namespaceAxis.next();
120 String prefix = nav.getNamespacePrefix( namespace );
121 String uri = nav.getNamespaceStringValue( namespace );
122 if ( translateNamespacePrefixToUri(prefix) == null ) {
123 addNamespace( prefix, uri );
124 }
125 }
126 }
127
128
129 /**
130 * Binds a prefix to a URI in this context.
131 *
132 * @param prefix the namespace prefix
133 * @param URI the namespace URI
134 */
135 public void addNamespace(String prefix, String URI)
136 {
137 this.namespaces.put( prefix, URI );
138 }
139
140 public String translateNamespacePrefixToUri(String prefix)
141 {
142 if ( this.namespaces.containsKey( prefix ) )
143 {
144 return (String) this.namespaces.get( prefix );
145 }
146
147 return null;
148 }
149 }