1 |
15
|
jones
|
/**
|
2 |
|
|
* Name: DBSAXElement.java
|
3 |
|
|
* Purpose: A Class that represents an XML element and its contents
|
4 |
35
|
jones
|
* Copyright: 2000 Regents of the University of California and the
|
5 |
|
|
* National Center for Ecological Analysis and Synthesis
|
6 |
15
|
jones
|
* Authors: Matt Jones
|
7 |
|
|
*
|
8 |
|
|
* Version: '$Id$'
|
9 |
|
|
*/
|
10 |
|
|
|
11 |
75
|
jones
|
package edu.ucsb.nceas.metacat;
|
12 |
15
|
jones
|
|
13 |
|
|
import java.sql.*;
|
14 |
|
|
import java.io.IOException;
|
15 |
|
|
import java.util.Hashtable;
|
16 |
|
|
import java.util.Enumeration;
|
17 |
|
|
|
18 |
31
|
jones
|
/**
|
19 |
|
|
* A Class that represents an XML element and its contents and
|
20 |
|
|
* can write its own representation to a database connection
|
21 |
|
|
*/
|
22 |
129
|
jones
|
public class DBSAXElement extends BasicNode {
|
23 |
15
|
jones
|
|
24 |
133
|
jones
|
private Connection conn;
|
25 |
15
|
jones
|
|
26 |
133
|
jones
|
/**
|
27 |
136
|
jones
|
* Construct a new node instance for DOCUMENT nodes
|
28 |
133
|
jones
|
*
|
29 |
|
|
* @param conn the JDBC Connection to which all information is written
|
30 |
136
|
jones
|
* @param tagname the name of the node
|
31 |
133
|
jones
|
*/
|
32 |
136
|
jones
|
public DBSAXElement (Connection conn, String tagname) {
|
33 |
|
|
super(tagname);
|
34 |
|
|
this.conn = conn;
|
35 |
|
|
writeChildNodeToDB("DOCUMENT", tagname, null);
|
36 |
|
|
setNodeID(getAssignedNodeID());
|
37 |
|
|
}
|
38 |
|
|
|
39 |
|
|
/**
|
40 |
|
|
* Construct a new node instance for ELEMENT nodes
|
41 |
|
|
*
|
42 |
|
|
* @param conn the JDBC Connection to which all information is written
|
43 |
|
|
* @param tagname the name of the node
|
44 |
|
|
* @param parentNode the parent node for this node being created
|
45 |
|
|
*/
|
46 |
133
|
jones
|
public DBSAXElement (Connection conn, String tagname,
|
47 |
136
|
jones
|
DBSAXElement parentNode) {
|
48 |
21
|
jones
|
|
49 |
136
|
jones
|
super(tagname);
|
50 |
|
|
setParentID(parentNode.getNodeID());
|
51 |
|
|
setNodeIndex(parentNode.incChildNum());
|
52 |
133
|
jones
|
this.conn = conn;
|
53 |
136
|
jones
|
writeChildNodeToDB("ELEMENT", getTagName(), null);
|
54 |
134
|
jones
|
setNodeID(getAssignedNodeID());
|
55 |
133
|
jones
|
}
|
56 |
15
|
jones
|
|
57 |
133
|
jones
|
/** creates SQL code and inserts new node into DB connection */
|
58 |
|
|
public void writeChildNodeToDB(String nodetype, String nodename,
|
59 |
136
|
jones
|
String data) {
|
60 |
133
|
jones
|
try {
|
61 |
|
|
PreparedStatement pstmt;
|
62 |
|
|
if (nodetype == "DOCUMENT") {
|
63 |
|
|
pstmt = conn.prepareStatement(
|
64 |
|
|
"INSERT INTO xml_nodes " +
|
65 |
|
|
"(nodeid, nodetype, nodename) " +
|
66 |
|
|
"VALUES (null, ?, ?)");
|
67 |
|
|
} else {
|
68 |
|
|
pstmt = conn.prepareStatement(
|
69 |
|
|
"INSERT INTO xml_nodes " +
|
70 |
|
|
"(nodeid, nodetype, nodename, " +
|
71 |
|
|
"parentnodeid, nodedata, nodeindex) " +
|
72 |
|
|
"VALUES (null, ?, ?, ?, ?, ?)");
|
73 |
|
|
}
|
74 |
19
|
jones
|
|
75 |
133
|
jones
|
// Bind the values to the query
|
76 |
|
|
pstmt.setString(1, nodetype);
|
77 |
|
|
pstmt.setString(2, nodename);
|
78 |
|
|
if (nodetype != "DOCUMENT") {
|
79 |
|
|
if (nodetype == "ELEMENT") {
|
80 |
|
|
pstmt.setLong(3, getParentID());
|
81 |
136
|
jones
|
pstmt.setString(4, data);
|
82 |
|
|
pstmt.setInt(5, getNodeIndex());
|
83 |
133
|
jones
|
} else {
|
84 |
134
|
jones
|
pstmt.setLong(3, getNodeID());
|
85 |
136
|
jones
|
pstmt.setString(4, data);
|
86 |
|
|
pstmt.setInt(5, incChildNum());
|
87 |
72
|
bojilova
|
}
|
88 |
133
|
jones
|
}
|
89 |
|
|
// Do the insertion
|
90 |
|
|
pstmt.execute();
|
91 |
|
|
pstmt.close();
|
92 |
|
|
} catch (SQLException e) {
|
93 |
|
|
System.err.println("Error inserting node: (" + nodetype + ", " +
|
94 |
|
|
nodename + ", " +
|
95 |
136
|
jones
|
data + ")" );
|
96 |
133
|
jones
|
System.err.println(e.getMessage());
|
97 |
110
|
bojilova
|
}
|
98 |
133
|
jones
|
}
|
99 |
110
|
bojilova
|
|
100 |
133
|
jones
|
/**
|
101 |
|
|
* creates SQL code to put nodename for the document node
|
102 |
|
|
* into DB connection
|
103 |
|
|
*/
|
104 |
|
|
public void writeNodename(String nodename) {
|
105 |
|
|
try {
|
106 |
|
|
PreparedStatement pstmt;
|
107 |
|
|
pstmt = conn.prepareStatement(
|
108 |
|
|
"UPDATE xml_nodes set nodename = ? " +
|
109 |
|
|
"WHERE nodeid = ?");
|
110 |
16
|
jones
|
|
111 |
133
|
jones
|
// Bind the values to the query
|
112 |
|
|
pstmt.setString(1, nodename);
|
113 |
134
|
jones
|
pstmt.setLong(2, getNodeID());
|
114 |
133
|
jones
|
// Do the insertion
|
115 |
|
|
pstmt.execute();
|
116 |
|
|
pstmt.close();
|
117 |
|
|
} catch (SQLException e) {
|
118 |
|
|
System.out.println(e.getMessage());
|
119 |
|
|
}
|
120 |
|
|
}
|
121 |
16
|
jones
|
|
122 |
133
|
jones
|
/** look up the assigned element id from DB connection */
|
123 |
134
|
jones
|
private long getAssignedNodeID() {
|
124 |
133
|
jones
|
long assigned_id=0;
|
125 |
|
|
Statement stmt;
|
126 |
|
|
try {
|
127 |
|
|
stmt = conn.createStatement();
|
128 |
|
|
stmt.execute("SELECT xml_nodes_id_seq.currval FROM dual");
|
129 |
|
|
ResultSet rs = stmt.getResultSet();
|
130 |
|
|
boolean tableHasRows = rs.next();
|
131 |
|
|
if (tableHasRows) {
|
132 |
|
|
assigned_id = rs.getLong(1);
|
133 |
19
|
jones
|
}
|
134 |
133
|
jones
|
stmt.close();
|
135 |
|
|
} catch (SQLException e) {
|
136 |
|
|
System.out.println("Error getting id: " + e.getMessage());
|
137 |
15
|
jones
|
}
|
138 |
|
|
|
139 |
133
|
jones
|
// assign the new ID number
|
140 |
|
|
return assigned_id;
|
141 |
|
|
}
|
142 |
18
|
jones
|
|
143 |
133
|
jones
|
/** Add a new attribute to this element, or set its value */
|
144 |
|
|
public void setAttribute(String attName, String attValue) {
|
145 |
|
|
if (attName != null) {
|
146 |
|
|
// Enter the attribute in the hash table
|
147 |
|
|
super.setAttribute(attName, attValue);
|
148 |
18
|
jones
|
|
149 |
133
|
jones
|
// And enter the attribute in the database
|
150 |
136
|
jones
|
writeChildNodeToDB("ATTRIBUTE", attName, attValue);
|
151 |
133
|
jones
|
} else {
|
152 |
|
|
System.err.println("Attribute name must not be null!");
|
153 |
18
|
jones
|
}
|
154 |
133
|
jones
|
}
|
155 |
15
|
jones
|
}
|