KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > style > SAXONGroup


1 package com.icl.saxon.style;
2 import com.icl.saxon.tree.AttributeCollection;
3 import com.icl.saxon.tree.NodeImpl;
4 import com.icl.saxon.*;
5 import com.icl.saxon.om.*;
6
7 import com.icl.saxon.expr.*;
8 import javax.xml.transform.*;
9 import java.util.*;
10
11 /**
12 * Handler for saxon:group elements in stylesheet. This is the same as xsl:for-each
13 * with the addition of a group-by attribute<BR>
14 */

15
16 public class SAXONGroup extends XSLForEach {
17
18     Expression groupBy = null;
19
20     /**
21     * Determine whether this node is an instruction.
22     * @return true - it is an instruction
23     */

24
25     public boolean isInstruction() {
26         return true;
27     }
28
29
30     public void prepareAttributes() throws TransformerConfigurationException {
31
32         StandardNames sn = getStandardNames();
33         AttributeCollection atts = getAttributeList();
34         
35         String JavaDoc selectAtt = null;
36         String JavaDoc groupByAtt = null;
37         
38         for (int a=0; a<atts.getLength(); a++) {
39             int nc = atts.getNameCode(a);
40             int f = nc & 0xfffff;
41             if (f==sn.SELECT) {
42                 selectAtt = atts.getValue(a);
43             } else if (f==sn.GROUP_BY) {
44                 groupByAtt = atts.getValue(a);
45             } else {
46                 checkUnknownAttribute(nc);
47             }
48         }
49         
50         if (selectAtt==null) {
51             reportAbsence("select");
52         } else {
53             select = makeExpression(selectAtt);
54         }
55         
56         if (groupByAtt == null) {
57             reportAbsence("group-by");
58         } else {
59             groupBy = makeExpression(groupByAtt);
60         }
61     }
62
63     public void validate() throws TransformerConfigurationException {
64         checkWithinTemplate();
65         select = handleSortKeys(select); // handle sort keys if any: inherited from xsl:for-each
66

67         // find the SAXONItem element
68
NodeImpl n = this;
69         SAXONItem item = null;
70         while(n!=null) {
71             if (n instanceof SAXONItem) {
72                 item = (SAXONItem)n;
73                 break;
74             }
75             n = n.getNextInDocument(this);
76         }
77         if (item==null) {
78             compileError("saxon:group must have a nested saxon:item element");
79         }
80     }
81
82     public void process(Context context) throws TransformerException
83     {
84         NodeEnumeration selection = select.enumerate(context, false);
85         if (!(selection instanceof LastPositionFinder)) {
86             selection = new LookaheadEnumerator(selection);
87         }
88         
89         Context c = context.newContext();
90         c.setLastPositionFinder((LastPositionFinder)selection);
91
92         // create a new GroupActivation object and fire it off
93

94         GroupActivation activation = new GroupActivation(this, groupBy, selection, c);
95         Stack stack = c.getGroupActivationStack();
96         stack.push(activation);
97
98         while(activation.hasMoreElements()) {
99             activation.nextElement();
100             processChildren(c);
101             context.setReturnValue(c.getReturnValue());
102         }
103         
104         stack.pop();
105     }
106
107 }
108
109 //
110
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
111
// you may not use this file except in compliance with the License. You may obtain a copy of the
112
// License at http://www.mozilla.org/MPL/
113
//
114
// Software distributed under the License is distributed on an "AS IS" basis,
115
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
116
// See the License for the specific language governing rights and limitations under the License.
117
//
118
// The Original Code is: all this file.
119
//
120
// The Initial Developer of the Original Code is
121
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
122
//
123
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
124
//
125
// Contributor(s): none.
126
//
127
Popular Tags