//# 0 errors, 114 messages
//#
/*
    //#combinedformatlogentryformat.java:1:1: class: net.sourceforge.pebble.logging.CombinedFormatLogEntryFormat
    //#combinedformatlogentryformat.java:1:1: method: net.sourceforge.pebble.logging.CombinedFormatLogEntryFormat.net.sourceforge.pebble.logging.CombinedFormatLogEntryFormat__static_init
 * Copyright (c) 2003-2006, Simon Brown
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in
 *     the documentation and/or other materials provided with the
 *     distribution.
 *
 *   - Neither the name of Pebble nor the names of its contributors may
 *     be used to endorse or promote products derived from this software
 *     without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package net.sourceforge.pebble.logging;

import net.sourceforge.pebble.domain.Blog;

import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * Represents a log entry in the combined log file format.
 *
 * @author Simon Brown
 */
public class CombinedFormatLogEntryFormat {

  /** the format used for dates */
  SimpleDateFormat dateFormatter = new SimpleDateFormat("[dd/MMM/yyyy:HH:mm:ss Z]");

  /**
   * Default, no args constructor.
   */
  public CombinedFormatLogEntryFormat(Blog blog) {
    //#combinedformatlogentryformat.java:52: method: void net.sourceforge.pebble.logging.CombinedFormatLogEntryFormat.net.sourceforge.pebble.logging.CombinedFormatLogEntryFormat(Blog)
    //#input(void net.sourceforge.pebble.logging.CombinedFormatLogEntryFormat(Blog)): blog
    //#input(void net.sourceforge.pebble.logging.CombinedFormatLogEntryFormat(Blog)): this
    //#output(void net.sourceforge.pebble.logging.CombinedFormatLogEntryFormat(Blog)): new SimpleDateFormat(CombinedFormatLogEntryFormat#1) num objects
    //#output(void net.sourceforge.pebble.logging.CombinedFormatLogEntryFormat(Blog)): this.dateFormatter
    //#new obj(void net.sourceforge.pebble.logging.CombinedFormatLogEntryFormat(Blog)): new SimpleDateFormat(CombinedFormatLogEntryFormat#1)
    //#pre[1] (void net.sourceforge.pebble.logging.CombinedFormatLogEntryFormat(Blog)): blog != null
    //#post(void net.sourceforge.pebble.logging.CombinedFormatLogEntryFormat(Blog)): this.dateFormatter == &new SimpleDateFormat(CombinedFormatLogEntryFormat#1)
    //#post(void net.sourceforge.pebble.logging.CombinedFormatLogEntryFormat(Blog)): new SimpleDateFormat(CombinedFormatLogEntryFormat#1) num objects == 1
    dateFormatter.setTimeZone(blog.getTimeZone());
    //#combinedformatlogentryformat.java:53: Warning: method not available
    //#    -- call on TimeZone net.sourceforge.pebble.domain.Blog:getTimeZone()
    //#    severity: INFORMATIONAL
    //#    class: net.sourceforge.pebble.logging.CombinedFormatLogEntryFormat
    //#    method: void net.sourceforge.pebble.logging.CombinedFormatLogEntryFormat(Blog)
    //#    unanalyzed callee: TimeZone net.sourceforge.pebble.domain.Blog:getTimeZone()
  }
    //#combinedformatlogentryformat.java:54: end of method: void net.sourceforge.pebble.logging.CombinedFormatLogEntryFormat.net.sourceforge.pebble.logging.CombinedFormatLogEntryFormat(Blog)

