1 |
6
|
jones
|
/**
|
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 |
8
|
jones
|
import org.w3c.dom.NodeList;
|
20 |
6
|
jones
|
import org.w3c.dom.ProcessingInstruction;
|
21 |
|
|
import org.w3c.dom.Text;
|
22 |
|
|
|
23 |
8
|
jones
|
import java.sql.*;
|
24 |
|
|
import java.io.IOException;
|
25 |
6
|
jones
|
|
26 |
8
|
jones
|
|
27 |
|
|
public class DBElement extends ElementNode {
|
28 |
|
|
|
29 |
6
|
jones
|
public DBElement () {
|
30 |
|
|
super();
|
31 |
|
|
};
|
32 |
|
|
|
33 |
8
|
jones
|
/**
|
34 |
|
|
* Writes this element and all of its children out to the
|
35 |
|
|
* given database connection
|
36 |
|
|
*/
|
37 |
11
|
jones
|
public void writeXmlToDB(Connection conn, int parentID)
|
38 |
|
|
throws IOException {
|
39 |
8
|
jones
|
/*
|
40 |
|
|
if (attributes != null)
|
41 |
|
|
attributes.writeXml(conn);
|
42 |
|
|
*/
|
43 |
|
|
int maxid=0;
|
44 |
11
|
jones
|
int newid=0;
|
45 |
8
|
jones
|
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 |
11
|
jones
|
|
71 |
|
|
// assign a new ID number
|
72 |
|
|
newid = maxid + 1;
|
73 |
8
|
jones
|
|
74 |
|
|
try {
|
75 |
11
|
jones
|
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 |
8
|
jones
|
|
86 |
11
|
jones
|
// Bind the values to the query
|
87 |
|
|
pstmt.setInt(1, newid); // The first ? is for NODEID
|
88 |
8
|
jones
|
pstmt.setString(2, getTagName());// The second ? is for NODENAME
|
89 |
11
|
jones
|
if (parentID != 0) {
|
90 |
|
|
pstmt.setInt(3, parentID);
|
91 |
|
|
}
|
92 |
8
|
jones
|
// 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 |
11
|
jones
|
writeChildrenXmlToDB(conn, newid);
|
102 |
8
|
jones
|
|
103 |
11
|
jones
|
System.out.println("ID: " + newid + "\tTag: " + getTagName());
|
104 |
6
|
jones
|
}
|
105 |
|
|
|
106 |
11
|
jones
|
public void writeChildrenXmlToDB(Connection conn, int parentID)
|
107 |
|
|
throws IOException {
|
108 |
8
|
jones
|
|
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 |
11
|
jones
|
((DBElement)children.item(i)).writeXmlToDB(conn, parentID);
|
114 |
8
|
jones
|
} else {
|
115 |
|
|
System.out.println(" Text Node skipped.");
|
116 |
|
|
}
|
117 |
|
|
}
|
118 |
|
|
}
|
119 |
|
|
}
|
120 |
|
|
|
121 |
6
|
jones
|
// 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 |
|
|
}
|