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 java.lang.reflect.*;
032:
033: import com.caucho.jsp.JspParseException;
034: import com.caucho.xml.QName;
035: import com.caucho.util.*;
036: import com.caucho.config.*;
037:
038: abstract public class JsfNode extends JspContainerNode {
039: protected String _var;
040: protected String _bodyVar;
041:
042: /**
043: * Returns the variable containing the jsf parent
044: */
045: @Override
046: public String getJsfVar() {
047: return _var;
048: }
049:
050: /**
051: * Returns the variable containing the jsf body
052: */
053: @Override
054: public String getJsfBodyVar() {
055: return _bodyVar;
056: }
057:
058: /**
059: * Adds an attribute.
060: */
061: @Override
062: public void addAttribute(QName name, String value)
063: throws JspParseException {
064: throw error(L.l("attribute '{0}' is not allowed in <{1}>.",
065: name.getName(), getTagName()));
066: }
067:
068: /**
069: * Adds a JspAttribute attribute.
070: *
071: * @param name the name of the attribute.
072: * @param value the value of the attribute.
073: */
074: @Override
075: public void addAttribute(QName name, JspAttribute value)
076: throws JspParseException {
077: throw error(L.l("attribute '{0}' is not allowed in <{1}>.",
078: name.getName(), getTagName()));
079: }
080:
081: /**
082: * generates prologue data.
083: */
084: @Override
085: public void generatePrologue(JspJavaWriter out) throws Exception {
086: super .generatePrologue(out);
087:
088: if (!_gen.isJsfPrologueInit()) {
089: _gen.setJsfPrologueInit(true);
090:
091: out
092: .println("final javax.faces.context.FacesContext _jsp_faces_context");
093: out
094: .println(" = javax.faces.context.FacesContext.getCurrentInstance();");
095: }
096: }
097:
098: /**
099: * Generates the code for the children.
100: *
101: * @param out the output writer for the generated java.
102: */
103: @Override
104: public void generateChildren(JspJavaWriter out) throws Exception {
105: if (_children == null)
106: return;
107:
108: String prevId = null;
109: boolean isFirst = (this instanceof JsfTagNode);
110: for (int i = 0; i < _children.size(); i++) {
111: JspNode child = _children.get(i);
112:
113: if (isFirst
114: && child instanceof StaticText
115: && (i + 1 == _children.size() || _children
116: .get(i + 1) instanceof JsfNode)) {
117: StaticText text = (StaticText) child;
118:
119: if (isWhitespaceOrComment(text.getText())) {
120: } else if (i + 1 == _children.size()) {
121: out
122: .print("com.caucho.jsp.jsf.JsfTagUtil.addVerbatim("
123: + _var + ", \"");
124: out.printJavaString(text.getText());
125: out.println("\");");
126: } else {
127: out
128: .print("com.caucho.jsp.jsf.JsfTagUtil.addVerbatim("
129: + _var + ", " + prevId + ", \"");
130: out.printJavaString(text.getText());
131: out.println("\");");
132: }
133:
134: continue;
135: }
136:
137: isFirst = false;
138:
139: child.generateStartLocation(out);
140: try {
141: child.generate(out);
142: } catch (Exception e) {
143: if (e instanceof LineCompileException)
144: throw e;
145: else
146: throw child.error(e);
147: }
148: child.generateEndLocation(out);
149:
150: if (child instanceof JsfTagNode) {
151: JsfTagNode jsfNode = (JsfTagNode) child;
152:
153: if (jsfNode.getJsfId() != null)
154: prevId = "\"" + jsfNode.getJsfId() + "\"";
155:
156: isFirst = true;
157: }
158: }
159:
160: if (_bodyVar != null && !isFirst)
161: out.println("com.caucho.jsp.jsf.JsfTagUtil.addVerbatim("
162: + _var + ", " + _bodyVar + ");");
163: }
164:
165: private boolean isWhitespaceOrComment(String text) {
166: text = text.trim();
167:
168: return (text.equals("") || text.startsWith("<!--")
169: && text.endsWith("-->"));
170: }
171:
172: /**
173: * Generates the code for the children.
174: *
175: * @param out the output writer for the generated java.
176: */
177: protected boolean hasBodyContent() throws Exception {
178: if (_children == null)
179: return false;
180:
181: String bodyVar = null;
182: String prevId = null;
183: boolean isFirst = true;
184: for (int i = 0; i < _children.size(); i++) {
185: JspNode child = _children.get(i);
186:
187: if (isFirst
188: && child instanceof StaticText
189: && (i + 1 == _children.size() || _children
190: .get(i + 1) instanceof JsfTagNode)) {
191: continue;
192: }
193:
194: if (!(child instanceof JsfTagNode)) {
195: if (isFirst) {
196: return true;
197: }
198:
199: // push body
200: }
201: }
202:
203: return false;
204: }
205:
206: protected Method findSetter(Class cl, String name) {
207: Method[] methods = cl.getMethods();
208:
209: for (int i = 0; i < methods.length; i++) {
210: Method method = methods[i];
211:
212: if (!method.getName().equals(name))
213: continue;
214:
215: if (!Modifier.isPublic(method.getModifiers())
216: || Modifier.isStatic(method.getModifiers()))
217: continue;
218:
219: if (method.getParameterTypes().length != 1)
220: continue;
221:
222: return method;
223: }
224:
225: return null;
226: }
227:
228: static class Attr {
229: private String _name;
230: private Method _method;
231:
232: private String _value;
233: private JspAttribute _attr;
234:
235: Attr(String name, Method method, String value) {
236: _name = name;
237: _method = method;
238: _value = value;
239: }
240:
241: Attr(String name, Method method, JspAttribute attr) {
242: _name = name;
243: _method = method;
244: _attr = attr;
245: }
246:
247: String getName() {
248: return _name;
249: }
250:
251: Method getMethod() {
252: return _method;
253: }
254:
255: String getValue() {
256: return _value;
257: }
258:
259: JspAttribute getAttr() {
260: return _attr;
261: }
262: }
263: }
|