1
|
/**
|
2
|
* Name: DBSAXElement.java
|
3
|
* Purpose: A Class that represents an XML element and its contents
|
4
|
* Institution: National Center for Ecological Analysis and Synthesis
|
5
|
* Copyright: 2000
|
6
|
* Authors: Matt Jones
|
7
|
*
|
8
|
* Version: '$Id: DBSAXElement.java 19 2000-04-11 17:57:28Z jones $'
|
9
|
*/
|
10
|
|
11
|
//package project;
|
12
|
|
13
|
import java.sql.*;
|
14
|
import java.io.IOException;
|
15
|
import java.util.Hashtable;
|
16
|
import java.util.Enumeration;
|
17
|
|
18
|
public class DBSAXElement {
|
19
|
|
20
|
private long element_id;
|
21
|
private String tagname;
|
22
|
private StringBuffer content;
|
23
|
private long parent_id;
|
24
|
private Hashtable attributes;
|
25
|
private Connection conn;
|
26
|
|
27
|
public DBSAXElement (Connection conn, String tagname,
|
28
|
long parent_id) {
|
29
|
this.conn = conn;
|
30
|
|
31
|
this.tagname = tagname;
|
32
|
this.parent_id = parent_id;
|
33
|
content = new StringBuffer();
|
34
|
attributes = new Hashtable();
|
35
|
writeElementToDB();
|
36
|
};
|
37
|
|
38
|
/** creates SQL code and inserts new element into DB connection */
|
39
|
private void writeElementToDB() {
|
40
|
try {
|
41
|
conn.setAutoCommit(false);
|
42
|
PreparedStatement pstmt;
|
43
|
if (parent_id != 0) {
|
44
|
pstmt = conn.prepareStatement(
|
45
|
"INSERT INTO xml_elements(nodeid, nodename, parentnodeid) " +
|
46
|
"VALUES (null, ?, ?)");
|
47
|
} else {
|
48
|
pstmt = conn.prepareStatement(
|
49
|
"INSERT INTO xml_elements(nodeid, nodename) " +
|
50
|
"VALUES (null, ?)");
|
51
|
}
|
52
|
|
53
|
// Bind the values to the query
|
54
|
pstmt.setString(1, getTagName());// The second ? is for NODENAME
|
55
|
if (parent_id != 0) {
|
56
|
pstmt.setLong(2, parent_id);
|
57
|
}
|
58
|
// Do the insertion
|
59
|
pstmt.execute();
|
60
|
pstmt.close();
|
61
|
this.element_id = getAssignedElementID();
|
62
|
conn.commit();
|
63
|
conn.setAutoCommit(true);
|
64
|
} catch (SQLException e) {
|
65
|
System.out.println(e.getMessage());
|
66
|
}
|
67
|
}
|
68
|
|
69
|
/** look up the assigned element id from DB connection */
|
70
|
private long getAssignedElementID() {
|
71
|
long assigned_id=0;
|
72
|
Statement stmt;
|
73
|
try {
|
74
|
stmt = conn.createStatement();
|
75
|
stmt.execute("SELECT xml_elements_id_seq.currval FROM dual");
|
76
|
try {
|
77
|
ResultSet rs = stmt.getResultSet();
|
78
|
try {
|
79
|
boolean tableHasRows = rs.next();
|
80
|
if (tableHasRows) {
|
81
|
try {
|
82
|
assigned_id = rs.getLong(1);
|
83
|
} catch (SQLException e) {
|
84
|
System.out.println("Error with getInt: " + e.getMessage());
|
85
|
}
|
86
|
}
|
87
|
} catch (SQLException e) {
|
88
|
System.out.println("Error with next: " + e.getMessage());
|
89
|
}
|
90
|
} catch (SQLException e) {
|
91
|
System.out.println("Error with getrset: " + e.getMessage());
|
92
|
}
|
93
|
stmt.close();
|
94
|
} catch (SQLException e) {
|
95
|
System.out.println("Error getting id: " + e.getMessage());
|
96
|
}
|
97
|
|
98
|
// assign the new ID number
|
99
|
return assigned_id;
|
100
|
}
|
101
|
|
102
|
// used by JTree to display this node
|
103
|
public String toString ()
|
104
|
{
|
105
|
StringBuffer value = new StringBuffer ();
|
106
|
value.append ('<');
|
107
|
value.append (getTagName ());
|
108
|
value.append (getAttributes ().toString ());
|
109
|
value.append ('>');
|
110
|
return value.toString ();
|
111
|
}
|
112
|
|
113
|
/** Get the id of this element */
|
114
|
public long getElementID() { return element_id; }
|
115
|
|
116
|
/** Get the name of this element */
|
117
|
public String getTagName() { return tagname; }
|
118
|
|
119
|
/** Get the attributes as a string */
|
120
|
public String getAttributes() {
|
121
|
StringBuffer buf = new StringBuffer();
|
122
|
String attName = null;
|
123
|
String attValue = null;
|
124
|
|
125
|
Enumeration attList = attributes.keys();
|
126
|
while (attList.hasMoreElements()) {
|
127
|
attName = (String)attList.nextElement();
|
128
|
attValue = (String)attributes.get(attName);
|
129
|
buf.append(" ").append(attName).append("=").append(attValue);
|
130
|
}
|
131
|
return buf.toString();
|
132
|
}
|
133
|
|
134
|
/** Add a new attribute to this element, or set its value */
|
135
|
public void setAttribute(String attName, String attValue) {
|
136
|
if (attName != null) {
|
137
|
// Enter the attribute in the hash table
|
138
|
attributes.put(attName, attValue);
|
139
|
|
140
|
// And enter the attribute in the database
|
141
|
try {
|
142
|
PreparedStatement pstmt = conn.prepareStatement(
|
143
|
"INSERT INTO xml_attributes(attributeid, nodeid, " +
|
144
|
"attributename, attributevalue) " +
|
145
|
"VALUES (null, ?, ?, ?)");
|
146
|
|
147
|
// Bind the values to the query
|
148
|
pstmt.setLong(1, getElementID());
|
149
|
pstmt.setString(2, attName);
|
150
|
pstmt.setString(3, attValue);
|
151
|
|
152
|
// Do the insertion
|
153
|
pstmt.execute();
|
154
|
pstmt.close();
|
155
|
} catch (SQLException e) {
|
156
|
System.out.println(e.getMessage());
|
157
|
}
|
158
|
|
159
|
} else {
|
160
|
System.err.println("Attribute name must not be null!");
|
161
|
}
|
162
|
}
|
163
|
|
164
|
/** Get an attribute value by name */
|
165
|
public String getAttribute(String attName) {
|
166
|
return (String)attributes.get(attName);
|
167
|
}
|
168
|
|
169
|
/** Append to the content of the element */
|
170
|
public void appendContent(char[] cbuf, int start, int len) {
|
171
|
this.content.append( cbuf, start, len );
|
172
|
}
|
173
|
|
174
|
/** Append to the content of the element */
|
175
|
public void appendContent(String new_content) {
|
176
|
this.content.append( new_content );
|
177
|
}
|
178
|
|
179
|
/** Get the content of the element */
|
180
|
public String getContent() {
|
181
|
return this.content.toString();
|
182
|
}
|
183
|
|
184
|
/** Write the element content to the db connection */
|
185
|
public void writeContentToDB() {
|
186
|
try {
|
187
|
PreparedStatement pstmt = conn.prepareStatement(
|
188
|
"UPDATE xml_elements SET nodedata = ? WHERE nodeid = ?");
|
189
|
|
190
|
// Bind the values to the query
|
191
|
pstmt.setString(1, getContent());// The first ? is for NODEDATA
|
192
|
pstmt.setLong(2, element_id); // The second ? is for NODEID
|
193
|
|
194
|
// Do the update
|
195
|
pstmt.execute();
|
196
|
pstmt.close();
|
197
|
|
198
|
} catch (SQLException e) {
|
199
|
System.out.println(e.getMessage());
|
200
|
}
|
201
|
}
|
202
|
|
203
|
}
|