001 package log; 002 003 import java.util.*; 004 import java.io.*; 005 006 /** 007 * Represents the content of a log file. 008 * 009 * <p>This class takes a log file or a {@link LogInputStream} as parameter, reads all {@link LogEntry LogEntries} 010 * from that file and stores them in a List.</p> 011 * 012 * Having all LogEntries in a List, makes it easy to display them in a {@link log.stdforms.LogTableForm LogTableForm} 013 * or writing them to the persistence file when saving the Shop's state. 014 * 015 * @author Andreas Bartho 016 * @since v3.1 017 */ 018 public class LogFileContent implements Serializable { 019 020 /** 021 * ID for serialization. 022 */ 023 private static final long serialVersionUID = -8390407452184927491L; 024 025 /** 026 * The list which contains all Entries from the log file. 027 */ 028 private List<LogEntry> m_lContentList; 029 030 /** 031 * Creates a LogFileContent. All {@link LogEntry LogEntries} are read from the passed log file and stored 032 * in a list. 033 * @param f the file to be read from. 034 */ 035 public LogFileContent(File f) { 036 update(f); 037 } 038 039 /** 040 * Creates a LogFileContent. All {@link LogEntry LogEntries} are read from the passed {@link LogInputStream} 041 * and stored in a list. 042 * @param lis the {@link LogInputStream} to be read from. 043 */ 044 public LogFileContent(LogInputStream lis) { 045 update(lis); 046 } 047 048 /** 049 * Replaces all currently saved {@link LogEntry LogEntries} with new ones. 050 * @param f the log file to be read from 051 */ 052 public void update(File f) { 053 try { 054 update(new LogInputStream(new FileInputStream(f))); 055 } 056 catch (IOException ioe) { 057 ioe.printStackTrace(); 058 } 059 } 060 061 /** 062 * Replaces all currently saved {@link LogEntry LogEntries} with new ones. 063 * @param lis the {@link LogInputStream} to be read from. 064 */ 065 public void update(LogInputStream lis) { 066 m_lContentList = streamToList(lis); 067 } 068 069 /** 070 * @return the List of currently saved {@link LogEntry LogEntries} 071 */ 072 public List<LogEntry> getContentList() { 073 return m_lContentList; 074 } 075 076 /** 077 * Does the actual conversion from a {@link LogInputStream} to a list of {@link LogEntry LogEntries}.<br> 078 * @param lis the LogInputStream to be read from 079 * @return a List with all LogEntries from the LogInputStream 080 */ 081 private List<LogEntry> streamToList(LogInputStream lis) { 082 List<LogEntry> l = new LinkedList<LogEntry>(); 083 try { 084 while (true) { 085 l.add(lis.readEntry()); 086 } 087 } 088 catch (IOException ioe) {} 089 catch (ClassNotFoundException cnfe) {} 090 try { 091 lis.close(); 092 } 093 catch (IOException ioe) {} 094 095 return l; 096 } 097 }