KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > xsltc > dom > SingleNodeCounter


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: SingleNodeCounter.java,v 1.5 2004/02/16 22:54:59 minchau Exp $
18  */

19
20 package org.apache.xalan.xsltc.dom;
21
22 import org.apache.xalan.xsltc.DOM;
23 import org.apache.xalan.xsltc.Translet;
24 import org.apache.xml.dtm.DTMAxisIterator;
25
26 /**
27  * @author Jacek Ambroziak
28  * @author Santiago Pericas-Geertsen
29  */

30 public abstract class SingleNodeCounter extends NodeCounter {
31     static private final int[] EmptyArray = new int[] { };
32     DTMAxisIterator _countSiblings = null;
33
34     public SingleNodeCounter(Translet translet,
35                  DOM document,
36                  DTMAxisIterator iterator) {
37     super(translet, document, iterator);
38     }
39
40     public NodeCounter setStartNode(int node) {
41     _node = node;
42     _nodeType = _document.getExpandedTypeID(node);
43     _countSiblings = _document.getAxisIterator(PRECEDINGSIBLING);
44     return this;
45     }
46
47     public String JavaDoc getCounter() {
48     int result;
49     if (_value != Integer.MIN_VALUE) {
50         result = _value;
51     }
52     else {
53         int next = _node;
54         result = 0;
55         if (!matchesCount(next)) {
56         while ((next = _document.getParent(next)) > END) {
57             if (matchesCount(next)) {
58             break; // found target
59
}
60             if (matchesFrom(next)) {
61             next = END;
62             break; // no target found
63
}
64         }
65         }
66
67         if (next != END) {
68         _countSiblings.setStartNode(next);
69         do {
70             if (matchesCount(next)) result++;
71         } while ((next = _countSiblings.next()) != END);
72         }
73         else {
74         // If no target found then pass the empty list
75
return formatNumbers(EmptyArray);
76         }
77     }
78     return formatNumbers(result);
79     }
80
81     public static NodeCounter getDefaultNodeCounter(Translet translet,
82                             DOM document,
83                             DTMAxisIterator iterator) {
84     return new DefaultSingleNodeCounter(translet, document, iterator);
85     }
86
87     static class DefaultSingleNodeCounter extends SingleNodeCounter {
88     public DefaultSingleNodeCounter(Translet translet,
89                     DOM document, DTMAxisIterator iterator) {
90         super(translet, document, iterator);
91     }
92
93     public NodeCounter setStartNode(int node) {
94         _node = node;
95         _nodeType = _document.getExpandedTypeID(node);
96         _countSiblings =
97         _document.getTypedAxisIterator(PRECEDINGSIBLING,
98                            _document.getExpandedTypeID(node));
99         return this;
100     }
101
102     public String JavaDoc getCounter() {
103         int result;
104         if (_value != Integer.MIN_VALUE) {
105         result = _value;
106         }
107         else {
108         int next;
109         result = 1;
110         _countSiblings.setStartNode(_node);
111         while ((next = _countSiblings.next()) != END) {
112             result++;
113         }
114         }
115         return formatNumbers(result);
116     }
117     }
118 }
119
120
Popular Tags