KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > om > Axis


1 package net.sf.saxon.om;
2 import net.sf.saxon.trans.StaticError;
3 import net.sf.saxon.type.Type;
4
5 /**
6  * An axis, that is a direction of navigation in the document structure.
7  */

8
9 public final class Axis {
10
11     /**
12      * Constant representing the ancestor axis
13      */

14
15     public static final byte ANCESTOR = 0;
16     /** Constant representing the ancestor-or-self axis
17      */

18     public static final byte ANCESTOR_OR_SELF = 1;
19     /** Constant representing the attribute axis
20      */

21     public static final byte ATTRIBUTE = 2;
22     /** Constant representing the child axis
23      */

24     public static final byte CHILD = 3;
25     /** Constant representing the descendant axis
26      */

27     public static final byte DESCENDANT = 4;
28     /** Constant representing the descendant-or-self axis
29      */

30     public static final byte DESCENDANT_OR_SELF = 5;
31     /** Constant representing the following axis
32      */

33     public static final byte FOLLOWING = 6;
34     /** Constant representing the following-sibling axis
35      */

36     public static final byte FOLLOWING_SIBLING = 7;
37     /** Constant representing the namespace axis
38      */

39     public static final byte NAMESPACE = 8;
40     /** Constant representing the parent axis
41      */

42     public static final byte PARENT = 9;
43     /** Constant representing the preceding axis
44      */

45     public static final byte PRECEDING = 10;
46     /** Constant representing the preceding-sibling axis
47      */

48     public static final byte PRECEDING_SIBLING = 11;
49     /** Constant representing the self axis
50      */

51     public static final byte SELF = 12;
52
53     // preceding-or-ancestor axis gives all preceding nodes including ancestors,
54
// in reverse document order
55

56     /** Constant representing the preceding-or-ancestor axis. This axis is used internally by the xsl:number implementation, it returns the union of the preceding axis and the ancestor axis.
57      */

58     public static final byte PRECEDING_OR_ANCESTOR = 13;
59
60
61     /**
62      * Table indicating the principal node type of each axis
63      */

64
65     public static final short[] principalNodeType =
66     {
67         Type.ELEMENT, // ANCESTOR
68
Type.ELEMENT, // ANCESTOR_OR_SELF;
69
Type.ATTRIBUTE, // ATTRIBUTE;
70
Type.ELEMENT, // CHILD;
71
Type.ELEMENT, // DESCENDANT;
72
Type.ELEMENT, // DESCENDANT_OR_SELF;
73
Type.ELEMENT, // FOLLOWING;
74
Type.ELEMENT, // FOLLOWING_SIBLING;
75
Type.NAMESPACE, // NAMESPACE;
76
Type.ELEMENT, // PARENT;
77
Type.ELEMENT, // PRECEDING;
78
Type.ELEMENT, // PRECEDING_SIBLING;
79
Type.ELEMENT, // SELF;
80
Type.ELEMENT, // PRECEDING_OR_ANCESTOR;
81
};
82
83     /**
84      * Table indicating for each axis whether it is in forwards document order
85      */

86
87     public static final boolean[] isForwards =
88     {
89         false, // ANCESTOR
90
false, // ANCESTOR_OR_SELF;
91
true, // ATTRIBUTE;
92
true, // CHILD;
93
true, // DESCENDANT;
94
true, // DESCENDANT_OR_SELF;
95
true, // FOLLOWING;
96
true, // FOLLOWING_SIBLING;
97
false, // NAMESPACE;
98
true, // PARENT;
99
false, // PRECEDING;
100
false, // PRECEDING_SIBLING;
101
true, // SELF;
102
false, // PRECEDING_OR_ANCESTOR;
103
};
104
105     /**
106      * Table indicating for each axis whether it is in reverse document order
107      */

108
109     public static final boolean[] isReverse =
110     {
111         true, // ANCESTOR
112
true, // ANCESTOR_OR_SELF;
113
false, // ATTRIBUTE;
114
false, // CHILD;
115
false, // DESCENDANT;
116
false, // DESCENDANT_OR_SELF;
117
false, // FOLLOWING;
118
false, // FOLLOWING_SIBLING;
119
false, // NAMESPACE;
120
true, // PARENT;
121
true, // PRECEDING;
122
true, // PRECEDING_SIBLING;
123
true, // SELF;
124
true, // PRECEDING_OR_ANCESTOR;
125
};
126
127     /**
128      * Table indicating for each axis whether it is a peer axis. An axis is a peer
129      * axis if no node on the axis is an ancestor of another node on the axis.
130      */

131
132     public static final boolean[] isPeerAxis =
133     {
134         false, // ANCESTOR
135
false, // ANCESTOR_OR_SELF;
136
true, // ATTRIBUTE;
137
true, // CHILD;
138
false, // DESCENDANT;
139
false, // DESCENDANT_OR_SELF;
140
false, // FOLLOWING;
141
true, // FOLLOWING_SIBLING;
142
true, // NAMESPACE;
143
true, // PARENT;
144
false, // PRECEDING;
145
true, // PRECEDING_SIBLING;
146
true, // SELF;
147
false, // PRECEDING_OR_ANCESTOR;
148
};
149
150     /**
151      * Table indicating for each axis whether it is contained within the subtree
152      * rooted at the origin node.
153      */

154
155     public static final boolean[] isSubtreeAxis =
156     {
157         false, // ANCESTOR
158
false, // ANCESTOR_OR_SELF;
159
true, // ATTRIBUTE;
160
true, // CHILD;
161
true, // DESCENDANT;
162
true, // DESCENDANT_OR_SELF;
163
false, // FOLLOWING;
164
false, // FOLLOWING_SIBLING;
165
true, // NAMESPACE;
166
false, // PARENT;
167
false, // PRECEDING;
168
false, // PRECEDING_SIBLING;
169
true, // SELF;
170
false, // PRECEDING_OR_ANCESTOR;
171
};
172
173     /**
174      * Table giving the name each axis
175      */

176
177     public static final String JavaDoc[] axisName =
178     {
179         "ancestor", // ANCESTOR
180
"ancestor-or-self", // ANCESTOR_OR_SELF;
181
"attribute", // ATTRIBUTE;
182
"child", // CHILD;
183
"descendant", // DESCENDANT;
184
"descendant-or-self", // DESCENDANT_OR_SELF;
185
"following", // FOLLOWING;
186
"following-sibling", // FOLLOWING_SIBLING;
187
"namespace", // NAMESPACE;
188
"parent", // PARENT;
189
"preceding", // PRECEDING;
190
"preceding-sibling", // PRECEDING_SIBLING;
191
"self", // SELF;
192
"preceding-or-ancestor",// PRECEDING_OR_ANCESTOR;
193
};
194
195     /**
196      * The class is never instantiated
197      */

198
199     private Axis() {
200     }
201
202     /**
203      * Resolve an axis name into a symbolic constant representing the axis
204      *
205      * @param name
206      * @exception net.sf.saxon.trans.StaticError
207      * @return integer value representing the named axis
208      */

209
210     public static byte getAxisNumber(String JavaDoc name) throws StaticError {
211         if (name.equals("ancestor")) return ANCESTOR;
212         if (name.equals("ancestor-or-self")) return ANCESTOR_OR_SELF;
213         if (name.equals("attribute")) return ATTRIBUTE;
214         if (name.equals("child")) return CHILD;
215         if (name.equals("descendant")) return DESCENDANT;
216         if (name.equals("descendant-or-self")) return DESCENDANT_OR_SELF;
217         if (name.equals("following")) return FOLLOWING;
218         if (name.equals("following-sibling")) return FOLLOWING_SIBLING;
219         if (name.equals("namespace")) return NAMESPACE;
220         if (name.equals("parent")) return PARENT;
221         if (name.equals("preceding")) return PRECEDING;
222         if (name.equals("preceding-sibling")) return PRECEDING_SIBLING;
223         if (name.equals("self")) return SELF;
224         // preceding-or-ancestor cannot be used in an XPath expression
225
throw new StaticError("Unknown axis name: " + name);
226     }
227
228     /**
229      * The following table indicates the combinations of axis and node-kind that always
230      * return an empty result.
231      */

232
233     private static final int DOC = 1<<Type.DOCUMENT;
234     private static final int ELE = 1<<Type.ELEMENT;
235     private static final int ATT = 1<<Type.ATTRIBUTE;
236     private static final int TEX = 1<<Type.TEXT;
237     private static final int PIN = 1<<Type.PROCESSING_INSTRUCTION;
238     private static final int COM = 1<<Type.COMMENT;
239     private static final int NAM = 1<<Type.NAMESPACE;
240
241     private static int[] voidAxisTable = {
242          DOC, // ANCESTOR
243
0, // ANCESTOR_OR_SELF;
244
DOC|ATT|TEX|PIN|COM|NAM, // ATTRIBUTE;
245
ATT|TEX|PIN|COM|NAM, // CHILD;
246
ATT|TEX|PIN|COM|NAM, // DESCENDANT;
247
0, // DESCENDANT_OR_SELF;
248
DOC, // FOLLOWING;
249
DOC|ATT|NAM, // FOLLOWING_SIBLING;
250
DOC|ATT|TEX|PIN|COM|NAM, // NAMESPACE;
251
DOC, // PARENT;
252
DOC, // PRECEDING;
253
DOC|ATT|NAM, // PRECEDING_SIBLING;
254
0, // SELF;
255
};
256
257     public static boolean isAlwaysEmpty(int axis, int nodeKind) {
258         return (voidAxisTable[axis] & (1<<nodeKind)) != 0;
259     }
260
261     /**
262      * The following table indicates the kinds of node found on each axis
263      */

264
265     private static int[] nodeKindTable = {
266              DOC|ELE, // ANCESTOR
267
DOC|ELE|ATT|TEX|PIN|COM|NAM, // ANCESTOR_OR_SELF;
268
ATT, // ATTRIBUTE;
269
ELE|TEX|PIN|COM, // CHILD;
270
ELE|TEX|PIN|COM, // DESCENDANT;
271
DOC|ELE|ATT|TEX|PIN|COM|NAM, // DESCENDANT_OR_SELF;
272
ELE|TEX|PIN|COM, // FOLLOWING;
273
ELE|TEX|PIN|COM, // FOLLOWING_SIBLING;
274
NAM, // NAMESPACE;
275
DOC|ELE, // PARENT;
276
DOC|ELE|TEX|PIN|COM, // PRECEDING;
277
ELE|TEX|PIN|COM, // PRECEDING_SIBLING;
278
DOC|ELE|ATT|TEX|PIN|COM|NAM, // SELF;
279
};
280
281     /**
282      * Determine whether a given kind of node can be found on a given axis
283      */

284
285     public static boolean containsNodeKind(int axis, int nodeKind) {
286         return (nodeKindTable[axis] & (1<<nodeKind)) != 0;
287     }
288
289
290
291
292 }
293
294 /*
295     // a list for any future cut-and-pasting...
296     ANCESTOR
297     ANCESTOR_OR_SELF;
298     ATTRIBUTE;
299     CHILD;
300     DESCENDANT;
301     DESCENDANT_OR_SELF;
302     FOLLOWING;
303     FOLLOWING_SIBLING;
304     NAMESPACE;
305     PARENT;
306     PRECEDING;
307     PRECEDING_SIBLING;
308     SELF;
309 */

310
311
312 //
313
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
314
// you may not use this file except in compliance with the License. You may obtain a copy of the
315
// License at http://www.mozilla.org/MPL/
316
//
317
// Software distributed under the License is distributed on an "AS IS" basis,
318
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
319
// See the License for the specific language governing rights and limitations under the License.
320
//
321
// The Original Code is: all this file.
322
//
323
// The Initial Developer of the Original Code is Michael H. Kay.
324
//
325
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
326
//
327
// Contributor(s): none.
328
//
329
Popular Tags