Project

General

Profile

« Previous | Next » 

Revision 6

Created java classes to write an XML stream to a database connection -- initial checkin

View differences:

DBElement.java
1
/**
2
 *        Name: DBElement.java
3
 *     Purpose: A Class that represents an XML element and can
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$'
10
 */
11

  
12
//package project;
13

  
14
import com.sun.xml.tree.ElementNode;
15

  
16
import org.w3c.dom.DOMException;
17
import org.w3c.dom.Element;
18
import org.w3c.dom.Node;
19
import org.w3c.dom.ProcessingInstruction;
20
import org.w3c.dom.Text;
21

  
22

  
23
public class DBElement extends ElementNode
24
{
25
    public DBElement () {
26
      super();
27
    };
28
    
29
    public String getJunk() {
30
	return "This is " + getTagName() + " junk!";
31
    }
32

  
33
    // used by JTree to display this node
34
    public String toString ()
35
    {
36
	StringBuffer	value = new StringBuffer ();
37
	value.append ('<');
38
	value.append (getTagName ());
39
	value.append (getAttributes ().toString ());
40
	value.append ('>');
41
	return value.toString ();
42
    }
43
    
44
    // TreeNode method implementations
45
/*
46
    public TreeNode getChildAt (int n) {
47
    	//System.out.println ("DBElement.getChildAt()");
48
	Node		node = item (n);
49
	
50
	if (node instanceof TreeNode)
51
            return (TreeNode) node;
52
        // Text, ProcessingInstruction ...
53
        return new MaskedNode (node);
54
    }
55
*/
56
    
57
    //
58
    // In this case we really _do_ want to show all the internal
59
    // nodes, so we really ought to use custom TreeNode and
60
    // ProcessingInstruction classes as we build.
61
    //
62
/*
63
    static class MaskedNode implements TreeNode {
64
    	private Node	real;
65
    	MaskedNode (Node e) { real = e; }
66
    	
67
    	public int getChildCount () { return 0; }
68
    	public TreeNode getChildAt (int n) { return null; }
69
    	public TreeNode getParent ()
70
    		{ return (TreeNode) real.getParentNode (); }
71
    	public int getIndex (TreeNode node) { return -1; }
72
    	public boolean getAllowsChildren () { return false; }
73
    	public boolean isLeaf () { return true; }
74
    	public Enumeration children () { return null; }
75
    	
76
    	public String toString () {
77
    	    if (real instanceof Text)
78
    	       return real.toString ();		// XXX constructor
79
    	    if (real instanceof ProcessingInstruction)
80
    	       return real.toString ();		// XXX unreasonable result
81
    	    return "... ? ...";
82
    	}
83
    }
84
 */   	
85
   /* 
86
    public int getChildCount () {
87
    	return getLength ();
88
    }
89
    
90
    public TreeNode getParent () {
91
        return (TreeNode) getParentNode ();
92
    }
93
    
94
    public int getIndex (TreeNode node) {
95
        return -1;                  // XXX implement
96
    }
97
    
98
    public boolean getAllowsChildren () {
99
        return true;
100
    }
101
    
102
    public boolean isLeaf () {
103
        return !hasChildNodes ();
104
    }
105
    
106
    public Enumeration children () {
107
    	//System.out.println ("DBElement.children()");
108

  
109
	throw new RuntimeException ("NYI");
110
    }
111
   */ 
112
}
0 113

  
DBElement.props
1
#
2
#        Name: DBElement.props
3
#     Purpose: A set of properties that map element types to
4
#              the classes that are used to represent that type
5
# Institution: National Center for Ecological Analysis and Synthesis
6
#   Copyright: 1998 
7
#     Authors: Matt Jones
8
#     
9
#     Version: '$Id$'
10
#
11

  
12
#
13
# NOTE:  Special tag name "*Element" defines the default class to be used,
14
# instead of com.sun.xml.tree.ElementNode, when no explicit entry exists.
15
# The name "*Document" is used similarly for the document itself.
16
#
17

  
18
*Element	= DBElement
19
# *Document	= DBDocument
20

  
21
# Note -- we only use the default tags in this application
0 22

  
DBWriter.java
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$'
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

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

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

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

  
36

  
37
public class DBWriter
38
{
39
    private XmlDocument			doc;
40

  
41
    //
42
    // usage:  main someprops.props someplay.xml 
43
    //
44
    public static void main (String argv [])
45
    {
46
	try {
47
	    if (argv.length != 2) {
48
		System.err.println ("usage: java project.DBWriter "
49
		    + "someprops.props someplay.xml");
50
		System.exit (1);
51
	    }
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
            System.out.println(root.getJunk());
68

  
69
	} catch (Throwable t) {
70
            t.printStackTrace (System.out);
71
	}
72
    }
73

  
74
    static private XmlDocument createDocument (
75
	InputSource	input,
76
	InputStream	propsStream,
77
	ErrorHandler	errorHandler
78
    ) throws SAXException, IOException
79
    {
80
	try {
81
	    XmlDocumentBuilder		builder = new XmlDocumentBuilder ();
82
	    ValidatingParser		parser = new ValidatingParser ();
83

  
84
	    //
85
	    // Configure the builder to create the right elements.
86
	    //
87
	    {
88
		Properties		props;
89
		SimpleElementFactory	factory;
90

  
91
		props = new Properties ();
92
		props.load (propsStream);
93
		factory = new SimpleElementFactory ();
94
		factory.addMapping (props, DBWriter.class.getClassLoader ());
95
		builder.setElementFactory (factory);
96
	    }
97

  
98
	    //
99
	    // Configure the parser
100
	    //
101
	    parser.setErrorHandler (errorHandler);
102
	    parser.setEntityResolver (new Resolver ());
103
	    parser.setDocumentHandler (builder);
104
	    parser.parse (input);
105

  
106
	    // Look at how the different versions print themselvs...
107
	    if (false) {
108
		builder.getDocument ().write (System.out);
109
		System.exit (0);
110
	    }
111

  
112
	    return builder.getDocument ();
113
	    
114
	} catch (SAXParseException e) {
115
	    // already reported
116
	    throw e;
117

  
118
	} catch (SAXException e) {
119
	    e.printStackTrace (System.out);
120
	    throw e;
121

  
122
	} catch (IOException e) {
123
	    e.printStackTrace (System.out);
124
	    throw e;
125

  
126
	}
127
    }
128

  
129
    static class ErrorPrinter implements ErrorHandler
130
    {
131
	private void message (String level, SAXParseException e)
132
	{
133
	    System.out.print ("** ");
134
	    System.out.println (level);
135
	    System.out.print ("   URI = ");
136
	    System.out.print (e.getSystemId ());
137
	    System.out.print (" Line = ");
138
	    System.out.println (e.getLineNumber ());
139
	    System.out.print ("   Message = ");
140
	    System.out.println (e.getMessage ());
141

  
142
	    /*
143
	    if (e.getException () != null)
144
		e.getException ().printStackTrace (System.out);
145
	    else
146
		e.printStackTrace (System.out);
147
	    */
148
	}
149

  
150
	public void error (SAXParseException e)
151
	{
152
	    // normally a validity error
153
	    message ("Error (recoverable)", e);
154
	}
155

  
156
	public void warning (SAXParseException e)
157
	{
158
	    message ("Warning", e);
159
	}
160

  
161
	public void fatalError (SAXParseException e)
162
	{
163
	    message ("FATAL ERROR", e);
164
	}
165
    }
166

  
167
}
0 168

  
Makefile
1
# For Win32, make 'SEP' be a semicolon
2
SEP=:
3
#CPATH=../../xml.jar$(SEP)$(JAVA_HOME)/lib/classes.zip
4
CPATH=.
5

  
6
default:
7
	javac -classpath "$(CPATH)" \
8
		DBWriter.java \
9
		DBElement.java
10
		
11
	#java -classpath ".$(SEP)$(CPATH)" \
12
		#main ./book-order.xml
13
		
14
	java -classpath ".$(SEP)$(CPATH)" \
15
		DBWriter DBElement.props ./book-order.xml
16

  
17
clean:
18
	-rm -f *.class Log
0 19

  

Also available in: Unified diff