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: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.jsp.java;
030:
031: import com.caucho.jsp.JspParseException;
032: import com.caucho.vfs.WriteStream;
033: import com.caucho.xml.QName;
034:
035: import java.io.IOException;
036:
037: public class JstlXmlIf extends JstlNode {
038: private static final QName SELECT = new QName("select");
039: private static final QName VAR = new QName("var");
040: private static final QName SCOPE = new QName("scope");
041:
042: private String _select;
043: private String _var;
044: private String _scope;
045:
046: /**
047: * Adds an attribute.
048: */
049: public void addAttribute(QName name, String value)
050: throws JspParseException {
051: if (SELECT.equals(name))
052: _select = value;
053: else if (VAR.equals(name))
054: _var = value;
055: else if (SCOPE.equals(name))
056: _scope = value;
057: else
058: throw error(L.l("'{0}' is an unknown attribute for <{1}>.",
059: name.getName(), getTagName()));
060: }
061:
062: /**
063: * Adds an attribute.
064: */
065: public void addAttribute(QName name, JspAttribute value)
066: throws JspParseException {
067: if (false)
068: throw error(L.l(
069: "'{0}' is an unsupported jsp:attribute for <{1}>.",
070: name.getName(), getTagName()));
071: }
072:
073: /**
074: * Called after all the attributes from the tag.
075: */
076: @Override
077: public void endAttributes() throws JspParseException {
078: if (_scope != null && !"".equals(_scope) && _var == null)
079: throw error(L
080: .l("x:if requires var attribute to be set if scope is set"));
081: }
082:
083: /**
084: * Generates the XML text representation for the tag validation.
085: *
086: * @param os write stream to the generated XML.
087: */
088: public void printXml(WriteStream os) throws IOException {
089: os.print("<x:if");
090:
091: if (_select != null) {
092: os.print(" select=\"");
093: printXmlText(os, _select);
094: os.print("\"");
095: }
096:
097: if (_var != null)
098: os.print(" var=\"" + _var + "\"");
099:
100: if (_scope != null)
101: os.print(" scope=\"" + _scope + "\"");
102:
103: os.print(">");
104:
105: printXmlChildren(os);
106:
107: os.print("</x:if>");
108: }
109:
110: /**
111: * Generates the code for the c:set tag.
112: */
113: public void generate(JspJavaWriter out) throws Exception {
114: if (_select == null)
115: throw error(L.l(
116: "required attribute 'select' missing from <{0}>",
117: getTagName()));
118:
119: String select = ("com.caucho.jstl.el.XmlIfTag.evalBoolean(pageContext, "
120: + _gen.addXPathExpr(_select, getNamespaceContext()) + ")");
121:
122: out.println("if (" + select + ") {");
123: out.pushDepth();
124:
125: if (_var != null) {
126: generateSetOrRemove(out, _var, _scope, "Boolean.TRUE");
127: }
128:
129: generateChildren(out);
130:
131: out.popDepth();
132: out.println("}");
133:
134: if (_var != null) {
135: out.println("else {");
136: out.pushDepth();
137:
138: generateSetOrRemove(out, _var, _scope, "Boolean.FALSE");
139:
140: out.popDepth();
141: out.println("}");
142: }
143: }
144: }
|