empire-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From doeb...@apache.org
Subject svn commit: r1382189 - in /empire-db/trunk: empire-db-jsf2/src/main/java/org/apache/empire/jsf2/app/ empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pages/ empire-db/src/main/java/org/apache/empire/exceptions/
Date Fri, 07 Sep 2012 22:01:48 GMT
Author: doebele
Date: Fri Sep  7 22:01:47 2012
New Revision: 1382189

URL: http://svn.apache.org/viewvc?rev=1382189&view=rev
Log:
EMPIREDB-157 
Allow error messages to have a variable amount of parameters

Modified:
    empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/app/TextResolver.java
    empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pages/Page.java
    empire-db/trunk/empire-db/src/main/java/org/apache/empire/exceptions/EmpireException.java

Modified: empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/app/TextResolver.java
URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/app/TextResolver.java?rev=1382189&r1=1382188&r2=1382189&view=diff
==============================================================================
--- empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/app/TextResolver.java
(original)
+++ empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/app/TextResolver.java
Fri Sep  7 22:01:47 2012
@@ -18,7 +18,6 @@
  */
 package org.apache.empire.jsf2.app;
 
-import java.text.MessageFormat;
 import java.util.Locale;
 import java.util.MissingResourceException;
 import java.util.ResourceBundle;
@@ -99,16 +98,14 @@ public class TextResolver
                 pattern = ee.getErrorType().getMessagePattern();
                 log.error("Error resolving error messsage pattern: {}", key);
             }
-            // get Params
+            // get Params and translate
             String[] params = ee.getErrorParams();
-            Object[] values = null;
             if (params!=null)
-            {   values = new Object[params.length];
-                for (int i=0; i<params.length; i++)
-                    values[i] = resolveText(params[i]);
+            {   for (int i=0; i<params.length; i++)
+                    params[i] = resolveText(params[i]);
             }
             // Format message
-            return MessageFormat.format(pattern, values);            
+            return EmpireException.formatErrorMessage(ee.getErrorType(), pattern, params);
         }
         else
         {   // Other exception try to resolve by class name

Modified: empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pages/Page.java
URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pages/Page.java?rev=1382189&r1=1382188&r2=1382189&view=diff
==============================================================================
--- empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pages/Page.java (original)
+++ empire-db/trunk/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pages/Page.java Fri
Sep  7 22:01:47 2012
@@ -278,7 +278,8 @@ public abstract class Page implements Se
     {
         String msg = getErrorMessage(e);
         String detail = extractErrorMessageDetail(action, e, 1);
-        log.error(msg + "\r\n" + detail, e);
+        if (log.isDebugEnabled())
+            log.debug(msg + "\r\n" + detail, e);
         FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, detail);
         FacesContext.getCurrentInstance().addMessage(getPageName(), facesMsg);
     }

Modified: empire-db/trunk/empire-db/src/main/java/org/apache/empire/exceptions/EmpireException.java
URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db/src/main/java/org/apache/empire/exceptions/EmpireException.java?rev=1382189&r1=1382188&r2=1382189&view=diff
==============================================================================
--- empire-db/trunk/empire-db/src/main/java/org/apache/empire/exceptions/EmpireException.java
(original)
+++ empire-db/trunk/empire-db/src/main/java/org/apache/empire/exceptions/EmpireException.java
Fri Sep  7 22:01:47 2012
@@ -40,18 +40,53 @@ public class EmpireException extends Run
     private final String[]  errorParams;
     // private final String errorSourceClassname;
     
-    private static String formatErrorMessage(final ErrorType errType, final String[] params)
+    public static String formatErrorMessage(final ErrorType errType, String pattern, final
String[] params)
     {
-        // Check parameter count
-        int paramCount = (params!=null) ? params.length : 0;
-        if (paramCount!= errType.getNumParams())
-        {   // Number of parameters does not match
-            log.warn("Invalid Number of arguments supplied for error " + errType.getKey()

-                   + "\nArguments supplied= " + String.valueOf(paramCount) + "; Arguments
expected= " + String.valueOf(errType.getNumParams()));
-        }
         // Log Error
-        String msg = MessageFormat.format(errType.getMessagePattern(), (Object[])params);
-        return msg;
+        try {
+            // the pattern
+            if (pattern==null)
+                pattern=errType.getMessagePattern();
+            // init format args
+            Object[] formatArgs = (Object[])params;
+            // Check parameter count
+            int patParamCount = errType.getNumParams();
+            int paramCount = (params!=null) ? params.length : 0;            
+            if (paramCount < patParamCount)
+            {   // Number of parameters does not match
+                log.warn("Invalid Number of arguments supplied for error " + errType.getKey()

+                       + "\nArguments supplied= " + String.valueOf(paramCount) + "; Arguments
expected= " + String.valueOf(errType.getNumParams()));
+                // Return the pattern
+                return pattern;
+            }
+            // more params than expected
+            else if (paramCount>patParamCount)
+            {   // Wildcard for the rest
+                if (pattern.contains("{*}")) 
+                {   pattern = pattern.replace("{*}", "{"+String.valueOf(patParamCount)+"}");
+                    patParamCount++;
+                }
+                // Build new array
+                formatArgs = new String[patParamCount];
+                int i=0;
+                for (; i<patParamCount-1; i++)
+                    formatArgs[i]=params[i];
+                // Build a array for the rest
+                StringBuilder b = new StringBuilder();
+                for (;i<paramCount;i++)
+                {   if (b.length()>0)
+                        b.append(", ");
+                    b.append(String.valueOf(params[i]));
+                }
+                formatArgs[patParamCount-1]=b.toString();
+            }
+            // format now
+            String msg = MessageFormat.format(pattern, formatArgs);
+            return msg;
+        } catch(Exception e) {
+            log.error("Unable to format error message: "+pattern, e);
+            return pattern;
+        }
     }
     
     /**
@@ -62,7 +97,7 @@ public class EmpireException extends Run
      */
     protected EmpireException(final ErrorType errType, final String[] params, final Throwable
cause)
     {
-        super(formatErrorMessage(errType, params), cause);
+        super(formatErrorMessage(errType, null, params), cause);
         // save type and params for custom message formatting
         this.errorType = errType;
         this.errorParams = params;



Mime
View raw message