Project

General

Profile

1
/**
2
 *      Name: DBWriter.java
3
 *   Purpose: A Class that reads in an XML text document and
4
 *            write its contents to a database connection using SAX
5
 * Copyright: 2000 Regents of the University of California and the
6
 *            National Center for Ecological Analysis and Synthesis
7
 *   Authors: Matt Jones
8
 *
9
 *   Version: '$Id: DBWriter.java 184 2000-06-21 02:57:19Z jones $'
10
 */
11

    
12
package edu.ucsb.nceas.metacat;
13

    
14
import org.xml.sax.*;
15

    
16
import java.io.*;
17
import java.net.URL;
18
import java.net.MalformedURLException;
19
import java.sql.*;
20
import java.util.Stack;
21

    
22
// Extensions to the SAX Interfaces for Namespace support.
23
import oracle.xml.parser.v2.XMLDocumentHandler;
24
import oracle.xml.parser.v2.DefaultXMLDocumentHandler;
25
import oracle.xml.parser.v2.NSName;
26
import oracle.xml.parser.v2.SAXAttrList;
27

    
28
import oracle.xml.parser.v2.SAXParser;
29

    
30
/**
31
 * A Class that reads in an XML text document and
32
 * write its contents to a database connection using SAX
33
 */
34
public class DBWriter {
35

    
36
  private Connection	conn = null;
37

    
38
  /**
39
   * the main routine used to test the DBWriter utility.
40
   * <p>
41
   * Usage: java DBWriter <filename>
42
   *
43
   * @param filename the filename to be loaded into the database
44
   */
45
  static public void main(String[] args) {
46
     
47
     if (args.length < 1)
48
     {
49
        System.err.println("Wrong number of arguments!!!");
50
        System.err.println("USAGE: java DBWriter <filename>");
51
        return;
52
     } else {
53
        try {
54
                    
55
          String filename = args[0];
56

    
57
          // Open a connection to the database
58
          MetaCatUtil   util = new MetaCatUtil();
59
          Connection dbconn = util.openDBConnection();
60

    
61
          new DBWriter(filename, dbconn);
62
          System.out.println("Document processing finished for: " + filename);
63

    
64
        } catch (Exception e) {
65
          System.err.println("EXCEPTION HANDLING REQUIRED");
66
          System.err.println(e.getMessage());
67
          e.printStackTrace(System.err);
68
        }
69
     }
70
  }
71
  
72
  /**
73
   * construct a new instance of the class to write an XML file to the database
74
   *
75
   * @param filename the filename to be loaded into the database
76
   * @param conn the database connection to which to write the XML file
77
   */
78
  public DBWriter( Reader xml, Connection conn)
79
                  throws IOException, 
80
                         SQLException, 
81
                         ClassNotFoundException
82
  {
83
    this.conn = conn;
84

    
85
    try {
86
        SAXParser parser = initializeParser(conn);
87
        conn.setAutoCommit(false);
88
        parser.parse(xml);
89
        conn.commit();
90
        conn.setAutoCommit(true);
91
      } catch (SAXParseException e) {
92
        conn.rollback();
93
        System.err.println(e.getMessage());
94
      } catch (SAXException e) {
95
        conn.rollback();
96
        System.err.println(e.getMessage());
97
      } catch (Exception e) {
98
        conn.rollback();
99
        System.err.println(e.toString());
100
      }
101
  }
102

    
103
  public DBWriter( String filename, Connection conn)
104
                  throws IOException, 
105
                         SQLException, 
106
                         ClassNotFoundException
107
  {
108
     this.conn = conn;
109
     FileReader xmlfile = new FileReader(new File(filename).toString());
110
     try {
111
       SAXParser parser = initializeParser(conn);
112
       conn.setAutoCommit(false);
113
       parser.parse(xmlfile);
114
       conn.commit();
115
       conn.setAutoCommit(true);
116
     } catch (SAXParseException e) {
117
       System.err.println(e.getMessage());
118
     } catch (SAXException e) {
119
       System.err.println(e.getMessage());
120
     } catch (Exception e) {
121
       System.err.println("from DBWriter:" + e.toString());
122
     }
123
  }
124
  
125
  private SAXParser initializeParser(Connection conn) {
126
    SAXParser parser = null;
127
    //
128
    // Set up the SAX document handlers for parsing
129
    //
130
    try {
131
      // Use the XMLDocumentHandler interface for namespace support
132
      // instead of org.xml.sax.DocumentHandler
133
      XMLDocumentHandler xmlDocHandler = new DBSAXHandler(conn);
134
      EntityResolver xmlEntityResolver = new DBEntityResolver(conn);
135
      DTDHandler xmlDTDHandler         = new DBDTDHandler(conn);
136

    
137
      // For all the other interface use the default provided by
138
      // Handler base
139
      HandlerBase defHandler = new HandlerBase();
140

    
141
      // Get an instance of the parser
142
      parser = new SAXParser();
143

    
144
      // Set Handlers in the parser
145
      // Set the DocumentHandler to XMLDocumentHandler
146
      parser.setDocumentHandler(xmlDocHandler);
147
      parser.setEntityResolver(xmlEntityResolver);
148
      parser.setDTDHandler(xmlDTDHandler);
149

    
150
      // Set the other Handler to the defHandler
151
      parser.setErrorHandler(defHandler);
152

    
153
    } catch (Exception e) {
154
       System.err.println(e.toString());
155
    }
156

    
157
    return parser;
158
  }
159

    
160
  /** Utility method to convert a file handle into a URL */
161
  static public URL fileToURL(File file) 
162
  {
163
     String path = file.getAbsolutePath();
164
     String fSep = System.getProperty("file.separator");
165
     if (fSep != null && fSep.length() == 1)
166
       path = path.replace(fSep.charAt(0), '/');
167
     if (path.length() > 0 && path.charAt(0) != '/')
168
       path = '/' + path;
169
     try {
170
       return new URL("file", null, path);
171
     }
172
     catch (java.net.MalformedURLException e) {
173
       /* According to the spec this could only happen if the file
174
	  protocol were not recognized. */
175
       throw new Error("unexpected MalformedURLException");
176
     }
177
  }
178
}
(13-13/20)