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.vfs.WriteStream;
034: import com.caucho.xml.QName;
035:
036: import java.io.IOException;
037:
038: /**
039: * Special generator for a JSTL c:when tag.
040: */
041: public class JstlCoreWhen extends JstlNode {
042: private static final QName TEST = new QName("test");
043:
044: private String _test;
045: private JspAttribute _testAttr;
046:
047: /**
048: * Adds an attribute.
049: */
050: public void addAttribute(QName name, String value)
051: throws JspParseException {
052: if (TEST.equals(name))
053: _test = value;
054: else
055: throw error(L.l("'{0}' is an unknown attribute for <{1}>.",
056: name.getName(), getTagName()));
057: }
058:
059: /**
060: * Adds an attribute.
061: */
062: public void addAttribute(QName name, JspAttribute value)
063: throws JspParseException {
064: if (TEST.equals(name))
065: _testAttr = value;
066: else
067: throw error(L.l(
068: "'{0}' is an unknown jsp:attribute for <{1}>.",
069: name.getName(), getTagName()));
070: }
071:
072: /**
073: * Called after all the attributes from the tag.
074: */
075: @Override
076: public void endAttributes() throws JspParseException {
077: }
078:
079: /**
080: * Called after the element from the tag.
081: */
082: @Override
083: public void endElement() throws JspParseException {
084: if (!(getParent() instanceof JstlCoreChoose)) {
085: throw error(L
086: .l("c:when must be contained in a c:choose tag."));
087: }
088:
089: if (_test == null && _testAttr == null)
090: throw error(L.l("'test' attribute missing from c:when."));
091: }
092:
093: /**
094: * Returns true if the tag has scripting values.
095: */
096: public boolean hasScripting() {
097: return (super .hasScripting() || hasScripting(_test) || hasScripting(_testAttr));
098: }
099:
100: /**
101: * Generates the XML text representation for the tag validation.
102: *
103: * @param os write stream to the generated XML.
104: */
105: public void printXml(WriteStream os) throws IOException {
106: os.print("<c:when test=\"");
107: printXmlText(os, _test);
108: os.print("\">");
109:
110: printXmlChildren(os);
111:
112: os.print("</c:when>");
113: }
114:
115: /**
116: * Generates the code for the c:if tag.
117: */
118: public void generate(JspJavaWriter out) throws Exception {
119: if (_test == null && _testAttr == null)
120: throw error(L.l(
121: "required attribute 'test' missing from <{0}>",
122: getTagName()));
123:
124: String ifExpr;
125:
126: if (_testAttr != null)
127: ifExpr = _testAttr.generateValue(boolean.class);
128: else
129: ifExpr = generateJstlValue(boolean.class, _test);
130:
131: out.println("if (" + ifExpr + ") {");
132: out.pushDepth();
133:
134: generateChildren(out);
135:
136: out.popDepth();
137: out.println("}");
138: }
139: }
|