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
 *   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: DBWriter.java 52 2000-04-17 23:29:00Z jones $'
10
 */
11

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

    
20
import org.xml.sax.ErrorHandler;
21
import org.xml.sax.InputSource;
22
import org.xml.sax.SAXException;
23
import org.xml.sax.SAXParseException;
24

    
25
import org.w3c.dom.Element;
26
import org.w3c.dom.Node;
27
import org.w3c.dom.NodeList;
28

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

    
35

    
36
public class DBWriter
37
{
38
    private XmlDocument			doc;
39
    private Connection			conn = null;
40

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

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

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

    
61
	DBElement		root;
62

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

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

    
77
            new DBWriter(argv);
78

    
79
	} catch (Throwable t) {
80
            t.printStackTrace (System.out);
81
	}
82
    }
83

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

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

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

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

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

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

    
128
	} catch (SAXException e) {
129
	    e.printStackTrace (System.out);
130
	    throw e;
131

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

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

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

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

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

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

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

    
189
}
190

    
(13-13/30)