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 17 2000-04-11 16:09:55Z 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
        } catch (Exception e) {
57
          System.err.println("EXCEPTION HANDLING REQUIRED");
58
          System.err.println(e.getMessage());
59
          e.printStackTrace(System.err);
60
        }
61
     }
62
  }
63
  
64
  private DBSAXWriter( String filename, String user, 
65
                  String password, String dbstring) 
66
                  throws IOException, 
67
                         SQLException, 
68
                         ClassNotFoundException
69
   {
70
     // Open a connection to the database
71
     conn = openDBConnection(
72
                "oracle.jdbc.driver.OracleDriver",
73
                dbstring, user, password);
74

    
75
    
76
     //
77
     // Set up the SAX document handlers for parsing
78
     //
79

    
80
     try {
81

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

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

    
90
        // Get an instance of the parser
91
        SAXParser parser = new SAXParser();
92

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

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

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

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

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

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

    
151
}
(5-5/9)