Changeset 3593


Ignore:
Timestamp:
2002-11-30T15:07:07+13:00 (21 years ago)
Author:
say1
Message:

more comments and error handling

Location:
trunk/gsdl3/src/java/org/greenstone/gsdl3/selfContained
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/selfContained/DocumentStream.java

    r3548 r3593  
    2323
    2424// other java classes
     25import java.io.Serializable;
    2526
    2627
     
    3536 *
    3637 */
    37 interface DocumentStream {
     38public interface DocumentStream extends Cloneable, Serializable {
    3839 
    3940  /**
     
    4344   * @return the next document
    4445   */
    45   public Document nextDocument() throws Exception;
     46  public Document nextDocument();
    4647  /**
    4748   * Is there a next document?
     
    5051   * @return true if there is a next document
    5152   */
    52   public boolean hasNextDocument() throws Exception;
     53  public boolean hasNextDocument();
    5354 
    5455}
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/selfContained/FilesDocumentStream.java

    r3588 r3593  
    4141 * @see DocumentStream
    4242 */
    43 class DocumentStreamFromFiles implements DocumentStream {
     43public class FilesDocumentStream
     44  implements DocumentStream {
    4445  /** the files we've found so far */
    4546  Vector files = new Vector();
     
    5354   * @param filename the filename of the file or directory
    5455   */
    55   public DocumentStreamFromFiles(String filename) throws IOException {
     56  public FilesDocumentStream(String filename) {
    5657    File file = new File(filename);
    5758    if (file.exists() && file.isDirectory())
     
    6667   * @param file a handle to the file
    6768   */
    68   public DocumentStreamFromFiles(File file) {
     69  public FilesDocumentStream(File file) {
    6970    if (file.exists() && file.isDirectory())
    7071      directories.add(file);
     
    7879   * @exception java.io.IOException when the underlying file access does
    7980   */
    80   protected void expandIfNecessary() throws IOException {
     81  protected void expandIfNecessary() {
    8182    while (files.size() < 1 || directories.size() > 1) {
    8283      File file = (File) directories.elementAt(directories.size() - 1);
    8384      directories.removeElementAt(directories.size() - 1);
    84       if (!file.exists()) throw new IOException ("error in expand: expecting a directory");
     85      if (!file.exists()) throw new Error ("error in expand: expecting a directory");
    8586      File[] fileArray = file.listFiles();
    8687      for (int i=0;i<fileArray.length;i++){
     
    99100   * @return the next document
    100101   */
    101   public Document nextDocument() throws Exception {
    102     if (!hasNextDocument()) throw new Error("Doesn't have another Document");
    103     File file = (File) files.elementAt(files.size() - 1);
    104     files.removeElementAt(files.size() - 1);
    105      
    106     Reader reader = new FileReader(file);
    107     InputSource xml_source = new InputSource(reader);
    108          
    109     XMLUtil.getDOMParser().parse(xml_source);
    110     Document doc = XMLUtil.getDOMParser().getDocument();
    111      
    112     return doc;
     102  public Document nextDocument() {
     103    try {
     104      if (!hasNextDocument()) throw new Error("Doesn't have another Document");
     105      File file = (File) files.elementAt(files.size() - 1);
     106      files.removeElementAt(files.size() - 1);
     107       
     108      Reader reader = new FileReader(file);
     109      InputSource xml_source = new InputSource(reader);
     110       
     111      XMLUtil.getDOMParser().parse(xml_source);
     112      Document doc = XMLUtil.getDOMParser().getDocument();
     113       
     114      return doc;
     115    } catch (org.xml.sax.SAXException s) {
     116      System.err.println("SAXException in nextDocument()");
     117      throw new Error(s.toString());
     118    } catch (IOException e) {
     119      System.err.println("IOException in nextDocument()");
     120      throw new Error(e.toString());
     121    }
    113122  }
    114123   
     
    120129   */
    121130
    122   public boolean hasNextDocument() throws IOException {
     131  public boolean hasNextDocument() {
    123132    expandIfNecessary();
    124133    return (files.size() > 1);
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/selfContained/GeneratedDocumentStream.java

    r3548 r3593  
    9595   * @see org.w3c.dom.Document
    9696   */
    97   public Document nextDocument() throws Exception {
     97  public Document nextDocument() {
    9898
    9999
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/selfContained/Increment.java

    r3548 r3593  
    4646   * @see Increment
    4747   */
    48   abstract String incrementString(String str) throws Exception;
     48  abstract String incrementString(String str);
    4949
    5050  /**
     
    5656   * @see Increment
    5757   */
    58   abstract long incrementStream(Reader rdr, Writer wtr) throws Exception;
     58  abstract long incrementStream(Reader rdr, Writer wtr);
    5959
    6060  /**
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/selfContained/IncrementRange.java

    r3548 r3593  
    4242  implements Cloneable, Serializable
    4343{
    44  /**
     44  /**
    4545   * Create an IncrementRange which counts between first and last inclusive
    4646   *
     
    8383   * Increment a stream of characters, reading from the reader and writing the incremented characters to the writer
    8484   *
    85    * @exception java.io.Exception if any of the characters in the string are outside the range of first-last
     85   * @exception java.io.Error if any of the characters in the string are outside the range of first-last
    8686   * @param str the string to be incremented
    8787   * @return the incremented string
    8888   * @see Increment
    8989   */
    90   long incrementStream(Reader rdr, Writer wtr)
    91     throws Exception {
    92     long count = 0; // the return value
     90  long incrementStream(Reader rdr, Writer wtr) {
     91    try {
     92      long count = 0; // the return value
    9393   
    94     int i = rdr.read();
    95     boolean carry = true;
     94      int i = rdr.read();
     95      boolean carry = true;
    9696   
    97     while (i != -1) {
    98       char c = (char) i;
     97      while (i != -1) {
     98    char c = (char) i;
    9999     
    100       // check the validity of the character we're about to increment
    101       if (c < first || c > last)
    102     throw new Exception("Character '" + c + "' is ouside range");
     100    // check the validity of the character we're about to increment
     101    if (c < first || c > last)
     102      throw new Error("Character '" + c + "' is ouside range");
    103103     
    104       if (c == last) {
    105     if (carry == true) {
    106       wtr.write(first);
    107       carry = true;
     104    if (c == last) {
     105      if (carry == true) {
     106        wtr.write(first);
     107        carry = true;
     108      } else {
     109        wtr.write(c);
     110        carry = false;
     111      }
    108112    } else {
    109       wtr.write(c);
    110       carry = false;
     113      if (carry == true) {
     114        wtr.write(++c);
     115        carry = false;
     116      } else {
     117        wtr.write(c);
     118        carry = false;
     119      }
    111120    }
    112       } else {
    113     if (carry == true) {
    114       wtr.write(++c);
    115       carry = false;
    116     } else {
    117       wtr.write(c);
    118       carry = false;
    119     }
    120       }
    121       count++;
    122 
    123       // read the next character
    124       i = rdr.read();
    125     }
    126     if (carry == true) {
    127       wtr.write(first);
    128       count++;
    129     }
    130     return count;
     121    count++;
     122
     123    // read the next character
     124    i = rdr.read();
     125      }
     126      if (carry == true) {
     127    wtr.write(first);
     128    count++;
     129      }
     130      return count;
     131    } catch (IOException e) {
     132      System.err.println("IOException in incrementStream()");
     133      throw new Error(e.toString());
     134    }
    131135  }
    132136
     
    134138   * Increment a string of characters
    135139   *
    136    * @exception java.io.Exception if any of the characters in the string are outside the range of first-last
     140   * @exception java.lang.Error if any of the characters in the string are outside the range of first-last
    137141   * @param str the string to be incremented
    138142   * @return the incremented string
    139143   * @see Increment
    140144   */
    141   public final String incrementString(String str)
    142     throws Exception {
     145  public final String incrementString(String str)  {
    143146    StringBuffer buffer = new StringBuffer();
    144147    if (str == "") {
     
    150153    char c = str.charAt(i);
    151154    if (c < first || c > last)
    152       throw new Exception("Character '" + c + "' is ouside range");
     155      throw new Error("Character '" + c + "' is ouside range");
    153156
    154157    if (c == last) {
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/selfContained/QueryDocumentStream.java

    r3592 r3593  
    11/*
    2  *    QueryDocumentStream.java.java
     2 *    QueryDocumentStream.java
    33 *    Copyright (C) 2002 New Zealand Digital Library, http://www.nzdl.org
    44 *
     
    2525import javax.xml.transform.TransformerException;
    2626import javax.xml.transform.TransformerFactory;
    27 import javax.xml.transform.Result;
    28 import javax.xml.transform.Source;
    2927import javax.xml.transform.dom.DOMResult;
    3028import javax.xml.transform.dom.DOMSource;
    3129import javax.xml.transform.stream.StreamResult;
    3230import javax.xml.transform.stream.StreamSource;
    33 import org.apache.xalan.xslt.*;
    34 import org.apache.xerces.dom.DocumentImpl;
    35 import org.apache.xerces.dom.ElementImpl;
    36 import org.apache.xerces.dom.TextImpl;
    3731import org.w3c.dom.Document;
    38 import org.w3c.dom.Node;
    39 import org.w3c.dom.NodeList;
    4032
    4133// other java classes
     
    4436/**
    4537 * QueryDocumentStream takes another document stream and removes
    46  * from the stream all documents which fail to match a XSLT query
    47  *
     38 * from the stream all documents which fail to match a XSLT query.
     39 * The default query returns only those documents containing the
     40 * string "the".
    4841 *
    4942 * @author <a href="http://www.cs.waikato.ac.nz/~say1/">stuart yeates</a> (<a href="mailto:[email protected]">s
     
    6760    "   </xsl:template>" +
    6861    "</xsl:stylesheet>";
    69 
     62  /** The first part of the default quesy for a string other than "the" */
    7063  public static final String DEFAULT_QUERY_STRING_1_1 =   
    7164    "<xsl:stylesheet version=\"1.0\" " +
     
    7669    "      <xsl:if test=\"contains(.,'";
    7770
     71  /** The second part of the default quesy for a string other than "the" */
    7872  public static final String DEFAULT_QUERY_STRING_1_2 =   
    7973    "')\">" +
     
    8478
    8579
    86   /** The next string */
     80  /** The query */
    8781  protected StreamSource query = new StreamSource(new StringReader(DEFAULT_QUERY_STRING));
    8882
     
    9286  /** A cached Document */
    9387  protected Document cached = null;
     88
     89  /** The transformer factory */
    9490  TransformerFactory transformerFactory  = null;
    95     Transformer transformer  = null;
     91
     92  /** The transformer */
     93  Transformer transformer  = null;
    9694
    9795  /** Uses the default query (looking for the) over the default DocumentStream (all ascii strings) */
     
    161159    }
    162160  }
    163  
    164 
    165  
    166  
    167161  /**
    168162   * Generates the nest document in the sequence
     
    172166   * @see org.w3c.dom.Document
    173167   */
    174   public Document nextDocument() throws Exception {
     168  public Document nextDocument() {
    175169
    176170    if (hasNextDocument() == false)
    177       throw new Exception("no more docs");
     171      throw new Error("no more docs");
    178172    Document result = cached;
    179173    cached = null;
     
    185179   * @return true if there are more docs
    186180   */
    187   public boolean hasNextDocument() throws Exception {
    188     lookInStream()
    189       return (cached != null);
     181  public boolean hasNextDocument() {
     182    lookInStream();
     183    return (cached != null);
    190184  }
    191185  /**
     
    199193   * @see blank
    200194   */
    201   protected boolean lookInStream() throws Exception {
     195  protected boolean lookInStream() {
    202196
    203197    while (stream.hasNextDocument() && cached == null) {
     
    210204      try {
    211205   
    212       transformer.transform(input, output);
    213      
    214       // if the transformed document is not empty don't cache it
    215       if (output.getNode().getFirstChild() != null)
    216     cached = (Document) output.getNode();
    217      
     206    transformer.transform(input, output);
     207     
     208    // if the transformed document is not empty don't cache it
     209    if (output.getNode().getFirstChild() != null)
     210      cached = (Document) output.getNode();
     211     
     212      } catch (TransformerConfigurationException e) {
     213    System.err.println("XMLTransformer: couldn't create transformer object: "+e.getMessage());
    218214      } catch (TransformerException e) {
    219215    System.err.println("XMLTransformer: couldn't transform the source: " + e.getMessage());
     
    228224      if (false) { // debugging info
    229225   
    230     StreamResult result = new StreamResult(System.out);
    231     TransformerFactory transformerFactory = TransformerFactory.newInstance();
    232     Transformer transformer = transformerFactory.newTransformer();
    233    
    234     Source source = new DOMSource(candidate);
    235     transformer.transform(source,result);
    236    
    237     System.out.print(" ==> ");
    238    
    239     if (cached != null) {
    240       Source source2 = new DOMSource(cached);
    241       transformer.transform(source2,result);
    242     }
    243     System.out.println();
    244     System.out.println("======================================================");
     226    try {
     227      StreamResult result = new StreamResult(System.out);
     228      Transformer transformer = transformerFactory.newTransformer();
     229     
     230      Source source = new DOMSource(candidate);
     231      transformer.transform(source,result);
     232     
     233      System.out.print(" ==> ");
     234     
     235      if (cached != null) {
     236        Source source2 = new DOMSource(cached);
     237        transformer.transform(source2,result);
     238      }
     239      System.out.println();
     240      System.out.println("======================================================");
     241     
     242    } catch (TransformerConfigurationException e) {
     243      System.err.println("XMLTransformer: couldn't create transformer object: "+e.getMessage());
     244    } catch (TransformerException e) {
     245      System.err.println("XMLTransformer: couldn't transform the source: " + e.getMessage());
     246      cached = null;
     247      return false;
     248    } catch (Exception e) {
     249      System.err.println("Exception: " + e.getMessage());
     250      cached = null;
     251      return false;
     252    }
    245253   
    246254      }
    247255    }
    248     return cached == null;
    249    
     256    return cached == null;   
    250257  }
    251258
Note: See TracChangeset for help on using the changeset viewer.