Index: src/edu/ucsb/nceas/metacat/DBWriter.java
===================================================================
--- src/edu/ucsb/nceas/metacat/DBWriter.java	(revision 406)
+++ src/edu/ucsb/nceas/metacat/DBWriter.java	(nonexistent)
@@ -1,251 +0,0 @@
-/**
- *  '$RCSfile$'
- *    Purpose: A Class that reads in an XML text document and
- *             write its contents to a database connection using SAX
- *  Copyright: 2000 Regents of the University of California and the
- *             National Center for Ecological Analysis and Synthesis
- *    Authors: Matt Jones
- *    Release: @release@
- *
- *   '$Author$'
- *     '$Date$'
- * '$Revision$'
- */
-
-package edu.ucsb.nceas.metacat;
-
-import java.io.*;
-import java.sql.*;
-import java.util.Stack;
-
-import org.xml.sax.AttributeList;
-import org.xml.sax.ContentHandler;
-import org.xml.sax.DTDHandler;
-import org.xml.sax.EntityResolver;
-import org.xml.sax.ErrorHandler;
-import org.xml.sax.InputSource;
-import org.xml.sax.XMLReader;
-import org.xml.sax.SAXException;
-import org.xml.sax.SAXParseException;
-import org.xml.sax.helpers.XMLReaderFactory;
-
-/**
- * A Class that reads in an XML text document and
- * write its contents to a database connection using SAX
- */
-public class DBWriter {
-
-  private Connection	conn = null;
-  private String parserName = null;
-
-  /**
-   * the main routine used to test the DBWriter utility.
-   * <p>
-   * Usage: java DBWriter <-f filename -a action -d docid>
-   *
-   * @param filename the filename to be loaded into the database
-   */
-  static public void main(String[] args) {
-     
-    try {
-      String filename = null;
-      String action   = null;
-      String docid    = null;
-
-      for ( int i=0 ; i < args.length; ++i ) {
-        if ( args[i].equals( "-f" ) ) {
-          filename =  args[++i];
-        } else if ( args[i].equals( "-a" ) ) {
-          action =  args[++i];
-        } else if ( args[i].equals( "-d" ) ) {
-          docid =  args[++i];
-        } else {
-          System.err.println
-            ( "   args[" +i+ "] '" +args[i]+ "' ignored." );
-        }
-      }
-      
-      boolean argsAreValid = false;
-      if (action != null) {
-        if (action.equals("INSERT")) {
-          if (filename != null) {
-            argsAreValid = true;
-          } 
-        } else if (action.equals("UPDATE")) {
-          if ((filename != null) && (docid != null)) {
-            argsAreValid = true;
-          } 
-        } else if (action.equals("DELETE")) {
-          if (docid != null) {
-            argsAreValid = true;
-          } 
-        } 
-      } 
-
-      if (!argsAreValid) {
-        System.err.println("Wrong number of arguments!!!");
-        System.err.println(
-              "USAGE: java DBWriter <-f filename -a action -d docid>");
-        return;
-      }
- 
-      // Open a connection to the database
-      MetaCatUtil   util = new MetaCatUtil();
-      Connection dbconn = util.openDBConnection();
-
-      DBWriter dbw = new DBWriter(dbconn, util.getOption("saxparser"));
-      if (action.equals("DELETE")) {
-        dbw.delete(docid);
-        System.out.println("Document deleted: " + docid);
-      } else {
-        String newdocid = dbw.write(filename, action, docid);
-        if ((docid != null) && (!docid.equals(newdocid))) {
-          if (action.equals("INSERT")) {
-            System.out.println("New document ID generated!!! ");
-          } else if (action.equals("UPDATE")) {
-            System.out.println("ERROR: Couldn't update document!!! ");
-          }
-        } else if ((docid == null) && (action.equals("UPDATE"))) {
-            System.out.println("ERROR: Couldn't update document!!! ");
-        }
-        System.out.println("Document processing finished for: " + filename
-              + " (" + newdocid + ")");
-      }
-
-    } catch (AccessionNumberException ane) {
-      System.out.println("ERROR: Couldn't delete document!!! ");
-      System.out.println(ane.getMessage());
-    } catch (Exception e) {
-      System.err.println("EXCEPTION HANDLING REQUIRED");
-      System.err.println(e.getMessage());
-      e.printStackTrace(System.err);
-    }
-  }
-  
-  /**
-   * construct a new instance of the class to write an XML file to the database
-   *
-   * @param conn the database connection to which to write the XML file
-   * @param parserName the name of a SAX2 compliant parser class
-   */
-  public DBWriter(Connection conn, String parserName) {
-    this.conn = conn;
-    this.parserName = parserName;
-  }
-
-  /**
-   * Insert XML stream to the database
-   *
-   * @param xml the xml stream to be loaded into the database
-   */
-  public String write( Reader xml, String action, String docid )
-                throws Exception, IOException, SQLException, 
-                       ClassNotFoundException, SAXException, SAXParseException {
-    try {
-        XMLReader parser = initializeParser(action, docid);
-        conn.setAutoCommit(false);
-        parser.parse(new InputSource(xml));
-        conn.commit();
-        conn.setAutoCommit(true);
-        return docid;
-      } catch (SAXParseException e) {
-        conn.rollback();
-        throw e;
-      } catch (SAXException e) {
-
-        // If its a problem with the accession number its ok, just the 
-        // accession number was regenerated
-        AccessionNumberGeneratedException ang = null;
-        try {
-          Exception embedded = e.getException();
-          if ((embedded != null) && 
-              (embedded instanceof AccessionNumberGeneratedException)) {
-            ang = (AccessionNumberGeneratedException)e.getException();
-          }
-        } catch (ClassCastException cce) {
-          // Do nothing and just fall through to the ang != null test
-        }
-        if (ang != null) {
-          conn.commit();
-          conn.setAutoCommit(true);
-          return (ang.getMessage());
-        } else {
-          conn.rollback();
-          throw e;
-        }
-      } catch (Exception e) {
-        conn.rollback();
-        throw e;
-      }
-  }
-
-  /**
-   * Insert an XML file to the database
-   *
-   * @param filename the filename to be loaded into the database
-   */
-  public String write( String filename, String action, String docid )
-                throws Exception, IOException, SQLException, 
-                       ClassNotFoundException, SAXException, SAXParseException {
-     return write(new FileReader(new File(filename).toString()), action, docid);
-  }
-  
-  private XMLReader initializeParser(String action, String docid) {
-    XMLReader parser = null;
-    //
-    // Set up the SAX document handlers for parsing
-    //
-    try {
-      ContentHandler chandler   = new DBSAXHandler(conn, action, docid);
-      EntityResolver dbresolver = new DBEntityResolver(conn, 
-                                      (DBSAXHandler)chandler);
-      DTDHandler dtdhandler     = new DBDTDHandler(conn);
-
-      // Get an instance of the parser
-      parser = XMLReaderFactory.createXMLReader(parserName);
-
-      // Turn off validation
-      parser.setFeature("http://xml.org/sax/features/validation", false);
-      
-      // Set Handlers in the parser
-      parser.setProperty("http://xml.org/sax/properties/declaration-handler",
-                         chandler);
-      parser.setProperty("http://xml.org/sax/properties/lexical-handler",
-                         chandler);
-      parser.setContentHandler((ContentHandler)chandler);
-      parser.setEntityResolver((EntityResolver)dbresolver);
-      parser.setDTDHandler((DTDHandler)dtdhandler);
-      parser.setErrorHandler((ErrorHandler)chandler);
-
-    } catch (Exception e) {
-       System.err.println(e.toString());
-    }
-
-    return parser;
-  }
-
-  /**
-   * Delete an XML file from the database (actually, just make it a revision
-   * in the xml_revisions table)
-   *
-   * @param docid the ID of the document to be deleted from the database
-   */
-  public void delete( String docid )
-                  throws IOException, SQLException, 
-                         ClassNotFoundException, AccessionNumberException {
-
-    String newdocid = AccessionNumber.generate(docid, "DELETE");
-
-    conn.setAutoCommit(false);
-    // Copy the record to the xml_revisions table
-    DocumentImpl document = new DocumentImpl(conn);
-    document.saveDocument( docid );
-
-    // Now delete it from the xml_documents table
-    Statement stmt = conn.createStatement();
-    stmt.execute("DELETE FROM xml_index WHERE docid LIKE '" + docid + "'");
-    stmt.execute("DELETE FROM xml_documents WHERE docid LIKE '" + docid + "'");
-    stmt.close();
-    conn.commit();
-  }
-}

