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: DBElement.java 11 1999-09-19 20:43:55Z jones $'
|
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.NodeList;
|
20
|
import org.w3c.dom.ProcessingInstruction;
|
21
|
import org.w3c.dom.Text;
|
22
|
|
23
|
import java.sql.*;
|
24
|
import java.io.IOException;
|
25
|
|
26
|
|
27
|
public class DBElement extends ElementNode {
|
28
|
|
29
|
public DBElement () {
|
30
|
super();
|
31
|
};
|
32
|
|
33
|
/**
|
34
|
* Writes this element and all of its children out to the
|
35
|
* given database connection
|
36
|
*/
|
37
|
public void writeXmlToDB(Connection conn, int parentID)
|
38
|
throws IOException {
|
39
|
/*
|
40
|
if (attributes != null)
|
41
|
attributes.writeXml(conn);
|
42
|
*/
|
43
|
int maxid=0;
|
44
|
int newid=0;
|
45
|
Statement stmt;
|
46
|
try {
|
47
|
stmt = conn.createStatement();
|
48
|
stmt.execute("SELECT MAX(nodeid) FROM xml_elements");
|
49
|
try {
|
50
|
ResultSet rs = stmt.getResultSet();
|
51
|
try {
|
52
|
boolean tableHasRows = rs.next();
|
53
|
if (tableHasRows) {
|
54
|
try {
|
55
|
maxid = rs.getInt(1);
|
56
|
} catch (SQLException e) {
|
57
|
System.out.println("Error with getInt: " + e.getMessage());
|
58
|
}
|
59
|
}
|
60
|
} catch (SQLException e) {
|
61
|
System.out.println("Error with next: " + e.getMessage());
|
62
|
}
|
63
|
} catch (SQLException e) {
|
64
|
System.out.println("Error with getrset: " + e.getMessage());
|
65
|
}
|
66
|
stmt.close();
|
67
|
} catch (SQLException e) {
|
68
|
System.out.println("Error getting id: " + e.getMessage());
|
69
|
}
|
70
|
|
71
|
// assign a new ID number
|
72
|
newid = maxid + 1;
|
73
|
|
74
|
try {
|
75
|
PreparedStatement pstmt;
|
76
|
if (parentID != 0) {
|
77
|
pstmt = conn.prepareStatement(
|
78
|
"INSERT INTO xml_elements(nodeid, nodename, parentnodeid) " +
|
79
|
"VALUES (?, ?, ?)");
|
80
|
} else {
|
81
|
pstmt = conn.prepareStatement(
|
82
|
"INSERT INTO xml_elements(nodeid, nodename) " +
|
83
|
"VALUES (?, ?)");
|
84
|
}
|
85
|
|
86
|
// Bind the values to the query
|
87
|
pstmt.setInt(1, newid); // The first ? is for NODEID
|
88
|
pstmt.setString(2, getTagName());// The second ? is for NODENAME
|
89
|
if (parentID != 0) {
|
90
|
pstmt.setInt(3, parentID);
|
91
|
}
|
92
|
// Do the insertion
|
93
|
pstmt.execute();
|
94
|
pstmt.close();
|
95
|
|
96
|
} catch (SQLException e) {
|
97
|
System.out.println(e.getMessage());
|
98
|
}
|
99
|
|
100
|
if (hasChildNodes())
|
101
|
writeChildrenXmlToDB(conn, newid);
|
102
|
|
103
|
System.out.println("ID: " + newid + "\tTag: " + getTagName());
|
104
|
}
|
105
|
|
106
|
public void writeChildrenXmlToDB(Connection conn, int parentID)
|
107
|
throws IOException {
|
108
|
|
109
|
NodeList children = getChildNodes();
|
110
|
if (children != null) {
|
111
|
for (int i = 0; i < children.getLength(); i++) {
|
112
|
if (children.item(i) instanceof ElementNode) {
|
113
|
((DBElement)children.item(i)).writeXmlToDB(conn, parentID);
|
114
|
} else {
|
115
|
System.out.println(" Text Node skipped.");
|
116
|
}
|
117
|
}
|
118
|
}
|
119
|
}
|
120
|
|
121
|
// used by JTree to display this node
|
122
|
public String toString ()
|
123
|
{
|
124
|
StringBuffer value = new StringBuffer ();
|
125
|
value.append ('<');
|
126
|
value.append (getTagName ());
|
127
|
value.append (getAttributes ().toString ());
|
128
|
value.append ('>');
|
129
|
return value.toString ();
|
130
|
}
|
131
|
}
|