KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xalan > internal > xsltc > dom > AnyNodeCounter


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: AnyNodeCounter.java,v 1.4 2004/02/16 22:54:59 minchau Exp $
18  */

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

30 public abstract class AnyNodeCounter extends NodeCounter {
31     public AnyNodeCounter(Translet translet,
32               DOM document, DTMAxisIterator iterator) {
33     super(translet, document, iterator);
34     }
35     
36     public NodeCounter setStartNode(int node) {
37     _node = node;
38     _nodeType = _document.getExpandedTypeID(node);
39     return this;
40     }
41
42     public String JavaDoc getCounter() {
43     int result;
44     if (_value != Integer.MIN_VALUE) {
45         result = _value;
46     }
47     else {
48         int next = _node;
49             final int root = _document.getDocument();
50         result = 0;
51         while (next >= root && !matchesFrom(next)) {
52         if (matchesCount(next)) {
53             ++result;
54         }
55         next--;
56 //%HZ%: Is this the best way of finding the root? Is it better to check
57
//%HZ%: parent(next)?
58
/*
59         if (next == root) {
60             break;
61                 }
62         else {
63             --next;
64                 }
65                 */

66         }
67     }
68     return formatNumbers(result);
69     }
70
71     public static NodeCounter getDefaultNodeCounter(Translet translet,
72                             DOM document,
73                             DTMAxisIterator iterator) {
74     return new DefaultAnyNodeCounter(translet, document, iterator);
75     }
76
77     static class DefaultAnyNodeCounter extends AnyNodeCounter {
78     public DefaultAnyNodeCounter(Translet translet,
79                      DOM document, DTMAxisIterator iterator) {
80         super(translet, document, iterator);
81     }
82
83     public String JavaDoc getCounter() {
84         int result;
85         if (_value != Integer.MIN_VALUE) {
86         result = _value;
87         }
88         else {
89         int next = _node;
90         result = 0;
91         final int ntype = _document.getExpandedTypeID(_node);
92                 final int root = _document.getDocument();
93         while (next >= 0) {
94             if (ntype == _document.getExpandedTypeID(next)) {
95             result++;
96             }
97 //%HZ%: Is this the best way of finding the root? Is it better to check
98
//%HZ%: parent(next)?
99
if (next == root) {
100                 break;
101                     }
102             else {
103                 --next;
104                     }
105         }
106         }
107         return formatNumbers(result);
108     }
109     }
110 }
111
Popular Tags