Property changes on: src/edu/ucsb/nceas/metacat/DBWriter.java
___________________________________________________________________
Deleted: svn:eol-style
## -1 +0,0 ##
-native
\ No newline at end of property
Deleted: svn:executable
## -1 +0,0 ##
-*
\ No newline at end of property
Deleted: svn:keywords
## -1 +0,0 ##
-Author Date Id Revision
\ No newline at end of property
Index: src/edu/ucsb/nceas/metacat/DocumentImpl.java
===================================================================
--- src/edu/ucsb/nceas/metacat/DocumentImpl.java	(revision 406)
+++ src/edu/ucsb/nceas/metacat/DocumentImpl.java	(revision 407)
@@ -17,13 +17,29 @@
 import java.io.PrintWriter;
 import java.util.TreeSet;
 
+import java.io.*;
+import java.util.Stack;
+
+import org.xml.sax.AttributeList;
+import org.xml.sax.ContentHandler;
+import org.xml.sax.DTDHandler;
+import org.xml.sax.EntityResolver;
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.InputSource;
+import org.xml.sax.XMLReader;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
+import org.xml.sax.helpers.XMLReaderFactory;
+
 /**
  * A class that represents an XML document. It can be created with a simple
- * document identifier from a database connection.
+ * document identifier from a database connection.  It also will write an
+ * XML text document to a database connection using SAX.
  */
 public class DocumentImpl {
 
   private Connection conn = null;
+  private MetaCatUtil util = null;
   private String docid = null;
   private String docname = null;
   private String doctype = null;
@@ -31,6 +47,7 @@
   private long rootnodeid;
   private ElementNode rootNode = null;
   private String doctitle = null;
+  private String parserName = null;
 
   /**
    * Constructor, creates document from database connection, used 
@@ -88,7 +105,7 @@
     this.docid = docid;
     writeDocumentToDB(action);
   }
-    
+
   /** 
    * Construct a new document instance, used for deleting documents
    *
@@ -97,6 +114,8 @@
   public DocumentImpl (Connection conn) 
   {
     this.conn = conn;
+    this.util = new MetaCatUtil();
+    this.parserName = util.getOption("saxparser");
   }
 
   /**
@@ -272,44 +291,90 @@
   }
 
   /**
-   * main routine used for testing.
-   * <p>
-   * Usage: java DocumentImpl <docid>
+   * Write an XML file to the database, given a filename
    *
-   * @param docid the id number of the document to display
+   * @param filename the filename to be loaded into the database
+   * @param action the action to be performed (INSERT OR UPDATE)
+   * @param docid the docid to use for the INSERT OR UPDATE
    */
-  static public void main(String[] args) {
-     
-     if (args.length < 1)
-     {
-        System.err.println("Wrong number of arguments!!!");
-        System.err.println("USAGE: java DocumentImpl <docid>");
-        return;
-     } else {
+  public String write( String filename, String action, String docid )
+                throws Exception, IOException, SQLException, 
+                       ClassNotFoundException, SAXException, SAXParseException {
+     return write(new FileReader(new File(filename).toString()), action, docid);
+  }
+  
+  /**
+   * Write an XML file to the database, given a Reader
+   *
+   * @param xml the xml stream to be loaded into the database
+   * @param action the action to be performed (INSERT OR UPDATE)
+   * @param docid the docid to use for the INSERT OR UPDATE
+   */
+  public String write( Reader xml, String action, String docid )
+                throws Exception, IOException, SQLException, 
+                       ClassNotFoundException, SAXException, SAXParseException {
+    try {
+        XMLReader parser = initializeParser(action, docid);
+        conn.setAutoCommit(false);
+        parser.parse(new InputSource(xml));
+        conn.commit();
+        conn.setAutoCommit(true);
+        return docid;
+      } catch (SAXParseException e) {
+        conn.rollback();
+        throw e;
+      } catch (SAXException e) {
+
+        // If its a problem with the accession number its ok, just the 
+        // accession number was regenerated
+        AccessionNumberGeneratedException ang = null;
         try {
-                    
-          String docid = args[0];
-    
-          // Open a connection to the database
-          MetaCatUtil   util = new MetaCatUtil();
-          Connection dbconn = util.openDBConnection();
-
-          DocumentImpl xmldoc = new DocumentImpl( dbconn, docid );
-          System.out.println(xmldoc.toString());
-
-        } catch (McdbException me) {
-          me.toXml(new PrintWriter(System.err));
-        } catch (Exception e) {
-          System.err.println("EXCEPTION HANDLING REQUIRED");
-          System.err.println(e.getMessage());
-          e.printStackTrace(System.err);
+          Exception embedded = e.getException();
+          if ((embedded != null) && 
+              (embedded instanceof AccessionNumberGeneratedException)) {
+            ang = (AccessionNumberGeneratedException)e.getException();
+          }
+        } catch (ClassCastException cce) {
+          // Do nothing and just fall through to the ang != null test
         }
-     }
+        if (ang != null) {
+          conn.commit();
+          conn.setAutoCommit(true);
+          return (ang.getMessage());
+        } else {
+          conn.rollback();
+          throw e;
+        }
+      } catch (Exception e) {
+        conn.rollback();
+        throw e;
+      }
   }
 
+  /**
+   * Delete an XML file from the database (actually, just make it a revision
+   * in the xml_revisions table)
+   *
+   * @param docid the ID of the document to be deleted from the database
+   */
+  public void delete( String docid )
+                  throws IOException, SQLException, 
+                         ClassNotFoundException, AccessionNumberException {
 
+    String newdocid = AccessionNumber.generate(docid, "DELETE");
 
+    conn.setAutoCommit(false);
+    // Copy the record to the xml_revisions table
+    DocumentImpl document = new DocumentImpl(conn);
+    document.archiveDocRevision( docid );
 
+    // Now delete it from the xml_documents table
+    Statement stmt = conn.createStatement();
+    stmt.execute("DELETE FROM xml_index WHERE docid LIKE '" + docid + "'");
+    stmt.execute("DELETE FROM xml_documents WHERE docid LIKE '" + docid + "'");
+    stmt.close();
+    conn.commit();
+  }
 
   /** creates SQL code and inserts new document into DB connection */
   private void writeDocumentToDB(String action) 
@@ -335,7 +400,7 @@
         this.docid = AccessionNumber.generate(docid, "UPDATE");
 
         // Save the old document entry in a backup table
-        saveDocument(docid);
+        archiveDocRevision(docid);
 
         // Update the new document to reflect the new node tree
         pstmt = conn.prepareStatement(
@@ -456,7 +521,7 @@
   }
 
   /** Save a document entry in the xml_revisions table */
-  public void saveDocument(String docid) throws SQLException {
+  private void archiveDocRevision(String docid) throws SQLException {
     // First get all of the values we need
     long rnodeid = -1;
     String docname = null;
@@ -503,4 +568,144 @@
     pstmt.execute();
     pstmt.close();
   }
+
+  /**
+   * Set up the parser handlers for writing the document to the database
+   */
+  private XMLReader initializeParser(String action, String docid) {
+    XMLReader parser = null;
+    //
+    // Set up the SAX document handlers for parsing
+    //
+    try {
+      ContentHandler chandler   = new DBSAXHandler(conn, action, docid);
+      EntityResolver dbresolver = new DBEntityResolver(conn, 
+                                      (DBSAXHandler)chandler);
+      DTDHandler dtdhandler     = new DBDTDHandler(conn);
+
+      // Get an instance of the parser
+      parser = XMLReaderFactory.createXMLReader(parserName);
+
+      // Turn off validation
+      parser.setFeature("http://xml.org/sax/features/validation", false);
+      
+      // Set Handlers in the parser
+      parser.setProperty("http://xml.org/sax/properties/declaration-handler",
+                         chandler);
+      parser.setProperty("http://xml.org/sax/properties/lexical-handler",
+                         chandler);
+      parser.setContentHandler((ContentHandler)chandler);
+      parser.setEntityResolver((EntityResolver)dbresolver);
+      parser.setDTDHandler((DTDHandler)dtdhandler);
+      parser.setErrorHandler((ErrorHandler)chandler);
+
+    } catch (Exception e) {
+       System.err.println(e.toString());
+    }
+
+    return parser;
+  }
+
+  /**
+   * the main routine used to test the DBWriter utility.
+   * <p>
+   * Usage: java DocumentImpl <-f filename -a action -d docid>
+   *
+   * @param filename the filename to be loaded into the database
+   * @param action the action to perform (READ, INSERT, UPDATE, DELETE)
+   * @param docid the id of the document to process
+   */
+  static public void main(String[] args) {
+     
+    try {
+      String filename = null;
+      String action   = null;
+      String docid    = null;
+
+      for ( int i=0 ; i < args.length; ++i ) {
+        if ( args[i].equals( "-f" ) ) {
+          filename =  args[++i];
+        } else if ( args[i].equals( "-a" ) ) {
+          action =  args[++i];
+        } else if ( args[i].equals( "-d" ) ) {
+          docid =  args[++i];
+        } else {
+          System.err.println
+            ( "   args[" +i+ "] '" +args[i]+ "' ignored." );
+        }
+      }
+      
+      boolean argsAreValid = false;
+      if (action != null) {
+        if (action.equals("INSERT")) {
+          if (filename != null) {
+            argsAreValid = true;
+          } 
+        } else if (action.equals("UPDATE")) {
+          if ((filename != null) && (docid != null)) {
+            argsAreValid = true;
+          } 
+        } else if (action.equals("DELETE")) {
+          if (docid != null) {
+            argsAreValid = true;
+          } 
+        } else if (action.equals("READ")) {
+          if (docid != null) {
+            argsAreValid = true;
+          } 
+        } 
+      } 
+
+      if (!argsAreValid) {
+        System.err.println("Wrong number of arguments!!!");
+        System.err.println(
+              "USAGE: java DocumentImpl <-a INSERT> [-d docid] <-f filename>");
+        System.err.println(
+              "   OR: java DocumentImpl <-a UPDATE -d docid -f filename>");
+        System.err.println(
+              "   OR: java DocumentImpl <-a DELETE -d docid>");
+        System.err.println(
+              "   OR: java DocumentImpl <-a READ -d docid>");
+        return;
+      }
+ 
+      // Open a connection to the database
+      MetaCatUtil util = new MetaCatUtil();
+      Connection dbconn = util.openDBConnection();
+
+      if (action.equals("READ")) {
+          DocumentImpl xmldoc = new DocumentImpl( dbconn, docid );
+          System.out.println(xmldoc.toString());
+      } else {
+        DocumentImpl doc = new DocumentImpl(dbconn);
+        if (action.equals("DELETE")) {
+          doc.delete(docid);
+          System.out.println("Document deleted: " + docid);
+        } else {
+          String newdocid = doc.write(filename, action, docid);
+          if ((docid != null) && (!docid.equals(newdocid))) {
+            if (action.equals("INSERT")) {
+              System.out.println("New document ID generated!!! ");
+            } else if (action.equals("UPDATE")) {
+              System.out.println("ERROR: Couldn't update document!!! ");
+            }
+          } else if ((docid == null) && (action.equals("UPDATE"))) {
+              System.out.println("ERROR: Couldn't update document!!! ");
+          }
+          System.out.println("Document processing finished for: " + filename
+                + " (" + newdocid + ")");
+        }
+      }
+
+    } catch (McdbException me) {
+      me.toXml(new PrintWriter(System.err));
+    } catch (AccessionNumberException ane) {
+      System.out.println("ERROR: Couldn't delete document!!! ");
+      System.out.println(ane.getMessage());
+    } catch (Exception e) {
+      System.err.println("EXCEPTION HANDLING REQUIRED");
+      System.err.println(e.getMessage());
+      e.printStackTrace(System.err);
+    }
+  }
 }
Index: src/edu/ucsb/nceas/metacat/MetaCatServlet.java
===================================================================
--- src/edu/ucsb/nceas/metacat/MetaCatServlet.java	(revision 406)
+++ src/edu/ucsb/nceas/metacat/MetaCatServlet.java	(revision 407)
@@ -662,7 +662,7 @@
             // get a connection from the pool
             conn = util.getConnection();
             // write the document to the database
-            DBWriter dbw = new DBWriter(conn, saxparser);
+            DocumentImpl doc = new DocumentImpl(conn);
 
             try {
                 String accNumber = docid[0];
@@ -669,9 +669,9 @@
                 if (accNumber.equals("")) {
                     accNumber = null;
                 }
-                newdocid = dbw.write(xml, doAction, accNumber);  
+                newdocid = doc.write(xml, doAction, accNumber);  
             } catch (NullPointerException npe) {
-              newdocid = dbw.write(xml, doAction, null);
+              newdocid = doc.write(xml, doAction, null);
             }
         } catch (Exception e) {
           response.setContentType("text/html");
@@ -724,12 +724,12 @@
     try {
       // get a connection from the pool
       conn = util.getConnection();
-      DBWriter dbw = new DBWriter(conn, saxparser);
+      DocumentImpl doc = new DocumentImpl(conn);
                                       // NOTE -- NEED TO TEST HERE
                                       // FOR EXISTENCE OF PARAM
                                       // BEFORE ACCESSING ARRAY
       try { 
-        dbw.delete(docid[0]);
+        doc.delete(docid[0]);
         response.setContentType("text/xml");
         out.println("<?xml version=\"1.0\"?>");
         out.println("<success>");
Index: bin/loadxml
===================================================================
--- bin/loadxml	(revision 406)
+++ bin/loadxml	(revision 407)
@@ -6,4 +6,4 @@
 
 export CPATH=$PDIR/build/metacat.jar:$PARSER:$JDBC
 
-java -cp ${CPATH} edu.ucsb.nceas.metacat.DBWriter $@
+java -cp ${CPATH} edu.ucsb.nceas.metacat.DocumentImpl $@
Index: bin/readxml
===================================================================
--- bin/readxml	(revision 406)
+++ bin/readxml	(revision 407)
@@ -6,4 +6,4 @@
 
 export CPATH=$PDIR/build/metacat.jar:$PARSER:$JDBC
 
-java -cp ${CPATH} edu.ucsb.nceas.metacat.DocumentImpl $1
+java -cp ${CPATH} edu.ucsb.nceas.metacat.DocumentImpl $@
