KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jsp > java > JspDirectiveVariable


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.jsp.java;
31
32 import com.caucho.jsp.JspParseException;
33 import com.caucho.jsp.cfg.TldAttribute;
34 import com.caucho.jsp.cfg.TldVariable;
35 import com.caucho.util.L10N;
36 import com.caucho.vfs.WriteStream;
37 import com.caucho.xml.QName;
38
39 import java.io.IOException JavaDoc;
40 import java.util.ArrayList JavaDoc;
41
42 public class JspDirectiveVariable extends JspNode {
43   private static final QName NAME_GIVEN = new QName("name-given");
44   private static final QName NAME_FROM_ATTRIBUTE =
45     new QName("name-from-attribute");
46   private static final QName ALIAS = new QName("alias");
47   private static final QName VARIABLE_CLASS = new QName("variable-class");
48   private static final QName DECLARE = new QName("declare");
49   private static final QName SCOPE = new QName("scope");
50   private static final QName DESCRIPTION = new QName("description");
51   
52   static final L10N L = new L10N(JspDirectiveVariable.class);
53
54   private String JavaDoc _nameGiven;
55   private String JavaDoc _nameFromAttribute;
56   private String JavaDoc _alias;
57   private String JavaDoc _variableClass;
58   private boolean _isDeclare = true;
59   private String JavaDoc _scope;
60   private String JavaDoc _description;
61   
62   /**
63    * Adds an attribute.
64    *
65    * @param name the attribute name
66    * @param value the attribute value
67    */

68   public void addAttribute(QName name, String JavaDoc value)
69     throws JspParseException
70   {
71     if (! _gen.getParseState().isTag())
72       throw error(L.l("'{0}' is only allowed in .tag files. Attribute directives are not allowed in normal JSP files.",
73                       getTagName()));
74     
75     JavaTagGenerator gen = (JavaTagGenerator) _gen;
76     if (NAME_GIVEN.equals(name)) {
77       if (gen.findVariable(value) != null) {
78     throw error(L.l("@variable name-given '{0}' is already used by another variable.",
79             value));
80       }
81       else if (gen.findAttribute(value) != null) {
82     throw error(L.l("@variable name-given '{0}' is already used by an attribute.",
83             value));
84       }
85       else if (value.equals(gen.getDynamicAttributes())) {
86     throw error(L.l("@variable name-given '{0}' cannot be the same as the tag's dynamic-attributes.",
87             value));
88       }
89       
90       _nameGiven = value;
91     }
92     else if (NAME_FROM_ATTRIBUTE.equals(name)) {
93       if (gen.findVariable(value) != null) {
94     throw error(L.l("@variable name-from-attribute '{0}' is already used by another variable.",
95             value));
96       }
97       /*
98       else if (gen.findAttribute(value) != null) {
99     throw error(L.l("@variable name-from-attribute '{0}' is already used by an attribute.",
100             value));
101       }
102       */

103       
104       _nameFromAttribute = value;
105     }
106     else if (ALIAS.equals(name))
107       _alias = value;
108     else if (VARIABLE_CLASS.equals(name))
109       _variableClass = value;
110     else if (DECLARE.equals(name))
111       _isDeclare = attributeToBoolean(name.getName(), value);
112     else if (SCOPE.equals(name)) {
113       if (! "NESTED".equals(value) &&
114       ! "AT_BEGIN".equals(value) &&
115       ! "AT_END".equals(value))
116     throw error(L.l("'{0}' is an illegal scope value. NESTED, AT_BEGIN, and AT_END are the only accepted values.",
117             value));
118
119       _scope = value;
120     }
121     else if (DESCRIPTION.equals(name))
122       _description = value;
123     else {
124       throw error(L.l("'{0}' is an unknown JSP variable directive attributes. Valid attributes are: alias, declare, description, name-from-attribute, name-given, scope, variable-class.",
125                       name.getName()));
126     }
127   }
128
129   /**
130    * When the element complets.
131    */

132   public void endElement()
133     throws JspParseException
134   {
135     if (! _gen.getParseState().isTag())
136       throw error(L.l("'{0}' is only allowed in .tag files. Variable directives are not allowed in normal JSP files.",
137                       getTagName()));
138     
139     if (_nameGiven == null && _nameFromAttribute == null)
140       throw error(L.l("<{0}> needs a 'name-given' or 'name-from-attribute' attribute.",
141                       getTagName()));
142
143     if (_nameFromAttribute != null && _alias == null)
144       throw error(L.l("<{0}> needs an 'alias' attribute. name-from-attribute requires an alias attribute.",
145                       getTagName()));
146     if (_alias != null && _nameFromAttribute == null)
147       throw error(L.l("<{0}> needs an 'name-from-attribute' attribute. alias requires a name-from-attribute attribute.",
148                       getTagName()));
149
150     JavaTagGenerator tagGen = (JavaTagGenerator) _gen;
151
152     TldVariable var = new TldVariable();
153     var.setNameGiven(_nameGiven);
154     var.setNameFromAttribute(_nameFromAttribute);
155     var.setAlias(_alias);
156
157     String JavaDoc name = _nameGiven;
158     if (name == null)
159       name = _nameFromAttribute;
160
161     if (_variableClass != null)
162       var.setVariableClass(_variableClass);
163
164     var.setDeclare(_isDeclare);
165     if (_scope != null)
166       var.setScope(_scope);
167
168     tagGen.addVariable(var);
169   }
170   
171   /**
172    * Return true if the node only has static text.
173    */

174   public boolean isStatic()
175   {
176     return true;
177   }
178
179   /**
180    * Generates the XML text representation for the tag validation.
181    *
182    * @param os write stream to the generated XML.
183    */

184   public void printXml(WriteStream os)
185     throws IOException JavaDoc
186   {
187     os.print("<jsp:directive.variable");
188     os.print(" jsp:id=\"" + _gen.generateJspId() + "\"");
189
190     if (_nameGiven != null)
191       os.print(" name-given=\"" + _nameGiven + "\"");
192
193     if (_nameFromAttribute != null)
194       os.print(" name-from-attribute=\"" + _nameFromAttribute + "\"");
195
196     if (_variableClass != null)
197       os.print(" variable-class=\"" + _variableClass + "\"");
198
199     os.print("/>");
200   }
201
202   /**
203    * Generates the code for the tag
204    *
205    * @param out the output writer for the generated java.
206    */

207   public void generatePrologue(JspJavaWriter out)
208     throws Exception JavaDoc
209   {
210     JavaTagGenerator gen = (JavaTagGenerator) _gen;
211
212     if (_nameFromAttribute == null)
213       return;
214
215     ArrayList JavaDoc<TldAttribute> attributes = gen.getAttributes();
216     for (int i = 0; i < attributes.size(); i++) {
217       TldAttribute attr = attributes.get(i);
218
219       if (! attr.getName().equals(_nameFromAttribute))
220     continue;
221
222       if (! String JavaDoc.class.equals(attr.getType()))
223     throw error(L.l("name-from-attribute variable '{0}' needs a matching String attribute, not '{1}' . name-from-attribute requires a matching String attribute.",
224             _nameFromAttribute, attr.getType().getName()));
225
226       return;
227     }
228     
229     throw error(L.l("name-from-attribute variable '{0}' needs a matching String attribute.",
230             _nameFromAttribute));
231   }
232
233   /**
234    * Generates the code for the tag
235    *
236    * @param out the output writer for the generated java.
237    */

238   public void generate(JspJavaWriter out)
239     throws Exception JavaDoc
240   {
241   }
242 }
243
Popular Tags