Revision 8
Added by Matt Jones over 25 years ago
DBElement.java | ||
---|---|---|
16 | 16 |
import org.w3c.dom.DOMException; |
17 | 17 |
import org.w3c.dom.Element; |
18 | 18 |
import org.w3c.dom.Node; |
19 |
import org.w3c.dom.NodeList; |
|
19 | 20 |
import org.w3c.dom.ProcessingInstruction; |
20 | 21 |
import org.w3c.dom.Text; |
21 | 22 |
|
23 |
import java.sql.*; |
|
24 |
import java.io.IOException; |
|
22 | 25 |
|
23 |
public class DBElement extends ElementNode |
|
24 |
{ |
|
26 |
|
|
27 |
public class DBElement extends ElementNode { |
|
28 |
|
|
25 | 29 |
public DBElement () { |
26 | 30 |
super(); |
27 | 31 |
}; |
28 | 32 |
|
29 |
public String getJunk() { |
|
30 |
return "This is " + getTagName() + " junk!"; |
|
33 |
/** |
|
34 |
* Writes this element and all of its children out to the |
|
35 |
* given database connection |
|
36 |
*/ |
|
37 |
public void writeXmlToDB(Connection conn) throws IOException { |
|
38 |
/* |
|
39 |
if (attributes != null) |
|
40 |
attributes.writeXml(conn); |
|
41 |
*/ |
|
42 |
int maxid=0; |
|
43 |
Statement stmt; |
|
44 |
try { |
|
45 |
stmt = conn.createStatement(); |
|
46 |
stmt.execute("SELECT MAX(nodeid) FROM xml_elements"); |
|
47 |
try { |
|
48 |
ResultSet rs = stmt.getResultSet(); |
|
49 |
try { |
|
50 |
boolean tableHasRows = rs.next(); |
|
51 |
if (tableHasRows) { |
|
52 |
try { |
|
53 |
maxid = rs.getInt(1); |
|
54 |
} catch (SQLException e) { |
|
55 |
System.out.println("Error with getInt: " + e.getMessage()); |
|
56 |
} |
|
57 |
} |
|
58 |
} catch (SQLException e) { |
|
59 |
System.out.println("Error with next: " + e.getMessage()); |
|
60 |
} |
|
61 |
} catch (SQLException e) { |
|
62 |
System.out.println("Error with getrset: " + e.getMessage()); |
|
63 |
} |
|
64 |
stmt.close(); |
|
65 |
} catch (SQLException e) { |
|
66 |
System.out.println("Error getting id: " + e.getMessage()); |
|
67 |
} |
|
68 |
|
|
69 |
try { |
|
70 |
PreparedStatement pstmt = conn.prepareStatement( |
|
71 |
"INSERT INTO xml_elements(nodeid, nodename) values (?, ?)"); |
|
72 |
|
|
73 |
// Add an element as element number 1500 |
|
74 |
pstmt.setInt(1, maxid+1); // The first ? is for NODEID |
|
75 |
pstmt.setString(2, getTagName());// The second ? is for NODENAME |
|
76 |
// Do the insertion |
|
77 |
pstmt.execute(); |
|
78 |
pstmt.close(); |
|
79 |
|
|
80 |
} catch (SQLException e) { |
|
81 |
System.out.println(e.getMessage()); |
|
82 |
} |
|
83 |
|
|
84 |
if (hasChildNodes()) |
|
85 |
writeChildrenXmlToDB(conn); |
|
86 |
|
|
87 |
System.out.println("ID: " + maxid + "\tTag: " + getTagName()); |
|
31 | 88 |
} |
32 | 89 |
|
90 |
public void writeChildrenXmlToDB(Connection conn) throws IOException { |
|
91 |
|
|
92 |
NodeList children = getChildNodes(); |
|
93 |
if (children != null) { |
|
94 |
for (int i = 0; i < children.getLength(); i++) { |
|
95 |
if (children.item(i) instanceof ElementNode) { |
|
96 |
((DBElement)children.item(i)).writeXmlToDB(conn); |
|
97 |
} else { |
|
98 |
System.out.println(" Text Node skipped."); |
|
99 |
} |
|
100 |
} |
|
101 |
} |
|
102 |
} |
|
103 |
|
|
33 | 104 |
// used by JTree to display this node |
34 | 105 |
public String toString () |
35 | 106 |
{ |
... | ... | |
40 | 111 |
value.append ('>'); |
41 | 112 |
return value.toString (); |
42 | 113 |
} |
43 |
|
|
44 |
// TreeNode method implementations |
|
45 |
/* |
|
46 |
public TreeNode getChildAt (int n) { |
|
47 |
//System.out.println ("DBElement.getChildAt()"); |
|
48 |
Node node = item (n); |
|
49 |
|
|
50 |
if (node instanceof TreeNode) |
|
51 |
return (TreeNode) node; |
|
52 |
// Text, ProcessingInstruction ... |
|
53 |
return new MaskedNode (node); |
|
54 |
} |
|
55 |
*/ |
|
56 |
|
|
57 |
// |
|
58 |
// In this case we really _do_ want to show all the internal |
|
59 |
// nodes, so we really ought to use custom TreeNode and |
|
60 |
// ProcessingInstruction classes as we build. |
|
61 |
// |
|
62 |
/* |
|
63 |
static class MaskedNode implements TreeNode { |
|
64 |
private Node real; |
|
65 |
MaskedNode (Node e) { real = e; } |
|
66 |
|
|
67 |
public int getChildCount () { return 0; } |
|
68 |
public TreeNode getChildAt (int n) { return null; } |
|
69 |
public TreeNode getParent () |
|
70 |
{ return (TreeNode) real.getParentNode (); } |
|
71 |
public int getIndex (TreeNode node) { return -1; } |
|
72 |
public boolean getAllowsChildren () { return false; } |
|
73 |
public boolean isLeaf () { return true; } |
|
74 |
public Enumeration children () { return null; } |
|
75 |
|
|
76 |
public String toString () { |
|
77 |
if (real instanceof Text) |
|
78 |
return real.toString (); // XXX constructor |
|
79 |
if (real instanceof ProcessingInstruction) |
|
80 |
return real.toString (); // XXX unreasonable result |
|
81 |
return "... ? ..."; |
|
82 |
} |
|
83 |
} |
|
84 |
*/ |
|
85 |
/* |
|
86 |
public int getChildCount () { |
|
87 |
return getLength (); |
|
88 |
} |
|
89 |
|
|
90 |
public TreeNode getParent () { |
|
91 |
return (TreeNode) getParentNode (); |
|
92 |
} |
|
93 |
|
|
94 |
public int getIndex (TreeNode node) { |
|
95 |
return -1; // XXX implement |
|
96 |
} |
|
97 |
|
|
98 |
public boolean getAllowsChildren () { |
|
99 |
return true; |
|
100 |
} |
|
101 |
|
|
102 |
public boolean isLeaf () { |
|
103 |
return !hasChildNodes (); |
|
104 |
} |
|
105 |
|
|
106 |
public Enumeration children () { |
|
107 |
//System.out.println ("DBElement.children()"); |
|
108 |
|
|
109 |
throw new RuntimeException ("NYI"); |
|
110 |
} |
|
111 |
*/ |
|
112 | 114 |
} |
Also available in: Unified diff
element insertions added to db