KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jasper > compiler > ScriptingVariabler


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.jasper.compiler;
19
20 import java.util.*;
21 import javax.servlet.jsp.tagext.*;
22 import org.apache.jasper.JasperException;
23
24 /**
25  * Class responsible for determining the scripting variables that every
26  * custom action needs to declare.
27  *
28  * @author Jan Luehe
29  */

30 class ScriptingVariabler {
31
32     private static final Integer JavaDoc MAX_SCOPE = new Integer JavaDoc(Integer.MAX_VALUE);
33
34     /*
35      * Assigns an identifier (of type integer) to every custom tag, in order
36      * to help identify, for every custom tag, the scripting variables that it
37      * needs to declare.
38      */

39     static class CustomTagCounter extends Node.Visitor {
40
41     private int count;
42     private Node.CustomTag parent;
43
44     public void visit(Node.CustomTag n) throws JasperException {
45         n.setCustomTagParent(parent);
46         Node.CustomTag tmpParent = parent;
47         parent = n;
48         visitBody(n);
49         parent = tmpParent;
50         n.setNumCount(new Integer JavaDoc(count++));
51     }
52     }
53
54     /*
55      * For every custom tag, determines the scripting variables it needs to
56      * declare.
57      */

58     static class ScriptingVariableVisitor extends Node.Visitor {
59
60     private ErrorDispatcher err;
61     private Hashtable scriptVars;
62     
63     public ScriptingVariableVisitor(ErrorDispatcher err) {
64         this.err = err;
65         scriptVars = new Hashtable();
66     }
67
68     public void visit(Node.CustomTag n) throws JasperException {
69         setScriptingVars(n, VariableInfo.AT_BEGIN);
70         setScriptingVars(n, VariableInfo.NESTED);
71         visitBody(n);
72         setScriptingVars(n, VariableInfo.AT_END);
73     }
74
75     private void setScriptingVars(Node.CustomTag n, int scope)
76             throws JasperException {
77
78         TagVariableInfo[] tagVarInfos = n.getTagVariableInfos();
79         VariableInfo[] varInfos = n.getVariableInfos();
80         if (tagVarInfos.length == 0 && varInfos.length == 0) {
81         return;
82         }
83
84         Vector vec = new Vector();
85
86         Integer JavaDoc ownRange = null;
87         if (scope == VariableInfo.AT_BEGIN
88             || scope == VariableInfo.AT_END) {
89         Node.CustomTag parent = n.getCustomTagParent();
90         if (parent == null)
91             ownRange = MAX_SCOPE;
92         else
93             ownRange = parent.getNumCount();
94         } else {
95         // NESTED
96
ownRange = n.getNumCount();
97         }
98
99         if (varInfos.length > 0) {
100         for (int i=0; i<varInfos.length; i++) {
101             if (varInfos[i].getScope() != scope
102                 || !varInfos[i].getDeclare()) {
103             continue;
104             }
105             String JavaDoc varName = varInfos[i].getVarName();
106             
107             Integer JavaDoc currentRange = (Integer JavaDoc) scriptVars.get(varName);
108             if (currentRange == null
109                 || ownRange.compareTo(currentRange) > 0) {
110             scriptVars.put(varName, ownRange);
111             vec.add(varInfos[i]);
112             }
113         }
114         } else {
115         for (int i=0; i<tagVarInfos.length; i++) {
116             if (tagVarInfos[i].getScope() != scope
117                 || !tagVarInfos[i].getDeclare()) {
118             continue;
119             }
120             String JavaDoc varName = tagVarInfos[i].getNameGiven();
121             if (varName == null) {
122             varName = n.getTagData().getAttributeString(
123                                 tagVarInfos[i].getNameFromAttribute());
124             if (varName == null) {
125                 err.jspError(n, "jsp.error.scripting.variable.missing_name",
126                      tagVarInfos[i].getNameFromAttribute());
127             }
128             }
129
130             Integer JavaDoc currentRange = (Integer JavaDoc) scriptVars.get(varName);
131             if (currentRange == null
132                 || ownRange.compareTo(currentRange) > 0) {
133             scriptVars.put(varName, ownRange);
134             vec.add(tagVarInfos[i]);
135             }
136         }
137         }
138
139         n.setScriptingVars(vec, scope);
140     }
141     }
142
143     public static void set(Node.Nodes page, ErrorDispatcher err)
144         throws JasperException {
145     page.visit(new CustomTagCounter());
146     page.visit(new ScriptingVariableVisitor(err));
147     }
148 }
149
Popular Tags