Project

General

Profile

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: DBSAXWriter.java 50 2000-04-17 22:40:19Z jones $'
10
 */
11

    
12
import org.xml.sax.*;
13

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

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

    
26
import oracle.xml.parser.v2.SAXParser;
27

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

    
34
  static  String 	defaultDB = "jdbc:oracle:thin:@localhost:1521:test";
35
  private Connection	conn = null;
36

    
37
  /**
38
   * the main routine used to test the DBSAXWriter utility.
39
   *
40
   * Usage: java DBSAXWriter <filename> <user> <password> [dbstring]
41
   *
42
   * @param filename the filename to be loaded into the database
43
   * @param user the username to use for the database connection
44
   * @param password the password to use for the database connection
45
   * @param dbstring the connection info to use for the database connection
46
   */
47
  static public void main(String[] args) {
48
     
49
     if (args.length < 3)
50
     {
51
        System.err.println("Wrong number of arguments!!!");
52
        System.err.println("USAGE: java DBSAXWriter " +
53
                           "<filename> <user> <password> [dbstring]");
54
        return;
55
     } else {
56
        try {
57
                    
58
          String filename = args[0];
59
          String user     = args[1];
60
          String password = args[2];
61
          String dbstring = null;
62

    
63
          if (args.length <= 3) {
64
            dbstring = defaultDB;
65
          } else {
66
            dbstring = args[3];
67
          }
68

    
69
          // Open a connection to the database
70
          Connection dbconn = MetaCatUtil.openDBConnection(
71
                "oracle.jdbc.driver.OracleDriver",
72
                dbstring, user, password);
73

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

    
77
        } catch (Exception e) {
78
          System.err.println("EXCEPTION HANDLING REQUIRED");
79
          System.err.println(e.getMessage());
80
          e.printStackTrace(System.err);
81
        }
82
     }
83
  }
84
  
85
  /**
86
   * construct a new instance of the class to write an XML file to the database
87
   *
88
   * @param filename the filename to be loaded into the database
89
   * @param conn the database connection to which to write the XML file
90
   */
91
  public DBSAXWriter( String filename, Connection conn)
92
                  throws IOException, 
93
                         SQLException, 
94
                         ClassNotFoundException
95
   {
96
     this.conn = conn;
97
    
98
     //
99
     // Set up the SAX document handlers for parsing
100
     //
101

    
102
     try {
103

    
104
        // Use the XMLDocumentHandler interface for namespace support
105
        // instead of org.xml.sax.DocumentHandler
106
        XMLDocumentHandler xmlDocHandler = new DBSAXHandler(conn);
107

    
108
        // For all the other interface use the default provided by
109
        // Handler base
110
        HandlerBase defHandler = new HandlerBase();
111

    
112
        // Get an instance of the parser
113
        SAXParser parser = new SAXParser();
114

    
115
        // Set Handlers in the parser
116
        // Set the DocumentHandler to XMLDocumentHandler
117
        parser.setDocumentHandler(xmlDocHandler);
118

    
119
        // Set the other Handler to the defHandler
120
        parser.setErrorHandler(defHandler);
121
        parser.setEntityResolver(defHandler);
122
        parser.setDTDHandler(defHandler);
123

    
124
        try
125
        {
126
           parser.parse(fileToURL(new File(filename)).toString());
127
        }
128
        catch (SAXParseException e)
129
        {
130
           System.err.println(filename + ": " + e.getMessage());
131
        }
132
        catch (SAXException e)
133
        {
134
           System.err.println(filename + ": " + e.getMessage());
135
        }
136
     }
137
     catch (Exception e)
138
     {
139
        System.err.println(e.toString());
140
     }
141

    
142
   }
143
  
144
   /** Utility method to convert a file handle into a URL */
145
   static public URL fileToURL(File file) 
146
   {
147
     String path = file.getAbsolutePath();
148
     String fSep = System.getProperty("file.separator");
149
     if (fSep != null && fSep.length() == 1)
150
       path = path.replace(fSep.charAt(0), '/');
151
     if (path.length() > 0 && path.charAt(0) != '/')
152
       path = '/' + path;
153
     try {
154
       return new URL("file", null, path);
155
     }
156
     catch (java.net.MalformedURLException e) {
157
       /* According to the spec this could only happen if the file
158
	  protocol were not recognized. */
159
       throw new Error("unexpected MalformedURLException");
160
     }
161
  }
162

    
163
}
(7-7/18)