View Javadoc

1   /*
2    * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
3    * 
4    * This software is open source. 
5    * See the bottom of this file for the licence.
6    * 
7    * $Id: Namespace.java,v 1.20 2004/06/25 08:03:33 maartenc Exp $
8    */
9   
10  package org.dom4j;
11  
12  import org.dom4j.tree.AbstractNode;
13  import org.dom4j.tree.DefaultNamespace;
14  import org.dom4j.tree.NamespaceCache;
15  
16  /*** <p><code>Namespace</code> is a Flyweight Namespace that can be shared amongst nodes.</p>
17    * 
18    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
19    * @version $Revision: 1.20 $
20    */
21  public class Namespace extends AbstractNode {
22      
23      /*** Cache of Namespace instances */
24      protected static final NamespaceCache cache = new NamespaceCache();
25      
26      /*** XML Namespace */
27      public static final Namespace XML_NAMESPACE 
28          = cache.get("xml", "http://www.w3.org/XML/1998/namespace");
29      
30      /*** No Namespace present */
31      public static final Namespace NO_NAMESPACE 
32          = cache.get("", "");
33  
34      
35      /*** The prefix mapped to this namespace */
36      private String prefix;
37  
38      /*** The URI for this namespace */
39      private String uri;
40  
41      /*** A cached version of the hashcode for efficiency */
42      private int hashCode;
43  
44      
45      /*** A helper method to return the Namespace instance
46        * for the given prefix and URI
47        *
48        * @return an interned Namespace object
49        */
50      public static Namespace get(String prefix, String uri) {
51          return cache.get(prefix, uri);
52      }
53      
54      /*** A helper method to return the Namespace instance
55        * for no prefix and the URI
56        *
57        * @return an interned Namespace object
58        */
59      public static Namespace get(String uri) {
60          return cache.get(uri);
61      }
62      
63      /*** @param prefix is the prefix for this namespace
64        * @param uri is the URI for this namespace
65        */
66      public Namespace(String prefix, String uri) {
67          this.prefix = (prefix != null) ? prefix : "";
68          this.uri = (uri != null) ? uri : "";;
69      }
70  
71      
72      public short getNodeType() {
73          return NAMESPACE_NODE;
74      }
75  
76      /*** @return the hash code based on the qualified name and the URI of the 
77        * namespace.
78        */
79      public int hashCode() {
80          if ( hashCode == 0 ) {
81              hashCode = createHashCode();
82          }
83          return hashCode;
84      }
85          
86      /*** Factory method to create the hashcode allowing derived classes to change the behaviour */
87      protected int createHashCode() {        
88          int hashCode = uri.hashCode() ^ prefix.hashCode();
89          if ( hashCode == 0 ) {
90              hashCode = 0xbabe;
91          }            
92          return hashCode;
93      }
94  
95      /***
96       * Checks whether this Namespace equals the given Namespace. Two Namespaces
97       * are equals if their URI and prefix are equal.
98       */  
99      public boolean equals(Object object) {
100         if ( this == object ) {
101             return true;
102         }
103         else if ( object instanceof Namespace ) {
104             Namespace that = (Namespace) object;
105             
106             // we cache hash codes so this should be quick
107             if ( hashCode() == that.hashCode() ) {
108                 return uri.equals( that.getURI() ) 
109                     && prefix.equals( that.getPrefix() );
110             }
111         }
112         return false;
113     }
114     
115     public String getText() {
116         return uri;
117     }
118     
119     public String getStringValue() {
120         return uri;
121     }
122     
123     /*** @return the prefix for this <code>Namespace</code>.
124       */
125     public String getPrefix() {
126         return prefix;
127     }
128 
129     /*** @return the URI for this <code>Namespace</code>.
130       */
131     public String getURI() {
132         return uri;
133     }
134 
135 
136     public String getXPathNameStep() {
137         if (prefix != null && !"".equals( prefix )) {
138             return "namespace::" + prefix;
139         }
140         return "namespace::*[name()='']";
141     }
142     
143     public String getPath(Element context) {
144         StringBuffer path = new StringBuffer(10);
145         Element parent = getParent();
146         if (parent != null && parent != context) {
147             path.append( parent.getPath( context ) );
148             path.append( '/' );
149         }
150         path.append( getXPathNameStep() );
151         return path.toString();
152     }
153     
154     public String getUniquePath(Element context) {
155         StringBuffer path = new StringBuffer(10);
156         Element parent = getParent();
157         if (parent != null && parent != context) {
158             path.append( parent.getUniquePath( context ) );
159             path.append( '/' );
160         }
161         path.append( getXPathNameStep() );
162         return path.toString();
163     }
164     
165     public String toString() {
166         return super.toString() + " [Namespace: prefix " + getPrefix() 
167             + " mapped to URI \"" + getURI() + "\"]";
168     }
169 
170     public String asXML() {
171         StringBuffer asxml = new StringBuffer(10);
172         String prefix = getPrefix();
173         if ( prefix != null && prefix.length() > 0 ) {
174             asxml.append("xmlns:");
175             asxml.append(prefix);
176             asxml.append("=\"");
177         }
178         else {
179             asxml.append("xmlns=\"");
180         }
181         asxml.append(getURI());
182         asxml.append("\"");
183         return asxml.toString();
184     }
185     
186     public void accept(Visitor visitor) {
187         visitor.visit(this);
188     }
189     
190     protected Node createXPathResult(Element parent) {
191         return new DefaultNamespace( parent, getPrefix(), getURI() );
192     }    
193     
194 }
195 
196 
197 
198 
199 /*
200  * Redistribution and use of this software and associated documentation
201  * ("Software"), with or without modification, are permitted provided
202  * that the following conditions are met:
203  *
204  * 1. Redistributions of source code must retain copyright
205  *    statements and notices.  Redistributions must also contain a
206  *    copy of this document.
207  *
208  * 2. Redistributions in binary form must reproduce the
209  *    above copyright notice, this list of conditions and the
210  *    following disclaimer in the documentation and/or other
211  *    materials provided with the distribution.
212  *
213  * 3. The name "DOM4J" must not be used to endorse or promote
214  *    products derived from this Software without prior written
215  *    permission of MetaStuff, Ltd.  For written permission,
216  *    please contact dom4j-info@metastuff.com.
217  *
218  * 4. Products derived from this Software may not be called "DOM4J"
219  *    nor may "DOM4J" appear in their names without prior written
220  *    permission of MetaStuff, Ltd. DOM4J is a registered
221  *    trademark of MetaStuff, Ltd.
222  *
223  * 5. Due credit should be given to the DOM4J Project - 
224  *    http://www.dom4j.org
225  *
226  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
227  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
228  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
229  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
230  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
231  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
232  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
233  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
234  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
235  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
236  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
237  * OF THE POSSIBILITY OF SUCH DAMAGE.
238  *
239  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
240  *
241  * $Id: Namespace.java,v 1.20 2004/06/25 08:03:33 maartenc Exp $
242  */