KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > instruct > Template


1 package net.sf.saxon.instruct;
2 import net.sf.saxon.expr.Expression;
3 import net.sf.saxon.expr.XPathContext;
4 import net.sf.saxon.expr.XPathContextMajor;
5 import net.sf.saxon.expr.ComputedExpression;
6 import net.sf.saxon.style.StandardNames;
7 import net.sf.saxon.trace.InstructionInfo;
8 import net.sf.saxon.trace.InstructionInfoProvider;
9 import net.sf.saxon.trans.XPathException;
10
11 /**
12 * An xsl:template element in the style sheet.
13 */

14
15 public class Template extends Procedure implements InstructionInfoProvider {
16
17     // The body of the template is represented by an expression,
18
// which is responsible for any type checking that's needed.
19

20     private int precedence;
21     private int minImportPrecedence;
22     private int templateFingerprint;
23     private transient InstructionDetails details;
24
25     public Template () { }
26
27     public void init ( int templateFingerprint,
28                         int precedence,
29                         int minImportPrecedence) {
30         this.templateFingerprint = templateFingerprint;
31         this.precedence = precedence;
32         this.minImportPrecedence = minImportPrecedence;
33     }
34
35     /**
36      * Get the namepool fingerprint of the name of the template (if it is named)
37      * @return the fingerprint of the template name, or -1 if unnamed
38      */

39
40     public int getFingerprint() {
41         return templateFingerprint;
42     }
43
44     public int getPrecedence() {
45         return precedence;
46     }
47
48     public int getMinImportPrecedence() {
49         return minImportPrecedence;
50     }
51
52     /**
53     * Process the template, without returning any tail calls
54     * @param context The dynamic context, giving access to the current node,
55     * the current variables, etc.
56     */

57
58     public void process(XPathContext context) throws XPathException {
59         TailCall tc = processLeavingTail(context);
60         while (tc != null) {
61             tc = tc.processLeavingTail(context);
62         }
63     }
64
65     /**
66     * Process this template, with the possibility of returning a tail call package if the template
67      * contains any tail calls that are to be performed by the caller.
68     */

69
70     public TailCall processLeavingTail(XPathContext context) throws XPathException {
71         if (getBody()==null) {
72             // fast path for an empty template
73
return null;
74         }
75         XPathContextMajor c2 = context.newContext();
76         c2.setOrigin(this);
77         c2.setCurrentTemplate(this);
78
79         TailCall tc = expand(c2);
80         return tc;
81     }
82
83     /**
84     * Expand the template. Called when the template is invoked using xsl:call-template.
85     * Invoking a template by this method does not change the current template.
86     */

87
88     public TailCall expand(XPathContext context) throws XPathException {
89         Expression body = getBody();
90         if (body==null) {
91             // fast path for an empty template
92
return null;
93         }
94         if (body instanceof TailCallReturner) {
95             return ((TailCallReturner)body).processLeavingTail(context);
96         } else {
97             body.process(context);
98             return null;
99         }
100     }
101
102     /**
103      * Get the InstructionInfo details about the construct. This information isn't used for tracing,
104      * but it is available when inspecting the context stack.
105      */

106
107     public InstructionInfo getInstructionInfo() {
108         if (details==null) {
109             details = new InstructionDetails();
110             details.setSystemId(getSystemId());
111             details.setLineNumber(getLineNumber());
112             details.setConstructType(StandardNames.XSL_TEMPLATE);
113             details.setProperty("template", this);
114         }
115         return details;
116     }
117
118     /**
119      * Diagnostic method
120      * @return
121      */

122
123     public boolean hasBadParentPointer() {
124         return ((ComputedExpression)getBody()).hasBadParentPointer();
125     }
126 }
127
128 //
129
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
130
// you may not use this file except in compliance with the License. You may obtain a copy of the
131
// License at http://www.mozilla.org/MPL/
132
//
133
// Software distributed under the License is distributed on an "AS IS" basis,
134
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
135
// See the License for the specific language governing rights and limitations under the License.
136
//
137
// The Original Code is: all this file.
138
//
139
// The Initial Developer of the Original Code is Michael H. Kay.
140
//
141
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
142
//
143
// Contributor(s):
144
// Portions marked "e.g." are from Edwin Glaser (edwin@pannenleiter.de)
145
//
146
Popular Tags