package org.mcraig.cs445.refentry;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Properties;
/**
* Generates and serializes a RefEntryBackEnd referencing a number
* of RefEntry documents.
* @author <a href="mailto:mark@mcraig.org">Mark Craig</a>
*/
class BackEndGenerator {
/** Default constructor. */
public BackEndGenerator() { CommonWords = new HashSet(); }
/**
* Generate a serialized RefEntryBackEnd.
* @param sources HashSet of RefEntry documents to add.
* @param serialized Path to serialized output BackEnd.
*/
public void generate(HashSet sources, String serialized) {
RefEntryBackEnd backend = new RefEntryBackEnd(CommonWords);
Iterator iter = sources.iterator();
while (iter.hasNext()) backend.add((RefEntry)iter.next());
try {
ObjectOutputStream os =
new ObjectOutputStream(
new BufferedOutputStream(
new FileOutputStream(serialized)));
os.writeObject(backend);
os.close();
} catch (Exception e) {
System.err.println("In BackEndGenerator::generate...");
System.err.println("Failed to serialze BackEnd: " + serialized);
System.err.println(e.toString());
}
}
/**
* Mutator for set of common words to ignore.
* @param words Path to serialized HashSet of common words.
*/
public void setCommonWords(String words) {
try {
ObjectInputStream os =
new ObjectInputStream(
new BufferedInputStream(
new FileInputStream(words)));
CommonWords = (HashSet)os.readObject();
os.close();
} catch (Exception e) {
System.err.println("In BackEndGenerator::setCommonWords...");
System.err.println("Failed to open common words: " + words);
System.err.println(e.toString());
}
}
/** Set of CommonWords to ignore. */
private HashSet CommonWords;
} |