cmsformhandler.java
来自「找了很久才找到到源代码」· Java 代码 · 共 810 行 · 第 1/3 页
JAVA
810 行
/*
* File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/frontend/templateone/form/CmsFormHandler.java,v $
* Date : $Date: 2007-08-13 16:29:41 $
* Version: $Revision: 1.28 $
*
* This library is part of OpenCms -
* the Open Source Content Management System
*
* Copyright (c) 2002 - 2007 Alkacon Software GmbH (http://www.alkacon.com)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* For further information about Alkacon Software GmbH, please see the
* company website: http://www.alkacon.com
*
* For further information about OpenCms, please see the
* project website: http://www.opencms.org
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.opencms.frontend.templateone.form;
import org.opencms.i18n.CmsEncoder;
import org.opencms.i18n.CmsMessages;
import org.opencms.jsp.CmsJspActionElement;
import org.opencms.mail.CmsHtmlMail;
import org.opencms.mail.CmsSimpleMail;
import org.opencms.main.CmsLog;
import org.opencms.main.OpenCms;
import org.opencms.module.CmsModule;
import org.opencms.util.CmsRequestUtil;
import org.opencms.util.CmsStringUtil;
import org.opencms.util.CmsByteArrayDataSource;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.PageContext;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.logging.Log;
/**
* The form handler controls the html or mail output of a configured email form.<p>
*
* Provides methods to determine the action that takes place and methods to create different
* output formats of a submitted form.<p>
*
* @author Andreas Zahner
* @author Thomas Weckert
* @author Jan Baudisch
*
* @version $Revision: 1.28 $
*
* @since 6.0.0
*/
public class CmsFormHandler extends CmsJspActionElement {
/** Request parameter value for the form action parameter: correct the input. */
public static final String ACTION_CONFIRMED = "confirmed";
/** Request parameter value for the form action parameter: correct the input. */
public static final String ACTION_CORRECT_INPUT = "correct";
/** Request parameter value for the form action parameter: form submitted. */
public static final String ACTION_SUBMIT = "submit";
/** Form error: mandatory field not filled out. */
public static final String ERROR_MANDATORY = "mandatory";
/** Form error: validation error of input. */
public static final String ERROR_VALIDATION = "validation";
/** Request parameter name for the hidden form action parameter to determine the action. */
public static final String PARAM_FORMACTION = "formaction";
/** Name of the file item session attribute. */
public static final String ATTRIBUTE_FILEITEMS = "fileitems";
/** The log object for this class. */
private static final Log LOG = CmsLog.getLog(CmsFormHandler.class);
private static final String MODULE = "org.opencms.frontend.templateone.form";
/** Contains eventual validation errors. */
private Map m_errors;
/** The form configuration object. */
private CmsForm m_formConfiguration;
/** Temporarily stores the fields as hidden fields in the String. */
private String m_hiddenFields;
/** Flag indicating if the form is displayed for the first time. */
private boolean m_initial;
/** The localized messages for the form handler. */
private CmsMessages m_messages;
/** Boolean indicating if the form is validated correctly. */
private Boolean m_isValidatedCorrect;
/** The map of request parameters. */
private Map m_parameterMap;
/** The multipart file items. */
private List m_mulipartFileItems;
/**
* Constructor, creates the necessary form configuration objects.<p>
*
* @param context the JSP page context object
* @param req the JSP request
* @param res the JSP response
* @throws Exception if creating the form configuration objects fails
*/
public CmsFormHandler(PageContext context, HttpServletRequest req, HttpServletResponse res)
throws Exception {
super(context, req, res);
init(req, null);
}
/**
* Constructor, creates the necessary form configuration objects using a given configuration file URI.<p>
*
* @param context the JSP page context object
* @param req the JSP request
* @param res the JSP response
* @param formConfigUri URI of the form configuration file, if not provided, current URI is used for configuration
* @throws Exception if creating the form configuration objects fails
*/
public CmsFormHandler(PageContext context, HttpServletRequest req, HttpServletResponse res, String formConfigUri)
throws Exception {
super(context, req, res);
init(req, formConfigUri);
}
/**
* Replaces line breaks with html <br>.<p>
*
* @param value the value to substitute
* @return the substituted value
*/
public String convertToHtmlValue(String value) {
return convertValue(value, "html");
}
/**
* Replaces html <br> with line breaks.<p>
*
* @param value the value to substitute
* @return the substituted value
*/
public String convertToPlainValue(String value) {
return convertValue(value, "");
}
/**
* Converts a given String value to the desired output format.<p>
*
* The following output formats are possible:
* <ul>
* <li>"html" meaning that <br> tags are added</li>
* <li>"plain" or any other String value meaning that <br> tags are removed</li>
* </ul>
*
* @param value the String value to convert
* @param outputType the type of the resulting output
* @return the converted String in the desired output format
*/
public String convertValue(String value, String outputType) {
if ("html".equalsIgnoreCase(outputType)) {
// output should be html, add line break tags and characters
value = CmsStringUtil.escapeHtml(value);
} else {
// output should be plain, remove html line break tags and characters
value = CmsStringUtil.substitute(value, "<br>", "\n");
}
return value;
}
/**
* Returns the configured form field values as hidden input fields.<p>
*
* @return the configured form field values as hidden input fields
*/
public String createHiddenFields() {
if (CmsStringUtil.isEmpty(m_hiddenFields)) {
List fields = getFormConfiguration().getFields();
StringBuffer result = new StringBuffer(fields.size() * 8);
// iterate the form fields
for (int i = 0, n = fields.size(); i < n; i++) {
I_CmsField currentField = (I_CmsField)fields.get(i);
if (currentField == null) {
continue;
} else if (CmsCheckboxField.class.isAssignableFrom(currentField.getClass())) {
// special case: checkbox, can have more than one value
Iterator k = currentField.getItems().iterator();
while (k.hasNext()) {
CmsFieldItem item = (CmsFieldItem)k.next();
if (item.isSelected()) {
result.append("<input type=\"hidden\" name=\"");
result.append(currentField.getName());
result.append("\" value=\"");
result.append(CmsEncoder.escapeXml(item.getValue()));
result.append("\">\n");
}
}
} else if (CmsStringUtil.isNotEmpty(currentField.getValue())) {
// all other fields are converted to a simple hidden field
result.append("<input type=\"hidden\" name=\"");
result.append(currentField.getName());
result.append("\" value=\"");
result.append(CmsEncoder.escapeXml(currentField.getValue()));
result.append("\">\n");
}
}
// store the generated input fields for further usage to avoid unnecessary rebuilding
m_hiddenFields = result.toString();
}
// return generated result list
return m_hiddenFields;
}
/**
* Returns the errors found when validating the form.<p>
*
* @return the errors found when validating the form
*/
public Map getErrors() {
return m_errors;
}
/**
* Returns the form configuration.<p>
*
* @return the form configuration
*/
public CmsForm getFormConfiguration() {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?