Revision 24
Added by Matt Jones over 24 years ago
src/edu/ucsb/nceas/metacat/ElementNode.java | ||
---|---|---|
21 | 21 |
private Connection conn; |
22 | 22 |
private Vector children; |
23 | 23 |
|
24 |
public ReaderElement (Connection conn) { |
|
25 |
this.conn = conn; |
|
26 |
this.children = new Vector(); |
|
27 |
} |
|
28 |
|
|
24 | 29 |
public ReaderElement (Connection conn, long nodeid) { |
30 |
this(conn); |
|
25 | 31 |
|
26 |
this.conn = conn; |
|
27 |
|
|
28 | 32 |
//Lookup data for self |
29 |
getElementInfo(nodeid);
|
|
33 |
setElementInfo(nodeid);
|
|
30 | 34 |
|
31 |
//Lookup list of child nodes |
|
32 |
//foreach childnode (childid,childtype) |
|
33 |
// if (type.equals("ATTRIBUTE")) { |
|
34 |
// add the att to attribute hash |
|
35 |
// else (type.equals("ELEMENT")) { |
|
36 |
// ReaderElement child = new ReaderElement(childid); |
|
37 |
// add child to vector of children |
|
38 |
// } |
|
39 |
//} |
|
35 |
//Create child nodes (elements or attributes) |
|
36 |
setChildrenNodes(nodeid); |
|
37 |
} |
|
40 | 38 |
|
39 |
public ReaderElement (Connection conn, long nodeid, long parentnodeid, |
|
40 |
String nodename, String nodedata) { |
|
41 |
this(conn); |
|
42 |
setElementID(nodeid); |
|
43 |
setParentID(parentnodeid); |
|
44 |
setTagName(nodename); |
|
45 |
appendContent(nodedata); |
|
46 |
|
|
47 |
// Create child nodes (elements or attributes) |
|
48 |
setChildrenNodes(nodeid); |
|
41 | 49 |
} |
42 | 50 |
|
43 |
private void getElementInfo(long nodeid) { |
|
51 |
/** Look up the info needed to construct this element from the DB */ |
|
52 |
private void setElementInfo(long nodeid) { |
|
44 | 53 |
long element_id=0; |
45 | 54 |
long parentnodeid=0; |
46 | 55 |
String nodetype=null; |
... | ... | |
86 | 95 |
setElementID(element_id); |
87 | 96 |
setParentID(parentnodeid); |
88 | 97 |
setTagName(nodename); |
89 |
content.append(nodedata);
|
|
98 |
appendContent(nodedata);
|
|
90 | 99 |
} |
91 | 100 |
} |
101 |
|
|
102 |
/** Look up each child node from the DB and and create it */ |
|
103 |
private void setChildrenNodes(long nodeid) { |
|
104 |
long element_id=0; |
|
105 |
long parentnodeid=0; |
|
106 |
String nodetype=null; |
|
107 |
String nodename=null; |
|
108 |
String nodedata=null; |
|
109 |
|
|
110 |
PreparedStatement pstmt; |
|
111 |
try { |
|
112 |
pstmt = |
|
113 |
conn.prepareStatement("SELECT nodeid,parentnodeid,nodetype, " + |
|
114 |
"nodename,nodedata FROM xml_nodes WHERE parentnodeid = ?"); |
|
115 |
// Bind the values to the query |
|
116 |
pstmt.setLong(1, nodeid); |
|
117 |
|
|
118 |
pstmt.execute(); |
|
119 |
try { |
|
120 |
ResultSet rs = pstmt.getResultSet(); |
|
121 |
try { |
|
122 |
boolean tableHasRows = rs.next(); |
|
123 |
while (tableHasRows) { |
|
124 |
try { |
|
125 |
element_id = rs.getLong(1); |
|
126 |
parentnodeid = rs.getLong(2); |
|
127 |
nodetype = rs.getString(3); |
|
128 |
nodename = rs.getString(4); |
|
129 |
nodedata = rs.getString(5); |
|
130 |
|
|
131 |
if (nodetype.equals("ELEMENT")) { |
|
132 |
ReaderElement child = new ReaderElement(conn, |
|
133 |
element_id,parentnodeid,nodename,nodedata); |
|
134 |
children.add(child); |
|
135 |
} else if (nodetype.equals("ATTRIBUTE")) { |
|
136 |
setAttribute(nodename,nodedata); |
|
137 |
} |
|
138 |
|
|
139 |
} catch (SQLException e) { |
|
140 |
System.out.println("Error with getInt: " + e.getMessage()); |
|
141 |
} |
|
142 |
|
|
143 |
// Advance to the next record in the cursor |
|
144 |
tableHasRows = rs.next(); |
|
145 |
} |
|
146 |
} catch (SQLException e) { |
|
147 |
System.out.println("Error with next: " + e.getMessage()); |
|
148 |
} |
|
149 |
} catch (SQLException e) { |
|
150 |
System.out.println("Error with getrset: " + e.getMessage()); |
|
151 |
} |
|
152 |
pstmt.close(); |
|
153 |
} catch (SQLException e) { |
|
154 |
System.out.println("Error getting id: " + e.getMessage()); |
|
155 |
} |
|
156 |
|
|
157 |
} |
|
158 |
|
|
159 |
/** String representation for display purposes */ |
|
160 |
public String toString () |
|
161 |
{ |
|
162 |
StringBuffer value = new StringBuffer(); |
|
163 |
value.append('<'); |
|
164 |
value.append(getTagName()); |
|
165 |
value.append(getAttributes().toString()); |
|
166 |
value.append('>'); |
|
167 |
|
|
168 |
// Process children recursively here |
|
169 |
// Or do it in ReaderElement using a stack so we don;t have the |
|
170 |
// whole thing in memory at once? |
|
171 |
ReaderElement child = null; |
|
172 |
Enumeration e = children.elements(); |
|
173 |
while (e.hasMoreElements()) { |
|
174 |
child = (ReaderElement)e.nextElement(); |
|
175 |
value.append(child); |
|
176 |
} |
|
177 |
|
|
178 |
String cont = getContent(); |
|
179 |
if (!cont.equals("null")) { |
|
180 |
value.append(cont); |
|
181 |
} |
|
182 |
|
|
183 |
value.append("</"); |
|
184 |
value.append(getTagName()); |
|
185 |
value.append('>'); |
|
186 |
return value.toString(); |
|
187 |
} |
|
188 |
|
|
92 | 189 |
} |
src/edu/ucsb/nceas/metacat/BasicNode.java | ||
---|---|---|
41 | 41 |
// used by JTree to display this node |
42 | 42 |
public String toString () |
43 | 43 |
{ |
44 |
StringBuffer value = new StringBuffer (); |
|
45 |
value.append ('<'); |
|
46 |
value.append (getTagName ()); |
|
47 |
value.append (getAttributes ().toString ()); |
|
48 |
value.append ('>'); |
|
49 |
return value.toString (); |
|
44 |
StringBuffer value = new StringBuffer(); |
|
45 |
value.append('<'); |
|
46 |
value.append(getTagName()); |
|
47 |
value.append(getAttributes().toString()); |
|
48 |
value.append('>'); |
|
49 |
// Process children recursively here? |
|
50 |
// Or do it in ReaderElement using a stack so we don;t have the |
|
51 |
// whole thing in memory at once? |
|
52 |
//value.append("</"); |
|
53 |
//value.append(getTagName()); |
|
54 |
//value.append('>'); |
|
55 |
return value.toString(); |
|
50 | 56 |
} |
51 | 57 |
|
52 | 58 |
/** Get the id of this element */ |
... | ... | |
95 | 101 |
while (attList.hasMoreElements()) { |
96 | 102 |
attName = (String)attList.nextElement(); |
97 | 103 |
attValue = (String)attributes.get(attName); |
98 |
buf.append(" ").append(attName).append("=").append(attValue);
|
|
104 |
buf.append(" ").append(attName).append("=\"").append(attValue).append("\"");
|
|
99 | 105 |
} |
100 | 106 |
return buf.toString(); |
101 | 107 |
} |
src/edu/ucsb/nceas/metacat/DBReader.java | ||
---|---|---|
81 | 81 |
public String readDocument(long nodeid) { |
82 | 82 |
StringBuffer doc = new StringBuffer(); |
83 | 83 |
|
84 |
System.out.println("\nGetting document with nodeid: " + nodeid + "\n"); |
|
85 |
//BasicElement element = readNodeFromDB(nodeid); |
|
86 | 84 |
ReaderElement element = new ReaderElement(conn, nodeid); |
87 |
doc.append(element); |
|
85 |
doc.append("<?xml version=\"1.0\"?>\n"); |
|
86 |
doc.append(element.toString()); |
|
88 | 87 |
|
89 | 88 |
return (doc.toString()); |
90 | 89 |
} |
91 |
|
|
92 |
/** look up the assigned element id from DB connection */ |
|
93 |
private BasicElement readNodeFromDB(long nodeid) { |
|
94 |
long element_id=0; |
|
95 |
long nodeparentid=0; |
|
96 |
String nodetype=null; |
|
97 |
String nodename=null; |
|
98 |
String nodedata=null; |
|
99 |
|
|
100 |
PreparedStatement pstmt; |
|
101 |
try { |
|
102 |
pstmt = |
|
103 |
conn.prepareStatement("SELECT nodeid,nodeparentid,nodetype, " + |
|
104 |
"nodename,nodedata FROM xml_nodes WHERE nodeid = ?"); |
|
105 |
// Bind the values to the query |
|
106 |
pstmt.setLong(1, nodeid); |
|
107 |
|
|
108 |
pstmt.execute(); |
|
109 |
try { |
|
110 |
ResultSet rs = pstmt.getResultSet(); |
|
111 |
try { |
|
112 |
boolean tableHasRows = rs.next(); |
|
113 |
if (tableHasRows) { |
|
114 |
try { |
|
115 |
element_id = rs.getLong(1); |
|
116 |
nodeparentid = rs.getLong(2); |
|
117 |
nodetype = rs.getString(3); |
|
118 |
nodename = rs.getString(4); |
|
119 |
nodedata = rs.getString(5); |
|
120 |
} catch (SQLException e) { |
|
121 |
System.out.println("Error with getInt: " + e.getMessage()); |
|
122 |
} |
|
123 |
} |
|
124 |
} catch (SQLException e) { |
|
125 |
System.out.println("Error with next: " + e.getMessage()); |
|
126 |
} |
|
127 |
} catch (SQLException e) { |
|
128 |
System.out.println("Error with getrset: " + e.getMessage()); |
|
129 |
} |
|
130 |
pstmt.close(); |
|
131 |
} catch (SQLException e) { |
|
132 |
System.out.println("Error getting id: " + e.getMessage()); |
|
133 |
} |
|
134 |
|
|
135 |
BasicElement element = null; |
|
136 |
if (nodetype.equals("ELEMENT")) { |
|
137 |
element = new BasicElement(element_id,nodename,nodeparentid); |
|
138 |
element.appendContent(nodedata); |
|
139 |
} |
|
140 |
|
|
141 |
// Get a list of child nodes and recursively retrieve them as well |
|
142 |
try { |
|
143 |
pstmt = |
|
144 |
conn.prepareStatement("SELECT nodeid,nodeparentid,nodetype, " + |
|
145 |
"nodename,nodedata FROM xml_nodes " + |
|
146 |
"WHERE nodeparentid = ?"); |
|
147 |
// Bind the values to the query |
|
148 |
pstmt.setLong(1, nodeid); |
|
149 |
|
|
150 |
pstmt.execute(); |
|
151 |
try { |
|
152 |
ResultSet rs = pstmt.getResultSet(); |
|
153 |
try { |
|
154 |
boolean tableHasRows = rs.next(); |
|
155 |
if (tableHasRows) { |
|
156 |
try { |
|
157 |
element_id = rs.getLong(1); |
|
158 |
nodeparentid = rs.getLong(2); |
|
159 |
nodetype = rs.getString(3); |
|
160 |
nodename = rs.getString(4); |
|
161 |
nodedata = rs.getString(5); |
|
162 |
} catch (SQLException e) { |
|
163 |
System.out.println("Error with getInt: " + e.getMessage()); |
|
164 |
} |
|
165 |
} |
|
166 |
} catch (SQLException e) { |
|
167 |
System.out.println("Error with next: " + e.getMessage()); |
|
168 |
} |
|
169 |
} catch (SQLException e) { |
|
170 |
System.out.println("Error with getrset: " + e.getMessage()); |
|
171 |
} |
|
172 |
pstmt.close(); |
|
173 |
} catch (SQLException e) { |
|
174 |
System.out.println("Error getting id: " + e.getMessage()); |
|
175 |
} |
|
176 |
|
|
177 |
|
|
178 |
return element; |
|
179 |
} |
|
180 | 90 |
} |
src/edu/ucsb/nceas/metacat/BasicElement.java | ||
---|---|---|
41 | 41 |
// used by JTree to display this node |
42 | 42 |
public String toString () |
43 | 43 |
{ |
44 |
StringBuffer value = new StringBuffer (); |
|
45 |
value.append ('<'); |
|
46 |
value.append (getTagName ()); |
|
47 |
value.append (getAttributes ().toString ()); |
|
48 |
value.append ('>'); |
|
49 |
return value.toString (); |
|
44 |
StringBuffer value = new StringBuffer(); |
|
45 |
value.append('<'); |
|
46 |
value.append(getTagName()); |
|
47 |
value.append(getAttributes().toString()); |
|
48 |
value.append('>'); |
|
49 |
// Process children recursively here? |
|
50 |
// Or do it in ReaderElement using a stack so we don;t have the |
|
51 |
// whole thing in memory at once? |
|
52 |
//value.append("</"); |
|
53 |
//value.append(getTagName()); |
|
54 |
//value.append('>'); |
|
55 |
return value.toString(); |
|
50 | 56 |
} |
51 | 57 |
|
52 | 58 |
/** Get the id of this element */ |
... | ... | |
95 | 101 |
while (attList.hasMoreElements()) { |
96 | 102 |
attName = (String)attList.nextElement(); |
97 | 103 |
attValue = (String)attributes.get(attName); |
98 |
buf.append(" ").append(attName).append("=").append(attValue);
|
|
104 |
buf.append(" ").append(attName).append("=\"").append(attValue).append("\"");
|
|
99 | 105 |
} |
100 | 106 |
return buf.toString(); |
101 | 107 |
} |
src/edu/ucsb/nceas/metacat/ReaderElement.java | ||
---|---|---|
21 | 21 |
private Connection conn; |
22 | 22 |
private Vector children; |
23 | 23 |
|
24 |
public ReaderElement (Connection conn) { |
|
25 |
this.conn = conn; |
|
26 |
this.children = new Vector(); |
|
27 |
} |
|
28 |
|
|
24 | 29 |
public ReaderElement (Connection conn, long nodeid) { |
30 |
this(conn); |
|
25 | 31 |
|
26 |
this.conn = conn; |
|
27 |
|
|
28 | 32 |
//Lookup data for self |
29 |
getElementInfo(nodeid);
|
|
33 |
setElementInfo(nodeid);
|
|
30 | 34 |
|
31 |
//Lookup list of child nodes |
|
32 |
//foreach childnode (childid,childtype) |
|
33 |
// if (type.equals("ATTRIBUTE")) { |
|
34 |
// add the att to attribute hash |
|
35 |
// else (type.equals("ELEMENT")) { |
|
36 |
// ReaderElement child = new ReaderElement(childid); |
|
37 |
// add child to vector of children |
|
38 |
// } |
|
39 |
//} |
|
35 |
//Create child nodes (elements or attributes) |
|
36 |
setChildrenNodes(nodeid); |
|
37 |
} |
|
40 | 38 |
|
39 |
public ReaderElement (Connection conn, long nodeid, long parentnodeid, |
|
40 |
String nodename, String nodedata) { |
|
41 |
this(conn); |
|
42 |
setElementID(nodeid); |
|
43 |
setParentID(parentnodeid); |
|
44 |
setTagName(nodename); |
|
45 |
appendContent(nodedata); |
|
46 |
|
|
47 |
// Create child nodes (elements or attributes) |
|
48 |
setChildrenNodes(nodeid); |
|
41 | 49 |
} |
42 | 50 |
|
43 |
private void getElementInfo(long nodeid) { |
|
51 |
/** Look up the info needed to construct this element from the DB */ |
|
52 |
private void setElementInfo(long nodeid) { |
|
44 | 53 |
long element_id=0; |
45 | 54 |
long parentnodeid=0; |
46 | 55 |
String nodetype=null; |
... | ... | |
86 | 95 |
setElementID(element_id); |
87 | 96 |
setParentID(parentnodeid); |
88 | 97 |
setTagName(nodename); |
89 |
content.append(nodedata);
|
|
98 |
appendContent(nodedata);
|
|
90 | 99 |
} |
91 | 100 |
} |
101 |
|
|
102 |
/** Look up each child node from the DB and and create it */ |
|
103 |
private void setChildrenNodes(long nodeid) { |
|
104 |
long element_id=0; |
|
105 |
long parentnodeid=0; |
|
106 |
String nodetype=null; |
|
107 |
String nodename=null; |
|
108 |
String nodedata=null; |
|
109 |
|
|
110 |
PreparedStatement pstmt; |
|
111 |
try { |
|
112 |
pstmt = |
|
113 |
conn.prepareStatement("SELECT nodeid,parentnodeid,nodetype, " + |
|
114 |
"nodename,nodedata FROM xml_nodes WHERE parentnodeid = ?"); |
|
115 |
// Bind the values to the query |
|
116 |
pstmt.setLong(1, nodeid); |
|
117 |
|
|
118 |
pstmt.execute(); |
|
119 |
try { |
|
120 |
ResultSet rs = pstmt.getResultSet(); |
|
121 |
try { |
|
122 |
boolean tableHasRows = rs.next(); |
|
123 |
while (tableHasRows) { |
|
124 |
try { |
|
125 |
element_id = rs.getLong(1); |
|
126 |
parentnodeid = rs.getLong(2); |
|
127 |
nodetype = rs.getString(3); |
|
128 |
nodename = rs.getString(4); |
|
129 |
nodedata = rs.getString(5); |
|
130 |
|
|
131 |
if (nodetype.equals("ELEMENT")) { |
|
132 |
ReaderElement child = new ReaderElement(conn, |
|
133 |
element_id,parentnodeid,nodename,nodedata); |
|
134 |
children.add(child); |
|
135 |
} else if (nodetype.equals("ATTRIBUTE")) { |
|
136 |
setAttribute(nodename,nodedata); |
|
137 |
} |
|
138 |
|
|
139 |
} catch (SQLException e) { |
|
140 |
System.out.println("Error with getInt: " + e.getMessage()); |
|
141 |
} |
|
142 |
|
|
143 |
// Advance to the next record in the cursor |
|
144 |
tableHasRows = rs.next(); |
|
145 |
} |
|
146 |
} catch (SQLException e) { |
|
147 |
System.out.println("Error with next: " + e.getMessage()); |
|
148 |
} |
|
149 |
} catch (SQLException e) { |
|
150 |
System.out.println("Error with getrset: " + e.getMessage()); |
|
151 |
} |
|
152 |
pstmt.close(); |
|
153 |
} catch (SQLException e) { |
|
154 |
System.out.println("Error getting id: " + e.getMessage()); |
|
155 |
} |
|
156 |
|
|
157 |
} |
|
158 |
|
|
159 |
/** String representation for display purposes */ |
|
160 |
public String toString () |
|
161 |
{ |
|
162 |
StringBuffer value = new StringBuffer(); |
|
163 |
value.append('<'); |
|
164 |
value.append(getTagName()); |
|
165 |
value.append(getAttributes().toString()); |
|
166 |
value.append('>'); |
|
167 |
|
|
168 |
// Process children recursively here |
|
169 |
// Or do it in ReaderElement using a stack so we don;t have the |
|
170 |
// whole thing in memory at once? |
|
171 |
ReaderElement child = null; |
|
172 |
Enumeration e = children.elements(); |
|
173 |
while (e.hasMoreElements()) { |
|
174 |
child = (ReaderElement)e.nextElement(); |
|
175 |
value.append(child); |
|
176 |
} |
|
177 |
|
|
178 |
String cont = getContent(); |
|
179 |
if (!cont.equals("null")) { |
|
180 |
value.append(cont); |
|
181 |
} |
|
182 |
|
|
183 |
value.append("</"); |
|
184 |
value.append(getTagName()); |
|
185 |
value.append('>'); |
|
186 |
return value.toString(); |
|
187 |
} |
|
188 |
|
|
92 | 189 |
} |
DBReader.java | ||
---|---|---|
81 | 81 |
public String readDocument(long nodeid) { |
82 | 82 |
StringBuffer doc = new StringBuffer(); |
83 | 83 |
|
84 |
System.out.println("\nGetting document with nodeid: " + nodeid + "\n"); |
|
85 |
//BasicElement element = readNodeFromDB(nodeid); |
|
86 | 84 |
ReaderElement element = new ReaderElement(conn, nodeid); |
87 |
doc.append(element); |
|
85 |
doc.append("<?xml version=\"1.0\"?>\n"); |
|
86 |
doc.append(element.toString()); |
|
88 | 87 |
|
89 | 88 |
return (doc.toString()); |
90 | 89 |
} |
91 |
|
|
92 |
/** look up the assigned element id from DB connection */ |
|
93 |
private BasicElement readNodeFromDB(long nodeid) { |
|
94 |
long element_id=0; |
|
95 |
long nodeparentid=0; |
|
96 |
String nodetype=null; |
|
97 |
String nodename=null; |
|
98 |
String nodedata=null; |
|
99 |
|
|
100 |
PreparedStatement pstmt; |
|
101 |
try { |
|
102 |
pstmt = |
|
103 |
conn.prepareStatement("SELECT nodeid,nodeparentid,nodetype, " + |
|
104 |
"nodename,nodedata FROM xml_nodes WHERE nodeid = ?"); |
|
105 |
// Bind the values to the query |
|
106 |
pstmt.setLong(1, nodeid); |
|
107 |
|
|
108 |
pstmt.execute(); |
|
109 |
try { |
|
110 |
ResultSet rs = pstmt.getResultSet(); |
|
111 |
try { |
|
112 |
boolean tableHasRows = rs.next(); |
|
113 |
if (tableHasRows) { |
|
114 |
try { |
|
115 |
element_id = rs.getLong(1); |
|
116 |
nodeparentid = rs.getLong(2); |
|
117 |
nodetype = rs.getString(3); |
|
118 |
nodename = rs.getString(4); |
|
119 |
nodedata = rs.getString(5); |
|
120 |
} catch (SQLException e) { |
|
121 |
System.out.println("Error with getInt: " + e.getMessage()); |
|
122 |
} |
|
123 |
} |
|
124 |
} catch (SQLException e) { |
|
125 |
System.out.println("Error with next: " + e.getMessage()); |
|
126 |
} |
|
127 |
} catch (SQLException e) { |
|
128 |
System.out.println("Error with getrset: " + e.getMessage()); |
|
129 |
} |
|
130 |
pstmt.close(); |
|
131 |
} catch (SQLException e) { |
|
132 |
System.out.println("Error getting id: " + e.getMessage()); |
|
133 |
} |
|
134 |
|
|
135 |
BasicElement element = null; |
|
136 |
if (nodetype.equals("ELEMENT")) { |
|
137 |
element = new BasicElement(element_id,nodename,nodeparentid); |
|
138 |
element.appendContent(nodedata); |
|
139 |
} |
|
140 |
|
|
141 |
// Get a list of child nodes and recursively retrieve them as well |
|
142 |
try { |
|
143 |
pstmt = |
|
144 |
conn.prepareStatement("SELECT nodeid,nodeparentid,nodetype, " + |
|
145 |
"nodename,nodedata FROM xml_nodes " + |
|
146 |
"WHERE nodeparentid = ?"); |
|
147 |
// Bind the values to the query |
|
148 |
pstmt.setLong(1, nodeid); |
|
149 |
|
|
150 |
pstmt.execute(); |
|
151 |
try { |
|
152 |
ResultSet rs = pstmt.getResultSet(); |
|
153 |
try { |
|
154 |
boolean tableHasRows = rs.next(); |
|
155 |
if (tableHasRows) { |
|
156 |
try { |
|
157 |
element_id = rs.getLong(1); |
|
158 |
nodeparentid = rs.getLong(2); |
|
159 |
nodetype = rs.getString(3); |
|
160 |
nodename = rs.getString(4); |
|
161 |
nodedata = rs.getString(5); |
|
162 |
} catch (SQLException e) { |
|
163 |
System.out.println("Error with getInt: " + e.getMessage()); |
|
164 |
} |
|
165 |
} |
|
166 |
} catch (SQLException e) { |
|
167 |
System.out.println("Error with next: " + e.getMessage()); |
|
168 |
} |
|
169 |
} catch (SQLException e) { |
|
170 |
System.out.println("Error with getrset: " + e.getMessage()); |
|
171 |
} |
|
172 |
pstmt.close(); |
|
173 |
} catch (SQLException e) { |
|
174 |
System.out.println("Error getting id: " + e.getMessage()); |
|
175 |
} |
|
176 |
|
|
177 |
|
|
178 |
return element; |
|
179 |
} |
|
180 | 90 |
} |
BasicElement.java | ||
---|---|---|
41 | 41 |
// used by JTree to display this node |
42 | 42 |
public String toString () |
43 | 43 |
{ |
44 |
StringBuffer value = new StringBuffer (); |
|
45 |
value.append ('<'); |
|
46 |
value.append (getTagName ()); |
|
47 |
value.append (getAttributes ().toString ()); |
|
48 |
value.append ('>'); |
|
49 |
return value.toString (); |
|
44 |
StringBuffer value = new StringBuffer(); |
|
45 |
value.append('<'); |
|
46 |
value.append(getTagName()); |
|
47 |
value.append(getAttributes().toString()); |
|
48 |
value.append('>'); |
|
49 |
// Process children recursively here? |
|
50 |
// Or do it in ReaderElement using a stack so we don;t have the |
|
51 |
// whole thing in memory at once? |
|
52 |
//value.append("</"); |
|
53 |
//value.append(getTagName()); |
|
54 |
//value.append('>'); |
|
55 |
return value.toString(); |
|
50 | 56 |
} |
51 | 57 |
|
52 | 58 |
/** Get the id of this element */ |
... | ... | |
95 | 101 |
while (attList.hasMoreElements()) { |
96 | 102 |
attName = (String)attList.nextElement(); |
97 | 103 |
attValue = (String)attributes.get(attName); |
98 |
buf.append(" ").append(attName).append("=").append(attValue);
|
|
104 |
buf.append(" ").append(attName).append("=\"").append(attValue).append("\"");
|
|
99 | 105 |
} |
100 | 106 |
return buf.toString(); |
101 | 107 |
} |
ReaderElement.java | ||
---|---|---|
21 | 21 |
private Connection conn; |
22 | 22 |
private Vector children; |
23 | 23 |
|
24 |
public ReaderElement (Connection conn) { |
|
25 |
this.conn = conn; |
|
26 |
this.children = new Vector(); |
|
27 |
} |
|
28 |
|
|
24 | 29 |
public ReaderElement (Connection conn, long nodeid) { |
30 |
this(conn); |
|
25 | 31 |
|
26 |
this.conn = conn; |
|
27 |
|
|
28 | 32 |
//Lookup data for self |
29 |
getElementInfo(nodeid);
|
|
33 |
setElementInfo(nodeid);
|
|
30 | 34 |
|
31 |
//Lookup list of child nodes |
|
32 |
//foreach childnode (childid,childtype) |
|
33 |
// if (type.equals("ATTRIBUTE")) { |
|
34 |
// add the att to attribute hash |
|
35 |
// else (type.equals("ELEMENT")) { |
|
36 |
// ReaderElement child = new ReaderElement(childid); |
|
37 |
// add child to vector of children |
|
38 |
// } |
|
39 |
//} |
|
35 |
//Create child nodes (elements or attributes) |
|
36 |
setChildrenNodes(nodeid); |
|
37 |
} |
|
40 | 38 |
|
39 |
public ReaderElement (Connection conn, long nodeid, long parentnodeid, |
|
40 |
String nodename, String nodedata) { |
|
41 |
this(conn); |
|
42 |
setElementID(nodeid); |
|
43 |
setParentID(parentnodeid); |
|
44 |
setTagName(nodename); |
|
45 |
appendContent(nodedata); |
|
46 |
|
|
47 |
// Create child nodes (elements or attributes) |
|
48 |
setChildrenNodes(nodeid); |
|
41 | 49 |
} |
42 | 50 |
|
43 |
private void getElementInfo(long nodeid) { |
|
51 |
/** Look up the info needed to construct this element from the DB */ |
|
52 |
private void setElementInfo(long nodeid) { |
|
44 | 53 |
long element_id=0; |
45 | 54 |
long parentnodeid=0; |
46 | 55 |
String nodetype=null; |
... | ... | |
86 | 95 |
setElementID(element_id); |
87 | 96 |
setParentID(parentnodeid); |
88 | 97 |
setTagName(nodename); |
89 |
content.append(nodedata);
|
|
98 |
appendContent(nodedata);
|
|
90 | 99 |
} |
91 | 100 |
} |
101 |
|
|
102 |
/** Look up each child node from the DB and and create it */ |
|
103 |
private void setChildrenNodes(long nodeid) { |
|
104 |
long element_id=0; |
|
105 |
long parentnodeid=0; |
|
106 |
String nodetype=null; |
|
107 |
String nodename=null; |
|
108 |
String nodedata=null; |
|
109 |
|
|
110 |
PreparedStatement pstmt; |
|
111 |
try { |
|
112 |
pstmt = |
|
113 |
conn.prepareStatement("SELECT nodeid,parentnodeid,nodetype, " + |
|
114 |
"nodename,nodedata FROM xml_nodes WHERE parentnodeid = ?"); |
|
115 |
// Bind the values to the query |
|
116 |
pstmt.setLong(1, nodeid); |
|
117 |
|
|
118 |
pstmt.execute(); |
|
119 |
try { |
|
120 |
ResultSet rs = pstmt.getResultSet(); |
|
121 |
try { |
|
122 |
boolean tableHasRows = rs.next(); |
|
123 |
while (tableHasRows) { |
|
124 |
try { |
|
125 |
element_id = rs.getLong(1); |
|
126 |
parentnodeid = rs.getLong(2); |
|
127 |
nodetype = rs.getString(3); |
|
128 |
nodename = rs.getString(4); |
|
129 |
nodedata = rs.getString(5); |
|
130 |
|
|
131 |
if (nodetype.equals("ELEMENT")) { |
|
132 |
ReaderElement child = new ReaderElement(conn, |
|
133 |
element_id,parentnodeid,nodename,nodedata); |
|
134 |
children.add(child); |
|
135 |
} else if (nodetype.equals("ATTRIBUTE")) { |
|
136 |
setAttribute(nodename,nodedata); |
|
137 |
} |
|
138 |
|
|
139 |
} catch (SQLException e) { |
|
140 |
System.out.println("Error with getInt: " + e.getMessage()); |
|
141 |
} |
|
142 |
|
|
143 |
// Advance to the next record in the cursor |
|
144 |
tableHasRows = rs.next(); |
|
145 |
} |
|
146 |
} catch (SQLException e) { |
|
147 |
System.out.println("Error with next: " + e.getMessage()); |
|
148 |
} |
|
149 |
} catch (SQLException e) { |
|
150 |
System.out.println("Error with getrset: " + e.getMessage()); |
|
151 |
} |
|
152 |
pstmt.close(); |
|
153 |
} catch (SQLException e) { |
|
154 |
System.out.println("Error getting id: " + e.getMessage()); |
|
155 |
} |
|
156 |
|
|
157 |
} |
|
158 |
|
|
159 |
/** String representation for display purposes */ |
|
160 |
public String toString () |
|
161 |
{ |
|
162 |
StringBuffer value = new StringBuffer(); |
|
163 |
value.append('<'); |
|
164 |
value.append(getTagName()); |
|
165 |
value.append(getAttributes().toString()); |
|
166 |
value.append('>'); |
|
167 |
|
|
168 |
// Process children recursively here |
|
169 |
// Or do it in ReaderElement using a stack so we don;t have the |
|
170 |
// whole thing in memory at once? |
|
171 |
ReaderElement child = null; |
|
172 |
Enumeration e = children.elements(); |
|
173 |
while (e.hasMoreElements()) { |
|
174 |
child = (ReaderElement)e.nextElement(); |
|
175 |
value.append(child); |
|
176 |
} |
|
177 |
|
|
178 |
String cont = getContent(); |
|
179 |
if (!cont.equals("null")) { |
|
180 |
value.append(cont); |
|
181 |
} |
|
182 |
|
|
183 |
value.append("</"); |
|
184 |
value.append(getTagName()); |
|
185 |
value.append('>'); |
|
186 |
return value.toString(); |
|
187 |
} |
|
188 |
|
|
92 | 189 |
} |
Makefile | ||
---|---|---|
37 | 37 |
rdtest: |
38 | 38 |
java -cp $(CPATH) DBReader 1 $(USER) $(PW) |
39 | 39 |
|
40 |
rdtest1: |
|
41 |
java -cp $(CPATH) DBReader 8 $(USER) $(PW) |
|
42 |
|
|
40 | 43 |
clean: |
41 | 44 |
-rm -f *.class Log |
Also available in: Unified diff
completed basic DBReader functionality. Can now read XML from DB using DBReader