~ubuntu-branches/ubuntu/precise/xom/precise

« back to all changes in this revision

Viewing changes to src/nu/xom/XPathContext.java

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2007-11-25 15:50:40 UTC
  • Revision ID: james.westby@ubuntu.com-20071125155040-r75ikcqf1vu0cei7
Tags: upstream-1.1
ImportĀ upstreamĀ versionĀ 1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright 2005 Elliotte Rusty Harold
 
2
   
 
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.
 
6
   
 
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.
 
11
   
 
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
 
16
   
 
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/
 
20
*/
 
21
 
 
22
package nu.xom;
 
23
 
 
24
import java.util.HashMap;
 
25
import java.util.Map;
 
26
 
 
27
import org.jaxen.NamespaceContext;
 
28
 
 
29
/**
 
30
 *
 
31
 * <p>
 
32
 *  Provides namespace prefix bindings for use in an XPath expression.
 
33
 * </p>
 
34
 * 
 
35
 * @author Elliotte Rusty Harold
 
36
 * @version 1.1b1
 
37
 *
 
38
 */
 
39
public final class XPathContext {
 
40
 
 
41
    
 
42
    Map namespaces = new HashMap();
 
43
    
 
44
    
 
45
    /**
 
46
     * <p>
 
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>.
 
51
     * </p>
 
52
     * 
 
53
     * @param prefix the prefix to bind
 
54
     * @param uri the namespace URI the prefix is bound to
 
55
     */
 
56
    public XPathContext(String prefix, String uri) {
 
57
        this();
 
58
        addNamespace(prefix, uri);
 
59
    }
 
60
    
 
61
    
 
62
    /**
 
63
     * <p>
 
64
     * Creates a new XPath context that binds the <code>xml</code> 
 
65
     * prefix to the URI 
 
66
     * <code>http://www.w3.org/XML/1998/namespace</code>.
 
67
     * </p>
 
68
     */
 
69
    public XPathContext() {
 
70
        addNamespace("xml", Namespace.XML_NAMESPACE);
 
71
    }
 
72
 
 
73
    
 
74
    /**
 
75
     * <p>
 
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
 
80
     * may not be changed.
 
81
     * </p>
 
82
     * 
 
83
     * @param prefix the prefix to bind
 
84
     * @param uri the namespace URI the prefix is bound to
 
85
     * 
 
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
 
91
     * 
 
92
     */
 
93
    public void addNamespace(String prefix, String uri) {
 
94
        
 
95
        if ("xml".equals(prefix) 
 
96
          && !Namespace.XML_NAMESPACE.equals(uri)) {
 
97
            throw new NamespaceConflictException(
 
98
              "Wrong namespace URI for xml prefix: " + uri);
 
99
        }
 
100
        if ("".equals(uri)) uri = null;
 
101
        if (prefix == null) {
 
102
            throw new NullPointerException("Prefixes used in XPath expressions cannot be null");
 
103
        }
 
104
        else if ("".equals(prefix)){
 
105
            throw new NamespaceConflictException(
 
106
              "XPath expressions do not use the default namespace");
 
107
        }
 
108
        
 
109
        // should there be a separate remove method????
 
110
        if (uri == null) {
 
111
            namespaces.remove(prefix);
 
112
        }
 
113
        else {
 
114
            namespaces.put(prefix, uri);
 
115
        }
 
116
        
 
117
    }
 
118
 
 
119
    
 
120
    // should this be a Node rather than an Element????
 
121
    /**
 
122
     * <p>
 
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.
 
127
     * </p>
 
128
     * 
 
129
     * @param element the element whose namespace bindings are copied
 
130
     * 
 
131
     * @return all the namespace prefix mappings 
 
132
     *     in scope on the element
 
133
     */
 
134
    public static XPathContext makeNamespaceContext(Element element) {
 
135
        
 
136
        XPathContext context = new XPathContext();
 
137
        context.namespaces = element.getNamespacePrefixesInScope();
 
138
        return context;
 
139
        
 
140
    }
 
141
    
 
142
    
 
143
    NamespaceContext getJaxenContext() {
 
144
        return new JaxenNamespaceContext();
 
145
    }
 
146
    
 
147
    
 
148
    private class JaxenNamespaceContext implements NamespaceContext {
 
149
        
 
150
        public String translateNamespacePrefixToUri(String prefix) {
 
151
            return (String) namespaces.get(prefix);
 
152
        }
 
153
        
 
154
    }
 
155
 
 
156
    
 
157
}