KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > xml > dom4j > o3impl > 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.2 2003/12/06 20:59:44 per_nyfelt Exp $
8  */

9
10 package org.ozoneDB.xml.dom4j.o3impl;
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 Namespace that can be shared amongst nodes.</p>
18  *
19  * @author <a HREF="mailto:jstrachan@apache.org">James Strachan</a>
20  * @version $Revision: 1.2 $
21  */

22 public class AbstractNamespace extends AbstractNode implements Namespace {
23
24     /** Cache of AbstractNamespace instances */
25     //protected static final NamespaceCacheImpl cache = new NamespaceCacheImpl();
26

27     /** XML AbstractNamespace */
28     // public static final Namespace XML_NAMESPACE = cache.get("xml", "http://www.w3.org/XML/1998/namespace");
29

30     /** No AbstractNamespace present */
31     //public static final Namespace NO_NAMESPACE = cache.get("", "");
32

33
34     /** The prefix mapped to this namespace */
35     private String JavaDoc prefix;
36
37     /** The URI for this namespace */
38     private String JavaDoc uri;
39
40     /** A cached version of the hashcode for efficiency */
41     private int hashCode;
42
43
44     /** A helper method to return the AbstractNamespace instance
45      * for the given prefix and URI
46      *
47      * @return an interned AbstractNamespace object
48      */

49 // public static Namespace get(String prefix, String uri) {
50
// return cache.get(prefix, uri);
51
// }
52

53     /** A helper method to return the AbstractNamespace instance
54      * for no prefix and the URI
55      *
56      * @return an interned AbstractNamespace object
57      */

58 // public static Namespace get(String uri) {
59
// return cache.get(uri);
60
// }
61

62     /** @param prefix is the prefix for this namespace
63      * @param uri is the URI for this namespace
64      */

65     public AbstractNamespace(String JavaDoc prefix, String JavaDoc uri) {
66         this.prefix = (prefix != null) ? prefix : "";
67         this.uri = (uri != null) ? uri : "";
68         ;
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     public boolean equals(Object JavaDoc object) {
97         if (this == object) {
98             return true;
99         } else if (object instanceof Namespace) {
100             Namespace that = (Namespace) object;
101
102             // we cache hash codes so this should be quick
103
if (hashCode() == that.hashCode()) {
104                 return uri.equals(that.getURI())
105                         && prefix.equals(that.getPrefix());
106             }
107         }
108         return false;
109     }
110
111     public String JavaDoc getText() {
112         return uri;
113     }
114
115     public String JavaDoc getStringValue() {
116         return uri;
117     }
118
119     /** @return the prefix for this <code>AbstractNamespace</code>.
120      */

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

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

236
Popular Tags