KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.jsp.java;
30
31 import com.caucho.jsp.JspParseException;
32 import com.caucho.util.L10N;
33 import com.caucho.vfs.WriteStream;
34 import com.caucho.xml.QName;
35
36 import java.io.IOException JavaDoc;
37
38 public class JspDirectiveTag extends JspNode {
39   private static final QName IMPORT = new QName("import");
40   private static final QName DISPLAY_NAME = new QName("display-name");
41   private static final QName BODY_CONTENT = new QName("body-content");
42   private static final QName DYNAMIC_ATTRIBUTES
43     = new QName("dynamic-attributes");
44   private static final QName SMALL_ICON = new QName("small-icon");
45   private static final QName LARGE_ICON = new QName("large-icon");
46   private static final QName DESCRIPTION = new QName("description");
47   private static final QName EXAMPLE = new QName("example");
48   private static final QName LANGUAGE = new QName("language");
49   private static final QName PAGE_ENCODING = new QName("pageEncoding");
50   private static final QName IS_EL_IGNORED = new QName("isELIgnored");
51   private static final QName DEFERRED_AS_LITERAL
52     = new QName("deferredSyntaxAllowedAsLiteral");
53   private static final QName TRIM_WHITESPACES
54     = new QName("trimDirectiveWhitespaces");
55   
56   private static final QName IS_VELOCITY_ENABLED =
57     new QName("isVelocityEnabled");
58   
59   static final L10N L = new L10N(JspDirectiveTag.class);
60
61   private Boolean JavaDoc _isElIgnored;
62   private String JavaDoc _import;
63   
64   /**
65    * Adds an attribute.
66    *
67    * @param name the attribute name
68    * @param value the attribute value
69    */

70   public void addAttribute(QName name, String JavaDoc value)
71     throws JspParseException
72   {
73     if (! _gen.isTag())
74       throw error(L.l("@tag directive is only allowed in tag files"));
75     
76     JavaTagGenerator gen = (JavaTagGenerator) _gen;
77
78     if (IS_EL_IGNORED.equals(name)) {
79       boolean isIgnored = value.equals("true");
80
81       _parseState.setELIgnored(isIgnored);
82       
83       if (_isElIgnored != null && _isElIgnored.booleanValue() != isIgnored)
84     throw error(L.l("isELIgnored values conflict"));
85
86       _isElIgnored = new Boolean JavaDoc(isIgnored);
87     }
88     /*
89     else if (name.equals("isScriptingInvalid"))
90       _parseState.setScriptingInvalid(value.equals("true"));
91     */

92     else if (IS_VELOCITY_ENABLED.equals(name))
93       _parseState.setVelocityEnabled(value.equals("true"));
94     else if (PAGE_ENCODING.equals(name)) {
95       String JavaDoc oldEncoding = _parseState.getPageEncoding();
96       
97       if (oldEncoding != null && ! value.equals(oldEncoding))
98         throw error(L.l("pageEncoding `{0}' conflicts with previous value of pageEncoding `{1}'. Check the .jsp and any included .jsp files for conflicts.", value, oldEncoding));
99       
100       _parseState.setPageEncoding(value);
101     }
102     else if (LANGUAGE.equals(name)) {
103       if (! value.equals("java"))
104         throw error(L.l("`{0}' is not supported as a JSP scripting language.",
105                         value));
106     }
107     else if (IMPORT.equals(name)) {
108       _import = value;
109       
110       _parseState.addImport(value);
111     }
112     else if (DISPLAY_NAME.equals(name)) {
113       String JavaDoc oldValue = gen.getDisplayName();
114       
115       if (oldValue != null && ! oldValue.equals(value))
116         throw error(L.l("@tag display-name '{0}' conflicts with previous value '{1}'. The display-name attribute may only be specified once.",
117             value, oldValue));
118
119       
120       gen.setDisplayName(value);
121     }
122     else if (SMALL_ICON.equals(name)) {
123       String JavaDoc oldValue = gen.getSmallIcon();
124       
125       if (oldValue != null && ! oldValue.equals(value))
126         throw error(L.l("@tag small-icon '{0}' conflicts with previous value '{1}'. The small-icon attribute may only be specified once.",
127             value, oldValue));
128
129       
130       gen.setSmallIcon(value);
131     }
132     else if (LARGE_ICON.equals(name)) {
133       String JavaDoc oldValue = gen.getLargeIcon();
134       
135       if (oldValue != null && ! oldValue.equals(value))
136         throw error(L.l("@tag large-icon '{0}' conflicts with previous value '{1}'. The large-icon attribute may only be specified once.",
137             value, oldValue));
138
139       
140       gen.setLargeIcon(value);
141     }
142     else if (DESCRIPTION.equals(name)) {
143       String JavaDoc oldValue = gen.getDescription();
144       
145       if (oldValue != null && ! oldValue.equals(value))
146         throw error(L.l("@tag description '{0}' conflicts with previous value '{1}'. The description attribute may only be specified once.",
147             value, oldValue));
148
149       
150       gen.setDescription(value);
151     }
152     else if (EXAMPLE.equals(name)) {
153       String JavaDoc oldValue = gen.getExample();
154       
155       if (oldValue != null && ! oldValue.equals(value))
156         throw error(L.l("@tag example '{0}' conflicts with previous value '{1}'. The example attribute may only be specified once.",
157             value, oldValue));
158
159       
160       gen.setExample(value);
161     }
162     else if (DYNAMIC_ATTRIBUTES.equals(name)) {
163       String JavaDoc oldValue = gen.getDynamicAttributes();
164       
165       if (oldValue != null && ! oldValue.equals(value))
166         throw error(L.l("@tag dynamic-attributes '{0}' conflicts with previous value '{1}'. The dynamic-attributes attribute may only be specified once.",
167             value, oldValue));
168       else if (gen.findAttribute(value) != null) {
169     throw error(L.l("@tag dynamic-attributes '{0}' conflicts with an attribute.",
170             value));
171       }
172       else if (gen.findVariable(value) != null) {
173     throw error(L.l("@tag dynamic-attributes '{0}' conflicts with a variable.",
174             value));
175       }
176       
177       gen.setDynamicAttributes(value);
178     }
179     else if (BODY_CONTENT.equals(name)) {
180       String JavaDoc oldValue = gen.getBodyContent();
181       
182       if (oldValue != null && ! oldValue.equals(value)) {
183         throw error(L.l("@tag body-content '{0}' conflicts with previous value '{1}'. The body-content attribute may only be specified once.",
184             value, oldValue));
185       }
186     
187       if (value.equals("scriptless") ||
188       value.equals("tagdependent") ||
189       value.equals("empty")) {
190       }
191       else
192     throw error(L.l("'{0}' is an unknown body-content value for the JSP tag directive attribute. 'scriptless', 'tagdependent', and 'empty' are the allowed values.",
193                       value));
194
195       gen.setBodyContent(value);
196     }
197     else if (DEFERRED_AS_LITERAL.equals(name)) {
198       _parseState.setDeferredSyntaxAllowedAsLiteral(value.equals("true"));
199     }
200     else {
201       throw error(L.l("'{0}' is an unknown JSP tag directive attribute. The valid attributes are: body-content, deferredSyntaxAllowedAsLiteral, display-name, dynamic-attributes, example, isELIgnored, language, large-icon, pageEncoding, small-icon, trimDirectiveWhitespace.",
202                       name.getName()));
203     }
204   }
205   
206   protected String JavaDoc getRelativeUrl(String JavaDoc value)
207   {
208     if (value.length() > 0 && value.charAt(0) == '/')
209       return value;
210     else
211       return _parseState.getUriPwd() + value;
212   }
213
214   /**
215    * Charset can be specific as follows:
216    * test/html; z=9; charset=utf8; w=12
217    */

218   static String JavaDoc parseCharEncoding(String JavaDoc type)
219     throws JspParseException
220   {
221     type = type.toLowerCase();
222     int i;
223     char ch;
224     while ((i = type.indexOf(';')) >= 0 && i < type.length()) {
225       i++;
226       while (i < type.length() && ((ch = type.charAt(i)) == ' ' || ch == '\t'))
227     i++;
228
229       if (i >= type.length())
230     return null;
231
232       type = type.substring(i);
233       i = type.indexOf('=');
234       if (i >= 0) {
235     if (! type.startsWith("charset"))
236       continue;
237
238     for (i++;
239          i < type.length() && ((ch = type.charAt(i)) == ' ' || ch == '\t');
240          i++) {
241     }
242
243     type = type.substring(i);
244       }
245       
246       for (i = 0;
247        i < type.length() && (ch = type.charAt(i)) != ';' && ch != ' ';
248        i++) {
249       }
250
251       return type.substring(0, i);
252     }
253
254     return null;
255   }
256   
257   /**
258    * Called when the tag ends.
259    */

260   public void endAttributes()
261     throws JspParseException
262   {
263     if (! _gen.isTag())
264       throw error(L.l("tag directive is only allowed in tag files."));
265   }
266   
267   /**
268    * Return true if the node only has static text.
269    */

270   public boolean isStatic()
271   {
272     return true;
273   }
274
275   /**
276    * Generates the XML text representation for the tag validation.
277    *
278    * @param os write stream to the generated XML.
279    */

280   public void printXml(WriteStream os)
281     throws IOException JavaDoc
282   {
283     JavaTagGenerator gen = (JavaTagGenerator) _gen;
284     
285     os.print("<jsp:directive.tag");
286     os.print(" jsp:id=\"" + gen.generateJspId() + "\"");
287
288     if (_isElIgnored != null)
289       os.print(" el-ignored='" + _isElIgnored + "'");
290
291     if (_import != null)
292       os.print(" import='" + _import + "'");
293     
294     /*
295     if (! _parseState.isScriptingEnabled())
296       os.print(" scripting-enabled='false'");
297     */

298     String JavaDoc dynAttr = gen.getDynamicAttributes();
299     if (dynAttr != null)
300       os.print("dynamic-attributes='" + dynAttr + "'");
301
302     os.print("/>");
303   }
304
305   /**
306    * Generates the code for the tag
307    *
308    * @param out the output writer for the generated java.
309    */

310   public void generate(JspJavaWriter out)
311     throws Exception JavaDoc
312   {
313   }
314 }
315
Popular Tags