1
/* Copyright 2005 Elliotte Rusty Harold
3
This library is free software; you can redistribute it and/or modify
4
it under the terms of version 2.1 of the GNU Lesser General Public
5
License as published by the Free Software Foundation.
7
This library is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU Lesser General Public License for more details.
12
You should have received a copy of the GNU Lesser General Public
13
License along with this library; if not, write to the
14
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
15
Boston, MA 02111-1307 USA
17
You can contact Elliotte Rusty Harold by sending e-mail to
18
elharo@metalab.unc.edu. Please include the word "XOM" in the
19
subject line. The XOM home page is located at http://www.xom.nu/
24
import java.util.HashMap;
27
import org.jaxen.NamespaceContext;
32
* Provides namespace prefix bindings for use in an XPath expression.
35
* @author Elliotte Rusty Harold
39
public final class XPathContext {
42
Map namespaces = new HashMap();
47
* Creates a new XPath context that binds the specified prefix to
48
* the specified URI. The <code>xml</code>
49
* prefix is also bound to the URI
50
* <code>http://www.w3.org/XML/1998/namespace</code>.
53
* @param prefix the prefix to bind
54
* @param uri the namespace URI the prefix is bound to
56
public XPathContext(String prefix, String uri) {
58
addNamespace(prefix, uri);
64
* Creates a new XPath context that binds the <code>xml</code>
66
* <code>http://www.w3.org/XML/1998/namespace</code>.
69
public XPathContext() {
70
addNamespace("xml", Namespace.XML_NAMESPACE);
76
* Binds the specified prefix to the specified namespace URI.
77
* If the prefix is already bound in this context, the new URI
78
* replaces the old URI. Binding a prefix to null removes the
79
* declaration. The binding of the <code>xml</code> prefix
83
* @param prefix the prefix to bind
84
* @param uri the namespace URI the prefix is bound to
86
* @throws NamespaceConflictException if the prefix is
87
* <code>xml</code> and the URI is not
88
* <code>http://www.w3.org/XML/1998/namespace</code> or the
89
* prefix is the empty string
90
* @throws NullPointerException if the prefix is null
93
public void addNamespace(String prefix, String uri) {
95
if ("xml".equals(prefix)
96
&& !Namespace.XML_NAMESPACE.equals(uri)) {
97
throw new NamespaceConflictException(
98
"Wrong namespace URI for xml prefix: " + uri);
100
if ("".equals(uri)) uri = null;
101
if (prefix == null) {
102
throw new NullPointerException("Prefixes used in XPath expressions cannot be null");
104
else if ("".equals(prefix)){
105
throw new NamespaceConflictException(
106
"XPath expressions do not use the default namespace");
109
// should there be a separate remove method????
111
namespaces.remove(prefix);
114
namespaces.put(prefix, uri);
120
// should this be a Node rather than an Element????
123
* Creates a new XPath context that contains all the namespace
124
* bindings <em>in scope</em> on the element. Changing
125
* the prefixes in scope on the element after the context
126
* is returned does not change the context.
129
* @param element the element whose namespace bindings are copied
131
* @return all the namespace prefix mappings
132
* in scope on the element
134
public static XPathContext makeNamespaceContext(Element element) {
136
XPathContext context = new XPathContext();
137
context.namespaces = element.getNamespacePrefixesInScope();
143
NamespaceContext getJaxenContext() {
144
return new JaxenNamespaceContext();
148
private class JaxenNamespaceContext implements NamespaceContext {
150
public String translateNamespacePrefixToUri(String prefix) {
151
return (String) namespaces.get(prefix);