You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@beehive.apache.org by ek...@apache.org on 2006/04/06 00:12:37 UTC

svn commit: r391828 - in /beehive/trunk/netui/src/util/org/apache/beehive/netui/util: HtmlExceptionFormatter.java type/TypeUtils.java

Author: ekoneil
Date: Wed Apr  5 15:12:35 2006
New Revision: 391828

URL: http://svn.apache.org/viewcvs?rev=391828&view=rev
Log:
Cleanup.

BB: self
Test: NetUI pass


Modified:
    beehive/trunk/netui/src/util/org/apache/beehive/netui/util/HtmlExceptionFormatter.java
    beehive/trunk/netui/src/util/org/apache/beehive/netui/util/type/TypeUtils.java

Modified: beehive/trunk/netui/src/util/org/apache/beehive/netui/util/HtmlExceptionFormatter.java
URL: http://svn.apache.org/viewcvs/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/HtmlExceptionFormatter.java?rev=391828&r1=391827&r2=391828&view=diff
==============================================================================
--- beehive/trunk/netui/src/util/org/apache/beehive/netui/util/HtmlExceptionFormatter.java (original)
+++ beehive/trunk/netui/src/util/org/apache/beehive/netui/util/HtmlExceptionFormatter.java Wed Apr  5 15:12:35 2006
@@ -17,15 +17,14 @@
  */
 package org.apache.beehive.netui.util;
 
-import org.apache.beehive.netui.util.internal.InternalStringBuilder;
-
 import java.io.PrintWriter;
 import java.io.StringWriter;
-
 import javax.servlet.jsp.JspException;
 import javax.servlet.jsp.el.ELException;
 import javax.servlet.ServletException;
 
+import org.apache.beehive.netui.util.internal.InternalStringBuilder;
+
 /**
  * Format a {@link java.lang.Throwable} so that it displays well in HTML.
  */
@@ -36,7 +35,6 @@
      */
     private static final String HTML_LINE_BREAK = "<br/>";
 
-    /* @TODO: is the formatting of Throwable.printStackTrace() dependent on the platform? */
     /**
      * The end of line character to replace with an HTML line break
      */
@@ -93,7 +91,7 @@
         return buf.toString().replaceAll(LINE_BREAK, HTML_LINE_BREAK);
     }
 
