KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > tree > NamespaceImpl


1 package com.icl.saxon.tree;
2 import com.icl.saxon.om.NodeInfo;
3 import com.icl.saxon.om.NamePool;
4 import com.icl.saxon.output.Outputter;
5
6 import javax.xml.transform.TransformerException JavaDoc;
7 import org.w3c.dom.Node JavaDoc;
8
9 /**
10   * A node in the XML parse tree representing a Namespace. Note that this is
11   * generated only "on demand", when the namespace axis is expanded.<P>
12   * @author <A HREF="mailto:mhkay@iclway.co.uk>Michael H. Kay</A>
13   * @version 3 November 1999
14   */

15
16 final class NamespaceImpl extends NodeImpl {
17
18     private int nsCode; // indexes the prefix and uri in the name pool
19
private int nameCode; // identifies the name of this node
20
private int index;
21
22     /**
23     * Construct a Namespace node
24     * @param element The element owning the namespace node
25     * @param nsCode The namespace code
26     * @param index Integer identifying this namespace node among the nodes for its parent
27     */

28
29     public NamespaceImpl(ElementImpl element, int nsCode, int index) {
30         this.parent = element;
31         this.nsCode = nsCode;
32         NamePool pool = getNamePool();
33         this.nameCode = pool.allocate("", "", pool.getPrefixFromNamespaceCode(nsCode));
34         this.index = index;
35     }
36
37     /**
38     * Get the namecode for this name. Not the same as the namespace code!
39     */

40     
41     public int getNameCode() {
42         return nameCode;
43     }
44
45     /**
46     * Get the namespace code for this prefix/uri pair. Not the same as the name code!
47     */

48     
49     public int getNamespaceCode() {
50         return nsCode;
51     }
52
53     /**
54     * Determine whether this is the same node as another node
55     * @return true if this Node object and the supplied Node object represent the
56     * same node in the tree.
57     */

58
59     public boolean isSameNode(NodeInfo other) {
60         if (!(other instanceof NamespaceImpl)) return false;
61         if (this==other) return true;
62         NamespaceImpl otherN = (NamespaceImpl)other;
63         return (parent.isSameNode(otherN.parent) &&
64                 this.nsCode==otherN.nsCode);
65     }
66
67     /**
68     * Get the prefix of the namespace that this node relates to
69     */

70
71     public String JavaDoc getLocalName() {
72         return getNamePool().getPrefixFromNamespaceCode(nsCode);
73     }
74     
75     /**
76     * Change the uri of the namespace that this node relates to
77     * (used to implement xsl:namespace-alias)
78     */

79
80     public void setNamespaceCode(int nsCode) {
81         NamePool pool = getNamePool();
82         this.nsCode = nsCode;
83         this.nameCode = pool.allocate("", "", pool.getPrefixFromNamespaceCode(nsCode));
84     }
85     
86     /**
87     * Return the type of node.
88     * @return NodeInfo.NAMESPACE
89     */

90
91     public final short getNodeType() {
92         return NAMESPACE;
93     }
94
95     /**
96     * Return the string value of the node.
97     * @return the namespace uri
98     */

99
100     public String JavaDoc getStringValue() {
101         return getNamePool().getURIFromNamespaceCode(nsCode);
102     }
103     
104     /**
105     * Get the name of this node, following the DOM rules (which aren't actually defined
106     * for Namespace nodes...)
107     * @return the namespace prefix
108     */

109
110     public String JavaDoc getNodeName() {
111         return getLocalName();
112     }
113
114     /**
115     * Get next sibling - not defined for namespace nodes
116     */

117
118     public Node JavaDoc getNextSibling() {
119         return null;
120     }
121
122     /**
123     * Get previous sibling - not defined for namespace nodes
124     */

125
126     public Node JavaDoc getPreviousSibling() {
127         return null;
128     }
129
130     /**
131     * Get the previous node in document order (skipping namespace nodes)
132     */

133
134     public NodeImpl getPreviousInDocument() {
135         return (NodeImpl)getParent();
136     }
137
138     /**
139     * Get the next node in document order (skipping namespace nodes)
140     */

141
142     public NodeImpl getNextInDocument(NodeImpl anchor) {
143         if (this==anchor) return null;
144         return ((NodeImpl)getParent()).getNextInDocument(anchor);
145     }
146    
147
148     /**
149     * Get sequential key. Returns key of owning element with the namespace prefix as a suffix
150     */

151
152     public String JavaDoc generateId() {
153         return parent.generateId() + "_xmlns_" + getLocalName();
154     }
155     
156     /**
157     * Copy this node to a given outputter
158     */

159
160     public void copy(Outputter out) throws TransformerException JavaDoc {
161         out.copyNamespaceNode(nsCode);
162     }
163
164     /**
165     * Get the node sequence number (in document order). Sequence numbers are monotonic but not
166     * consecutive. In the current implementation, parent nodes (elements and roots) have a zero
167     * least-significant word, while namespaces, attributes, text nodes, comments, and PIs have
168     * the top word the same as their owner and the bottom half reflecting their relative position.
169     */

170
171     protected long getSequenceNumber() {
172         return parent.getSequenceNumber() + index;
173     }
174         
175 }
176
177 //
178
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
179
// you may not use this file except in compliance with the License. You may obtain a copy of the
180
// License at http://www.mozilla.org/MPL/
181
//
182
// Software distributed under the License is distributed on an "AS IS" basis,
183
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
184
// See the License for the specific language governing rights and limitations under the License.
185
//
186
// The Original Code is: all this file.
187
//
188
// The Initial Developer of the Original Code is
189
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
190
//
191
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
192
//
193
// Contributor(s): none.
194
//
195
Popular Tags