KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.icl.saxon.style;
2 import com.icl.saxon.tree.AttributeCollection;
3 import com.icl.saxon.*;
4 import com.icl.saxon.om.*;
5 import com.icl.saxon.tree.NodeImpl;
6 import com.icl.saxon.expr.*;
7 import com.icl.saxon.trace.*; // e.g.
8

9 import javax.xml.transform.*;
10
11 /**
12 * An xsl:choose elements in the stylesheet.<BR>
13 */

14
15 public class XSLChoose extends StyleElement {
16
17     private StyleElement otherwise;
18
19     /**
20     * Determine whether this node is an instruction.
21     * @return true - it is an instruction
22     */

23
24     public boolean isInstruction() {
25         return true;
26     }
27
28     /**
29     * Determine whether this element does any processing after instantiating any children.
30     * This implementation says it doesn't, thus enabling tail recursion.
31     */

32
33     public boolean doesPostProcessing() {
34         return false;
35     }
36
37     public void prepareAttributes() throws TransformerConfigurationException {
38         AttributeCollection atts = getAttributeList();
39         for (int a=0; a<atts.getLength(); a++) {
40             int nc = atts.getNameCode(a);
41             checkUnknownAttribute(nc);
42         }
43     }
44
45     public void validate() throws TransformerConfigurationException {
46         checkWithinTemplate();
47
48         NodeImpl xslwhen = null;
49
50         NodeImpl curr = (NodeImpl)getFirstChild();
51         while(curr!=null) {
52             if (curr instanceof XSLWhen) {
53                 if (otherwise!=null) {
54                     compileError("xsl:otherwise must come last");
55                 }
56                 xslwhen = curr;
57             } else if (curr instanceof XSLOtherwise) {
58                 if (otherwise!=null) {
59                     compileError("Only one xsl:otherwise allowed in an xsl:choose");
60                 } else {
61                     otherwise = (StyleElement)curr;
62                 }
63             } else {
64                 compileError("Only xsl:when and xsl:otherwise are allowed here");
65             }
66             curr = (NodeImpl)curr.getNextSibling();
67         }
68
69         if (xslwhen==null)
70             compileError("xsl:choose must contain at least one xsl:when");
71     }
72
73     public void process(Context context) throws TransformerException
74     {
75         boolean isTracing = context.getController().isTracing(); // e.g.
76
StyleElement option = (StyleElement)getFirstChild();
77
78         // find the first matching "when" condition
79

80         while (option!=null) {
81             boolean go;
82             
83             if (option instanceof XSLWhen) {
84                 go = ((XSLWhen)option).getCondition().evaluateAsBoolean(context);
85             } else { // xsl:otherwise
86
go = true;
87             }
88
89             if (go) {
90                 if (isTracing) { // e.g.
91
TraceListener listener = context.getController().getTraceListener();
92                     listener.enter(option, context);
93                     option.process(context);
94                     listener.leave(option, context);
95                 } else {
96                     option.process(context);
97                 }
98                 option = null;
99             } else {
100                 option = (StyleElement)option.getNextSibling();
101             }
102         }
103
104     }
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):
126
// Portions marked "e.g." are from Edwin Glaser (edwin@pannenleiter.de)
127
//
128
Popular Tags