KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > transformer > Counter


1 /*
2  * Copyright 1999-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: Counter.java,v 1.14 2004/02/16 20:41:30 minchau Exp $
18  */

19 package org.apache.xalan.transformer;
20
21 import javax.xml.transform.TransformerException JavaDoc;
22
23 import org.apache.xalan.templates.ElemNumber;
24 import org.apache.xml.dtm.DTM;
25 import org.apache.xpath.NodeSetDTM;
26 import org.apache.xpath.XPathContext;
27
28 /**
29  * A class that does incremental counting for support of xsl:number.
30  * This class stores a cache of counted nodes (m_countNodes).
31  * It tries to cache the counted nodes in document order...
32  * the node count is based on its position in the cache list
33  * @xsl.usage internal
34  */

35 public class Counter
36 {
37
38   /**
39    * Set the maximum ammount the m_countNodes list can
40    * grow to.
41    */

42   static final int MAXCOUNTNODES = 500;
43
44   /**
45    * The start count from where m_countNodes counts
46    * from. In other words, the count of a given node
47    * in the m_countNodes vector is node position +
48    * m_countNodesStartCount.
49    */

50   int m_countNodesStartCount = 0;
51
52   /**
53    * A vector of all nodes counted so far.
54    */

55   NodeSetDTM m_countNodes;
56
57   /**
58    * The node from where the counting starts. This is needed to
59    * find a counter if the node being counted is not immediatly
60    * found in the m_countNodes vector.
61    */

62   int m_fromNode = DTM.NULL;
63
64   /**
65    * The owning xsl:number element.
66    */

67   ElemNumber m_numberElem;
68
69   /**
70    * Value to store result of last getCount call, for benifit
71    * of returning val from CountersTable.getCounterByCounted,
72    * who calls getCount.
73    */

74   int m_countResult;
75
76   /**
77    * Construct a counter object.
78    *
79    * @param numberElem The owning xsl:number element.
80    * @param countNodes A vector of all nodes counted so far.
81    *
82    * @throws TransformerException
83    */

84   Counter(ElemNumber numberElem, NodeSetDTM countNodes) throws TransformerException JavaDoc
85   {
86     m_countNodes = countNodes;
87     m_numberElem = numberElem;
88   }
89
90   /**
91    * Construct a counter object.
92    *
93    * @param numberElem The owning xsl:number element.
94    *
95    * @throws TransformerException
96    *
97   Counter(ElemNumber numberElem) throws TransformerException
98   {
99     m_numberElem = numberElem;
100   }*/

101
102   /**
103    * Try and find a node that was previously counted. If found,
104    * return a positive integer that corresponds to the count.
105    *
106    * @param support The XPath context to use
107    * @param node The node to be counted.
108    *
109    * @return The count of the node, or -1 if not found.
110    */

111   int getPreviouslyCounted(XPathContext support, int node)
112   {
113
114     int n = m_countNodes.size();
115
116     m_countResult = 0;
117
118     for (int i = n - 1; i >= 0; i--)
119     {
120       int countedNode = m_countNodes.elementAt(i);
121
122       if (node == countedNode)
123       {
124
125         // Since the list is in backwards order, the count is
126
// how many are in the rest of the list.
127
m_countResult = i + 1 + m_countNodesStartCount;
128
129         break;
130       }
131       
132       DTM dtm = support.getDTM(countedNode);
133
134       // Try to see if the given node falls after the counted node...
135
// if it does, don't keep searching backwards.
136
if (dtm.isNodeAfter(countedNode, node))
137         break;
138     }
139
140     return m_countResult;
141   }
142
143   /**
144    * Get the last node in the list.
145    *
146    * @return the last node in the list.
147    */

148   int getLast()
149   {
150
151     int size = m_countNodes.size();
152
153     return (size > 0) ? m_countNodes.elementAt(size - 1) : DTM.NULL;
154   }
155 }
156
Popular Tags