  /**
   * Formats a given entry into the combined log file format.
   *
   * @param entry   a log entry
   * @return  a formatted String
   */
  public String format(LogEntry entry) {
    StringBuffer buf = new StringBuffer();
    //#combinedformatlogentryformat.java:63: method: String net.sourceforge.pebble.logging.CombinedFormatLogEntryFormat.format(LogEntry)
    //#input(String format(LogEntry)): entry
    //#input(String format(LogEntry)): entry.__Tag
    //#input(String format(LogEntry)): entry.agent
    //#input(String format(LogEntry)): entry.date
    //#input(String format(LogEntry)): entry.host
    //#input(String format(LogEntry)): entry.referer
    //#input(String format(LogEntry)): entry.request
    //#input(String format(LogEntry)): entry.statusCode
    //#input(String format(LogEntry)): net/sourceforge/pebble/logging/LogEntry.__Descendant_Table[net/sourceforge/pebble/logging/LogEntry]
    //#input(String format(LogEntry)): net/sourceforge/pebble/logging/LogEntry.__Descendant_Table[others]
    //#input(String format(LogEntry)): net/sourceforge/pebble/logging/LogEntry.__Dispatch_Table.getAgent()Ljava/lang/String;
    //#input(String format(LogEntry)): net/sourceforge/pebble/logging/LogEntry.__Dispatch_Table.getDate()Ljava/util/Date;
    //#input(String format(LogEntry)): net/sourceforge/pebble/logging/LogEntry.__Dispatch_Table.getHost()Ljava/lang/String;
    //#input(String format(LogEntry)): net/sourceforge/pebble/logging/LogEntry.__Dispatch_Table.getReferer()Ljava/lang/String;
    //#input(String format(LogEntry)): net/sourceforge/pebble/logging/LogEntry.__Dispatch_Table.getRequest()Ljava/lang/String;
    //#input(String format(LogEntry)): net/sourceforge/pebble/logging/LogEntry.__Dispatch_Table.getStatusCode()I
    //#input(String format(LogEntry)): this
    //#input(String format(LogEntry)): this.dateFormatter
    //#output(String format(LogEntry)): return_value
    //#pre[1] (String format(LogEntry)): entry != null
    //#pre[2] (String format(LogEntry)): entry.__Tag == net/sourceforge/pebble/logging/LogEntry
    //#pre[3] (String format(LogEntry)): init'ed(entry.agent)
    //#pre[4] (String format(LogEntry)): init'ed(entry.date)
    //#pre[5] (String format(LogEntry)): init'ed(entry.host)
    //#pre[6] (String format(LogEntry)): init'ed(entry.referer)
    //#pre[7] (String format(LogEntry)): init'ed(entry.request)
    //#pre[8] (String format(LogEntry)): init'ed(entry.statusCode)
    //#pre[10] (String format(LogEntry)): this.dateFormatter != null
    //#post(String format(LogEntry)): return_value != null
    //#test_vector(String format(LogEntry)): entry.agent: Addr_Set{null}, Inverse{null}
    //#test_vector(String format(LogEntry)): entry.host: Addr_Set{null}, Inverse{null}
    //#test_vector(String format(LogEntry)): entry.referer: Addr_Set{null}, Inverse{null}
    if (entry.getHost() != null) {
      buf.append(entry.getHost());
    } else {
      buf.append("-");
    }
    buf.append(" ");
    buf.append("-");
    buf.append(" ");
    buf.append("-");
    buf.append(" ");
    buf.append(dateFormatter.format(entry.getDate()));
    buf.append(" ");
    buf.append("\"" + entry.getRequest() + "\"");
    buf.append(" ");
    buf.append(entry.getStatusCode());
    buf.append(" ");
    buf.append("-");
    buf.append(" ");
    if (entry.getReferer() != null) {
      buf.append("\"" + entry.getReferer() + "\"");
    } else {
      buf.append("-");
    }
    buf.append(" ");
    if (entry.getAgent() != null) {
      buf.append("\"" + entry.getAgent() + "\"");
    } else {
      buf.append("-");
    }

    return buf.toString();
    //#combinedformatlogentryformat.java:94: end of method: String net.sourceforge.pebble.logging.CombinedFormatLogEntryFormat.format(LogEntry)
  }

