KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > tree > ElementImpl


1 package net.sf.saxon.tree;
2 import net.sf.saxon.event.LocationCopier;
3 import net.sf.saxon.event.Receiver;
4 import net.sf.saxon.om.*;
5 import net.sf.saxon.trans.XPathException;
6 import net.sf.saxon.type.Type;
7
8 /**
9   * ElementImpl implements an element with no attributes or namespace declarations.<P>
10   * This class is an implementation of NodeInfo. For elements with attributes or
11   * namespace declarations, class ElementWithAttributes is used.
12   * @author Michael H. Kay
13   */

14
15
16 public class ElementImpl extends ParentNodeImpl {
17
18     private static final AttributeCollectionImpl emptyAtts = new AttributeCollectionImpl(null);
19
20     protected int nameCode;
21     protected DocumentImpl root;
22
23     /**
24     * Construct an empty ElementImpl
25     */

26
27     public ElementImpl() {}
28
29     /**
30     * Set the name code. Used when creating a dummy element in the Stripper
31     */

32
33     public void setNameCode(int nameCode) {
34         this.nameCode = nameCode;
35     }
36
37     /**
38      * Initialise a new ElementImpl with an element name
39      * @param nameCode Integer representing the element name, with namespaces resolved
40      * @param atts The attribute list: always null
41      * @param parent The parent node
42      * @param baseURI The base URI of the new element
43      * @param lineNumber The line number of the element in the source document
44      * @param sequenceNumber Integer identifying this element within the document
45      */

46
47     public void initialise(int nameCode, AttributeCollectionImpl atts, NodeInfo parent,
48                             String JavaDoc baseURI, int lineNumber, int sequenceNumber) {
49         this.nameCode = nameCode;
50         this.parent = (ParentNodeImpl)parent;
51         this.sequence = sequenceNumber;
52         this.root = (DocumentImpl)parent.getDocumentRoot();
53         root.setLineNumber(sequenceNumber, lineNumber);
54         root.setSystemId(sequenceNumber, baseURI);
55     }
56
57     /**
58     * Set the system ID of this node. This method is provided so that a NodeInfo
59     * implements the javax.xml.transform.Source interface, allowing a node to be
60     * used directly as the Source of a transformation
61     */

62
63     public void setSystemId(String JavaDoc uri) {
64         root.setSystemId(sequence, uri);
65     }
66
67     /**
68     * Get the root node
69     */

70
71     public NodeInfo getRoot() {
72         return root;
73     }
74
75     /**
76     * Get the root document node
77     */

78
79     public DocumentInfo getDocumentRoot() {
80         return root;
81     }
82
83     /**
84     * Get the system ID of the entity containing this element node.
85     */

86
87     public final String JavaDoc getSystemId() {
88         return ((DocumentImpl)getDocumentRoot()).getSystemId(sequence);
89     }
90
91     /**
92     * Get the base URI of this element node. This will be the same as the System ID unless
93     * xml:base has been used.
94     */

95
96     public String JavaDoc getBaseURI() {
97         return Navigator.getBaseURI(this);
98     }
99
100     /**
101     * Set the line number of the element within its source document entity
102     */

103
104     public void setLineNumber(int line) {
105         ((DocumentImpl)getDocumentRoot()).setLineNumber(sequence, line);
106     }
107
108     /**
109     * Get the line number of the node within its source document entity
110     */

111
112     public int getLineNumber() {
113         return ((DocumentImpl)getDocumentRoot()).getLineNumber(sequence);
114     }
115
116
117     /**
118     * Get the nameCode of the node. This is used to locate the name in the NamePool
119     */

120
121     public int getNameCode() {
122         return nameCode;
123     }
124
125     /**
126     * Get a character string that uniquely identifies this node
127     * @return a string.
128     */

129
130     public String JavaDoc generateId() {
131         return getDocumentRoot().generateId() + 'e' + sequence;
132     }
133
134     /**
135     * Output all namespace nodes associated with this element.
136     * @param out The relevant outputter
137      */

138
139     public void sendNamespaceDeclarations(Receiver out, boolean includeAncestors) throws XPathException {
140
141         // just add the namespaces defined on the ancestor nodes. We rely on the outputter
142
// to eliminate multiple declarations of the same prefix
143

144         if (includeAncestors) {
145             if (!(parent instanceof DocumentInfo)) {
146                 parent.sendNamespaceDeclarations(out, true);
147             }
148         }
149     }
150
151     /**
152      * Get all namespace undeclarations and undeclarations defined on this element.
153      *
154      * @param buffer If this is non-null, and the result array fits in this buffer, then the result
155      * may overwrite the contents of this array, to avoid the cost of allocating a new array on the heap.
156      * @return An array of integers representing the namespace declarations and undeclarations present on
157      * this element. For a node other than an element, return null. Otherwise, the returned array is a
158      * sequence of namespace codes, whose meaning may be interpreted by reference to the name pool. The
159      * top half word of each namespace code represents the prefix, the bottom half represents the URI.
160      * If the bottom half is zero, then this is a namespace undeclaration rather than a declaration.
161      * The XML namespace is never included in the list. If the supplied array is larger than required,
162      * then the first unused entry will be set to -1.
163      * <p/>
164      * <p>For a node other than an element, the method returns null.</p>
165      */

166
167     public int[] getDeclaredNamespaces(int[] buffer) {
168         return EMPTY_NAMESPACE_LIST;
169     }
170
171
172     /**
173     * Return the type of node.
174     * @return Type.ELEMENT
175     */

176
177     public final int getNodeKind() {
178         return Type.ELEMENT;
179     }
180
181     /**
182     * Get the attribute list for this element.
183     * @return The attribute list. This will not include any
184     * namespace attributes. The attribute names will be in expanded form, with prefixes
185     * replaced by URIs
186     */

187
188     public AttributeCollection getAttributeList() {
189         return emptyAtts;
190     }
191
192     /**
193      * Find the value of a given attribute of this element. <BR>
194      * This is a short-cut method; the full capability to examine
195      * attributes is offered via the getAttributeList() method. <BR>
196      * The attribute may either be one that was present in the original XML document,
197      * or one that has been set by the application using setAttribute(). <BR>
198      * @param name the name of an attribute. There must be no prefix in the name.
199      * @return the value of the attribute, if it exists, otherwise null
200      */

201
202 // public String getAttributeValue( String name ) {
203
// return null;
204
// }
205

206
207     /**
208     * Copy this node to a given outputter (supporting xsl:copy-of)
209     * @param out The outputter
210     * @param whichNamespaces indicates which namespaces should be output: all, none, or local
211     * namespaces only (those not declared on the parent element)
212     */

213
214     public void copy(Receiver out, int whichNamespaces, boolean copyAnnotations, int locationId) throws XPathException {
215
216         int typeCode = (copyAnnotations ? getTypeAnnotation() : -1);
217         if (locationId == 0 && out instanceof LocationCopier) {
218             out.setSystemId(getSystemId());
219             ((LocationCopier)out).setLineNumber(getLineNumber());
220         }
221         out.startElement(getNameCode(), typeCode, locationId, 0);
222
223         // output the namespaces
224

225         if (whichNamespaces != NO_NAMESPACES) {
226             sendNamespaceDeclarations(out, whichNamespaces==ALL_NAMESPACES);
227         }
228
229         // output the children
230

231         int childNamespaces = (whichNamespaces==NO_NAMESPACES ? NO_NAMESPACES : LOCAL_NAMESPACES);
232         NodeImpl next = (NodeImpl)getFirstChild();
233         while (next!=null) {
234             next.copy(out, childNamespaces, copyAnnotations, locationId);
235             next = (NodeImpl)next.getNextSibling();
236         }
237
238         out.endElement();
239     }
240
241 }
242
243 //
244
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
245
// you may not use this file except in compliance with the License. You may obtain a copy of the
246
// License at http://www.mozilla.org/MPL/
247
//
248
// Software distributed under the License is distributed on an "AS IS" basis,
249
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
250
// See the License for the specific language governing rights and limitations under the License.
251
//
252
// The Original Code is: all this file.
253
//
254
// The Initial Developer of the Original Code is Michael H. Kay.
255
//
256
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
257
//
258
// Contributor(s): none.
259
//
260
Popular Tags