From: <fg...@us...> - 2007-02-13 21:21:39
|
Revision: 253 http://svn.sourceforge.net/openutils/?rev=253&view=rev Author: fgiust Date: 2007-02-13 13:21:37 -0800 (Tue, 13 Feb 2007) Log Message: ----------- adding debug tags Modified Paths: -------------- trunk/openutils-tags-spring/src/main/resources/META-INF/tags/ou/debug.tag Added Paths: ----------- trunk/openutils-tags-spring/src/main/java/it/openutils/web/tag/ trunk/openutils-tags-spring/src/main/java/it/openutils/web/tag/debug/ trunk/openutils-tags-spring/src/main/java/it/openutils/web/tag/debug/BaseDebugTag.java trunk/openutils-tags-spring/src/main/java/it/openutils/web/tag/debug/DebugAttributesTag.java trunk/openutils-tags-spring/src/main/java/it/openutils/web/tag/debug/DebugExecutionTag.java trunk/openutils-tags-spring/src/main/java/it/openutils/web/tag/debug/DebugHeadersTag.java trunk/openutils-tags-spring/src/main/java/it/openutils/web/tag/debug/DebugParametersTag.java trunk/openutils-tags-spring/src/main/resources/META-INF/openutils-tags-debug.tld Added: trunk/openutils-tags-spring/src/main/java/it/openutils/web/tag/debug/BaseDebugTag.java =================================================================== --- trunk/openutils-tags-spring/src/main/java/it/openutils/web/tag/debug/BaseDebugTag.java (rev 0) +++ trunk/openutils-tags-spring/src/main/java/it/openutils/web/tag/debug/BaseDebugTag.java 2007-02-13 21:21:37 UTC (rev 253) @@ -0,0 +1,214 @@ +package it.openutils.web.tag.debug; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import javax.servlet.jsp.JspException; +import javax.servlet.jsp.tagext.TagSupport; + +import org.apache.commons.lang.ArrayUtils; +import org.apache.commons.lang.ObjectUtils; +import org.apache.commons.lang.StringUtils; + + +/** + * @author fgiust + * @version $Revision$ ($Author$) + */ +public class BaseDebugTag extends TagSupport +{ + + /** + * Stable <code>serialVersionUID</code> + */ + private static final long serialVersionUID = 222; + + /** + * codice html HTML_TABLE_START_4COL + */ + protected static final String HTML_TABLE_START_4COL = "<table><thead><tr>" + + "<th>key</th><th>value</th><th>class</th><th>size</th>" + + "</tr></thead><tbody>"; + + /** + * codice html HTML_TABLE_START_3COL + */ + protected static final String HTML_TABLE_START_3COL = "<table><thead><tr>" + + "<th>key</th><th>value</th><th>class</th>" + + "</tr></thead><tbody>"; + + /** + * codice html HTML_TABLE_START_2COL + */ + protected static final String HTML_TABLE_START_2COL = "<table><thead><tr>" + + "<th>key</th><th>value</th>" + + "</tr></thead><tbody>"; + + /** + * codice html HTML_TABLE_END + */ + protected static final String HTML_TABLE_END = "</tbody></table>"; + + /** + * Map contenente i valori per il debug + */ + private Map debugMap; + + /** + * write ClassName In Table? + */ + private boolean outputClassName; + + /** + * Setta la map con i valori da mostrare nel debug + * @param debug map con i valori da mostrare nel debug + */ + protected void setDebugMap(Map debug) + { + debugMap = debug; + } + + /** + * Scrivi il nome della classe nel debug + * @param writeClassNameInTable <code>true</code> per far si che il nome della classe appaia nel debug + */ + public void setOutputClassName(boolean writeClassNameInTable) + { + outputClassName = writeClassNameInTable; + } + + /** + * doEndTag. Scrive come tabella html la Map settata col metodo setDebugMap() + * @return int + * @throws JspException in caso di eccezioni nella scrittura sull'out + * @see javax.servlet.jsp.tagext.Tag#doEndTag() + */ + @Override + public int doEndTag() throws JspException + { + + if (debugMap.size() > 0) + { + StringBuffer buffer = new StringBuffer(debugMap.size() * 200); + + if (outputClassName) + { + buffer.append(HTML_TABLE_START_3COL); + } + else + { + buffer.append(HTML_TABLE_START_2COL); + } + + Set set = debugMap.entrySet(); + Iterator iterator = set.iterator(); + int totalSize = 0; + + while (iterator.hasNext()) + { + Map.Entry entry = (Map.Entry) iterator.next(); + + Object value = entry.getValue(); + String key = (String) entry.getKey(); + + buffer.append("<tr>"); + buffer.append("<td>" + key + "</td>"); + buffer.append("<td>" + escapeXml(value) + "</td>"); + if (outputClassName) + { + if (value != null) + { + buffer.append("<td>" + value.getClass().getName() + "</td>"); + } + else + { + buffer.append("<td>null</td>"); + } + } + + buffer.append("</tr>"); + } + if (totalSize > 0) + { + buffer.append("<tr><td colspan=\"3\"><strong>total size (solo portlet webbank)</strong></td><td>" + + totalSize + + " byte</td></tr>"); + } + + buffer.append(HTML_TABLE_END); + writeToOut(buffer); + } + else + { + writeToOut("nessun valore"); + } + + return super.doEndTag(); + } + + /** + * trasforma tutte le occorrenze di < in &lt; + * @param string stringa di cui fare l'escape - il metodo accetta un Object per comodit� (utilizza il toString() + * dell'Object) + * @return String + */ + private String escapeXml(Object string) + { + if (string == null) + { + return null; + } + + String stringed; + + // supporto per array di oggetti + if (string.getClass().isArray()) + { + stringed = ArrayUtils.toString(string); + } + // supporto per iterators + else if (string instanceof Iterator) + { + List list = new ArrayList(); + Iterator iterator = (Iterator) string; + while (iterator.hasNext()) + { + list.add(iterator.next()); + } + + stringed = ArrayUtils.toString(list.toArray()); + } + else + { + stringed = string.toString(); + } + + return StringUtils.replace(stringed, "<", "<"); + } + + /** + * metodo di utility per scrivere una semplice stringa sul JspWriter + * @param string stringa da scrivere + * @throws JspException nel caso si verifichi una IOException nella scrittura + */ + protected void writeToOut(Object string) throws JspException + { + if (string != null) + { + try + { + pageContext.getOut().write(ObjectUtils.toString(string)); + } + catch (IOException e) + { + throw new JspException("errore nella scrittura dell'output. String=[" + string + "]"); + } + } + + } + +} \ No newline at end of file Property changes on: trunk/openutils-tags-spring/src/main/java/it/openutils/web/tag/debug/BaseDebugTag.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:keywords + Author Date Id Revision Name: svn:eol-style + native Added: trunk/openutils-tags-spring/src/main/java/it/openutils/web/tag/debug/DebugAttributesTag.java =================================================================== --- trunk/openutils-tags-spring/src/main/java/it/openutils/web/tag/debug/DebugAttributesTag.java (rev 0) +++ trunk/openutils-tags-spring/src/main/java/it/openutils/web/tag/debug/DebugAttributesTag.java 2007-02-13 21:21:37 UTC (rev 253) @@ -0,0 +1,114 @@ +package it.openutils.web.tag.debug; + +import java.util.Enumeration; +import java.util.HashMap; + +import javax.servlet.jsp.JspException; +import javax.servlet.jsp.PageContext; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + + +/** + * @author fgiust + * @version $Revision$ ($Author$) + */ +public class DebugAttributesTag extends BaseDebugTag +{ + + /** + * Stable <code>serialVersionUID</code> + */ + private static final long serialVersionUID = 222; + + /** + * riferimento al Logger + */ + private static Log log = LogFactory.getLog(DebugAttributesTag.class); + + /** + * Scope. Pu� assumere i valori: + * <ul> + * <li>PageContext.PAGE_SCOPE</li> + * <li>PageContext.REQUEST_SCOPE</li> + * <li>PageContext.SESSION_SCOPE</li> + * <li>PageContext.APPLICATION_SCOPE</li> + * </ul> + * Corrispondenti alle stringhe settate tramite l'attributo "scope" del tag: + * <ul> + * <li>page</li> + * <li>request</li> + * <li>session</li> + * <li>application</li> + * </ul> + */ + private int scope = PageContext.PAGE_SCOPE; + + /** + * Setta lo scope in cui recuperare la collezione su cui iterare + * @param scopeString Scope dell'oggetto collection + * @throws JspException se il valore passato non � tra: "page", "request", "session", "application" + */ + public void setScope(String scopeString) throws JspException + { + + if (scopeString.equalsIgnoreCase("page")) + { + scope = PageContext.PAGE_SCOPE; + } + else if (scopeString.equalsIgnoreCase("request")) + { + scope = PageContext.REQUEST_SCOPE; + } + else if (scopeString.equalsIgnoreCase("session")) + { + scope = PageContext.SESSION_SCOPE; + } + else if (scopeString.equalsIgnoreCase("application")) + { + scope = PageContext.APPLICATION_SCOPE; + } + else + { + log.error("Attributo \"scope\" non valido. Valore fornito= [" + + scopeString + + "]; valori ammessi: page, request, session, application"); + throw new JspException("Attributo \"scope\" non valido. Valore fornito= [" + + scopeString + + "]; valori ammessi: page, request, session, application"); + } + } + + /** + * crea l'HashMap che evrr� stampata nel metodo doEndTag() + * @return int + * @throws JspException eccezione generica + * @see javax.servlet.jsp.tagext.Tag#doStartTag() + */ + @Override + public int doStartTag() throws JspException + { + + HashMap map = new HashMap(); + + Enumeration enm = pageContext.getAttributeNamesInScope(scope); + + while (enm.hasMoreElements()) + { + String key = enm.nextElement().toString(); + + if (!(scope == PageContext.PAGE_SCOPE && key.startsWith("javax.servlet"))) + { + Object value = pageContext.getAttribute(key, scope); + map.put(key, value); + } + } + + setOutputClassName(true); + setDebugMap(map); + + return super.doStartTag(); + } + +} \ No newline at end of file Property changes on: trunk/openutils-tags-spring/src/main/java/it/openutils/web/tag/debug/DebugAttributesTag.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:keywords + Author Date Id Revision Name: svn:eol-style + native Added: trunk/openutils-tags-spring/src/main/java/it/openutils/web/tag/debug/DebugExecutionTag.java =================================================================== --- trunk/openutils-tags-spring/src/main/java/it/openutils/web/tag/debug/DebugExecutionTag.java (rev 0) +++ trunk/openutils-tags-spring/src/main/java/it/openutils/web/tag/debug/DebugExecutionTag.java 2007-02-13 21:21:37 UTC (rev 253) @@ -0,0 +1,43 @@ +package it.openutils.web.tag.debug; + +import java.util.HashMap; + +import javax.servlet.ServletRequest; +import javax.servlet.jsp.JspException; + + +/** + * @author fgiust + * @version $Revision$ ($Author$) + */ +public class DebugExecutionTag extends BaseDebugTag +{ + + /** + * Stable <code>serialVersionUID</code> + */ + private static final long serialVersionUID = 222; + + /** + * crea l'HashMap che verr� stampata nel metodo doEndTag() + * @return int + * @throws JspException eccezione generica + * @see javax.servlet.jsp.tagext.Tag#doStartTag() + */ + @Override + public int doStartTag() throws JspException + { + + HashMap map = new HashMap(); + + ServletRequest request = pageContext.getRequest(); + + map.put("action", request.getAttribute("")); + + setOutputClassName(false); + setDebugMap(map); + + return super.doStartTag(); + } + +} \ No newline at end of file Property changes on: trunk/openutils-tags-spring/src/main/java/it/openutils/web/tag/debug/DebugExecutionTag.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:keywords + Author Date Id Revision Name: svn:eol-style + native Added: trunk/openutils-tags-spring/src/main/java/it/openutils/web/tag/debug/DebugHeadersTag.java =================================================================== --- trunk/openutils-tags-spring/src/main/java/it/openutils/web/tag/debug/DebugHeadersTag.java (rev 0) +++ trunk/openutils-tags-spring/src/main/java/it/openutils/web/tag/debug/DebugHeadersTag.java 2007-02-13 21:21:37 UTC (rev 253) @@ -0,0 +1,73 @@ +package it.openutils.web.tag.debug; + +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.util.Enumeration; +import java.util.HashMap; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.jsp.JspException; + + +/** + * @author fgiust + * @version $Revision$ ($Author$) + */ +public class DebugHeadersTag extends BaseDebugTag +{ + + /** + * Stable <code>serialVersionUID</code> + */ + private static final long serialVersionUID = 222; + + /** + * crea l'HashMap che evrr� stampata nel metodo doEndTag() + * @return int + * @throws JspException eccezione generica + * @see javax.servlet.jsp.tagext.Tag#doStartTag() + */ + @Override + public int doStartTag() throws JspException + { + + HashMap map = new HashMap(); + + HttpServletRequest request = (HttpServletRequest) pageContext.getRequest(); + Enumeration enm = request.getHeaderNames(); + + while (enm.hasMoreElements()) + { + String key = enm.nextElement().toString(); + Object value = request.getHeader(key); + map.put(key, value); + } + + String remoteHost = request.getRemoteHost(); + map.put("getRemoteHost", remoteHost); + map.put("getRemoteUser", request.getRemoteUser()); + map.put("getCharacterEncoding", request.getCharacterEncoding()); + map.put("getAuthType", request.getAuthType()); + map.put("getAuthType", request.getAuthType()); + map.put("getPathInfo", request.getPathInfo()); + map.put("getPathTranslated", request.getPathTranslated()); + map.put("getMethod", request.getMethod()); + + if (remoteHost != null) + { + try + { + map.put("getHostName(remoteHost)", InetAddress.getByName(remoteHost).getHostName()); + } + catch (UnknownHostException e) + { + map.put("getHostName(remoteHost)", "UnknownHostException!"); + } + } + + setOutputClassName(false); + setDebugMap(map); + + return super.doStartTag(); + } +} \ No newline at end of file Property changes on: trunk/openutils-tags-spring/src/main/java/it/openutils/web/tag/debug/DebugHeadersTag.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:keywords + Author Date Id Revision Name: svn:eol-style + native Added: trunk/openutils-tags-spring/src/main/java/it/openutils/web/tag/debug/DebugParametersTag.java =================================================================== --- trunk/openutils-tags-spring/src/main/java/it/openutils/web/tag/debug/DebugParametersTag.java (rev 0) +++ trunk/openutils-tags-spring/src/main/java/it/openutils/web/tag/debug/DebugParametersTag.java 2007-02-13 21:21:37 UTC (rev 253) @@ -0,0 +1,50 @@ +package it.openutils.web.tag.debug; + +import java.util.Enumeration; +import java.util.HashMap; + +import javax.servlet.ServletRequest; +import javax.servlet.jsp.JspException; + + +/** + * @author fgiust + * @version $Revision$ ($Author$) + */ +public class DebugParametersTag extends BaseDebugTag +{ + + /** + * Stable <code>serialVersionUID</code> + */ + private static final long serialVersionUID = 222; + + /** + * crea l'HashMap che verr� stampata nel metodo doEndTag() + * @return int + * @throws JspException eccezione generica + * @see javax.servlet.jsp.tagext.Tag#doStartTag() + */ + @Override + public int doStartTag() throws JspException + { + + HashMap map = new HashMap(); + + ServletRequest request = pageContext.getRequest(); + Enumeration enm = request.getParameterNames(); + + while (enm.hasMoreElements()) + { + String key = enm.nextElement().toString(); + Object value = request.getParameter(key); + map.put(key, value); + } + + setOutputClassName(false); + setDebugMap(map); + + return super.doStartTag(); + } + +} Property changes on: trunk/openutils-tags-spring/src/main/java/it/openutils/web/tag/debug/DebugParametersTag.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:keywords + Author Date Id Revision Name: svn:eol-style + native Added: trunk/openutils-tags-spring/src/main/resources/META-INF/openutils-tags-debug.tld =================================================================== --- trunk/openutils-tags-spring/src/main/resources/META-INF/openutils-tags-debug.tld (rev 0) +++ trunk/openutils-tags-spring/src/main/resources/META-INF/openutils-tags-debug.tld 2007-02-13 21:21:37 UTC (rev 253) @@ -0,0 +1,35 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" + "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd"> +<taglib> + <tlib-version>1.1</tlib-version> + <jsp-version>1.2</jsp-version> + <short-name>debug</short-name> + <uri>http://openutils.sourceforge.net/openutils-tags-debug</uri> + <description>Debug tag library</description> + <tag> + <name>attributes</name> + <tag-class>it.openutils.web.tag.debug.DebugAttributesTag</tag-class> + <body-content>empty</body-content> + <attribute> + <name>scope</name> + <required>false</required> + <rtexprvalue>true</rtexprvalue> + </attribute> + </tag> + <tag> + <name>parameters</name> + <tag-class>it.openutils.web.tag.debug.DebugParametersTag</tag-class> + <body-content>empty</body-content> + </tag> + <tag> + <name>execution</name> + <tag-class>it.openutils.web.tag.debug.DebugExecutionTag</tag-class> + <body-content>empty</body-content> + </tag> + <tag> + <name>headers</name> + <tag-class>it.openutils.web.tag.debug.DebugHeadersTag</tag-class> + <body-content>empty</body-content> + </tag> +</taglib> \ No newline at end of file Property changes on: trunk/openutils-tags-spring/src/main/resources/META-INF/openutils-tags-debug.tld ___________________________________________________________________ Name: svn:mime-type + text/xml Name: svn:keywords + Author Date Id Revision Name: svn:eol-style + native Modified: trunk/openutils-tags-spring/src/main/resources/META-INF/tags/ou/debug.tag =================================================================== --- trunk/openutils-tags-spring/src/main/resources/META-INF/tags/ou/debug.tag 2007-02-13 21:21:00 UTC (rev 252) +++ trunk/openutils-tags-spring/src/main/resources/META-INF/tags/ou/debug.tag 2007-02-13 21:21:37 UTC (rev 253) @@ -1,7 +1,6 @@ <jsp:root version="2.0" xmlns:jsp="http://java.sun.com/JSP/Page" - xmlns:debug="urn:jsptld:debug" xmlns:c="http://java.sun.com/jsp/jstl/core"> - - <script type="text/javascript"> + xmlns:debug="http://openutils.sourceforge.net/openutils-tags-debug" xmlns:c="http://java.sun.com/jsp/jstl/core"> + <script type="text/javascript"> <![CDATA[ function showDebug(pId) { lDebugDiv = document.getElementById(pId); @@ -14,34 +13,48 @@ } } ]]> - </script> - <div class="debugtabs"><a href="javascript:showDebug('debug')">&raquo; debug</a></div> - <div id="debug" class="debug" style="display:none"> + </script> + <div class="debugtabs"> + <a href="javascript:showDebug('debug')">&raquo; debug</a> + </div> + <div id="debug" class="debug" style="display:none"> <table> - <tr> - <th onclick="showDebug('parameters')">Parameters</th> - <td id="parameters" style="display:none"><debug:parameters /></td> - </tr> - <tr> - <th onclick="showDebug('request')">Request</th> - <td id="request" style="display:none"><debug:attributes scope="request" /></td> - </tr> - <tr> - <th onclick="showDebug('session')">Session</th> - <td id="session" style="display:none"><debug:attributes scope="session" /></td> - </tr> - <tr> - <th onclick="showDebug('page')">Page</th> - <td id="page" style="display:none"><debug:attributes scope="page" /></td> - </tr> - <tr> - <th onclick="showDebug('application')">Application</th> - <td id="application" style="display:none"><debug:attributes scope="application" /></td> - </tr> - <tr> - <th onclick="showDebug('headers')">Request headers</th> - <td id="headers" style="display:none"><debug:headers /></td> - </tr> + <tr> + <th onclick="showDebug('parameters')">Parameters</th> + <td id="parameters" style="display:none"> + <debug:parameters /> + </td> + </tr> + <tr> + <th onclick="showDebug('request')">Request</th> + <td id="request" style="display:none"> + <debug:attributes scope="request" /> + </td> + </tr> + <tr> + <th onclick="showDebug('session')">Session</th> + <td id="session" style="display:none"> + <debug:attributes scope="session" /> + </td> + </tr> + <tr> + <th onclick="showDebug('page')">Page</th> + <td id="page" style="display:none"> + <debug:attributes scope="page" /> + </td> + </tr> + <tr> + <th onclick="showDebug('application')">Application</th> + <td id="application" style="display:none"> + <debug:attributes scope="application" /> + </td> + </tr> + <tr> + <th onclick="showDebug('headers')">Request headers</th> + <td id="headers" style="display:none"> + <debug:headers /> + </td> + </tr> </table> - </div> + </div> </jsp:root> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |