KickJava   Java API By Example, From Geeks To Geeks.

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


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: MultipleNodeCounter.java,v 1.4 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.xalan.xsltc.util.IntegerArray;
25 import org.apache.xml.dtm.DTMAxisIterator;
26
27 /**
28  * @author Jacek Ambroziak
29  * @author Santiago Pericas-Geertsen
30  */

31 public abstract class MultipleNodeCounter extends NodeCounter {
32     private DTMAxisIterator _precSiblings = null;
33
34     public MultipleNodeCounter(Translet translet,
35                    DOM document, DTMAxisIterator iterator) {
36     super(translet, document, iterator);
37     }
38     
39     public NodeCounter setStartNode(int node) {
40     _node = node;
41     _nodeType = _document.getExpandedTypeID(node);
42     _precSiblings = _document.getAxisIterator(PRECEDINGSIBLING);
43     return this;
44     }
45
46     public String JavaDoc getCounter() {
47     if (_value != Integer.MIN_VALUE) {
48         return formatNumbers(_value);
49     }
50
51     IntegerArray ancestors = new IntegerArray();
52
53     // Gather all ancestors that do not match from pattern
54
int next = _node;
55     ancestors.add(next); // include self
56
while ((next = _document.getParent(next)) > END &&
57            !matchesFrom(next)) {
58         ancestors.add(next);
59     }
60
61     // Create an array of counters
62
final int nAncestors = ancestors.cardinality();
63     final int[] counters = new int[nAncestors];
64     for (int i = 0; i < nAncestors; i++) {
65         counters[i] = Integer.MIN_VALUE;
66     }
67
68     // Increment array of counters according to semantics
69
for (int j = 0, i = nAncestors - 1; i >= 0 ; i--, j++) {
70         final int counter = counters[j];
71         final int ancestor = ancestors.at(i);
72
73         if (matchesCount(ancestor)) {
74         _precSiblings.setStartNode(ancestor);
75         while ((next = _precSiblings.next()) != END) {
76             if (matchesCount(next)) {
77             counters[j] = (counters[j] == Integer.MIN_VALUE) ? 1
78                 : counters[j] + 1;
79             }
80         }
81         // Count the node itself
82
counters[j] = counters[j] == Integer.MIN_VALUE
83             ? 1
84             : counters[j] + 1;
85         }
86     }
87     return formatNumbers(counters);
88     }
89
90     public static NodeCounter getDefaultNodeCounter(Translet translet,
91                             DOM document,
92                             DTMAxisIterator iterator) {
93     return new DefaultMultipleNodeCounter(translet, document, iterator);
94     }
95
96     static class DefaultMultipleNodeCounter extends MultipleNodeCounter {
97     public DefaultMultipleNodeCounter(Translet translet,
98                       DOM document,
99                       DTMAxisIterator iterator) {
100         super(translet, document, iterator);
101     }
102     }
103 }
104
Popular Tags