1 /*
2 * $Header$
3 * $Revision$
4 * $Date$
5 *
6 * ====================================================================
7 *
8 * Copyright 2000-2002 bob mcwhirter & James Strachan.
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions are
13 * met:
14 *
15 * * Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 *
18 * * Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 *
22 * * Neither the name of the Jaxen Project nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
27 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
29 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
30 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 *
38 * ====================================================================
39 * This software consists of voluntary contributions made by many
40 * individuals on behalf of the Jaxen Project and was originally
41 * created by bob mcwhirter <bob@werken.com> and
42 * James Strachan <jstrachan@apache.org>. For more information on the
43 * Jaxen Project, please see <http://www.jaxen.org/>.
44 *
45 * $Id$
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 private static final long serialVersionUID = -808928409643497762L;
65 // XXX should this prebind the xml prefix?
66 private Map<String, String> namespaces;
67
68 /**
69 * Creates a new empty namespace context.
70 */
71 public SimpleNamespaceContext()
72 {
73 this.namespaces = new HashMap<String, String>();
74 }
75
76 /**
77 * Creates a new namespace context pre-populated with the specified bindings.
78 *
79 * @param namespaces the initial namespace bindings in scope. The keys in this
80 * must be strings containing the prefixes and the values are strings
81 * containing the namespace URIs.
82 *
83 * @throws NullPointerException if the argument is null
84 * @throws ClassCastException if any keys or values in the map are not strings
85 */
86 public SimpleNamespaceContext(Map namespaces)
87 {
88 Iterator<?> entries = namespaces.entrySet().iterator();
89 while (entries.hasNext()) {
90 Map.Entry entry = (Map.Entry) entries.next();
91 if (! (entry.getKey() instanceof String)
92 || ! (entry.getValue() instanceof String)) {
93 throw new ClassCastException("Non-string namespace binding");
94 }
95 }
96 this.namespaces = new HashMap(namespaces);
97 }
98
99 /**
100 * Adds all the namespace declarations that are in scope on the given
101 * element. In the case of an XSLT stylesheet, this would be the element
102 * that has the XPath expression in one of its attributes; e.g.
103 * <code><xsl:if test="condition/xpath/expression"></code>.
104 *
105 * @param nav the navigator for use in conjunction with
106 * <code>element</code>
107 * @param element the element to copy the namespaces from
108 * @throws UnsupportedAxisException if the navigator does not support the
109 * namespace axis
110 */
111 public void addElementNamespaces( Navigator nav, Object element )
112 throws UnsupportedAxisException
113 {
114 Iterator namespaceAxis = nav.getNamespaceAxisIterator( element );
115
116 while ( namespaceAxis.hasNext() ) {
117 Object namespace = namespaceAxis.next();
118 String prefix = nav.getNamespacePrefix( namespace );
119 String uri = nav.getNamespaceStringValue( namespace );
120 if ( translateNamespacePrefixToUri(prefix) == null ) {
121 addNamespace( prefix, uri );
122 }
123 }
124 }
125
126 // ???? What if prefix or URI is null, or both?
127 /**
128 * Binds a prefix to a URI in this context.
129 *
130 * @param prefix the namespace prefix
131 * @param URI the namespace URI
132 */
133 public void addNamespace(String prefix, String URI)
134 {
135 this.namespaces.put( prefix, URI );
136 }
137
138 public String translateNamespacePrefixToUri(String prefix)
139 {
140 if ( this.namespaces.containsKey( prefix ) )
141 {
142 return this.namespaces.get( prefix );
143 }
144
145 return null;
146 }
147 }