001package net.sf.logdistiller.logtypes;
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.io.*;
018import java.text.ParseException;
019
020import net.sf.logdistiller.LogEvent;
021import net.sf.logdistiller.LogType;
022
023/**
024 * Generic log event for simple files where one line is one logevent, with an unique attribute named <code>text</code>.
025 * By default, the classification rules generated by the GUI for this type of logs won't be able to sort events, as the
026 * log event structure is too generic.
027 */
028public class SimpleLineLogEvent
029    extends LogEvent
030{
031    public final static String ID = "simple";
032
033    public final String text;
034
035    public final static LogType LOGTYPE = new LogType.Basic( ID );
036
037    private final static String[] ATTRIBUTE_NAMES = { "logSource", "text" };
038
039    public final static LogType.Description DESCRIPTION = new Description( (LogType.Basic) LOGTYPE, ATTRIBUTE_NAMES );
040
041    public SimpleLineLogEvent( Factory factory, String rawLog )
042        throws ParseException
043    {
044        super( factory, rawLog );
045        this.text = rawLog;
046        setAttributes( new String[] { factory.getLogSource(), text } );
047    }
048
049    private static class Description
050        extends LogType.Description
051    {
052        public Description( LogType.Basic logtype, String[] attributeNames )
053        {
054            super( logtype, attributeNames );
055            logtype.setDescription( this );
056        }
057
058        public LogEvent.Factory newFactory( Reader reader, String logSource )
059            throws IOException
060        {
061            return new Factory( this, reader, logSource );
062        }
063
064        public int getTimestampAttribute()
065        {
066            return -1;
067        }
068    }
069
070    private static class Factory
071        extends LogEvent.Factory
072    {
073        private final LineNumberReader reader;
074
075        public Factory( Description description, Reader reader, String logSource )
076            throws FileNotFoundException
077        {
078            super( description, logSource );
079            this.reader = new LineNumberReader( reader );
080        }
081
082        protected LogEvent readNextEvent()
083            throws IOException, ParseException
084        {
085            String curLine = reader.readLine();
086            if ( curLine == null )
087            {
088                // EOF
089                return null;
090            }
091            return new SimpleLineLogEvent( this, curLine );
092        }
093    }
094}