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
 * Institution: National Center for Ecological Analysis and Synthesis
6
 *   Copyright: 2000
7
 *     Authors: Matt Jones
8
 *
9
 *     Version: '$Id: DBSAXWriter.java 30 2000-04-12 02:15:07Z 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
public class DBSAXWriter {
29

    
30
  static  String 	defaultDB = "jdbc:oracle:thin:@localhost:1521:test";
31
  private Connection	conn = null;
32

    
33
  static public void main(String[] args) {
34
     
35
     if (args.length < 3)
36
     {
37
        System.err.println("Wrong number of arguments!!!");
38
        System.err.println("USAGE: java DBSAXWriter " +
39
                           "<filename> <user> <password> [dbstring]");
40
        return;
41
     } else {
42
        try {
43
                    
44
          String filename = args[0];
45
          String user     = args[1];
46
          String password = args[2];
47
          String dbstring = null;
48

    
49
          if (args.length <= 3) {
50
            dbstring = defaultDB;
51
          } else {
52
            dbstring = args[3];
53
          }
54

    
55
          new DBSAXWriter(filename, user, password, dbstring);
56
          System.out.println("Document processing finished for: " + filename);
57

    
58
        } catch (Exception e) {
59
          System.err.println("EXCEPTION HANDLING REQUIRED");
60
          System.err.println(e.getMessage());
61
          e.printStackTrace(System.err);
62
        }
63
     }
64
  }
65
  
66
  public DBSAXWriter( String filename, String user, 
67
                  String password, String dbstring) 
68
                  throws IOException, 
69
                         SQLException, 
70
                         ClassNotFoundException
71
   {
72
     // Open a connection to the database
73
     conn = openDBConnection(
74
                "oracle.jdbc.driver.OracleDriver",
75
                dbstring, user, password);
76

    
77
    
78
     //
79
     // Set up the SAX document handlers for parsing
80
     //
81

    
82
     try {
83

    
84
        // Use the XMLDocumentHandler interface for namespace support
85
        // instead of org.xml.sax.DocumentHandler
86
        XMLDocumentHandler xmlDocHandler = new DBSAXHandler(conn);
87

    
88
        // For all the other interface use the default provided by
89
        // Handler base
90
        HandlerBase defHandler = new HandlerBase();
91

    
92
        // Get an instance of the parser
93
        SAXParser parser = new SAXParser();
94

    
95
        // Set Handlers in the parser
96
        // Set the DocumentHandler to XMLDocumentHandler
97
        parser.setDocumentHandler(xmlDocHandler);
98

    
99
        // Set the other Handler to the defHandler
100
        parser.setErrorHandler(defHandler);
101
        parser.setEntityResolver(defHandler);
102
        parser.setDTDHandler(defHandler);
103

    
104
        try
105
        {
106
           parser.parse(fileToURL(new File(filename)).toString());
107
        }
108
        catch (SAXParseException e)
109
        {
110
           System.err.println(filename + ": " + e.getMessage());
111
        }
112
        catch (SAXException e)
113
        {
114
           System.err.println(filename + ": " + e.getMessage());
115
        }
116
     }
117
     catch (Exception e)
118
     {
119
        System.err.println(e.toString());
120
     }
121

    
122
   }
123
  
124
   private Connection openDBConnection(String dbDriver, String connection,
125
                String user, String password)
126
                throws SQLException, ClassNotFoundException {
127
     // Load the Oracle JDBC driver
128
     Class.forName (dbDriver);
129

    
130
     // Connect to the database
131
     Connection conn = DriverManager.getConnection( connection, user, password);
132
     return conn;
133
   }
134

    
135
   static public URL fileToURL(File file) 
136
   {
137
     String path = file.getAbsolutePath();
138
     String fSep = System.getProperty("file.separator");
139
     if (fSep != null && fSep.length() == 1)
140
       path = path.replace(fSep.charAt(0), '/');
141
     if (path.length() > 0 && path.charAt(0) != '/')
142
       path = '/' + path;
143
     try {
144
       return new URL("file", null, path);
145
     }
146
     catch (java.net.MalformedURLException e) {
147
       /* According to the spec this could only happen if the file
148
	  protocol were not recognized. */
149
       throw new Error("unexpected MalformedURLException");
150
     }
151
  }
152

    
153
}
(7-7/13)