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