/*
* Copyright 2004, 2005, 2006 Odysseus Software GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.odysseus.calyxo.forms.taglib;
import java.io.IOException;
import java.util.Iterator;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import de.odysseus.calyxo.forms.FormInputResult;
import de.odysseus.calyxo.forms.FormResult;
import de.odysseus.calyxo.forms.FormsSupport;
/**
* Debug validation result tag.
* This tag displays a html table showing the current form validation result.
*
* @author Christoph Beck
*/
public class DebugTag extends TagSupport {
private String toString(Object value) {
if (value instanceof Object[]) {
Object[] array = (Object[])value;
StringBuffer b = new StringBuffer();
b.append("{ ");
for (int i = 0; i < Math.min(10, array.length); i++) {
b.append(toString(array[i]));
if (i < array.length - 1) {
b.append(", ");
}
}
if (array.length > 10) {
b.append("...");
}
b.append(" }");
return b.toString();
} else if (value != null) {
return value.toString();
}
return "null";
}
public int doEndTag() throws JspException {
StringBuffer s = new StringBuffer();
HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
FormResult result =
(FormResult)request.getAttribute(FormsSupport.FORM_RESULT_KEY);
if (result != null) {
s.append("<table>");
s.append("<th align=\"left\">Input</th>");
s.append("<th align=\"left\">Property</th>");
s.append("<th align=\"left\">Value/Msg</th>");
Iterator values = result.getFormInputResults();
while (values.hasNext()) {
FormInputResult value = (FormInputResult)values.next();
s.append("<tr><td>");
s.append(value.getFormInput().getName());
s.append("</td><td>");
s.append(value.getProperty());
s.append("</td><td>");
if (value.isValid()) {
Object o = value.getValue();
String f = toString(o);
if (f.length() > 60) {
f = f.substring(0, 60) + "...";
}
s.append(f);
} else {
s.append(value.getFirstMessage());
}
s.append("</td></tr>");
}
s.append("</table>");
} else {
s.append("No validation result.");
}
try {
pageContext.getOut().print(s.toString());
} catch (IOException e) {
throw new JspException(e);
}
return EVAL_PAGE;
}
}