RefEntry Class

package org.mcraig.cs445.refentry;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Properties;
import java.util.StringTokenizer;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;

/** 
 * A single RefEntry document.
 * @author <a href="mailto:mark@mcraig.org">Mark Craig</a>
 */
class RefEntry implements Comparable, Serializable {

    /** XML source of the document. */
    private File InFile;
    /** XHTML div of the document. */
    private File OutFile;
    /** XSLT stylesheets. */
    private Properties StyleSheets;

    /** Default constructor. 
      * @param infile XML source.
      * @param outifile XHTML format output.
      * @param xslt Set of XSLT stylesheets.
      */
    RefEntry(File infile, File outfile, Properties xslt) {
        if (infile.canRead()) InFile = infile;
        OutFile = new File(outfile.getPath());
        if (!xslt.isEmpty()) StyleSheets = xslt;

        setRefName();
        setManVolNum();
        setRefPurpose();
        setKeys();
        format();

        setSource();
        setXHTML();
    }
    
    /** 
     * Apply a stylesheet to the source.
     * @param xml Source to transform.
     * @param xslt Stylesheet to apply.
     * @param out Output file.
     */
    private File applyTransform(File xml, File xslt, File out) {
        try {
            BufferedReader rin  = new BufferedReader(new FileReader(xml));
            BufferedReader rxsl = new BufferedReader(new FileReader(xslt));
            BufferedWriter wout = new BufferedWriter(new FileWriter(out));

            StreamSource src = new StreamSource(rin);
            StreamSource xsl = new StreamSource(rxsl);
            StreamResult res = new StreamResult(wout);

            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer        tr = tf.newTransformer(xsl);
            tr.transform(src, res);

            rin.close(); rxsl.close(); wout.close();
        } catch (Exception e) {
            System.err.println(e.toString());
        }
        return out;
    }
    
    /** 
     * Comparison between RefEntries.
     * @param other Object to compare this one to.
     */
    public int compareTo(Object other) {
        if (this == other) return 0;
        int bySect = ManVolNum.compareTo(((RefEntry)other).getManVolNum());
        if (bySect != 0) return bySect;
        int byName = RefName.compareTo(((RefEntry)other).getRefName());
        if (byName != 0) return byName;
        return RefPurpose.compareTo(((RefEntry)other).getRefPurpose());
    }
    
    /** Format the document as an XHTML div. */
    private void format() {
        try {
            File xslt = new File(StyleSheets.getProperty("RefEntryXSLT"));
            OutFile   = applyTransform(InFile, xslt, OutFile);
        } catch (Exception e) {
            System.err.println(e.toString());
        }
    }

    /** Return an ID for the page. */
    String getID() {
        return (new String(getRefName() + "-" + getManVolNum()));
    }

    /**
     * Get strings from the document content to use
     * when indexing. No strings shorter than three
     * characters are returned.
     * @param ignore Strings not to return in the results.
     */
    HashSet getKeys(HashSet ignore) {
        HashSet keys = new HashSet(Keys);
        keys.removeAll(ignore);
        return keys;
    }
    /** Strings to use when indexing and searching. */
    HashSet Keys;
    /** Build a set of Strings to use when indexing and searching. */
    private void setKeys() {
        Keys       = new HashSet();
        String str = new String();
        try {
            File xsl = new File(StyleSheets.getProperty("StripXMLXSLT"));
            File out = File.createTempFile("keys-", ".out");
            out      = applyTransform(InFile, xsl, out);

            BufferedReader br = new BufferedReader(new FileReader(out));
            String         tmp;
            while ((tmp = br.readLine()) != null) str = str.concat(tmp + " ");

            br.close();
            out.delete();
        } catch (Exception e) {
            System.err.println(e.toString());
        }

        String TokenDelimiters =
            new String(" \t\n\f\r!\"#$%&'()*+,./:;<=>?@[\\]^_`{|}~");
        StringTokenizer in = new StringTokenizer(str, TokenDelimiters);
        while (in.hasMoreTokens()) {
            str = in.nextToken();
            if (str.matches("\\w+") && (str.length() >= 3))
                Keys.add(str.toLowerCase());
        }
    }

    /** Return XML document as a String. */
    String getSource() { return Source; }
    /** XML document content as a String. */
    private String Source;
    /** Build a String holding the XML document. */
    private void setSource() { Source = fileToString(InFile); }

    /** Return XHTML as a string. */
    String getXHTML() { return XHTML; }
    /** Formatted document content as a String. */
    private String XHTML;
    /** Build a String holding the formatted document content. */
    private void setXHTML() { XHTML = fileToString(OutFile); }

    /** 
     * Helper method to manage translations. 
     * @param file File to convert to a string.
     */
    private String fileToString(File file) {
        String str = new String();
        try {
            BufferedReader br = new BufferedReader(new FileReader(file));
            String tmp;
            while ((tmp = br.readLine()) != null)
                str = str.concat(tmp + "\n");
            br.close();
        } catch (Exception e) {
            System.err.println(e.toString());
        }
        return str;
    }

    /** Content of the manvolnum element. */
    private String ManVolNum;
    /** Return the content of the manvolnum element. */
    String getManVolNum() { return ManVolNum; }
    /** Set content of manvolnum element. */
    private void setManVolNum() {
        try {
            File xslt = new File(StyleSheets.getProperty("ManVolNumXSLT"));
            File out  = File.createTempFile("mvn-", ".out");
            BufferedReader br = 
                new BufferedReader(
                    new FileReader(applyTransform(InFile, xslt, out))); 
            ManVolNum = br.readLine().trim();
            br.close();
            out.delete();
        } catch (Exception e) {
            System.err.println(e.toString());
        }
    }

    /** Content of the refname element. */
    private String RefName;
    /** Return the content of the refname element. */
    String getRefName() { return RefName; }
    /** Set content of refname element. */
    private void setRefName() {
        try {
            File xslt = new File(StyleSheets.getProperty("RefNameXSLT"));
            File out  = File.createTempFile("rn-", ".out");
            BufferedReader br = 
                new BufferedReader(
                    new FileReader(applyTransform(InFile, xslt, out))); 
            RefName   = br.readLine().trim();
            br.close();
            out.delete();
        } catch (Exception e) {
            System.err.println(e.toString());
        }
    }
    
    /** Content of the refpurpose element. */
    private String RefPurpose;
    /** Return the content of the refpurpose element. */
    String getRefPurpose () { return RefPurpose;}
    /** Set content of refpurpose element. */
    private void setRefPurpose() {
        try {     
            File xslt  = new File(StyleSheets.getProperty("RefPurposeXSLT"));
            File out   = File.createTempFile("rp-", ".out");
            BufferedReader br = 
                new BufferedReader(
                    new FileReader(applyTransform(InFile, xslt, out))); 
            RefPurpose = br.readLine().trim();
            br.close();
            out.delete();
        } catch (Exception e) {
            System.err.println(e.toString());
        }
    }
}