⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 debugbean.java

📁 大家好啊 快来抢购J2ME东东 挺不错的啊 不要后悔啊 抓住机会
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     * otherwise the value itself.
     */
    private String nullToString(String value) {
        if (value == null) {
            return "null";
        }
        else {
            return value;
        }
    }
    
    /**
     * Returns true if the pageContext attribute has been set
     * and a valid "debug" request parameter is present in the request.
     */
    private boolean isDebugActive() {
        if (pageContext == null) {
            return false;
        }
        
        if (debugType == null) {
            String debugParam = pageContext.getRequest().getParameter("debug");
            if (debugParam != null) {
                debugType = debugParam;
            }
            else {
                debugType = "off";
            }
        }
        return "resp stdout log".indexOf(debugType) != -1;
    }
    
    /**
     * Returns a String suitable for browser debug display
     * and sends the debug message to System.out and/or
     * the servlet log file, depending on the value of
     * the "debug" request parameter. If debug is not active,
     * an empty String is returned.
     */
    private String handleMsg(String propName, String msg) {
        String returnVal = "";
        if (isDebugActive()) {
            log(propName, msg);
            if (debugType.indexOf("resp") != -1) {
                returnVal = propName + ": " + msg;
            }
        }
        return returnVal;
    }
    
    /**
     * If debug is activated, writes the specified property
     * value to the log file and returns it as an HTML table,
     * depending on the requested debug type.
     */
    private String handleMsg(String propName, Hashtable values) {
        String returnVal = "";
        if (isDebugActive()) {
            log(propName, values);
            if (debugType.indexOf("resp") != -1) {
                returnVal = toHTMLTable(propName, values);
            }
        }
        return returnVal;
    }
        
    /**
     * Writes the specified property value to the System.out or
     * the log file, depending on the requested debug type.
     */
    private void log(String propName, String msg) {
        HttpServletRequest request = 
            (HttpServletRequest) pageContext.getRequest();
        msg = "[DebugBean] " + request.getRequestURI() + " : " + 
            propName + " : " + msg;
        if (debugType.indexOf("stdout") != -1) {
            System.out.println(msg);
        }
        if (debugType.indexOf("log") != -1) {
            if (context == null) {
                context = pageContext.getServletContext();
            }
            context.log(msg);
        }
    }
        
    private void log(String propName, Hashtable values) {
        log(propName, toTabbedTable(values));
    }
    
    /**
     * Returns an HTML table with all the values of the
     * specified property.
     */
    private String toHTMLTable(String propName, Hashtable values) {
        StringBuffer tableSB = new StringBuffer("<table border=\"1\">");
        tableSB.append("<caption align=\"top\"><b>").
            append(propName).
            append("</b></caption>");
        Enumeration keys = values.keys();
        while (keys.hasMoreElements()) {
            String key = (String) keys.nextElement();
            tableSB.append("<tr><td>").
                append(key).
                append("</td><td>").
                append(values.get(key)).
                append("</td></tr>");
        }
        tableSB.append("</table>");
        return tableSB.toString();
    }
    
    /**
     * Returns an simple ASCII table with all the values of the
     * specified property, used for log output.
     */
    private String toTabbedTable(Hashtable values) {
        StringBuffer tableSB = new StringBuffer();
        Enumeration keys = values.keys();
        while (keys.hasMoreElements()) {
            String key = (String) keys.nextElement();
            tableSB.append(LINE_FEED).
                append(key).append("\t\t").
                append(values.get(key));
        }
        return tableSB.toString();
    }
    
    /**
     * Returns a String representation of the specified
     * Object, in a format suitable for debug output.
     */
    private String toStringValue(Object value) {
        StringBuffer sb = new StringBuffer();
        Class type = value.getClass();
        if (type.isArray()) {
            Class componentType = type.getComponentType();
            sb.append(componentType.getName());
            sb.append("[]: {");
            if (!componentType.isPrimitive()) {
                Object[] arr = (Object[]) value;
                for (int i = 0; i < arr.length; i++) {
                    if (i != 0) {
                        sb.append(", ");
                    }
                    sb.append(arr[i]);
                }
            }
            else if (componentType == Boolean.TYPE) {
                boolean[] arr = (boolean[]) value;
                for (int i = 0; i < arr.length; i++) {
                    if (i != 0) {
                        sb.append(", ");
                    }
                    sb.append(arr[i]);
                }
            }
            else if (componentType == Byte.TYPE) {
                byte[] arr = (byte[]) value;
                for (int i = 0; i < arr.length; i++) {
                    if (i != 0) {
                        sb.append(", ");
                    }
                    sb.append(arr[i]);
                }
            }
            else if (componentType == Character.TYPE) {
                char[] arr = (char[]) value;
                for (int i = 0; i < arr.length; i++) {
                    if (i != 0) {
                        sb.append(", ");
                    }
                    sb.append(arr[i]);
                }
            }
            else if (componentType == Double.TYPE) {
                double[] arr = (double[]) value;
                for (int i = 0; i < arr.length; i++) {
                    if (i != 0) {
                        sb.append(", ");
                    }
                    sb.append(arr[i]);
                }
            }
            else if (componentType == Float.TYPE) {
                float[] arr = (float[]) value;
                for (int i = 0; i < arr.length; i++) {
                    if (i != 0) {
                        sb.append(", ");
                    }
                    sb.append(arr[i]);
                }
            }
            else if (componentType == Integer.TYPE) {
                int[] arr = (int[]) value;
                for (int i = 0; i < arr.length; i++) {
                    if (i != 0) {
                        sb.append(", ");
                    }
                    sb.append(arr[i]);
                }
            }
            else if (componentType == Long.TYPE) {
                long[] arr = (long[]) value;
                for (int i = 0; i < arr.length; i++) {
                    if (i != 0) {
                        sb.append(", ");
                    }
                    sb.append(arr[i]);
                }
            }
            else if (componentType == Short.TYPE) {
                short[] arr = (short[]) value;
                for (int i = 0; i < arr.length; i++) {
                    if (i != 0) {
                        sb.append(", ");
                    }
                    sb.append(arr[i]);
                }
            }
            sb.append("}");
        }
        else {
            sb.append(value.getClass().getName()).
                append(": ").
                append(value.toString());
        }
        return sb.toString();
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -