KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.L10N;
34 import com.caucho.vfs.WriteStream;
35 import com.caucho.xml.QName;
36
37 import java.io.IOException JavaDoc;
38 import java.util.ArrayList JavaDoc;
39
40 public class JspElement extends JspContainerNode {
41   static final L10N L = new L10N(JspElement.class);
42
43   static final private QName NAME = new QName("name");
44
45   private String JavaDoc _name;
46   private JspAttribute _attrName;
47
48   private ArrayList JavaDoc<QName> _attrNames = new ArrayList JavaDoc<QName>();
49   private ArrayList JavaDoc<JspAttribute> _attrValues = new ArrayList JavaDoc<JspAttribute>();
50   
51   /**
52    * Adds an attribute.
53    *
54    * @param name the attribute name
55    * @param value the attribute value
56    */

57   public void addAttribute(QName name, String JavaDoc value)
58     throws JspParseException
59   {
60     if (NAME.equals(name)) {
61       _name = value;
62     }
63     else {
64       throw error(L.l("`{0}' is an unknown jsp:element attribute. See the JSP documentation for a complete list of jsp:element directive attributes.",
65                       name.getName()));
66     }
67   }
68   
69   /**
70    * Adds an attribute.
71    *
72    * @param name the attribute name
73    * @param value the attribute value
74    */

75   public void addAttribute(QName name, JspAttribute value)
76     throws JspParseException
77   {
78     if (_name == null && NAME.equals(name)) {
79       _attrName = value;
80     }
81     else {
82       _attrNames.add(name);
83       _attrValues.add(value);
84     }
85   }
86
87   /**
88    * When the element complets.
89    */

90   public void endElement()
91     throws JspParseException
92   {
93     if (_name == null && _attrName == null) {
94       throw error(L.l("jsp:element requires a `name' attribute."));
95     }
96   }
97   
98   /**
99    * Return true if the node only has static text.
100    */

101   public boolean isStatic()
102   {
103     return false;
104   }
105
106   /**
107    * Generates the XML text representation for the tag validation.
108    *
109    * @param os write stream to the generated XML.
110    */

111   public void printXml(WriteStream os)
112     throws IOException JavaDoc
113   {
114     os.print("<jsp:element name=\"" + _name + "\">");
115     printXmlChildren(os);
116     os.print("</jsp:element>");
117   }
118
119   /**
120    * Generates the code for the tag
121    *
122    * @param out the output writer for the generated java.
123    */

124   public void generate(JspJavaWriter out)
125     throws Exception JavaDoc
126   {
127     String JavaDoc var = null;
128
129     if (_attrName != null) {
130       var = "_caucho_var" + _gen.uniqueId();
131
132       out.println("String " + var + " = " + _attrName.generateValue() + ";");
133     }
134     else if (hasELAttribute(_name)) {
135       var = "_caucho_var" + _gen.uniqueId();
136       int index = _gen.addExpr(_name);
137       
138       out.println("String " + var + " = _caucho_expr_" + index + ".evalString(_jsp_env);");
139     }
140     else if (hasRuntimeAttribute(_name)) {
141       // jsp/0408
142

143       var = "_caucho_var" + _gen.uniqueId();
144       
145       out.println("String " + var + " = " + getRuntimeAttribute(_name) + ";");
146     }
147
148     if (var != null) {
149       out.addText("<");
150       out.println("out.print(" + var + ");");
151     }
152     else
153       out.addText("<" + _name);
154
155     for (int i = 0; i < _attrNames.size(); i++) {
156       QName name = _attrNames.get(i);
157       JspAttribute value = _attrValues.get(i);
158
159       out.addText(" " + name.getName() + "=\"");
160
161       if (value.isStatic())
162     out.addText(value.getStaticText());
163       else
164     out.print("out.print(" + value.generateValue() + ");");
165
166       out.addText("\"");
167     }
168
169     
170     out.addText(">");
171
172     generateChildren(out);
173
174     if (var != null) {
175       out.println("out.print(\"</\");");
176       out.println("out.print(" + var + ");");
177       out.println("out.print(\">\");");
178     }
179     else
180       out.addText("</" + _name + ">");
181   }
182 }
183
Popular Tags