001package net.sf.logdistiller.util;
002
003/*
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017import java.text.ParseException;
018
019import net.sf.logdistiller.LogEvent;
020
021/**
022 * A helper to build log events and properly report parsing errors.
023 * 
024 * @since 1.2
025 */
026public abstract class LogEventBuilder
027{
028    protected abstract LogEvent newEvent( LogEvent.Factory factory, String curLine, Object... objects )
029        throws ParseException;
030
031    public LogEvent buildLogEvent( LogEvent.Factory factory, int lineNumber, String curLine, Object... objects )
032    {
033        try
034        {
035            return newEvent( factory, curLine, objects );
036        }
037        catch ( ParseException pe )
038        {
039            throw newRuntimeException( lineNumber, curLine, pe );
040        }
041        catch ( RuntimeException re )
042        {
043            throw newRuntimeException( lineNumber, curLine, re );
044        }
045    }
046
047    private RuntimeException newRuntimeException( int lineNumber, String curLine, Throwable t )
048    {
049        String message;
050        if ( lineNumber > 0 )
051        {
052            message = "error while parsing line #" + lineNumber + " '" + curLine + "'";
053        }
054        else
055        {
056            message = "error while parsing '" + curLine + "'";
057        }
058
059        return new RuntimeException( message, t );
060    }
061}