Project

General

Profile

1 15 jones
/**
2 168 jones
 *      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 15 jones
 *
9 168 jones
 *   Version: '$Id$'
10 15 jones
 */
11
12 75 jones
package edu.ucsb.nceas.metacat;
13 51 jones
14 15 jones
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 jones
/**
31
 * A Class that reads in an XML text document and
32
 * write its contents to a database connection using SAX
33
 */
34 144 jones
public class DBWriter {
35 15 jones
36 72 bojilova
  static  String 	defaultDB = "jdbc:oracle:thin:@penelope.nceas.ucsb.edu:1526:DEV8";
37 15 jones
  private Connection	conn = null;
38
39 31 jones
  /**
40 144 jones
   * the main routine used to test the DBWriter utility.
41 31 jones
   *
42 144 jones
   * Usage: java DBWriter <filename> <user> <password> [dbstring]
43 31 jones
   *
44
   * @param filename the filename to be loaded into the database
45
   * @param user the username to use for the database connection
46
   * @param password the password to use for the database connection
47
   * @param dbstring the connection info to use for the database connection
48
   */
49 15 jones
  static public void main(String[] args) {
50
51
     if (args.length < 3)
52
     {
53
        System.err.println("Wrong number of arguments!!!");
54 144 jones
        System.err.println("USAGE: java DBWriter " +
55 15 jones
                           "<filename> <user> <password> [dbstring]");
56
        return;
57
     } else {
58
        try {
59
60
          String filename = args[0];
61
          String user     = args[1];
62
          String password = args[2];
63
          String dbstring = null;
64
65
          if (args.length <= 3) {
66
            dbstring = defaultDB;
67
          } else {
68
            dbstring = args[3];
69
          }
70
71 50 jones
          // Open a connection to the database
72
          Connection dbconn = MetaCatUtil.openDBConnection(
73
                "oracle.jdbc.driver.OracleDriver",
74
                dbstring, user, password);
75
76 144 jones
          new DBWriter(filename, dbconn);
77 30 jones
          System.out.println("Document processing finished for: " + filename);
78
79 15 jones
        } catch (Exception e) {
80
          System.err.println("EXCEPTION HANDLING REQUIRED");
81
          System.err.println(e.getMessage());
82
          e.printStackTrace(System.err);
83
        }
84
     }
85
  }
86
87 31 jones
  /**
88
   * construct a new instance of the class to write an XML file to the database
89
   *
90
   * @param filename the filename to be loaded into the database
91 50 jones
   * @param conn the database connection to which to write the XML file
92 31 jones
   */
93 144 jones
  public DBWriter( Reader xml, Connection conn)
94 55 jones
                  throws IOException,
95
                         SQLException,
96
                         ClassNotFoundException
97
  {
98
    this.conn = conn;
99
100 72 bojilova
    try {
101 55 jones
        SAXParser parser = initializeParser(conn);
102 148 bojilova
        conn.setAutoCommit(false);
103 55 jones
        parser.parse(xml);
104 148 bojilova
        conn.commit();
105
        conn.setAutoCommit(true);
106 55 jones
      } catch (SAXParseException e) {
107 177 jones
        conn.rollback();
108 55 jones
        System.err.println(e.getMessage());
109
      } catch (SAXException e) {
110 177 jones
        conn.rollback();
111 55 jones
        System.err.println(e.getMessage());
112 72 bojilova
      } catch (Exception e) {
113 177 jones
        conn.rollback();
114 72 bojilova
        System.err.println(e.toString());
115 55 jones
      }
116
  }
117
118 144 jones
  public DBWriter( String filename, Connection conn)
119 15 jones
                  throws IOException,
120
                         SQLException,
121
                         ClassNotFoundException
122 55 jones
  {
123 50 jones
     this.conn = conn;
124 55 jones
     FileReader xmlfile = new FileReader(new File(filename).toString());
125 15 jones
     try {
126 55 jones
       SAXParser parser = initializeParser(conn);
127 148 bojilova
       conn.setAutoCommit(false);
128 55 jones
       parser.parse(xmlfile);
129 148 bojilova
       conn.commit();
130
       conn.setAutoCommit(true);
131 55 jones
     } catch (SAXParseException e) {
132
       System.err.println(e.getMessage());
133
     } catch (SAXException e) {
134
       System.err.println(e.getMessage());
135 72 bojilova
     } catch (Exception e) {
136 148 bojilova
       System.err.println("from DBWriter:" + e.toString());
137 55 jones
     }
138
  }
139
140
  private SAXParser initializeParser(Connection conn) {
141
    SAXParser parser = null;
142
    //
143
    // Set up the SAX document handlers for parsing
144
    //
145
    try {
146
      // Use the XMLDocumentHandler interface for namespace support
147
      // instead of org.xml.sax.DocumentHandler
148
      XMLDocumentHandler xmlDocHandler = new DBSAXHandler(conn);
149 72 bojilova
      EntityResolver xmlEntityResolver = new DBEntityResolver(conn);
150
      DTDHandler xmlDTDHandler         = new DBDTDHandler(conn);
151 15 jones
152 55 jones
      // For all the other interface use the default provided by
153
      // Handler base
154
      HandlerBase defHandler = new HandlerBase();
155 15 jones
156 55 jones
      // Get an instance of the parser
157
      parser = new SAXParser();
158 15 jones
159 55 jones
      // Set Handlers in the parser
160
      // Set the DocumentHandler to XMLDocumentHandler
161
      parser.setDocumentHandler(xmlDocHandler);
162 72 bojilova
      parser.setEntityResolver(xmlEntityResolver);
163
      parser.setDTDHandler(xmlDTDHandler);
164 15 jones
165 55 jones
      // Set the other Handler to the defHandler
166
      parser.setErrorHandler(defHandler);
167 15 jones
168 55 jones
    } catch (Exception e) {
169
       System.err.println(e.toString());
170
    }
171 15 jones
172 55 jones
    return parser;
173
  }
174 15 jones
175 55 jones
  /** Utility method to convert a file handle into a URL */
176
  static public URL fileToURL(File file)
177
  {
178 16 jones
     String path = file.getAbsolutePath();
179
     String fSep = System.getProperty("file.separator");
180
     if (fSep != null && fSep.length() == 1)
181
       path = path.replace(fSep.charAt(0), '/');
182
     if (path.length() > 0 && path.charAt(0) != '/')
183
       path = '/' + path;
184
     try {
185
       return new URL("file", null, path);
186
     }
187
     catch (java.net.MalformedURLException e) {
188
       /* According to the spec this could only happen if the file
189
	  protocol were not recognized. */
190
       throw new Error("unexpected MalformedURLException");
191
     }
192 15 jones
  }
193
}