001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.jsp.java;
031:
032: import com.caucho.jsp.JspParseException;
033: import com.caucho.util.L10N;
034: import com.caucho.vfs.WriteStream;
035: import com.caucho.xml.QName;
036:
037: import java.io.IOException;
038: import java.util.ArrayList;
039:
040: public class JspElement extends JspContainerNode {
041: static final L10N L = new L10N(JspElement.class);
042:
043: static final private QName NAME = new QName("name");
044:
045: private String _name;
046: private JspAttribute _attrName;
047:
048: private ArrayList<QName> _attrNames = new ArrayList<QName>();
049: private ArrayList<JspAttribute> _attrValues = new ArrayList<JspAttribute>();
050:
051: /**
052: * Adds an attribute.
053: *
054: * @param name the attribute name
055: * @param value the attribute value
056: */
057: public void addAttribute(QName name, String value)
058: throws JspParseException {
059: if (NAME.equals(name)) {
060: _name = value;
061: } else {
062: throw error(L
063: .l(
064: "`{0}' is an unknown jsp:element attribute. See the JSP documentation for a complete list of jsp:element directive attributes.",
065: name.getName()));
066: }
067: }
068:
069: /**
070: * Adds an attribute.
071: *
072: * @param name the attribute name
073: * @param value the attribute value
074: */
075: public void addAttribute(QName name, JspAttribute value)
076: throws JspParseException {
077: if (_name == null && NAME.equals(name)) {
078: _attrName = value;
079: } else {
080: _attrNames.add(name);
081: _attrValues.add(value);
082: }
083: }
084:
085: /**
086: * When the element complets.
087: */
088: public void endElement() throws JspParseException {
089: if (_name == null && _attrName == null) {
090: throw error(L.l("jsp:element requires a `name' attribute."));
091: }
092: }
093:
094: /**
095: * Return true if the node only has static text.
096: */
097: public boolean isStatic() {
098: return false;
099: }
100:
101: /**
102: * Generates the XML text representation for the tag validation.
103: *
104: * @param os write stream to the generated XML.
105: */
106: public void printXml(WriteStream os) throws IOException {
107: os.print("<jsp:element name=\"" + _name + "\">");
108: printXmlChildren(os);
109: os.print("</jsp:element>");
110: }
111:
112: /**
113: * Generates the code for the tag
114: *
115: * @param out the output writer for the generated java.
116: */
117: public void generate(JspJavaWriter out) throws Exception {
118: String var = null;
119:
120: if (_attrName != null) {
121: var = "_caucho_var" + _gen.uniqueId();
122:
123: out.println("String " + var + " = "
124: + _attrName.generateValue() + ";");
125: } else if (hasELAttribute(_name)) {
126: var = "_caucho_var" + _gen.uniqueId();
127: int index = _gen.addExpr(_name);
128:
129: out.println("String " + var + " = _caucho_expr_" + index
130: + ".evalString(_jsp_env);");
131: } else if (hasRuntimeAttribute(_name)) {
132: // jsp/0408
133:
134: var = "_caucho_var" + _gen.uniqueId();
135:
136: out.println("String " + var + " = "
137: + getRuntimeAttribute(_name) + ";");
138: }
139:
140: if (var != null) {
141: out.addText("<");
142: out.println("out.print(" + var + ");");
143: } else
144: out.addText("<" + _name);
145:
146: for (int i = 0; i < _attrNames.size(); i++) {
147: QName name = _attrNames.get(i);
148: JspAttribute value = _attrValues.get(i);
149:
150: out.addText(" " + name.getName() + "=\"");
151:
152: if (value.isStatic())
153: out.addText(value.getStaticText());
154: else
155: out.print("out.print(" + value.generateValue() + ");");
156:
157: out.addText("\"");
158: }
159:
160: out.addText(">");
161:
162: generateChildren(out);
163:
164: if (var != null) {
165: out.println("out.print(\"</\");");
166: out.println("out.print(" + var + ");");
167: out.println("out.print(\">\");");
168: } else
169: out.addText("</" + _name + ">");
170: }
171: }
|