Project

General

Profile

« Previous | Next » 

Revision 144

Added by Matt Jones over 24 years ago

renamed DBSAXWriter to DBWriter

View differences:

src/edu/ucsb/nceas/metacat/DBSAXWriter.java
1
/**
2
 *        Name: DBSAXWriter.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$'
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 DBSAXWriter {
35

  
36
  static  String 	defaultDB = "jdbc:oracle:thin:@penelope.nceas.ucsb.edu:1526:DEV8";
37
  private Connection	conn = null;
38

  
39
  /**
40
   * the main routine used to test the DBSAXWriter utility.
41
   *
42
   * Usage: java DBSAXWriter <filename> <user> <password> [dbstring]
43
   *
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
  static public void main(String[] args) {
50
     
51
     if (args.length < 3)
52
     {
53
        System.err.println("Wrong number of arguments!!!");
54
        System.err.println("USAGE: java DBSAXWriter " +
55
                           "<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
          // Open a connection to the database
72
          Connection dbconn = MetaCatUtil.openDBConnection(
73
                "oracle.jdbc.driver.OracleDriver",
74
                dbstring, user, password);
75

  
76
          new DBSAXWriter(filename, dbconn);
77
          System.out.println("Document processing finished for: " + filename);
78

  
79
        } 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
  /**
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
   * @param conn the database connection to which to write the XML file
92
   */
93
  public DBSAXWriter( Reader xml, Connection conn)
94
                  throws IOException, 
95
                         SQLException, 
96
                         ClassNotFoundException
97
  {
98
    this.conn = conn;
99

  
100
    try {
101
        SAXParser parser = initializeParser(conn);
102
        parser.parse(xml);
103
      } catch (SAXParseException e) {
104
        System.err.println(e.getMessage());
105
      } catch (SAXException e) {
106
        System.err.println(e.getMessage());
107
      } catch (Exception e) {
108
        System.err.println(e.toString());
109
      }
110
  }
111

  
112
  public DBSAXWriter( String filename, Connection conn)
113
                  throws IOException, 
114
                         SQLException, 
115
                         ClassNotFoundException
116
  {
117
     this.conn = conn;
118
     FileReader xmlfile = new FileReader(new File(filename).toString());
119
     try {
120
       SAXParser parser = initializeParser(conn);
121
       parser.parse(xmlfile);
122
     } catch (SAXParseException e) {
123
       System.err.println(e.getMessage());
124
     } catch (SAXException e) {
125
       System.err.println(e.getMessage());
126
     } catch (Exception e) {
127
       System.err.println(e.toString());
128
     }
129
  }
130
  
131
  private SAXParser initializeParser(Connection conn) {
132
    SAXParser parser = null;
133
    //
134
    // Set up the SAX document handlers for parsing
135
    //
136
    try {
137
      // Use the XMLDocumentHandler interface for namespace support
138
      // instead of org.xml.sax.DocumentHandler
139
      XMLDocumentHandler xmlDocHandler = new DBSAXHandler(conn);
140
      EntityResolver xmlEntityResolver = new DBEntityResolver(conn);
141
      DTDHandler xmlDTDHandler         = new DBDTDHandler(conn);
142

  
143
      // For all the other interface use the default provided by
144
      // Handler base
145
      HandlerBase defHandler = new HandlerBase();
146

  
147
      // Get an instance of the parser
148
      parser = new SAXParser();
149

  
150
      // Set Handlers in the parser
151
      // Set the DocumentHandler to XMLDocumentHandler
152
      parser.setDocumentHandler(xmlDocHandler);
153
      parser.setEntityResolver(xmlEntityResolver);
154
      parser.setDTDHandler(xmlDTDHandler);
155

  
156
      // Set the other Handler to the defHandler
157
      parser.setErrorHandler(defHandler);
158

  
159
    } catch (Exception e) {
160
       System.err.println(e.toString());
161
    }
162

  
163
    return parser;
164
  }
165

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

  
src/edu/ucsb/nceas/metacat/DBWriter.java
1 1
/**
2
 *        Name: DBSAXWriter.java
2
 *        Name: DBWriter.java
3 3
 *     Purpose: A Class that reads in an XML text document and
4 4
 *              write its contents to a database connection using SAX
5 5
 *   Copyright: 2000 Regents of the University of California and the
......
31 31
 * A Class that reads in an XML text document and
32 32
 * write its contents to a database connection using SAX
33 33
 */
34
public class DBSAXWriter {
34
public class DBWriter {
35 35

  
36 36
  static  String 	defaultDB = "jdbc:oracle:thin:@penelope.nceas.ucsb.edu:1526:DEV8";
37 37
  private Connection	conn = null;
38 38

  
39 39
  /**
40
   * the main routine used to test the DBSAXWriter utility.
40
   * the main routine used to test the DBWriter utility.
41 41
   *
42
   * Usage: java DBSAXWriter <filename> <user> <password> [dbstring]
42
   * Usage: java DBWriter <filename> <user> <password> [dbstring]
43 43
   *
44 44
   * @param filename the filename to be loaded into the database
45 45
   * @param user the username to use for the database connection
......
51 51
     if (args.length < 3)
52 52
     {
53 53
        System.err.println("Wrong number of arguments!!!");
54
        System.err.println("USAGE: java DBSAXWriter " +
54
        System.err.println("USAGE: java DBWriter " +
55 55
                           "<filename> <user> <password> [dbstring]");
56 56
        return;
57 57
     } else {
......
73 73
                "oracle.jdbc.driver.OracleDriver",
74 74
                dbstring, user, password);
75 75

  
76
          new DBSAXWriter(filename, dbconn);
76
          new DBWriter(filename, dbconn);
77 77
          System.out.println("Document processing finished for: " + filename);
78 78

  
79 79
        } catch (Exception e) {
......
90 90
   * @param filename the filename to be loaded into the database
91 91
   * @param conn the database connection to which to write the XML file
92 92
   */
93
  public DBSAXWriter( Reader xml, Connection conn)
93
  public DBWriter( Reader xml, Connection conn)
94 94
                  throws IOException, 
95 95
                         SQLException, 
96 96
                         ClassNotFoundException
......
109 109
      }
110 110
  }
111 111

  
112
  public DBSAXWriter( String filename, Connection conn)
112
  public DBWriter( String filename, Connection conn)
113 113
                  throws IOException, 
114 114
                         SQLException, 
115 115
                         ClassNotFoundException
src/edu/ucsb/nceas/metacat/MetaCatServlet.java
344 344

  
345 345
      // write the document to the database
346 346
      try {
347
        DBSAXWriter dbw = new DBSAXWriter(xml, conn);
347
        DBWriter dbw = new DBWriter(xml, conn);
348 348
      } catch (SQLException e1) {
349 349
          out.println("Error 1 loading document:<p>\n" + e1.getMessage());
350 350
      }catch (IOException e2) {
bin/loadxml
4 4
export PW=your-pw-goes-here
5 5
export DBC=jdbc:oracle:thin:@localhost:1521:test
6 6

  
7
java -cp ${CPATH} edu.ucsb.nceas.metacat.DBSAXWriter $1 $USER $PW $DBC
7
java -cp ${CPATH} edu.ucsb.nceas.metacat.DBWriter $1 $USER $PW $DBC

Also available in: Unified diff