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         * The list which contains all Entries from the log file.
022         */
023        private List m_lContentList;
024    
025        /**
026         * Creates a LogFileContent. All {@link LogEntry LogEntries} are read from the passed log file and stored
027         * in a list.
028         * @param f the file to be read from.
029         */
030        public LogFileContent(File f) {
031            update(f);
032        }
033    
034        /**
035         * Creates a LogFileContent. All {@link LogEntry LogEntries} are read from the passed {@link LogInputStream}
036         * and stored in a list.
037         * @param lis the {@link LogInputStream} to be read from.
038         */
039        public LogFileContent(LogInputStream lis) {
040            update(lis);
041        }
042    
043        /**
044         * Replaces all currently saved {@link LogEntry LogEntries} with new ones.
045         * @param f the log file to be read from
046         */
047        public void update(File f) {
048            try {
049                update(new LogInputStream(new FileInputStream(f)));
050            }
051            catch (IOException ioe) {
052                ioe.printStackTrace();
053            }
054        }
055    
056        /**
057         * Replaces all currently saved {@link LogEntry LogEntries} with new ones.
058         * @param lis the {@link LogInputStream} to be read from.
059         */
060        public void update(LogInputStream lis) {
061            m_lContentList = streamToList(lis);
062        }
063    
064        /**
065         * @return the List of currently saved {@link LogEntry LogEntries}
066         */
067        public List getContentList() {
068            return m_lContentList;
069        }
070    
071        /**
072         * Does the actual conversion from a {@link LogInputStream} to a list of {@link LogEntry LogEntries}.<br>
073         * @param lis the LogInputStream to be read from
074         * @return a List with all LogEntries from the LogInputStream
075         */
076        private List streamToList(LogInputStream lis) {
077            List l = new LinkedList();
078            try {
079                while (true) {
080                    l.add(lis.readEntry());
081                }
082            }
083            catch (IOException ioe) {}
084            catch (ClassNotFoundException cnfe) {}
085            try {
086                lis.close();
087            }
088            catch (IOException ioe) {}
089    
090            return l;
091        }
092    }