cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From anto...@apache.org
Subject cvs commit: cocoon-2.1/src/java/org/apache/cocoon/util/log XMLCocoonLogFormatter.java CocoonTargetFactory.java CocoonLogFormatter.java ExtensiblePatternFormatter.java
Date Sun, 28 Mar 2004 10:27:20 GMT
antonio     2004/03/28 02:27:20

  Modified:    src/java/org/apache/cocoon/util/log
                        XMLCocoonLogFormatter.java CocoonTargetFactory.java
                        CocoonLogFormatter.java
                        ExtensiblePatternFormatter.java
  Log:
  Using Apache.commons.lang
  
  Revision  Changes    Path
  1.4       +39 -60    cocoon-2.1/src/java/org/apache/cocoon/util/log/XMLCocoonLogFormatter.java
  
  Index: XMLCocoonLogFormatter.java
  ===================================================================
  RCS file: /home/cvs//cocoon-2.1/src/java/org/apache/cocoon/util/log/XMLCocoonLogFormatter.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- XMLCocoonLogFormatter.java	5 Mar 2004 13:03:01 -0000	1.3
  +++ XMLCocoonLogFormatter.java	28 Mar 2004 10:27:20 -0000	1.4
  @@ -18,8 +18,11 @@
   import org.apache.avalon.framework.CascadingThrowable;
   import org.apache.cocoon.environment.ObjectModelHelper;
   import org.apache.cocoon.environment.Request;
  +import org.apache.commons.lang.ClassUtils;
  +import org.apache.commons.lang.SystemUtils;
   import org.apache.log.ContextMap;
   import org.apache.log.LogEvent;
  +import org.apache.log.Logger;
   import org.apache.log.format.Formatter;
   
   import java.io.StringWriter;
  @@ -61,8 +64,7 @@
    * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
    * @version CVS $Id$
    */
  -public class XMLCocoonLogFormatter
  -implements Formatter {
  +public class XMLCocoonLogFormatter implements Formatter {
   
       protected final static String  TYPE_CLASS_STR       = "class";
       protected final static String  TYPE_CLASS_SHORT_STR = "short";
  @@ -91,7 +93,6 @@
            "thread",   // 9
            "host"};   // 10
   
  -    protected final static String EOL = System.getProperty("line.separator", "\n");
       protected final SimpleDateFormat dateFormatter = new SimpleDateFormat("(yyyy-MM-dd)
HH:mm.ss:SSS");
   
       protected int[] types;
  @@ -104,54 +105,47 @@
        */
       public String format( final LogEvent event ) {
           final StringBuffer sb = new StringBuffer();
  -        sb.append("<log-entry>").append(EOL);
  +        sb.append("<log-entry>").append(SystemUtils.LINE_SEPARATOR);
           final String value = this.getRequestId(event.getContextMap());
           if (value != null) {
  -            sb.append("<request-id>").append(value).append("</request-id>").append(EOL);
  +            sb.append("<request-id>").append(value).append("</request-id>").append(SystemUtils.LINE_SEPARATOR);
           }
           for(int i = 0; i < this.types.length; i++) {
  -
               switch(this.types[i]) {
  -
                   case TYPE_REQUEST_URI:
                       sb.append("<uri>");
                       sb.append(this.getURI(event.getContextMap()));
  -                    sb.append("</uri>").append(EOL);
  +                    sb.append("</uri>").append(SystemUtils.LINE_SEPARATOR);
                       break;
  -
                   case TYPE_CLASS:
                       sb.append("<class>");
  -                    sb.append(this.getClass(TYPE_CLASS_STR));
  -                    sb.append("</class>").append(EOL);
  +                    sb.append(this.getClass(TYPE_CLASS));
  +                    sb.append("</class>").append(SystemUtils.LINE_SEPARATOR);
                       break;
                   case TYPE_CLASS_SHORT:
                       sb.append("<class>");
  -                    sb.append(this.getClass(TYPE_CLASS_SHORT_STR));
  -                    sb.append("</class>").append(EOL);
  +                    sb.append(this.getClass(TYPE_CLASS_SHORT));
  +                    sb.append("</class>").append(SystemUtils.LINE_SEPARATOR);
                       break;
  -
                   case TYPE_THREAD:
                       sb.append("<thread>");
                       sb.append(this.getThread(event.getContextMap()));
  -                    sb.append("</thread>").append(EOL);
  +                    sb.append("</thread>").append(SystemUtils.LINE_SEPARATOR);
                       break;
  -
                   case TYPE_RELATIVE_TIME:
                       sb.append("<relative-time>");
                       sb.append(event.getRelativeTime());
  -                    sb.append("</relative-time>").append(EOL);
  +                    sb.append("</relative-time>").append(SystemUtils.LINE_SEPARATOR);
                       break;
  -
                   case TYPE_TIME:
                       sb.append("<time>");
                       sb.append(dateFormatter.format(new Date(event.getTime())));
  -                    sb.append("</time>").append(EOL);
  +                    sb.append("</time>").append(SystemUtils.LINE_SEPARATOR);
                       break;
  -
                   case TYPE_THROWABLE:
                       Throwable throwable = event.getThrowable();
                       if (throwable != null) {
  -                        sb.append("<throwable><![CDATA[").append(EOL);
  +                        sb.append("<throwable><![CDATA[").append(SystemUtils.LINE_SEPARATOR);
                           while (throwable != null) {
                               final StringWriter sw = new StringWriter();
                               throwable.printStackTrace( new java.io.PrintWriter( sw ) );
  @@ -162,28 +156,24 @@
                                   throwable = null;
                               }
                           }
  -                        sb.append(EOL).append("]]> </throwable>").append(EOL);
  +                        sb.append(SystemUtils.LINE_SEPARATOR).append("]]> </throwable>").append(SystemUtils.LINE_SEPARATOR);
                       }
                       break;
  -
                   case TYPE_MESSAGE:
  -                    sb.append("<message><![CDATA[").append(EOL);
  +                    sb.append("<message><![CDATA[").append(SystemUtils.LINE_SEPARATOR);
                       sb.append(event.getMessage());
  -                    sb.append(EOL).append("]]> </message>").append(EOL);
  +                    sb.append(SystemUtils.LINE_SEPARATOR).append("]]> </message>").append(SystemUtils.LINE_SEPARATOR);
                       break;
  -
                   case TYPE_CATEGORY:
                       sb.append("<category>");
                       sb.append(event.getCategory());
  -                    sb.append("</category>").append(EOL);
  +                    sb.append("</category>").append(SystemUtils.LINE_SEPARATOR);
                       break;
  -
                   case TYPE_PRIORITY:
                       sb.append("<priority>");
                       sb.append(event.getPriority().getName());
  -                    sb.append("</priority>").append(EOL);
  +                    sb.append("</priority>").append(SystemUtils.LINE_SEPARATOR);
                       break;
  -                
                   case TYPE_HOST:
                       sb.append("<host>");
                       sb.append(getHost(event.getContextMap()));
  @@ -192,7 +182,7 @@
               }
           }
           sb.append("</log-entry>");
  -        sb.append(EOL);
  +        sb.append(SystemUtils.LINE_SEPARATOR);
           return sb.toString();
       }
   
  @@ -213,13 +203,12 @@
                   }
               }
           }
  -
           return result;
       }
   
       private String getHost(ContextMap ctxMap) {
           String result = "Unknown-host";
  -        
  +
           if (ctxMap != null) {
               Object context = ctxMap.get("objectModel");
               if (context != null && context instanceof Map) {
  @@ -230,7 +219,6 @@
                   }
               }
           }
  -        
           return result;
       }
       
  @@ -243,16 +231,17 @@
           // Get URI from the the object model.
           if (ctxMap != null) {
               Object context = ctxMap.get("request-id");
  -            if (context != null) result = context.toString();
  +            if (context != null) {
  +                result = context.toString();
  +            }
           }
  -
           return result;
       }
   
       /**
        * Finds the class that has called Logger.
        */
  -    private String getClass(String format) {
  +    private String getClass(int format) {
   
           Class[] stack = this.callStack.get();
   
  @@ -264,13 +253,9 @@
                   String className = stack[i+1].getName();
   
                   // Handle optional format
  -                if (TYPE_CLASS_SHORT_STR.equalsIgnoreCase(format))
  -                {
  -                    int pos = className.lastIndexOf('.');
  -                    if (pos >= 0)
  -                        className = className.substring(pos + 1);
  +                if (format  == TYPE_CLASS_SHORT) {
  +                    className = ClassUtils.getShortClassName(className);
                   }
  -
                   return className;
               }
           }
  @@ -297,31 +282,26 @@
        * @param type the string
        * @return the type-id
        */
  -    protected int getTypeIdFor( final String type ) {
  -        int index = 0;
  -        boolean found = false;
  -        while (!found && index < typeStrings.length) {
  +    protected int getTypeIdFor(final String type) {
  +        for (int index = 0; index < typeStrings.length; index++) {
               if (type.equalsIgnoreCase(typeStrings[index])) {
  -                found = true;
  -            } else {
  -                index++;
  +                return index;
               }
           }
  -        if (found) return index;
           throw new IllegalArgumentException( "Unknown Type - " + type );
       }
   
       /**
        * Set the types from an array of strings.
        */
  -    public void setTypes(String [] typeStrings) {
  -        if (typeStrings == null) {
  -            this.types = new int[0];
  -        } else {
  +    public void setTypes(String[] typeStrings) {
  +        if (typeStrings != null) {
               this.types = new int[typeStrings.length];
  -            for(int i = 0; i < typeStrings.length; i++) {
  +            for (int i = 0; i < typeStrings.length; i++) {
                   this.types[i] = this.getTypeIdFor(typeStrings[i]);
               }
  +        } else {
  +            this.types = new int[0];
           }
       }
   
  @@ -342,7 +322,7 @@
       }
   
       /** The class that we will search for in the call stack */
  -    private Class loggerClass = org.apache.log.Logger.class;
  +    private Class loggerClass = Logger.class;
       private CallStack callStack = new CallStack();
   
       /**
  @@ -361,8 +341,7 @@
            *
            * @return current execution stack as an array of classes.
            */
  -        public Class[] get()
  -        {
  +        public Class[] get() {
               return getClassContext();
           }
       }
  
  
  
  1.3       +4 -9      cocoon-2.1/src/java/org/apache/cocoon/util/log/CocoonTargetFactory.java
  
  Index: CocoonTargetFactory.java
  ===================================================================
  RCS file: /home/cvs//cocoon-2.1/src/java/org/apache/cocoon/util/log/CocoonTargetFactory.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- CocoonTargetFactory.java	5 Mar 2004 13:03:01 -0000	1.2
  +++ CocoonTargetFactory.java	28 Mar 2004 10:27:20 -0000	1.3
  @@ -102,26 +102,21 @@
       private static final String XFORMAT =
           "priority time category uri thread class message throwable";
   
  -    protected Formatter getFormatter(final Configuration conf)
  -    {
  +    protected Formatter getFormatter(final Configuration conf) {
           final String type = conf.getAttribute("type", "unknown");
   
  -        if ("cocoon".equals(type))
  -        {
  +        if ("cocoon".equals(type)) {
               int depth = conf.getAttributeAsInteger( "depth", 0 );
               final CocoonLogFormatter formatter = new CocoonLogFormatter( depth );
               final String format = conf.getValue(CFORMAT);
               formatter.setFormat(format);
               return formatter;
  -        }
  -        else if ("xml".equals(type))
  -        {
  +        } else if ("xml".equals(type)) {
               final XMLCocoonLogFormatter formatter = new XMLCocoonLogFormatter();
               final String format = conf.getValue(XFORMAT);
               formatter.setTypes(format);
               return formatter;
           }
  -
           // default formatter
           return super.getFormatter(conf);
       }
  
  
  
  1.4       +29 -39    cocoon-2.1/src/java/org/apache/cocoon/util/log/CocoonLogFormatter.java
  
  Index: CocoonLogFormatter.java
  ===================================================================
  RCS file: /home/cvs//cocoon-2.1/src/java/org/apache/cocoon/util/log/CocoonLogFormatter.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- CocoonLogFormatter.java	5 Mar 2004 13:03:01 -0000	1.3
  +++ CocoonLogFormatter.java	28 Mar 2004 10:27:20 -0000	1.4
  @@ -20,10 +20,13 @@
   import java.util.Map;
   
   import org.apache.avalon.framework.ExceptionUtil;
  +import org.apache.avalon.framework.logger.LogKitLogger;
   import org.apache.cocoon.environment.ObjectModelHelper;
   import org.apache.cocoon.environment.Request;
  +import org.apache.commons.lang.ClassUtils;
   import org.apache.log.ContextMap;
   import org.apache.log.LogEvent;
  +import org.apache.log.Logger;
   
   /**
    * An extended pattern formatter. New patterns are defined by this class are :
  @@ -68,8 +71,7 @@
        * SecurityManager class provides it as a protected method, so
        * change it to public through a new method !
        */
  -    static public class CallStack extends SecurityManager
  -    {
  +    static public class CallStack extends SecurityManager {
           /**
            * Returns the current execution stack as an array of classes.
            * The length of the array is the number of methods on the execution
  @@ -79,8 +81,7 @@
            *
            * @return current execution stack as an array of classes.
            */
  -        public Class[] get()
  -        {
  +        public Class[] get() {
               return getClassContext();
           }
       }
  @@ -89,13 +90,13 @@
        * The class that we will search for in the call stack
        * (Avalon logging abstraction)
        */
  -    private Class logkitClass = org.apache.avalon.framework.logger.LogKitLogger.class;
  +    private Class logkitClass = LogKitLogger.class;
   
       /**
        * The class that we will search for in the call stack
        * (LogKit logger)
        */
  -    private Class loggerClass = org.apache.log.Logger.class;
  +    private Class loggerClass = Logger.class;
   
       private CallStack callStack = new CallStack();
   
  @@ -116,36 +117,32 @@
   
           // Search for new patterns defined here, or else delegate
           // to the parent class
  -        if (type.equalsIgnoreCase(TYPE_CLASS_STR))
  +        if (type.equalsIgnoreCase(TYPE_CLASS_STR)) {
               return TYPE_CLASS;
  -        else if (type.equalsIgnoreCase(TYPE_URI_STR))
  +        } else if (type.equalsIgnoreCase(TYPE_URI_STR)) {
               return TYPE_URI;
  -        else if (type.equalsIgnoreCase(TYPE_THREAD_STR))
  +        } else if (type.equalsIgnoreCase(TYPE_THREAD_STR)) {
               return TYPE_THREAD;
  -        else if (type.equalsIgnoreCase(TYPE_HOST_STR))
  +        } else if (type.equalsIgnoreCase(TYPE_HOST_STR)) {
               return TYPE_HOST;
  -        else
  -            return super.getTypeIdFor( type );
  +        } else {
  +            return super.getTypeIdFor(type);
  +        }
       }
   
       protected String formatPatternRun(LogEvent event, PatternRun run) {
  -
           // Format new patterns defined here, or else delegate to
           // the parent class
           switch (run.m_type) {
               case TYPE_CLASS :
                   return getClass(run.m_format);
  -
               case TYPE_URI :
                   return getURI(event.getContextMap());
  -
               case TYPE_THREAD :
                   return getThread(event.getContextMap());
  -            
               case TYPE_HOST :
                   return getHost(event.getContextMap());
           }
  -
           return super.formatPatternRun(event, run);
       }
   
  @@ -153,29 +150,21 @@
        * Finds the class that has called Logger.
        */
       private String getClass(String format) {
  -
           Class[] stack = this.callStack.get();
   
           // Traverse the call stack in reverse order until we find a Logger
  -        for (int i = stack.length-1; i >= 0; i--) {
  +        for (int i = stack.length - 1; i >= 0; i--) {
               if (this.logkitClass.isAssignableFrom(stack[i]) ||
  -                this.loggerClass.isAssignableFrom(stack[i]))
  -            {
  +                this.loggerClass.isAssignableFrom(stack[i])) {
                   // Found : the caller is the previous stack element
                   String className = stack[i+1].getName();
  -
                   // Handle optional format
  -                if (TYPE_CLASS_SHORT_STR.equalsIgnoreCase(format))
  -                {
  -                    int pos = className.lastIndexOf('.');
  -                    if (pos >= 0)
  -                        className = className.substring(pos + 1);
  +                if (TYPE_CLASS_SHORT_STR.equalsIgnoreCase(format)) {
  +                    className = ClassUtils.getShortClassName(className);
                   }
  -
                   return className;
               }
           }
  -
           // No Logger found in call stack : can occur with AsyncLogTarget
           // where formatting takes place in a different thread.
           return "Unknown-class";
  @@ -198,7 +187,6 @@
                   }
               }
           }
  -
           return result;
       }
   
  @@ -219,7 +207,6 @@
                   }
               }
           }
  -
           return result;
       }
   
  @@ -228,10 +215,11 @@
        * Find the thread that is logged this event.
        */
       private String getThread(ContextMap ctxMap) {
  -        if (ctxMap != null && ctxMap.get("threadName") != null)
  +        if (ctxMap != null && ctxMap.get("threadName") != null) {
               return (String)ctxMap.get("threadName");
  -        else
  +        } else {
               return "Unknown-thread";
  +        }
       }
   
       /**
  @@ -245,9 +233,12 @@
        * @param format ancilliary format parameter - allowed to be null
        * @return the formatted string
        */
  -    protected String getStackTrace( final Throwable throwable, final String format )
  -    {
  -        return throwable == null ? null : ExceptionUtil.printStackTrace(throwable,m_stackDepth);
  +    protected String getStackTrace(final Throwable throwable, final String format) {
  +        if (throwable != null) {
  +            return ExceptionUtil.printStackTrace(throwable, m_stackDepth);
  +        } else {
  +            return null;
  +        }
       }
   
       /**
  @@ -257,8 +248,7 @@
        * @param format ancilliary format parameter - allowed to be null
        * @return the formatted string
        */
  -    protected String getTime( final long time, final String format )
  -    {
  +    protected String getTime(final long time, final String format) {
           return this.dateFormatter.format(new Date());
       }
   }
  
  
  
  1.5       +116 -235  cocoon-2.1/src/java/org/apache/cocoon/util/log/ExtensiblePatternFormatter.java
  
  Index: ExtensiblePatternFormatter.java
  ===================================================================
  RCS file: /home/cvs//cocoon-2.1/src/java/org/apache/cocoon/util/log/ExtensiblePatternFormatter.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- ExtensiblePatternFormatter.java	5 Mar 2004 13:03:01 -0000	1.4
  +++ ExtensiblePatternFormatter.java	28 Mar 2004 10:27:20 -0000	1.5
  @@ -18,6 +18,8 @@
   import java.io.StringWriter;
   import java.util.Stack;
   
  +import org.apache.commons.lang.StringUtils;
  +import org.apache.commons.lang.SystemUtils;
   import org.apache.log.LogEvent;
   import org.apache.log.Priority;
   import org.apache.log.format.Formatter;
  @@ -26,8 +28,7 @@
   /**
    * A refactoring of <code>org.apache.log.format.PatternFormatter</code> that
    * can be extended.
  - * This formater formats the LogEntries according to a input pattern
  - * string.
  + * This formater formats the LogEntries according to a input pattern string.
    *
    * The format of each pattern element can be %[+|-]#.#{field:subformat}
    *
  @@ -58,7 +59,6 @@
        */
       protected final static int         MAX_TYPE             = 8;
   
  -
       protected final static String      TYPE_CATEGORY_STR      = "category";
       protected final static String      TYPE_MESSAGE_STR       = "message";
       protected final static String      TYPE_TIME_STR          = "time";
  @@ -66,16 +66,7 @@
       protected final static String      TYPE_THROWABLE_STR     = "throwable";
       protected final static String      TYPE_PRIORITY_STR      = "priority";
   
  -    protected final static String      SPACE_16               = "                ";
  -    protected final static String      SPACE_8                = "        ";
  -    protected final static String      SPACE_4                = "    ";
  -    protected final static String      SPACE_2                = "  ";
  -    protected final static String      SPACE_1                = " ";
  -
  -    protected final static String      EOL                    = System.getProperty("line.separator",
"\n");
  -
  -    protected static class PatternRun
  -    {
  +    protected static class PatternRun {
           public String    m_data;
           public boolean   m_rightJustify;
           public int       m_minSize;
  @@ -84,7 +75,7 @@
           public String    m_format;
       }
   
  -    protected PatternRun                      m_formatSpecification[];
  +    protected PatternRun m_formatSpecification[];
   
       /**
        * Extract and build a pattern from input string.
  @@ -94,42 +85,33 @@
        * @param index the start of pattern run
        * @return the number of characters in pattern run
        */
  -    protected int addPatternRun( final Stack stack,
  -                                 final char pattern[],
  -                                 int index )
  -    {
  +    protected int addPatternRun(final Stack stack, final char pattern[], int index) {
           final PatternRun run = new PatternRun();
           final int start = index++;
   
  -        //first check for a +|- sign
  -        if( '+' == pattern[ index ] ) index++;
  -        else if( '-' == pattern[ index ] )
  -        {
  +        // first check for a +|- sign
  +        if ('+' == pattern[index]) {
  +            index++;
  +        } else if ('-' == pattern[index]) {
               run.m_rightJustify = true;
               index++;
           }
   
  -        if( Character.isDigit( pattern[ index ] ))
  -        {
  +        if (Character.isDigit(pattern[index])) {
               int total = 0;
  -            while( Character.isDigit( pattern[ index ] ) )
  -            {
  -                total = total * 10 + (pattern[ index ] - '0');
  +            while (Character.isDigit(pattern[index])) {
  +                total = total * 10 + (pattern[index] - '0');
                   index++;
               }
               run.m_minSize = total;
           }
   
           //check for . sign indicating a maximum is to follow
  -        if( index < pattern.length && '.' == pattern[ index ] )
  -        {
  +        if (index < pattern.length && '.' == pattern[index]) {
               index++;
  -
  -            if( Character.isDigit( pattern[ index ] ))
  -            {
  +            if (Character.isDigit(pattern[index])) {
                   int total = 0;
  -                while( Character.isDigit( pattern[ index ] ) )
  -                {
  +                while (Character.isDigit(pattern[index])) {
                       total = total * 10 + (pattern[ index ] - '0');
                       index++;
                   }
  @@ -137,52 +119,42 @@
               }
           }
   
  -        if( index >= pattern.length || '{' != pattern[ index ] )
  -        {
  -            throw
  -                new IllegalArgumentException( "Badly formed pattern at character " +
  -                                              index );
  +        if (index >= pattern.length || '{' != pattern[index]) {
  +            throw new IllegalArgumentException(
  +                    "Badly formed pattern at character " + index );
           }
   
           int typeStart = index;
   
  -        while( index < pattern.length &&
  -               pattern[ index ]!= ':' && pattern[ index ] != '}' )
  -        {
  +        while (index < pattern.length &&
  +               pattern[index]!= ':' && pattern[index] != '}' ) {
               index++;
           }
   
           int typeEnd = index - 1;
   
  -        final String type =
  -            new String( pattern, typeStart + 1, typeEnd - typeStart );
  +        final String type = new String(pattern, typeStart + 1, typeEnd - typeStart);
   
           run.m_type = getTypeIdFor( type );
   
  -        if( index < pattern.length && pattern[ index ] == ':' )
  -        {
  +        if (index < pattern.length && pattern[index] == ':' ) {
               index++;
  -            while( index < pattern.length && pattern[ index ] != '}' ) index++;
  -
  +            while (index < pattern.length && pattern[index] != '}' ) {
  +                index++;
  +            }
               final int length = index - typeEnd - 2;
   
  -            if( 0 != length )
  -            {
  -                run.m_format = new String( pattern, typeEnd + 2, length );
  +            if (0 != length) {
  +                run.m_format = new String(pattern, typeEnd + 2, length);
               }
           }
   
  -        if( index >= pattern.length || '}' != pattern[ index ] )
  -        {
  -            throw new
  -                IllegalArgumentException("Unterminated type in pattern at character "
  -                                         + index );
  +        if (index >= pattern.length || '}' != pattern[index]) {
  +            throw new IllegalArgumentException(
  +                    "Unterminated type in pattern at character " + index );
           }
  -
           index++;
  -
           stack.push( run );
  -
           return index - start;
       }
   
  @@ -196,37 +168,35 @@
        * @param index the start of the text run
        * @return the number of characters in run
        */
  -    protected int addTextRun( final Stack stack,
  -                              final char pattern[],
  -                              int index )
  -    {
  +    protected int addTextRun( final Stack stack, final char pattern[], int index ) {
           final PatternRun run = new PatternRun();
           final int start = index;
           boolean escapeMode = false;
   
  -        if( '%' == pattern[ index ] ) index++;
  -
  +        if ('%' == pattern[index]) {
  +            index++;
  +        }
           final StringBuffer sb = new StringBuffer();
  -
  -        while( index < pattern.length && pattern[ index ] != '%' )
  -        {
  -            if( escapeMode )
  -            {
  -                if( 'n' == pattern[ index ] ) sb.append( EOL );
  -                else if( 't' == pattern[ index ] ) sb.append( '\t' );
  -                else sb.append( pattern[ index ] );
  +        while (index < pattern.length && pattern[index] != '%') {
  +            if (escapeMode) {
  +                if ('n' == pattern[ index ]) {
  +                    sb.append( SystemUtils.LINE_SEPARATOR );
  +                } else if ('t' == pattern[ index ]) {
  +                    sb.append( '\t' );
  +                } else {
  +                    sb.append( pattern[ index ] );
  +                }
                   escapeMode = false;
  +            } else if ('\\' == pattern[ index ]) {
  +                escapeMode = true;
  +            } else {
  +                sb.append( pattern[ index ] );
               }
  -            else if( '\\' == pattern[ index ] ) escapeMode = true;
  -            else sb.append( pattern[ index ] );
               index++;
           }
  -
           run.m_data = sb.toString();
           run.m_type = TYPE_TEXT;
  -
  -        stack.push( run );
  -
  +        stack.push(run);
           return index - start;
       }
   
  @@ -239,81 +209,22 @@
        * @param rightJustify true if the string is to be right justified in it's box.
        * @param output the input string
        */
  -    protected void append( final StringBuffer sb,
  -                           final int minSize,
  -                           final int maxSize,
  -                           final boolean rightJustify,
  -                           final String output )
  -    {
  -        final int size = output.length();
  -
  -        if( size < minSize )
  -        {
  -            //assert( minSize > 0 );
  -            if( rightJustify )
  -            {
  -                appendWhiteSpace( sb, minSize - size );
  -                sb.append( output );
  -            }
  -            else
  -            {
  -                sb.append( output );
  -                appendWhiteSpace( sb, minSize - size );
  +    protected void append(final StringBuffer sb, final int minSize, final int maxSize,
  +            final boolean rightJustify, final String output) {
  +        if (output.length() < minSize) {
  +            if (rightJustify) {
  +                sb.append(StringUtils.leftPad(output, minSize));
  +            } else {
  +                sb.append(StringUtils.rightPad(output, minSize));
  +            }
  +        } else if (maxSize > 0) {
  +            if (rightJustify) {
  +                sb.append(StringUtils.right(output, maxSize));
  +            } else {
  +                sb.append(StringUtils.left(output, maxSize));
               }
  -        }
  -        else if( maxSize > 0 && maxSize < size )
  -        {
  -            if( rightJustify )
  -            {
  -                sb.append( output.substring( size - maxSize ) );
  -            }
  -            else
  -        {
  -            sb.append( output.substring( 0, maxSize ) );
  -        }
  -        }
  -        else
  -        {
  -            sb.append( output );
  -        }
  -    }
  -
  -    /**
  -     * Append a certain number of whitespace characters to a StringBuffer.
  -     *
  -     * @param sb the StringBuffer
  -     * @param length the number of spaces to append
  -     */
  -    protected void appendWhiteSpace( final StringBuffer sb, int length )
  -    {
  -        while( length >= 16 )
  -        {
  -            sb.append( SPACE_16 );
  -            length -= 16;
  -        }
  -
  -        if( length >= 8 )
  -        {
  -            sb.append( SPACE_8 );
  -            length -= 8;
  -        }
  -
  -        if( length >= 4 )
  -        {
  -            sb.append( SPACE_4 );
  -            length -= 4;
  -        }
  -
  -        if( length >= 2 )
  -        {
  -            sb.append( SPACE_2 );
  -            length -= 2;
  -        }
  -
  -        if( length >= 1 )
  -        {
  -            sb.append( SPACE_1 );
  -            length -= 1;
  +        } else {
  +            sb.append(output);
           }
       }
   
  @@ -323,30 +234,21 @@
        * @param event the event
        * @return the formatted output
        */
  -    public String format( final LogEvent event )
  -    {
  +    public String format(final LogEvent event) {
           final StringBuffer sb = new StringBuffer();
   
  -        for( int i = 0; i < m_formatSpecification.length; i++ )
  -        {
  -            final PatternRun run =  m_formatSpecification[ i ];
  -
  +        for( int i = 0; i < m_formatSpecification.length; i++ ) {
  +            final PatternRun run =  m_formatSpecification[i];
               //treat text differently as it doesn't need min/max padding
  -            if ( run.m_type == TYPE_TEXT )
  -            {
  -                sb.append( run.m_data );
  -            }
  -            else
  -            {
  -                final String data = formatPatternRun( event, run );
  -
  -                if( null != data )
  -                {
  -                    append( sb, run.m_minSize, run.m_maxSize, run.m_rightJustify, data
);
  +            if (run.m_type == TYPE_TEXT) {
  +                sb.append(run.m_data);
  +            } else {
  +                final String data = formatPatternRun(event, run);
  +                if (null != data) {
  +                    append(sb, run.m_minSize, run.m_maxSize, run.m_rightJustify, data);
                   }
               }
           }
  -
           return sb.toString();
       }
   
  @@ -360,36 +262,29 @@
       {
           String str = null;
   
  -        switch( run.m_type )
  +        switch (run.m_type)
           {
               case TYPE_RELATIVE_TIME:
                   str = getTime( event.getRelativeTime(), run.m_format );
                   break;
  -
               case TYPE_TIME:
                   str = getTime( event.getTime(), run.m_format );
                   break;
  -
               case TYPE_THROWABLE:
                   str = getStackTrace( event.getThrowable(), run.m_format );
                   break;
  -
               case TYPE_MESSAGE:
                   str = getMessage( event.getMessage(), run.m_format );
                   break;
  -
               case TYPE_CATEGORY:
                   str = getCategory( event.getCategory(), run.m_format );
                   break;
  -
               case TYPE_PRIORITY:
                   str = getPriority( event.getPriority(), run.m_format );
                   break;
  -
               default:
                   new DefaultErrorHandler().error("Unknown Pattern specification." + run.m_type,
null, null);
           }
  -
           return str;
       }
   
  @@ -400,16 +295,14 @@
        * @param format ancilliary format parameter - allowed to be null
        * @return the formatted string
        */
  -    protected String getCategory( final String category, final String format )
  -    {
  +    protected String getCategory(final String category, final String format) {
           return category;
       }
   
       /**
        * Get formatted priority string.
        */
  -    protected String getPriority( final Priority priority, final String format )
  -    {
  +    protected String getPriority(final Priority priority, final String format) {
           return priority.getName();
       }
   
  @@ -419,8 +312,7 @@
        * @param context the un-fixed context
        * @return the fixed context
        */
  -    protected final String fix( final String context )
  -    {
  +    protected final String fix(final String context) {
           return context.replace( '.', '_' );
       }
   
  @@ -431,8 +323,7 @@
        * @param format ancilliary format parameter - allowed to be null
        * @return the formatted string
        */
  -    protected String getMessage( final String message, final String format )
  -    {
  +    protected String getMessage(final String message, final String format) {
           return message;
       }
   
  @@ -443,12 +334,13 @@
        * @param format ancilliary format parameter - allowed to be null
        * @return the formatted string
        */
  -    protected String getStackTrace( final Throwable throwable, final String format )
  -    {
  -        if( null == throwable ) return "";
  -        final StringWriter sw = new StringWriter();
  -        throwable.printStackTrace( new java.io.PrintWriter( sw ) );
  -        return sw.toString();
  +    protected String getStackTrace(final Throwable throwable, final String format) {
  +        if (null != throwable) {
  +            final StringWriter sw = new StringWriter();
  +            throwable.printStackTrace(new java.io.PrintWriter(sw));
  +            return sw.toString();
  +        }
  +        return "";
       }
   
       /**
  @@ -458,9 +350,8 @@
        * @param format ancilliary format parameter - allowed to be null
        * @return the formatted string
        */
  -    protected String getTime( final long time, final String format )
  -    {
  -        return Long.toString( time );
  +    protected String getTime(final long time, final String format) {
  +        return Long.toString(time);
       }
   
       /**
  @@ -469,21 +360,21 @@
        * @param type the string
        * @return the type-id
        */
  -    protected int getTypeIdFor( final String type )
  -    {
  -        if( type.equalsIgnoreCase( TYPE_CATEGORY_STR ) ) return TYPE_CATEGORY;
  -        else if( type.equalsIgnoreCase( TYPE_MESSAGE_STR ) ) return TYPE_MESSAGE;
  -        else if( type.equalsIgnoreCase( TYPE_PRIORITY_STR ) ) return TYPE_PRIORITY;
  -        else if( type.equalsIgnoreCase( TYPE_TIME_STR ) ) return TYPE_TIME;
  -        else if( type.equalsIgnoreCase( TYPE_RELATIVE_TIME_STR ) ) return TYPE_RELATIVE_TIME;
  -        else if( type.equalsIgnoreCase( TYPE_THROWABLE_STR ) )
  -        {
  +    protected int getTypeIdFor(final String type) {
  +        if (type.equalsIgnoreCase(TYPE_CATEGORY_STR)) {
  +            return TYPE_CATEGORY;
  +        } else if (type.equalsIgnoreCase(TYPE_MESSAGE_STR)) {
  +            return TYPE_MESSAGE;
  +        } else if (type.equalsIgnoreCase(TYPE_PRIORITY_STR)) {
  +            return TYPE_PRIORITY;
  +        } else if (type.equalsIgnoreCase(TYPE_TIME_STR)) {
  +            return TYPE_TIME;
  +        } else if (type.equalsIgnoreCase(TYPE_RELATIVE_TIME_STR)) {
  +            return TYPE_RELATIVE_TIME;
  +        } else if (type.equalsIgnoreCase(TYPE_THROWABLE_STR)) {
               return TYPE_THROWABLE;
  -        }
  -        else
  -        {
  -            throw new IllegalArgumentException( "Unknown Type in pattern - " +
  -                                                type );
  +        } else {
  +            throw new IllegalArgumentException( "Unknown Type in pattern - " + type );
           }
       }
   
  @@ -492,35 +383,26 @@
        *
        * @param patternString the pattern
        */
  -    protected void parse( final String patternString )
  -    {
  +    protected void parse(final String patternString) {
           final Stack stack = new Stack();
           final int size = patternString.length();
  -        final char pattern[] = new char[ size ];
  +        final char pattern[] = new char[size];
           int index = 0;
   
  -        patternString.getChars( 0, size, pattern, 0 );
  -
  -        while( index < size )
  -        {
  -            if( pattern[ index ] == '%' &&
  -                !( index != size - 1 && pattern[ index + 1 ] == '%' ) )
  -            {
  -                index += addPatternRun( stack, pattern, index );
  -            }
  -            else
  -            {
  -                index +=  addTextRun( stack, pattern, index );
  +        patternString.getChars(0, size, pattern, 0);
  +        while (index < size) {
  +            if (pattern[index] == '%' &&
  +                !(index != size - 1 && pattern[index + 1] == '%' )) {
  +                index += addPatternRun(stack, pattern, index);
  +            } else {
  +                index +=  addTextRun(stack, pattern, index);
               }
           }
  -
           final int elementCount = stack.size();
  +        m_formatSpecification = new PatternRun[elementCount];
   
  -        m_formatSpecification = new PatternRun[ elementCount ];
  -
  -        for( int i = 0; i < elementCount; i++ )
  -        {
  -            m_formatSpecification[ i ] = (PatternRun) stack.elementAt( i );
  +        for (int i = 0; i < elementCount; i++) {
  +            m_formatSpecification[i] = (PatternRun) stack.elementAt(i);
           }
       }
   
  @@ -529,8 +411,7 @@
        *
        * @param format the string format
        */
  -    public void setFormat( final String format )
  -    {
  -        parse( format );
  +    public void setFormat(final String format) {
  +        parse(format);
       }
   }
  
  
  

Mime
View raw message