
import java.util.*;
import java.io.*;


public class CpLogEntry
{
    String hostName;
    String dashOne;
    String dashTwo;
    String date;
    String instructionType;
    String instructionArg;
    String httpVersion;
    String returnCode;
    String bytesSent;

    public String getHostName()
    {
        return hostName;
    }

    public void setHostName(String hostName)
    {
        this.hostName = hostName;
    }

    public String getFilePath()
    {
        StringTokenizer tokenizer = new StringTokenizer(getHostName(), ".");
        String path = ".txt";
        boolean firstElement = true;

        while (tokenizer.hasMoreElements()) {
            path = tokenizer.nextElement() + (firstElement ? "" : "\\") + path;
            firstElement = false;
        }

        return path;
    }

    public String getDirectoryPath()
    {
        StringTokenizer tokenizer = new StringTokenizer(getHostName(), ".");
        String path = "";
        boolean firstElement = true;

        while (tokenizer.hasMoreElements()) {
            if (firstElement) {
                tokenizer.nextElement();
            } else {
                path = tokenizer.nextElement() + "\\" + path;
            }
            firstElement = false;
        }

        return path;
    }

    public String getDashOne()
    {
        return dashOne;
    }

    public void setDashOne(String dashOne)
    {
        this.dashOne = dashOne;
    }


    public String getDashTwo()
    {
        return dashTwo;
    }

    public void setDashTwo(String dashTwo)
    {
        this.dashTwo = dashTwo;
    }

    public String getDateString()
    {
        return date;
    }

    public void setDateString(String date)
    {
        this.date = date;
    }

    public String getInstructionType()
    {
        return instructionType;
    }

    public void setInstructionType(String instructionType)
    {
        this.instructionType = instructionType;
    }

    public String getInstructionArg()
    {
        return instructionArg;
    }

    public void setInstructionArg(String instructionArg)
    {
        this.instructionArg = instructionArg;
    }


    public String getHttpVersion()
    {
        return httpVersion;
    }

    public void setHttpVersion(String httpVersion)
    {
        this.httpVersion = httpVersion;
    }

    public String getReturnCode()
    {
        return returnCode;
    }

    public void setReturnCode(String returnCode)
    {
        this.returnCode = returnCode;
    }


    public String getBytesSent()
    {
        return bytesSent;
    }

    public void setBytesSent(String bytesSent)
    {
        this.bytesSent = bytesSent;
    }

    public String toString()
    {
        return (
            getHostName() + " " + getDashOne() + " " + getDashTwo() +
            " [" + getDateString() + "] \"" + getInstructionType() + " "
            + getInstructionArg() + " " + getHttpVersion() + "\" " + getReturnCode() + " " + getBytesSent());
    }

    public static CpLogEntry logEntryFromStream(DataInputStream inputStream) throws IOException
    {
        CpLogEntry logEntry = new CpLogEntry();
        String string;

        string = inputStream.readLine();
        if (string == null) {
            return null;
        }

        StringTokenizer tokenizer = new StringTokenizer(string);


        if (!tokenizer.hasMoreElements()) {
            System.out.println("RETURNING NULL FOR " + string);
            return null;
        }
        logEntry.setHostName(tokenizer.nextToken());

        if (!tokenizer.hasMoreElements()) {
            System.out.println("1 RETURNING NULL FOR " + string);
            return null;
        }
        logEntry.setDashOne(tokenizer.nextToken());


        if (!tokenizer.hasMoreElements()) {
            System.out.println("2 RETURNING NULL FOR " + string);
            return null;
        }
        logEntry.setDashTwo(tokenizer.nextToken());


        if (!tokenizer.hasMoreElements()) {
            System.out.println("3 RETURNING NULL FOR " + string);
            return null;
        }
        logEntry.setDateString(tokenizer.nextToken("[]"));


        if (!tokenizer.hasMoreElements()) {
            System.out.println("4 RETURNING NULL FOR " + string);
            return null;
        }
        logEntry.setInstructionType(tokenizer.nextToken("\"\t\r\n "));

        if (!logEntry.getInstructionType().equals("-")) {
            if (!tokenizer.hasMoreElements()) {
            System.out.println(" 5RETURNING NULL FOR " + string);
                return null;
            }
            logEntry.setInstructionArg(tokenizer.nextToken());


            if (!tokenizer.hasMoreElements()) {
            System.out.println("6 RETURNING NULL FOR " + string);
                return null;
            }
            logEntry.setHttpVersion(tokenizer.nextToken());
        }


        if (!tokenizer.hasMoreElements()) {
            System.out.println("7 RETURNING NULL FOR " + string);
            return null;
        }
        logEntry.setReturnCode(tokenizer.nextToken());


        if (!tokenizer.hasMoreElements()) {
            System.out.println("8 RETURNING NULL FOR " + string);
            return null;
        }
        logEntry.setBytesSent(tokenizer.nextToken());

        return logEntry;
	}

	public boolean isInterestingEntry()
	{
	    if (getHostName().length() == 0 || Character.isDigit(getHostName().charAt(0))) {
	        return false;
	    }

    	if (getInstructionArg() == null || getInstructionArg().length() == 1) {
    	    return false;
    	}

    	return true;
	}
}