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.config.types.Signature;
032: import com.caucho.jsp.JspParseException;
033: import com.caucho.jsp.cfg.TldAttribute;
034: import com.caucho.util.L10N;
035: import com.caucho.vfs.WriteStream;
036: import com.caucho.xml.QName;
037:
038: import javax.servlet.jsp.tagext.JspFragment;
039: import java.io.IOException;
040:
041: public class JspDirectiveAttribute extends JspNode {
042: static L10N L = new L10N(JspDirectiveAttribute.class);
043:
044: private static final QName NAME = new QName("name");
045: private static final QName REQUIRED = new QName("required");
046: private static final QName FRAGMENT = new QName("fragment");
047: private static final QName RTEXPRVALUE = new QName("rtexprvalue");
048: private static final QName TYPE = new QName("type");
049: private static final QName DESCRIPTION = new QName("description");
050: private static final QName DEFERRED_VALUE = new QName(
051: "deferredValue");
052: private static final QName DEFERRED_VALUE_TYPE = new QName(
053: "deferredValueType");
054: private static final QName DEFERRED_METHOD = new QName(
055: "deferredMethod");
056: private static final QName DEFERRED_METHOD_SIGNATURE = new QName(
057: "deferredMethodSignature");
058:
059: private String _name;
060: private String _type;
061: private boolean _isRequired;
062: private boolean _isFragment;
063: private Boolean _isRtexprvalue;
064: private String _description;
065:
066: private boolean _deferredValue;
067: private String _deferredValueType;
068: private Boolean _deferredMethod;
069: private String _deferredMethodSignature;
070:
071: /**
072: * Adds an attribute.
073: *
074: * @param name the attribute name
075: * @param value the attribute value
076: */
077: public void addAttribute(QName name, String value)
078: throws JspParseException {
079: if (!_gen.getParseState().isTag())
080: throw error(L
081: .l(
082: "'{0}' is only allowed in .tag files. Attribute directives are not allowed in normal JSP files.",
083: getTagName()));
084:
085: JavaTagGenerator gen = (JavaTagGenerator) _gen;
086:
087: if (NAME.equals(name)) {
088: if (gen.findVariable(value) != null) {
089: throw error(L
090: .l(
091: "@attribute name '{0}' is already used by a variable.",
092: value));
093: } else if (gen.findAttribute(value) != null) {
094: throw error(L
095: .l(
096: "@attribute name '{0}' is already used by another attribute.",
097: value));
098: }
099:
100: _name = value;
101: } else if (TYPE.equals(name))
102: _type = value;
103: else if (REQUIRED.equals(name))
104: _isRequired = attributeToBoolean(name.getName(), value);
105: else if (FRAGMENT.equals(name))
106: _isFragment = attributeToBoolean(name.getName(), value);
107: else if (RTEXPRVALUE.equals(name))
108: _isRtexprvalue = attributeToBoolean(name.getName(), value);
109: else if (DESCRIPTION.equals(name))
110: _description = value;
111: else if (DEFERRED_VALUE.equals(name)) {
112: _deferredValue = attributeToBoolean(name.getName(), value);
113: if (_deferredValue)
114: _type = "javax.el.ValueExpression";
115: } else if (DEFERRED_VALUE_TYPE.equals(name)) {
116: _type = "javax.el.ValueExpression";
117: _deferredValueType = value;
118: } else if (DEFERRED_METHOD.equals(name)) {
119: _deferredMethod = attributeToBoolean(name.getName(), value);
120: if (Boolean.TRUE.equals(_deferredMethod))
121: _type = "javax.el.MethodExpression";
122: } else if (DEFERRED_METHOD_SIGNATURE.equals(name)) {
123: try {
124: new Signature(value);
125: } catch (Exception e) {
126: throw error(e.getMessage());
127: }
128:
129: _type = "javax.el.MethodExpression";
130: _deferredMethodSignature = value;
131: } else {
132: throw error(L
133: .l(
134: "'{0}' is an unknown JSP attribute directive attributes. The valid attributes are: deferredMethod, deferredMethodSignature, deferredValue, deferredValueType, description, fragment, name, rtexprvalue, type.",
135: name.getName()));
136: }
137: }
138:
139: /**
140: * When the element complets.
141: */
142: public void endElement() throws JspParseException {
143: if (!_gen.getParseState().isTag())
144: throw error(L
145: .l(
146: "'{0}' is only allowed in .tag files. Attribute directives are not allowed in normal JSP files.",
147: getTagName()));
148:
149: if (_name == null)
150: throw error(L.l("<{0}> needs a 'name' attribute.",
151: getTagName()));
152:
153: JavaTagGenerator tagGen = (JavaTagGenerator) _gen;
154:
155: TldAttribute attr = new TldAttribute();
156: attr.setName(_name);
157:
158: if (_type != null) {
159: Class type = loadClass(_type);
160:
161: if (type == null)
162: throw error(L
163: .l(
164: "type '{0}' is an unknown class for tag attribute {1}.",
165: _type, _name));
166:
167: if (type.isPrimitive())
168: throw error(L
169: .l(
170: "attribute type '{0}' cannot be a Java primitive for {1}.",
171: _type, _name));
172:
173: attr.setType(type);
174: }
175:
176: attr.setRequired(_isRequired);
177:
178: if (_isFragment && _isRtexprvalue != null)
179: throw error(L
180: .l("@attribute rtexprvalue cannot be set when fragment is true."));
181:
182: if (_isFragment && _type != null)
183: throw error(L
184: .l("@attribute type cannot be set when fragment is true."));
185:
186: if (_isRtexprvalue == null
187: || Boolean.TRUE.equals(_isRtexprvalue))
188: attr.setRtexprvalue(Boolean.TRUE);
189:
190: if (_isFragment)
191: attr.setType(JspFragment.class);
192:
193: if (_deferredValue && _deferredValueType != null)
194: throw error(L
195: .l("@attribute deferredValue and deferredValueType may not both be specified"));
196:
197: if (Boolean.FALSE.equals(_deferredMethod)
198: && _deferredMethodSignature != null)
199: throw error(L
200: .l("@attribute deferredMethod and deferredMethodSignature may not both be specified"));
201:
202: if ((_deferredValue || _deferredValueType != null)
203: && (_deferredMethod != null || _deferredMethodSignature != null))
204: throw error(L
205: .l("@attribute deferredValue and deferredMethod may not both be specified"));
206:
207: if (_deferredValue || _deferredValueType != null) {
208: attr.setDeferredValue(new TldAttribute.DeferredValue());
209:
210: if (_deferredValueType != null)
211: attr.getDeferredValue().setType(_deferredValueType);
212: }
213:
214: if (Boolean.TRUE.equals(_deferredMethod)
215: || _deferredMethodSignature != null) {
216: attr.setDeferredMethod(new TldAttribute.DeferredMethod());
217:
218: if (_deferredMethodSignature != null)
219: attr.getDeferredMethod().setMethodSignature(
220: new Signature(_deferredMethodSignature));
221: }
222:
223: tagGen.addAttribute(attr);
224: }
225:
226: /**
227: * Return true if the node only has static text.
228: */
229: public boolean isStatic() {
230: return true;
231: }
232:
233: /**
234: * Generates the XML text representation for the tag validation.
235: *
236: * @param os write stream to the generated XML.
237: */
238: public void printXml(WriteStream os) throws IOException {
239: os.print("<jsp:directive.attribute");
240: os.print(" jsp:id=\"" + _gen.generateJspId() + "\"");
241: os.print(" name=\"" + _name + "\"");
242:
243: if (_type != null)
244: os.print(" type=\"" + _type + "\"");
245:
246: os.println("/>");
247: }
248:
249: /**
250: * Generates the code for the tag
251: *
252: * @param out the output writer for the generated java.
253: */
254: public void generate(JspJavaWriter out) throws Exception {
255: }
256: }
|