Viewing file: comp249/assignment3/MovieDB.java | Back to directory listing
Author: Loren Segal | Last modified: February 20 2006 07:00 pm | Download

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.zip.GZIPOutputStream;
 
public class MovieDB
{
	public MovieDB()
	{
		String line;
		LineNumberReader dataIn;
		ArrayList<ValidMovie> validMovies = new ArrayList<ValidMovie>(1000);
		ArrayList<InvalidMovie> invalidMovies = new ArrayList<InvalidMovie>();
		try
		{
			// Read the data from the database file movie.txt
			dataIn = new LineNumberReader(new FileReader("movie.txt"));
			while (true)
			{
				try
				{
					line = dataIn.readLine();
					if (line == null) break;
					ValidMovie movie = parseDatabaseLine(line);
					validMovies.add(movie);
				}
				/* This catch(MovieException e) is a nice little trick that saves
				 * a lot of effort in coding. It's possible to write out each exception
				 * individually as a catch(), but since the same function is performed
				 * for each one, we can group all the exceptions inside a generic
				 * MovieException class and catch that.
				 */
				catch (MovieException e)
				{
					System.err.println("% Invalid movie on line "
						+ dataIn.getLineNumber() + ": " + e.getMessage());
					invalidMovies.add(
							new InvalidMovie(dataIn.getLineNumber(),
											 e.getType(),
											 e.getInvalidData()));
				}
			}
			// *******************************************
			// Sort the valid movies now that we have them
			// *******************************************
			Collections.sort(validMovies);
 
			// ****************************************
			// Now we can write the three output files
			// ****************************************
 
			// Serialize the valid movie definitions to validmovies.ser
			System.out.print("Writing valid movie definitions to validmovies.ser...");
			try
			{
				FileOutputStream fout = new FileOutputStream("validmovies.ser");
				ObjectOutputStream oos = new ObjectOutputStream(fout);
				oos.writeObject(validMovies);
				oos.flush();
				oos.close();
			}
			catch (IOException e)
			{
				System.err.println("\nCritical error! Could not write to 'validmovies.ser'!");
			}
			System.out.println(" DONE.");
 
			// Write GZIP output
			System.out.print("Writing GZIP output for valid movies to validmovies.txt.gz...");
			try
			{
				BufferedWriter gzipOut = new BufferedWriter(new OutputStreamWriter(
								new GZIPOutputStream(new FileOutputStream("validmovies.txt.gz"))));
				for(int i = 0; i < validMovies.size(); i++)
				{
					gzipOut.write((ValidMovie)validMovies.get(i) + "\r\n");
					gzipOut.flush();
				}
				gzipOut.close();
			}
			catch (IOException e)
			{
				System.err.println("\nError! Could not write GZIPPED output file\n");
			}
			System.out.println(" DONE.");
			// Done GZIP output
 
			// Serialize the error.log (invalid movie definitions)
			if (invalidMovies.size() > 0)
			{
				System.out.print("Writing invalid movie definitions to error.log...");
				try
				{
					FileOutputStream fout = new FileOutputStream("error.log");
					ObjectOutputStream oos = new ObjectOutputStream(fout);
					oos.writeObject(invalidMovies);
					oos.flush();
					oos.close();
				}
				catch (IOException e)
				{
					System.err.println("\nCritical error! Could not write to 'error.log'!");
				}
				System.out.println(" DONE.");
			}
			else
			{
				FileOutputStream fout = new FileOutputStream("error.log");
				fout.close();
			}
			// Done Serializing error.log
 
		}
		catch (FileNotFoundException e)
		{
			System.err.println("Critical error! Could not find the movie database file!");
		}
		catch (IOException e)
		{
			System.err.println("Critical error! Could not parse movie database file!");
		}
	}
 
	/** Parses a line of the database file and validates it to create a ValidMovie object.
	  *
	  * @param line - The database line to analyze
	  * @return A valid movie object
	  */
	public ValidMovie parseDatabaseLine(String line) throws MovieException
	{
		String[] splitLine = line.split("\\t+");
		if (splitLine.length < 4)
		{
			throw new TooFewFieldsException(String.valueOf(splitLine.length));
		}
		else if (splitLine.length > 4)
		{
			throw new TooManyFieldsException(String.valueOf(splitLine.length));
		}
		return new ValidMovie(splitLine[0], splitLine[1], splitLine[2], splitLine[3]);
	}
 
	public static void main(String[] args)
	{
		new MovieDB();
	}
}