Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: junit/log4j/JUnitPatternParser.java


1   package junit.log4j;
2   import org.apache.log4j.helpers.PatternParser;
3   import org.apache.log4j.helpers.PatternConverter;
4   import org.apache.log4j.helpers.FormattingInfo;
5   import org.apache.log4j.spi.LoggingEvent;
6   import java.io.PrintWriter;
7   import java.io.StringWriter;
8   /*
9    * This library is free software; you can redistribute it and/or
10   * modify it under the terms of the GNU Lesser General Public
11   * License as published by the Free Software Foundation; either
12   * version 2 of the License, or (at your option) any later version.<p>
13   *
14   * This library is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   * Lesser General Public License for more details.<p>
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this library; if not, write to the Free Software
21   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA<br>
22   * http://www.gnu.org/copyleft/lesser.html
23   */
24  
25  // Configuration Management Information: 
26  // -------------------------------------
27  // $Id: JUnitPatternParser.java,v 1.1 2001/11/17 22:38:46 wreissen Exp $
28  //
29  // Version History:
30  // ----------------
31  // $Log: JUnitPatternParser.java,v $
32  // Revision 1.1  2001/11/17 22:38:46  wreissen
33  // initial version.
34  //
35  //
36  // ***********************************************************************************
37  /**
38   * <p>Extension of {@link org.apache.log4j.helpers.PatternParser}
39   * writing the stack trace of the throwable to the result string.</p>
40   *
41   *
42   * Created: Sat Nov 17 15:19:09 2001
43   *
44   * @author <a href="mailto:wolfgang@openfuture.de">Wolfgang Reissenberger</a>
45   * @version $Revision: 1.1 $
46   */
47  
48  public class JUnitPatternParser extends PatternParser {
49  
50      /**
51       * The conversion character <b>T</b> stands for stack traces. 
52       *
53       */
54      public static final char STACKTRACE_CHAR = 'T';
55      
56      /**
57       * Creates a new <code>JUnitPatternParser</code> instance.
58       *
59       * @param pattern the supplied conversion pattern
60       */
61      public JUnitPatternParser(String pattern) {
62    super(pattern);
63      }
64      
65  
66      /**
67       * In case of {@link #STACKTRACE_CHAR T}, the stack trace
68       * is written. Otherwise, the character is delegated to the
69       * super class.
70       *
71       * @param formatChar the format character
72       */
73      public void finalizeConverter(char formatChar) {
74  
75         PatternConverter pc = null;
76         switch( formatChar ) {
77            case STACKTRACE_CHAR:
78  
79               pc = new StacktracePatternConverter(formattingInfo);
80  
81               currentLiteral.setLength(0);
82               addConverter(pc);
83               break;
84  
85            default:
86               super.finalizeConverter( formatChar );
87  
88         }
89      }
90  
91      /**
92       * Converter writing the stack trace of the throwable.
93       *
94       */
95      public static class StacktracePatternConverter
96    extends PatternConverter {
97  
98    /**
99     * Creates a new <code>StacktracePatternConverter</code> instance.
100    *
101    * @param formattingInfo a <code>FormattingInfo</code> value
102    */
103   public StacktracePatternConverter(FormattingInfo formattingInfo) {
104       super(formattingInfo);     
105   }
106 
107   /**
108    * writing the stack trace of the throwable contained in
109    * <code>event</code>.
110    *
111    * @param event a <code>LoggingEvent</code> value
112    * @return <code>null</code>, if the event is null or its
113    * {@link org.apache.log4j.spi.ThrowableInformation#getThrowable()
114    *        throwable}. Otherwise, a string representation of the
115    * stack trace is returned.
116    */
117   public String convert(LoggingEvent event) {
118       if (event == null) return null;
119       if (event.getThrowableInformation() == null) return null;
120       String result = throwable2String(event.getThrowableInformation().getThrowable());
121       return result;
122   }
123   
124   /**
125    * Convert the stack trace into a string using 
126    * {@link java.io.StringWriter} and {@link java.io.PrintWriter}.
127    *
128    * @param throwable a <code>Throwable</code> value
129    * @return the string representation of the stack trace
130    */
131   protected String throwable2String(Throwable throwable) {
132       if (throwable == null) return null;
133       StringWriter writer = new StringWriter();
134       throwable.printStackTrace(new PrintWriter(writer));
135       return(writer.toString());
136   }
137     } // StacktracePatternConverter
138 } // JUnitPatternParser