001: /*
002: * (C) Copyright 2004 Nabh Information Systems, Inc.
003: *
004: * All copyright notices regarding Nabh's products MUST remain
005: * intact in the scripts and in the outputted HTML.
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021: package com.nabhinc.portlet.mvcportlet.taglib;
022:
023: import java.io.IOException;
024: import java.util.List;
025:
026: import javax.portlet.PortletRequest;
027: import javax.portlet.RenderRequest;
028: import javax.servlet.jsp.JspWriter;
029:
030: import com.nabhinc.portlet.mvcportlet.core.Constants;
031: import com.nabhinc.portlet.mvcportlet.core.ControllerPortletConfig;
032: import com.nabhinc.portlet.mvcportlet.core.Form;
033: import com.nabhinc.portlet.mvcportlet.core.FormField;
034: import com.nabhinc.portlet.mvcportlet.core.FormFieldValidator;
035: import com.nabhinc.portlet.mvcportlet.core.JavascriptValidationInfo;
036:
037: /**
038: *
039: *
040: * @author Padmanabh Dabke
041: * (c) 2004 Nabh Information Systems, Inc. All Rights Reserved.
042: */
043: public class JavascriptFormTag extends FormTag {
044: private boolean vjtIsXHTML = false;
045: private String vjtSrc = null;
046:
047: public void release() {
048: super .release();
049: vjtIsXHTML = false;
050: vjtSrc = null;
051: }
052:
053: /**
054: * The JavaScript methods will enclosed with html comments if this is set to "true".
055: */
056: protected boolean vjtHTMLComment = true;
057:
058: /**
059: * Hide JavaScript methods in a CDATA section for XHTML when "true".
060: */
061: protected boolean vjtCDATA = false;
062:
063: private String vjtHTMLBeginComment = "\n<!-- Begin \n";
064:
065: private String vjtHTMLEndComment = "//End --> \n";
066:
067: public void setXHTML(boolean flag) {
068: vjtIsXHTML = flag;
069: }
070:
071: public boolean isXHTML() {
072: return vjtIsXHTML;
073: }
074:
075: public void setSrc(String src) {
076: vjtSrc = src;
077: }
078:
079: public String getSrc() {
080: return vjtSrc;
081: }
082:
083: public boolean getCDATA() {
084: return vjtCDATA;
085: }
086:
087: public void setCDATA(boolean cdata) {
088: vjtCDATA = cdata;
089: }
090:
091: public boolean isHTMLComment() {
092: return vjtHTMLComment;
093: }
094:
095: public void setHTMLComment(boolean flag) {
096: vjtHTMLComment = flag;
097: }
098:
099: public String getHTMLBeginComment() {
100: return vjtHTMLBeginComment;
101: }
102:
103: public void setHTMLBeginComment(String comment) {
104: vjtHTMLBeginComment = comment;
105: }
106:
107: public String getHTMLEndComment() {
108: return vjtHTMLEndComment;
109: }
110:
111: public void setHTMLEndComment(String comment) {
112: vjtHTMLEndComment = comment;
113: }
114:
115: /**
116: * Returns the opening script element and some initial javascript.
117: */
118: protected String getJavascriptBegin() {
119: StringBuffer sb = new StringBuffer();
120:
121: sb.append(this .getStartElement());
122:
123: if (isXHTML() && vjtCDATA) {
124: sb.append("<![CDATA[\r\n");
125: }
126:
127: if (!isXHTML() && vjtHTMLComment) {
128: sb.append(vjtHTMLBeginComment);
129: }
130:
131: // sb.append(" } \n\n");
132:
133: return sb.toString();
134: }
135:
136: /**
137: * Constructs the beginning <script> element depending on xhtml status.
138: */
139: private String getStartElement() {
140: StringBuffer start = new StringBuffer(
141: "<script type=\"text/javascript\"");
142:
143: // there is no language attribute in xhtml
144: if (!isXHTML()) {
145: start.append(" language=\"Javascript1.2\"");
146: }
147:
148: if (vjtSrc != null) {
149: start.append(" src=\"" + vjtSrc + "\"");
150: }
151:
152: start.append("> \n");
153: return start.toString();
154: }
155:
156: /**
157: * Returns the closing script element.
158: */
159: protected String getJavascriptEnd() {
160: StringBuffer sb = new StringBuffer();
161:
162: sb.append("\n");
163: if (!vjtIsXHTML && vjtHTMLComment) {
164: sb.append(vjtHTMLEndComment);
165: }
166:
167: if (vjtIsXHTML && vjtCDATA) {
168: sb.append("]]>\r\n");
169: }
170:
171: sb.append("</script>\n\n");
172:
173: return sb.toString();
174: }
175:
176: public void writeJavascript(RenderRequest request, JspWriter out)
177: throws IOException {
178: ControllerPortletConfig config = (ControllerPortletConfig) request
179: .getAttribute(Constants.PORTLET_CONFIG_ATTRIB);
180:
181: if (config == null)
182: return;
183:
184: Form form = config.getForm(getName());
185: if (form == null)
186: return;
187:
188: out.print(getJavascriptBegin());
189:
190: // Write common Javascript
191: JavascriptValidationInfo commonJS = config
192: .getJavascriptValidationInfo("common");
193: if (commonJS != null) {
194: out.print(commonJS.getJavascript());
195: }
196:
197: // Write form field specific functions
198: out.print(form.getJavascript());
199:
200: // Write form validation function
201: out.print("var bCancel = false;\n");
202: out.print("function ");
203: out.print(getValidationMethod());
204: out.print("(form) {\n");
205: out.print(" var elem;\n");
206: out.print(" var errs=0;\n");
207: out.print(" var errorMsg = \"\";\n");
208: out
209: .print(" var cancelButton = Form.getElement(form, 'cancel');");
210: out
211: .print(" if (cancelButton && cancelButton.getAttribute('clicked')) {\n");
212: out.print(" return true;\n");
213: out.print(" } else {\n");
214:
215: List fieldList = form.getFieldList();
216: String errorPrefix = "mvcportlet.form_error." + getName() + ".";
217: for (int i = 0; i < fieldList.size(); i++) {
218: FormField f = (FormField) fieldList.get(i);
219: writeFieldValidationCode(errorPrefix, f, out, config,
220: request);
221:
222: }
223: out.print(" }\n");
224: out.print(" if (errs > 0) {\n");
225: out.print(" alert(errorMsg);\n");
226: out.print(" return false;\n");
227: out.print(" } else {\n");
228: out.print(" return true;\n");
229: out.print(" }\n");
230: out.print("}\n");
231:
232: // Write closing tags
233: out.print(getJavascriptEnd());
234: }
235:
236: private void writeFieldValidationCode(String errorPrefix,
237: FormField f, JspWriter out, ControllerPortletConfig config,
238: PortletRequest request) throws IOException {
239: FormFieldValidator[] vals = f.getValidators();
240: if (vals == null || vals.length == 0)
241: return;
242:
243: for (int i = 0; i < vals.length; i++) {
244: JavascriptValidationInfo jInfo = config
245: .getJavascriptValidationInfo(vals[i].getClass()
246: .getName());
247: if (jInfo == null)
248: continue;
249: String functionArgs = vals[i].getJavascriptFunctionArgs(
250: request, f.getName());
251:
252: if (i > 0) {
253: out.print(" else if (!");
254: } else {
255: out.print(" if (!");
256: }
257: out.print(jInfo.getMethodName());
258: out.print("(");
259: out.print(functionArgs);
260: out.print(")) {\n");
261: out.print(" errs += 1;\n");
262: out.print(" errorMsg += \"");
263: out.print(config.getLocalizedMessage(errorPrefix
264: + f.getName() + "." + vals[i].getName(), request));
265: out.print("\";\n");
266: out.print(" errorMsg += \"\\n\";\n");
267: out.print(" }\n");
268: }
269:
270: }
271:
272: }
|