View Javadoc
1   package net.sf.logdistiller.util;
2   
3   /*
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  import java.text.ParseException;
18  
19  import net.sf.logdistiller.LogEvent;
20  
21  /**
22   * A helper to build log events and properly report parsing errors.
23   * 
24   * @since 1.2
25   */
26  public abstract class LogEventBuilder
27  {
28      protected abstract LogEvent newEvent( LogEvent.Factory factory, String curLine, Object... objects )
29          throws ParseException;
30  
31      public LogEvent buildLogEvent( LogEvent.Factory factory, int lineNumber, String curLine, Object... objects )
32      {
33          try
34          {
35              return newEvent( factory, curLine, objects );
36          }
37          catch ( ParseException pe )
38          {
39              throw newRuntimeException( lineNumber, curLine, pe );
40          }
41          catch ( RuntimeException re )
42          {
43              throw newRuntimeException( lineNumber, curLine, re );
44          }
45      }
46  
47      private RuntimeException newRuntimeException( int lineNumber, String curLine, Throwable t )
48      {
49          String message;
50          if ( lineNumber > 0 )
51          {
52              message = "error while parsing line #" + lineNumber + " '" + curLine + "'";
53          }
54          else
55          {
56              message = "error while parsing '" + curLine + "'";
57          }
58  
59          return new RuntimeException( message, t );
60      }
61  }