-    private static final String addStackTrace(Throwable t) {
+    private static String addStackTrace(Throwable t) {
         InternalStringBuilder buf = new InternalStringBuilder();
         StringWriter sw = new StringWriter();
         PrintWriter pw = new PrintWriter(sw);
@@ -118,14 +116,11 @@
         return buf.toString();
     }
 
-    private static final boolean hasRootCause(Throwable t) {
-        if(t.getCause() == null && (t instanceof JspException || t instanceof ServletException || t instanceof ELException))
-            return true;
-        else
-            return false;
+    private static boolean hasRootCause(Throwable t) {
+        return t.getCause() == null && (t instanceof JspException || t instanceof ServletException || t instanceof ELException);
     }
 
-    private static final Throwable discoverRootCause(Throwable t) {
+    private static Throwable discoverRootCause(Throwable t) {
         Throwable cause = null;
         if(t instanceof JspException)
             cause = ((JspException)t).getRootCause();
@@ -133,8 +128,7 @@
             cause = ((ServletException)t).getRootCause();
         else if(t instanceof ELException)
             cause = ((ELException)t).getRootCause();
-        else
-            cause = t.getCause();
+        else cause = t.getCause();
 
         return cause;
     }

Modified: beehive/trunk/netui/src/util/org/apache/beehive/netui/util/type/TypeUtils.java
URL: http://svn.apache.org/viewcvs/beehive/trunk/netui/src/util/org/apache/beehive/netui/util/type/TypeUtils.java?rev=391828&r1=391827&r2=391828&view=diff
==============================================================================
--- beehive/trunk/netui/src/util/org/apache/beehive/netui/util/type/TypeUtils.java (original)
+++ beehive/trunk/netui/src/util/org/apache/beehive/netui/util/type/TypeUtils.java Wed Apr  5 15:12:35 2006
@@ -17,8 +17,6 @@
  */
 package org.apache.beehive.netui.util.type;
 
-import org.apache.beehive.netui.util.internal.InternalStringBuilder;
-
 import java.io.InputStream;
 import java.math.BigDecimal;
 import java.math.BigInteger;
@@ -36,6 +34,7 @@
 import org.apache.beehive.netui.util.config.ConfigUtil;
 import org.apache.beehive.netui.util.config.bean.TypeConverterConfig;
 import org.apache.beehive.netui.util.config.bean.NetUIConfig;
+import org.apache.beehive.netui.util.internal.InternalStringBuilder;
 import org.apache.beehive.netui.util.logging.Logger;
 
 /**
@@ -47,24 +46,23 @@
     private static final Logger LOGGER = Logger.getInstance(TypeUtils.class);
     private static final String TYPE_CONVERTER_PROPERTIES = "/properties/netui-typeconverter.properties";
     private static final String EMPTY_STRING = "";
+
     private static final HashMap/*<Class, BaseTypeConverter>*/ TYPE_CONVERTERS = new HashMap/*<Class, BaseTypeConverter>*/();
-    private static String TO_STRING = null;
 
     static {
-        // initialize the default type converters
-        initialize();
+        // Load the default type converters.
+        loadDefaultConverters();
 
-        Map/*<String, String>*/ map = readFromConfig();
-        if(map != null) {
-            load(map);
-            map.clear();
-        }
+        // Load the type converters from the beehive-netui-config.xml file
+        Map/*<String, String>*/ map = null;
+        map = readFromConfig();
+        load(map);
 
+        // Load the type converters from the .properties file.  This supports back compat.
         map = readFromProperties();
-        if(map != null)
-            load(map);
+        load(map);
 
-        LOGGER.info(registeredConvertersToString());
+        LOGGER.info(convertersToString());
     }
 
     /* do not construct */
@@ -94,73 +92,73 @@
      * @return the Object result of converting the String to the type.
      * @throws TypeConverterNotFoundException if a TypeConverter for the target type can not be found.
      */
-    public static final Object convertToObject(String value, Class type, Locale locale) {
+    public static Object convertToObject(String value, Class type, Locale locale) {
         BaseTypeConverter converter = lookupTypeConverter(type);
         assert converter != null;
         return converter.convertToObject(type, value, locale);
     }
 
-    public static final byte convertToByte(String value) {
+    public static byte convertToByte(String value) {
         return ((Byte)convertToObject(value, byte.class, null)).byteValue();
     }
 
-    public static final boolean convertToBoolean(String value) {
+    public static boolean convertToBoolean(String value) {
         return ((Boolean)convertToObject(value, boolean.class, null)).booleanValue();
     }
 
-    public static final char convertToChar(String value) {
+    public static char convertToChar(String value) {
         return ((Character)convertToObject(value, char.class, null)).charValue();
     }
 
-    public static final double convertToDouble(String value) {
+    public static double convertToDouble(String value) {
         return ((Double)convertToObject(value, double.class, null)).doubleValue();
     }
 
-    public static final float convertToFloat(String value) {
+    public static float convertToFloat(String value) {
         return ((Float)convertToObject(value, float.class, null)).floatValue();
     }
 
-    public static final int convertToInt(String value) {
+    public static int convertToInt(String value) {
         return ((Integer)convertToObject(value, int.class, null)).intValue();
     }
 
-    public static final long convertToLong(String value) {
+    public static long convertToLong(String value) {
         return ((Long)convertToObject(value, long.class, null)).longValue();
     }
 
-    public static final short convertToShort(String value) {
+    public static short convertToShort(String value) {
         return ((Short)convertToObject(value, short.class, null)).shortValue();
     }
 
-    public static final Byte convertToByteObject(String value) {
+    public static Byte convertToByteObject(String value) {
         return (Byte)convertToObject(value, Byte.class, null);
     }
 
-    public static final Boolean convertToBooleanObject(String value) {
+    public static Boolean convertToBooleanObject(String value) {
         return (Boolean)convertToObject(value, Boolean.class, null);
     }
 
-    public static final Character convertToCharacterObject(String value) {
+    public static Character convertToCharacterObject(String value) {
         return (Character)convertToObject(value, Character.class, null);
     }
 
-    public static final Double convertToDoubleObject(String value) {
+    public static Double convertToDoubleObject(String value) {
         return (Double)convertToObject(value, Double.class, null);
     }
 
-    public static final Float convertToFloatObject(String value) {
+    public static Float convertToFloatObject(String value) {
         return (Float)convertToObject(value, Float.class, null);
     }
 
-    public static final Integer convertToIntegerObject(String value) {
+    public static Integer convertToIntegerObject(String value) {
         return (Integer)convertToObject(value, Integer.class, null);
     }
 
-    public static final Long convertToLongObject(String value) {
+    public static Long convertToLongObject(String value) {
         return (Long)convertToObject(value, Long.class, null);
     }
 
-    public static final Short convertToShortObject(String value) {
+    public static Short convertToShortObject(String value) {
         return (Short)convertToObject(value, Short.class, null);
     }
 
@@ -171,7 +169,7 @@
      * @return a {@link BaseTypeConverter} to use for conversion
      * @throws TypeConverterNotFoundException if a TypeConverter for the target type can not be found.
      */
-    private static final BaseTypeConverter lookupTypeConverter(Class type) {
+    private static BaseTypeConverter lookupTypeConverter(Class type) {
         BaseTypeConverter converter = (BaseTypeConverter)TYPE_CONVERTERS.get(type);
         if(converter == null) {
             String msg = "Could not find a TypeConverter for converting a String to an object of type \"" +
@@ -191,27 +189,23 @@
         return converter;
     }
 
-    private static String registeredConvertersToString() {
-        if(TO_STRING != null)
-            return TO_STRING;
+    public String toString() {
+        return convertersToString();
+    }
 
+    private static String convertersToString() {
         InternalStringBuilder sb = new InternalStringBuilder(256);
-        sb.append(TypeUtils.class.getName() + " regestered converters (class name, TypeConverter implementation):\n");
-        sb.append(":::::\n");
+        sb.append(TypeUtils.class.getName()).append(" regestered converters (Class name, TypeConverter implementation):\n");
+
+        assert TYPE_CONVERTERS != null;
         Iterator iterator = TYPE_CONVERTERS.keySet().iterator();
         while(iterator.hasNext()) {
             Class key = (Class)iterator.next();
             String keyName = key.getName();
             String value = (TYPE_CONVERTERS.get(key) != null ? TYPE_CONVERTERS.get(key).getClass().getName() : "null");
-            sb.append(keyName);
-            sb.append(", ");
-            sb.append(value);
-            sb.append("\n");
+            sb.append("  ").append(keyName).append(", ").append(value).append("\n");
         }
-        sb.append(":::::\n");
-        TO_STRING = sb.toString();
-
-        return TO_STRING;
+        return sb.toString();
     }
 
     private static Map/*<String, String>*/ readFromProperties() {
@@ -246,7 +240,7 @@
         return map;
     }
 
-    private static final Map/*<String, String>*/ readFromConfig() {
+    private static Map/*<String, String>*/ readFromConfig() {
         NetUIConfig config = ConfigUtil.getConfig();
         if(config == null)
             return null;
@@ -264,11 +258,13 @@
    }
 
     private static void load(Map/*<String, String>*/ map) {
+        if(map == null || map.size() == 0)
+            return;
+
         // load the properties and continue to populate the map
-        for ( Iterator i = map.keySet().iterator(); i.hasNext(); )
-        {
-            String key = (String) i.next();
-            String className = (String) map.get(key);
+        for (Iterator i = map.keySet().iterator(); i.hasNext();) {
+            String key = (String)i.next();
+            String className = (String)map.get(key);
 
             if((key == null || key.equals(EMPTY_STRING)) || (className == null || className.equals(EMPTY_STRING))) {
                 LOGGER.warn("Could not create a TypeConverter for type \"" + key + "\" and TypeConverter \"" + className + "\"");
@@ -276,7 +272,6 @@
             }
 
             Class targetClazz = null;
-
             /* attempt to load the "convert-to" class */
             try {
                 targetClazz = Class.forName(key);
@@ -285,34 +280,32 @@
                 continue;
             }
 
-            Class tcClazz = null;
-            BaseTypeConverter tc = null;
+            Class typeConverterClass = null;
+            BaseTypeConverter typeConverter = null;
             // try to find the TypeConverter implementation
             try {
-                tcClazz = Class.forName(className);
-                Object obj = tcClazz.newInstance();
+                typeConverterClass = Class.forName(className);
+                Object obj = typeConverterClass.newInstance();
 
-                // this supports existing TypeConverter implementations
-                // but allows TypeUtils make calls against the BaseTypeConverter
-                // API, which supports Locale-based conversion
+                // this supports existing TypeConverter implementations but allows TypeUtils make calls against
+                // the BaseTypeConverter API, which supports Locale-based conversion
                 if(obj instanceof TypeConverter)
-                    tc = new DelegatingTypeConverter((TypeConverter)obj);
+                    typeConverter = new DelegatingTypeConverter((TypeConverter)obj);
                 else if(obj instanceof BaseTypeConverter)
-                    tc = (BaseTypeConverter)obj;
-                else throw new IllegalStateException("Attempt to load illegal type converter type: " + tcClazz);
-            } catch(ClassNotFoundException cnf) {
+                    typeConverter = (BaseTypeConverter)obj;
+                else throw new IllegalStateException("Attempt to load illegal type converter type: " + typeConverterClass);
+            }
+            catch(ClassNotFoundException cnf) {
                 LOGGER.warn("Could not create a TypeConverter for type \"" + key + "\" because the TypeConverter implementation class \"" +
-                            (tcClazz != null ? tcClazz.getName() : null) + "\" could not be found.");
-
+                            (typeConverterClass != null ? typeConverterClass.getName() : null) + "\" could not be found.");
                 continue;
             } catch(Exception e) {
-                if(LOGGER.isWarnEnabled())
-                    LOGGER.warn("Could not create a TypeConverter for type \"" + key + "\" because the implementation class \"" +
-                        (tcClazz != null ? tcClazz.getName() : null) + "\" could not be instantiated.");
+                LOGGER.warn("Could not create a TypeConverter for type \"" + key + "\" because the implementation class \"" +
+                    (typeConverterClass != null ? typeConverterClass.getName() : null) + "\" could not be instantiated.");
                 continue;
             }
             
-            /* found two type converters for the same class -- warn */
+            /* oops -- found two type converters for the same class */
             if(TYPE_CONVERTERS.containsKey(targetClazz))
                 if(LOGGER.isWarnEnabled())
                     LOGGER.warn("Overwriting a previously defined TypeConverter named \"" + targetClazz +
@@ -320,9 +313,9 @@
 
             if(LOGGER.isInfoEnabled())
                 LOGGER.info("Adding a type converter; target type=\"" + targetClazz.getName() +
-                    "\" TypeConverter implementation=\"" + tc.getClass().getName() + "\"");
+                    "\" TypeConverter implementation=\"" + typeConverter.getClass().getName() + "\"");
 
-            TYPE_CONVERTERS.put(targetClazz, tc);
+            TYPE_CONVERTERS.put(targetClazz, typeConverter);
         }
     }
 
@@ -332,7 +325,7 @@
      * primitive wrappers, String, and BigDecimal.  Types like BigDecimal are included
      * because JDBC uses these complex types to map SQL types to Java objects.
      */
-    private static void initialize() {
+    private static void loadDefaultConverters() {
         TYPE_CONVERTERS.put(byte.class, new BaseTypeConverter() {
             public Object convertToObject(Class type, String value, Locale locale) {
                 return (value == null || value.equals(EMPTY_STRING) ? new Byte((byte)0) : new Byte(value.trim()));