KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dom4j > tree > AbstractNamespace


1 /*
2  * Copyright 2001 (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: AbstractNamespace.java,v 1.1 2003/11/02 18:06:06 per_nyfelt Exp $
8  */

9
10 package org.dom4j.tree;
11
12 import org.dom4j.Element;
13 import org.dom4j.Namespace;
14 import org.dom4j.Node;
15 import org.dom4j.Visitor;
16
17 /** <p><code>AbstractNamespace</code> is a Flyweight AbstractNamespace that can be shared amongst nodes.</p>
18   *
19   * @author <a HREF="mailto:jstrachan@apache.org">James Strachan</a>
20   * @version $Revision: 1.1 $
21   */

22 public class AbstractNamespace extends AbstractNode implements Namespace {
23
24     /** Cache of AbstractNamespace instances */
25     protected static final NamespaceCache cache = new NamespaceCache();
26
27     /** XML AbstractNamespace */
28     public static final AbstractNamespace XML_NAMESPACE
29         = cache.get("xml", "http://www.w3.org/XML/1998/namespace");
30
31     /** No AbstractNamespace present */
32     public static final AbstractNamespace NO_NAMESPACE
33         = cache.get("", "");
34
35
36     /** The prefix mapped to this namespace */
37     private String JavaDoc prefix;
38
39     /** The URI for this namespace */
40     private String JavaDoc uri;
41
42     /** A cached version of the hashcode for efficiency */
43     private int hashCode;
44
45
46     /** A helper method to return the AbstractNamespace instance
47       * for the given prefix and URI
48       *
49       * @return an interned AbstractNamespace object
50       */

51     public static AbstractNamespace get(String JavaDoc prefix, String JavaDoc uri) {
52         return cache.get(prefix, uri);
53     }
54
55     /** A helper method to return the AbstractNamespace instance
56       * for no prefix and the URI
57       *
58       * @return an interned AbstractNamespace object
59       */

60     public static Namespace get(String JavaDoc uri) {
61         return cache.get(uri);
62     }
63
64     /** @param prefix is the prefix for this namespace
65       * @param uri is the URI for this namespace
66       */

67     public AbstractNamespace(String JavaDoc prefix, String JavaDoc uri) {
68         this.prefix = (prefix != null) ? prefix : "";
69         this.uri = (uri != null) ? uri : "";;
70     }
71
72
73     public short getNodeType() {
74         return NAMESPACE_NODE;
75     }
76
77     /** @return the hash code based on the qualified name and the URI of the
78       * namespace.
79       */

80     public int hashCode() {
81         if ( hashCode == 0 ) {
82             hashCode = createHashCode();
83         }
84         return hashCode;
85     }
86
87     /** Factory method to create the hashcode allowing derived classes to change the behaviour */
88     protected int createHashCode() {
89         int hashCode = uri.hashCode() ^ prefix.hashCode();
90         if ( hashCode == 0 ) {
91             hashCode = 0xbabe;
92         }
93         return hashCode;
94     }
95
96
97     public boolean equals(Object JavaDoc object) {
98         if ( this == object ) {
99             return true;
100         }
101         else if ( object instanceof Namespace ) {
102             Namespace that = (Namespace) object;
103
104             // we cache hash codes so this should be quick
105
if ( hashCode() == that.hashCode() ) {
106                 return uri.equals( that.getURI() )
107                     && prefix.equals( that.getPrefix() );
108             }
109         }
110         return false;
111     }
112
113     public String JavaDoc getText() {
114         return uri;
115     }
116
117     public String JavaDoc getStringValue() {
118         return uri;
119     }
120
121     /** @return the prefix for this <code>AbstractNamespace</code>.
122       */

123     public String JavaDoc getPrefix() {
124         return prefix;
125     }
126
127     /** @return the URI for this <code>AbstractNamespace</code>.
128       */

129     public String JavaDoc getURI() {
130         return uri;
131     }
132
133
134     public String JavaDoc getXPathNameStep() {
135         if (prefix != null && !"".equals( prefix )) {
136             return "namespace::" + prefix;
137         }
138         return "namespace::*[name()='']";
139     }
140
141     public String JavaDoc getPath(Element context) {
142         StringBuffer JavaDoc path = new StringBuffer JavaDoc(10);
143         Element parent = getParent();
144         if (parent != null && parent != context) {
145             path.append( parent.getPath( context ) );
146             path.append( '/' );
147         }
148         path.append( getXPathNameStep() );
149         return path.toString();
150     }
151
152     public String JavaDoc getUniquePath(Element context) {
153         StringBuffer JavaDoc path = new StringBuffer JavaDoc(10);
154         Element parent = getParent();
155         if (parent != null && parent != context) {
156             path.append( parent.getUniquePath( context ) );
157             path.append( '/' );
158         }
159         path.append( getXPathNameStep() );
160         return path.toString();
161     }
162
163     public String JavaDoc toString() {
164         return super.toString() + " [AbstractNamespace: prefix " + getPrefix()
165             + " mapped to URI \"" + getURI() + "\"]";
166     }
167
168     public String JavaDoc asXML() {
169         StringBuffer JavaDoc asxml = new StringBuffer JavaDoc(10);
170         String JavaDoc prefix = getPrefix();
171         if ( prefix != null && prefix.length() > 0 ) {
172             asxml.append("xmlns:");
173             asxml.append(prefix);
174             asxml.append("=\"");
175         }
176         else {
177             asxml.append("xmlns=\"");
178         }
179         asxml.append(getURI());
180         asxml.append("\"");
181         return asxml.toString();
182     }
183
184     public void accept(Visitor visitor) {
185         visitor.visit(this);
186     }
187
188     protected Node createXPathResult(Element parent) {
189         return new org.dom4j.tree.DefaultNamespace( parent, getPrefix(), getURI() );
190     }
191
192 }
193
194
195
196
197 /*
198  * Redistribution and use of this software and associated documentation
199  * ("Software"), with or without modification, are permitted provided
200  * that the following conditions are met:
201  *
202  * 1. Redistributions of source code must retain copyright
203  * statements and notices. Redistributions must also contain a
204  * copy of this document.
205  *
206  * 2. Redistributions in binary form must reproduce the
207  * above copyright notice, this list of conditions and the
208  * following disclaimer in the documentation and/or other
209  * materials provided with the distribution.
210  *
211  * 3. The name "DOM4J" must not be used to endorse or promote
212  * products derived from this Software without prior written
213  * permission of MetaStuff, Ltd. For written permission,
214  * please contact dom4j-info@metastuff.com.
215  *
216  * 4. Products derived from this Software may not be called "DOM4J"
217  * nor may "DOM4J" appear in their names without prior written
218  * permission of MetaStuff, Ltd. DOM4J is a registered
219  * trademark of MetaStuff, Ltd.
220  *
221  * 5. Due credit should be given to the DOM4J Project
222  * (http://dom4j.org/).
223  *
224  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
225  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
226  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
227  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
228  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
229  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
230  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
231  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
232  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
233  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
234  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
235  * OF THE POSSIBILITY OF SUCH DAMAGE.
236  *
237  * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
238  *
239  * $Id: AbstractNamespace.java,v 1.1 2003/11/02 18:06:06 per_nyfelt Exp $
240  */

241
Popular Tags