Project

General

Profile

1
/**
2
 *        Name: DBWriter.java
3
 *     Purpose: A Class that reads in an XML text document and
4
 *              write its contents to a database connection
5
 * Institution: National Center for Ecological Analysis and Synthesis
6
 *   Copyright: 1998 
7
 *     Authors: Matt Jones
8
 *     
9
 *     Version: '$Id: DBWriter.java 15 2000-04-11 02:12:25Z jones $'
10
 */
11

    
12
//package edu.ucsb.nceas.dbwriter;
13

    
14
import java.io.File;
15
import java.io.FileInputStream;
16
import java.io.IOException;
17
import java.io.InputStream;
18
import java.net.URL;
19
import java.util.Properties;
20
import java.sql.*;
21

    
22
import org.xml.sax.ErrorHandler;
23
import org.xml.sax.InputSource;
24
import org.xml.sax.SAXException;
25
import org.xml.sax.SAXParseException;
26

    
27
import org.w3c.dom.Element;
28
import org.w3c.dom.Node;
29
import org.w3c.dom.NodeList;
30

    
31
import com.sun.xml.tree.XmlDocument;
32
import com.sun.xml.tree.XmlDocumentBuilder;
33
import com.sun.xml.tree.SimpleElementFactory;
34
import com.sun.xml.parser.Resolver;
35
import com.sun.xml.parser.ValidatingParser;
36

    
37

    
38
public class DBWriter
39
{
40
    private XmlDocument			doc;
41
    private Connection			conn = null;
42

    
43
    // Constructer to init the class
44
    public DBWriter(String argv[]) 
45
	throws SAXException, SQLException, IOException, ClassNotFoundException {
46

    
47
        // Open a connection to the database
48
        conn = openDBConnection(
49
		"oracle.jdbc.driver.OracleDriver",
50
		"jdbc:oracle:thin:@localhost:test",
51
		argv[2], argv[3]);
52

    
53
	//
54
	// Load the document, using the appropriate custom
55
	// DOM elements.
56
	//
57
	XmlDocument	doc = createDocument (
58
		Resolver.createInputSource (new File (argv [1])),
59
		new FileInputStream (argv [0]),
60
		new ErrorPrinter ()
61
	);
62

    
63
	DBElement		root;
64

    
65
    	root = (DBElement)doc.getDocumentElement();
66
	root.normalize();
67
        root.writeXmlToDB(conn, 0);
68
    }
69

    
70
    public static void main (String argv [])
71
    {
72
	try {
73
	    if (argv.length != 4) {
74
		System.err.println ("usage: java DBWriter "
75
		    + "DBElement.props somefile.xml username password");
76
		System.exit (1);
77
	    }
78

    
79
            new DBWriter(argv);
80

    
81
	} catch (Throwable t) {
82
            t.printStackTrace (System.out);
83
	}
84
    }
85

    
86
    static private XmlDocument createDocument (
87
	InputSource	input,
88
	InputStream	propsStream,
89
	ErrorHandler	errorHandler
90
    ) throws SAXException, IOException
91
    {
92
	try {
93
	    XmlDocumentBuilder		builder = new XmlDocumentBuilder ();
94
	    ValidatingParser		parser = new ValidatingParser ();
95

    
96
	    //
97
	    // Configure the builder to create the right elements.
98
	    //
99
	    {
100
		Properties		props;
101
		SimpleElementFactory	factory;
102

    
103
		props = new Properties ();
104
		props.load (propsStream);
105
		factory = new SimpleElementFactory ();
106
		factory.addMapping (props, DBWriter.class.getClassLoader ());
107
		builder.setElementFactory (factory);
108
	    }
109

    
110
	    //
111
	    // Configure the parser
112
	    //
113
	    parser.setErrorHandler (errorHandler);
114
	    parser.setEntityResolver (new Resolver ());
115
	    parser.setDocumentHandler (builder);
116
	    parser.parse (input);
117

    
118
	    // Look at how the different versions print themselvs...
119
	    if (false) {
120
		builder.getDocument ().write (System.out);
121
		System.exit (0);
122
	    }
123

    
124
	    return builder.getDocument ();
125
	    
126
	} catch (SAXParseException e) {
127
	    // already reported
128
	    throw e;
129

    
130
	} catch (SAXException e) {
131
	    e.printStackTrace (System.out);
132
	    throw e;
133

    
134
	} catch (IOException e) {
135
	    e.printStackTrace (System.out);
136
	    throw e;
137
	}
138
    }
139
    
140
    private Connection openDBConnection(String dbDriver, String connection, 
141
		String user, String password) 
142
		throws SQLException, ClassNotFoundException {
143
      // Load the Oracle JDBC driver
144
      Class.forName (dbDriver);
145

    
146
      // Connect to the database
147
      // You can put a database name after the @ sign in the connection URL.
148
      Connection conn = DriverManager.getConnection(
149
	connection, user, password);
150
      return conn;
151
    }
152

    
153
    static class ErrorPrinter implements ErrorHandler
154
    {
155
	private void message (String level, SAXParseException e)
156
	{
157
	    System.out.print ("** ");
158
	    System.out.println (level);
159
	    System.out.print ("   URI = ");
160
	    System.out.print (e.getSystemId ());
161
	    System.out.print (" Line = ");
162
	    System.out.println (e.getLineNumber ());
163
	    System.out.print ("   Message = ");
164
	    System.out.println (e.getMessage ());
165

    
166
	    /*
167
	    if (e.getException () != null)
168
		e.getException ().printStackTrace (System.out);
169
	    else
170
		e.printStackTrace (System.out);
171
	    */
172
	}
173

    
174
	public void error (SAXParseException e)
175
	{
176
	    // normally a validity error
177
	    message ("Error (recoverable)", e);
178
	}
179

    
180
	public void warning (SAXParseException e)
181
	{
182
	    message ("Warning", e);
183
	}
184

    
185
	public void fatalError (SAXParseException e)
186
	{
187
	    message ("FATAL ERROR", e);
188
	}
189
    }
190

    
191
}
192

    
(9-9/13)