KickJava   Java API By Example, From Geeks To Geeks.

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


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.Name;
6 import com.icl.saxon.om.NamespaceException;
7 import com.icl.saxon.om.NodeInfo;
8
9 import com.icl.saxon.om.Navigator;
10 import com.icl.saxon.expr.*;
11 import com.icl.saxon.om.Axis;
12 import com.icl.saxon.pattern.AnyNodeTest;
13
14 import javax.xml.transform.*;
15 import org.w3c.dom.Node JavaDoc;
16 //import java.util.*;
17
//import java.io.*;
18

19 /**
20 * An xsl:apply-templates element in the stylesheet
21 */

22
23 public class XSLApplyTemplates extends StyleElement {
24
25     private Expression select;
26     private boolean usesParams;
27     private int modeNameCode = -1; // -1 if no mode specified
28
private Mode mode;
29     private String JavaDoc modeAttribute;
30
31     /**
32     * Determine whether this node is an instruction.
33     * @return true - it is an instruction
34     */

35
36     public boolean isInstruction() {
37         return true;
38     }
39
40
41     public void prepareAttributes() throws TransformerConfigurationException {
42
43         StandardNames sn = getStandardNames();
44         AttributeCollection atts = getAttributeList();
45         
46         String JavaDoc selectAtt = null;
47         
48         for (int a=0; a<atts.getLength(); a++) {
49             int nc = atts.getNameCode(a);
50             int f = nc & 0xfffff;
51             if (f==sn.MODE) {
52                 modeAttribute = atts.getValue(a);
53             } else if (f==sn.SELECT) {
54                 selectAtt = atts.getValue(a);
55             } else {
56                 checkUnknownAttribute(nc);
57             }
58         }
59                 
60         if (modeAttribute!=null) {
61             if (!Name.isQName(modeAttribute)) {
62                 compileError("Mode name is not a valid QName");
63             }
64             try {
65                 modeNameCode = makeNameCode(modeAttribute, false);
66             } catch (NamespaceException err) {
67                 compileError(err.getMessage());
68             }
69         }
70           
71         if (selectAtt!=null) {
72             select = makeExpression(selectAtt);
73         }
74     }
75
76     public void validate() throws TransformerConfigurationException {
77
78         checkWithinTemplate();
79
80         // get the Mode object
81
mode = getPrincipalStyleSheet().getRuleManager().getMode(modeNameCode);
82
83         // handle sorting if requested
84

85         boolean sorted = false;
86         NodeImpl child = (NodeImpl)getFirstChild();
87         while (child!=null) {
88             if (child instanceof XSLSort) {
89                 sorted = true;
90             } else if (child instanceof XSLWithParam) {
91                 usesParams = true;
92             } else {
93                 if (child.getNodeType() == NodeInfo.TEXT) {
94                     // with xml:space=preserve, white space nodes may still be there
95
if (!Navigator.isWhite(child.getStringValue())) {
96                         compileError(
97                             "No character data allowed within xsl:apply-templates");
98                     }
99                 } else {
100                     compileError("Invalid element within xsl:apply-templates: ");
101                 }
102             }
103             child = (NodeImpl)child.getNextSibling();
104         }
105         
106         if (select==null && sorted) {
107             select = new PathExpression(
108                             new ContextNodeExpression(),
109                             new Step(Axis.CHILD, AnyNodeTest.getInstance()));
110         }
111         if (select!=null) {
112             select = handleSortKeys(select);
113         }
114     }
115         
116     public void process(Context context) throws TransformerException
117     {
118         // handle parameters if any
119

120         ParameterSet params = null;
121         if (usesParams) {
122             params = new ParameterSet();
123             Node JavaDoc child = getFirstChild();
124             while (child!=null) {
125                 if (child instanceof XSLWithParam) {
126                     XSLWithParam param = (XSLWithParam)child;
127                     params.put(param.getVariableFingerprint(), param.getParamValue(context));
128                 }
129                 child = child.getNextSibling();
130             }
131         }
132
133         // Process the selected nodes in the source document
134

135         context.getController().applyTemplates(context, select, mode, params);
136
137     }
138
139 }
140
141 //
142
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
143
// you may not use this file except in compliance with the License. You may obtain a copy of the
144
// License at http://www.mozilla.org/MPL/
145
//
146
// Software distributed under the License is distributed on an "AS IS" basis,
147
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
148
// See the License for the specific language governing rights and limitations under the License.
149
//
150
// The Original Code is: all this file.
151
//
152
// The Initial Developer of the Original Code is
153
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
154
//
155
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
156
//
157
// Contributor(s): none.
158
//
159
Popular Tags