  /**
   * Parses a string in the combined log file format into a log entry.
   *
   * @param s   the String to parse
   * @return  a LogEntry instance
   */
  public LogEntry parse(String s) {
    LogEntry logEntry = new LogEntry();
    //#combinedformatlogentryformat.java:104: method: LogEntry net.sourceforge.pebble.logging.CombinedFormatLogEntryFormat.parse(String)
    //#input(LogEntry parse(String)): net/sourceforge/pebble/logging/LogEntry.__Descendant_Table[net/sourceforge/pebble/logging/LogEntry]
    //#input(LogEntry parse(String)): net/sourceforge/pebble/logging/LogEntry.__Dispatch_Table.setAgent(Ljava/lang/String;)V
    //#input(LogEntry parse(String)): net/sourceforge/pebble/logging/LogEntry.__Dispatch_Table.setBytes(J)V
    //#input(LogEntry parse(String)): net/sourceforge/pebble/logging/LogEntry.__Dispatch_Table.setDate(Ljava/util/Date;)V
    //#input(LogEntry parse(String)): net/sourceforge/pebble/logging/LogEntry.__Dispatch_Table.setHost(Ljava/lang/String;)V
    //#input(LogEntry parse(String)): net/sourceforge/pebble/logging/LogEntry.__Dispatch_Table.setReferer(Ljava/lang/String;)V
    //#input(LogEntry parse(String)): net/sourceforge/pebble/logging/LogEntry.__Dispatch_Table.setRequest(Ljava/lang/String;)V
    //#input(LogEntry parse(String)): net/sourceforge/pebble/logging/LogEntry.__Dispatch_Table.setStatusCode(I)V
    //#input(LogEntry parse(String)): s
    //#input(LogEntry parse(String)): this
    //#input(LogEntry parse(String)): this.dateFormatter
    //#output(LogEntry parse(String)): new Date(LogEntry#1) num objects
    //#output(LogEntry parse(String)): new LogEntry(parse#1) num objects
    //#output(LogEntry parse(String)): return_value.__Tag
    //#output(LogEntry parse(String)): return_value.agent
    //#output(LogEntry parse(String)): return_value.bytes
    //#output(LogEntry parse(String)): return_value.date
    //#output(LogEntry parse(String)): return_value.host
    //#output(LogEntry parse(String)): return_value.referer
    //#output(LogEntry parse(String)): return_value.request
    //#output(LogEntry parse(String)): return_value.statusCode
    //#output(LogEntry parse(String)): return_value
    //#new obj(LogEntry parse(String)): new Date(LogEntry#1)
    //#new obj(LogEntry parse(String)): new LogEntry(parse#1)
    //#pre[1] (LogEntry parse(String)): s != null
    //#pre[3] (LogEntry parse(String)): (soft) this.dateFormatter != null
    //#presumption(LogEntry parse(String)): java.lang.String:indexOf(...)@108 <= 4_294_967_294
    //#presumption(LogEntry parse(String)): java.lang.String:indexOf(...)@111 <= 4_294_967_294
    //#presumption(LogEntry parse(String)): java.lang.String:indexOf(...)@114 <= 4_294_967_294
    //#presumption(LogEntry parse(String)): java.lang.String:indexOf(...)@117 <= 4_294_967_292
    //#presumption(LogEntry parse(String)): java.lang.String:indexOf(...)@120 <= 4_294_967_293
    //#presumption(LogEntry parse(String)): java.lang.String:indexOf(...)@123 <= 4_294_967_294
    //#presumption(LogEntry parse(String)): java.lang.String:indexOf(...)@126 <= 4_294_967_290
    //#presumption(LogEntry parse(String)): java.lang.String:indexOf(...)@134 <= 4_294_967_291
    //#presumption(LogEntry parse(String)): java.lang.String:indexOf(...)@144 <= 4_294_967_293
    //#post(LogEntry parse(String)): return_value == &new LogEntry(parse#1)
    //#post(LogEntry parse(String)): new Date(LogEntry#1) num objects == 1
    //#post(LogEntry parse(String)): new LogEntry(parse#1) num objects == 1
    //#post(LogEntry parse(String)): return_value.__Tag == net/sourceforge/pebble/logging/LogEntry
    //#post(LogEntry parse(String)): init'ed(return_value.agent)
    //#post(LogEntry parse(String)): init'ed(return_value.bytes)
    //#post(LogEntry parse(String)): init'ed(return_value.date)
    //#post(LogEntry parse(String)): init'ed(return_value.host)
    //#post(LogEntry parse(String)): init'ed(return_value.referer)
    //#post(LogEntry parse(String)): return_value.request != null
    //#post(LogEntry parse(String)): init'ed(return_value.statusCode)
    //#unanalyzed(LogEntry parse(String)): Effects-of-calling:java.util.Date
    //#test_vector(LogEntry parse(String)): java.lang.String:charAt(...)@130: {0..44, 46..65_535}, {45}
    //#test_vector(LogEntry parse(String)): java.lang.String:charAt(...)@140: {0..44, 46..65_535}, {45}
    //#test_vector(LogEntry parse(String)): java.lang.String:equals(...)@149: {1}, {0}
    //#test_vector(LogEntry parse(String)): java.lang.String:equals(...)@169: {1}, {0}
    //#test_vector(LogEntry parse(String)): java.lang.String:equals(...)@177: {1}, {0}
    //#test_vector(LogEntry parse(String)): java.lang.String:equals(...)@185: {1}, {0}
    //#test_vector(LogEntry parse(String)): java.lang.String:equals(...)@189: {1}, {0}

    // there are 9 tokens in the combined log file format
    int start = 0;
    int end = s.indexOf(" ", start);
    String host = s.substring(start, end);
    start = end + 1;
    end = s.indexOf(" ", start);
    String rfc931 = s.substring(start, end);
    start = end + 1;
    end = s.indexOf(" ", start);
    String authuser = s.substring(start, end);
    start = end + 1;
    end = s.indexOf("]", start);
    String date = s.substring(start, end+1);
    start = end + 2;
    end = s.indexOf("\"", start+1);
    String request = s.substring(start+1, end);
    start = end + 2;
    end = s.indexOf(" ", start);
    String statusCode = s.substring(start, end);
    start = end + 1;
    end = s.indexOf(" ", start);
    String bytes = s.substring(start, end);
    start = end + 1;
    String referer;
    if (s.charAt(start) == '-') {
      referer = "-";
      start = start + 2;
    } else {
      end = s.indexOf("\"", start+1);
      referer = s.substring(start+1, end);
      start = end + 2;
    }
    end = s.length();
    //#combinedformatlogentryformat.java:138: Warning: unused assignment
    //#    unused assignment into end
    //#    severity: LOW
    //#    class: net.sourceforge.pebble.logging.CombinedFormatLogEntryFormat
    //#    method: LogEntry parse(String)
    String agent;
    if (s.charAt(start) == '-') {
      agent = "-";
      start = start + 2;
    //#combinedformatlogentryformat.java:142: Warning: unused assignment
    //#    unused assignment into start
    //#    severity: LOW
    //#    class: net.sourceforge.pebble.logging.CombinedFormatLogEntryFormat
    //#    method: LogEntry parse(String)
    } else {
      end = s.indexOf("\"", start+1);
      agent = s.substring(start+1, end);
      start = end + 2;
    //#combinedformatlogentryformat.java:146: Warning: unused assignment
    //#    unused assignment into start
    //#    severity: LOW
    //#    class: net.sourceforge.pebble.logging.CombinedFormatLogEntryFormat
    //#    method: LogEntry parse(String)
    }

    if (!host.equals("-")) {
      logEntry.setHost(host);
    }

    if (!rfc931.equals("-")) {
      //logEntry.setHost(host);
    }

    if (!authuser.equals("-")) {
      //logEntry.setUser(authuser);
    }

    try {
      logEntry.setDate(dateFormatter.parse(date));
    } catch (ParseException e) {
      // ignore
    }

    logEntry.setRequest(request);

    if (!statusCode.equals("-")) {
      try {
        logEntry.setStatusCode(Integer.parseInt(statusCode));
      } catch (NumberFormatException e) {
        // ignore
      }
    }

    if (!bytes.equals("-")) {
      try {
        logEntry.setBytes(Long.parseLong(bytes));
      } catch (NumberFormatException e) {
        // ignore
      }
    }

    if (!referer.equals("-")) {
      logEntry.setReferer(referer);
    }

    if (!agent.equals("-")) {
      logEntry.setAgent(agent);
    }

    return logEntry;
    //#combinedformatlogentryformat.java:193: end of method: LogEntry net.sourceforge.pebble.logging.CombinedFormatLogEntryFormat.parse(String)
  }

}


    //#output(net.sourceforge.pebble.logging.CombinedFormatLogEntryFormat__static_init): __Descendant_Table[net/sourceforge/pebble/logging/CombinedFormatLogEntryFormat]
    //#output(net.sourceforge.pebble.logging.CombinedFormatLogEntryFormat__static_init): __Dispatch_Table.format(Lnet/sourceforge/pebble/logging/LogEntry;)Ljava/lang/String;
    //#output(net.sourceforge.pebble.logging.CombinedFormatLogEntryFormat__static_init): __Dispatch_Table.parse(Ljava/lang/String;)Lnet/sourceforge/pebble/logging/LogEntry;
    //#post(net.sourceforge.pebble.logging.CombinedFormatLogEntryFormat__static_init): __Descendant_Table[net/sourceforge/pebble/logging/CombinedFormatLogEntryFormat] == &__Dispatch_Table
    //#post(net.sourceforge.pebble.logging.CombinedFormatLogEntryFormat__static_init): __Dispatch_Table.format(Lnet/sourceforge/pebble/logging/LogEntry;)Ljava/lang/String; == &format
    //#post(net.sourceforge.pebble.logging.CombinedFormatLogEntryFormat__static_init): __Dispatch_Table.parse(Ljava/lang/String;)Lnet/sourceforge/pebble/logging/LogEntry; == &parse
    //#combinedformatlogentryformat.java:: end of method: net.sourceforge.pebble.logging.CombinedFormatLogEntryFormat.net.sourceforge.pebble.logging.CombinedFormatLogEntryFormat__static_init
    //#combinedformatlogentryformat.java:: end of class: net.sourceforge.pebble.logging.CombinedFormatLogEntryFormat
