Revision 109
Added by bojilova over 24 years ago
DBSAXDocument.java | ||
---|---|---|
20 | 20 |
*/ |
21 | 21 |
public class DBSAXDocument { |
22 | 22 |
|
23 |
private Connection conn;
|
|
23 |
private Connection conn;
|
|
24 | 24 |
private long rootnodeid; |
25 |
private long docid; |
|
26 | 25 |
private String docname; |
27 | 26 |
private String doctype; |
28 |
private String doctitle; |
|
29 | 27 |
|
30 | 28 |
/** |
31 | 29 |
* Construct a new document instance |
32 | 30 |
* |
33 | 31 |
* @param conn the JDBC Connection to which all information is written |
34 | 32 |
* @param rootnodeid - sequence id of the root node in the document |
35 |
* @param docname - the name of DTD, |
|
36 |
* i.e. the name immediately following the DOCTYPE keyword - |
|
37 |
* should be the root element name. |
|
38 |
* (Oracle's and IBM parsers are not aware if it is not the |
|
39 |
* root element name) |
|
40 |
* @param doctype - Public ID of the DTD, |
|
41 |
* i.e. the name immediately following the PUBLIC keyword in |
|
42 |
* DOCTYPE declaration. |
|
43 |
* @param doctitle - document title from first child element named "title" |
|
33 |
* @param docname - the name of DTD, i.e. the name immediately following the DOCTYPE keyword - should be the root element name. |
|
34 |
* (Oracle's and IBM parsers are not aware if it is not the root element name) |
|
35 |
* @param doctype - Public ID of the DTD, i.e. the name immediately following the PUBLIC keyword in DOCTYPE declaration. |
|
44 | 36 |
* |
45 | 37 |
*/ |
46 |
public DBSAXDocument (Connection conn, long rootnodeid, String docname, |
|
47 |
String doctype) |
|
38 |
public DBSAXDocument (Connection conn, long rootnodeid, String docname, String doctype) |
|
48 | 39 |
{ |
49 | 40 |
this.conn = conn; |
50 | 41 |
this.rootnodeid = rootnodeid; |
... | ... | |
60 | 51 |
conn.setAutoCommit(false); |
61 | 52 |
PreparedStatement pstmt; |
62 | 53 |
pstmt = conn.prepareStatement( |
63 |
"INSERT INTO xml_documents " + |
|
64 |
"(docid, rootnodeid, docname, doctype, doctitle) " + |
|
65 |
"VALUES (null, ?, ?, ?, ?)"); |
|
54 |
"INSERT INTO xml_documents (docid, rootnodeid, docname, doctype) " + |
|
55 |
"VALUES (null, ?, ?, ?)"); |
|
66 | 56 |
|
67 | 57 |
// Bind the values to the query |
68 | 58 |
pstmt.setLong(1, rootnodeid); |
69 | 59 |
pstmt.setString(2, docname); |
70 | 60 |
pstmt.setString(3, doctype); |
71 |
pstmt.setString(4, doctitle); |
|
72 | 61 |
// Do the insertion |
73 | 62 |
pstmt.execute(); |
74 | 63 |
pstmt.close(); |
75 |
|
|
76 |
long assigned_id=0; |
|
77 |
Statement stmt; |
|
78 |
stmt = conn.createStatement(); |
|
79 |
stmt.execute("SELECT xml_documents_id_seq.currval FROM dual"); |
|
80 |
ResultSet rs = stmt.getResultSet(); |
|
81 |
boolean tableHasRows = rs.next(); |
|
82 |
if (tableHasRows) { |
|
83 |
assigned_id = rs.getLong(1); |
|
84 |
this.docid = assigned_id; |
|
85 |
} |
|
86 |
stmt.close(); |
|
87 |
|
|
88 | 64 |
conn.commit(); |
89 | 65 |
conn.setAutoCommit(true); |
90 | 66 |
} catch (SQLException e) { |
... | ... | |
92 | 68 |
} |
93 | 69 |
} |
94 | 70 |
|
95 |
/** |
|
96 |
* Get the document title |
|
97 |
*/ |
|
98 |
public String getTitle() { |
|
99 |
return doctitle; |
|
100 |
} |
|
101 |
|
|
102 |
/** |
|
103 |
* Set the document title |
|
104 |
* |
|
105 |
* @param title the new title for the document |
|
106 |
*/ |
|
107 |
public void setTitle( String title ) { |
|
108 |
this.doctitle = title; |
|
109 |
try { |
|
110 |
PreparedStatement pstmt; |
|
111 |
pstmt = conn.prepareStatement( |
|
112 |
"UPDATE xml_documents " + |
|
113 |
" SET doctitle = ? " + |
|
114 |
"WHERE docid = ?"); |
|
115 |
|
|
116 |
// Bind the values to the query |
|
117 |
pstmt.setString(1, doctitle); |
|
118 |
pstmt.setLong(2, docid); |
|
119 |
|
|
120 |
// Do the insertion |
|
121 |
pstmt.execute(); |
|
122 |
pstmt.close(); |
|
123 |
} catch (SQLException e) { |
|
124 |
System.out.println(e.getMessage()); |
|
125 |
} |
|
126 |
} |
|
127 |
|
|
128 |
/** |
|
129 |
* Look up the title of the first child element named "title" |
|
130 |
* and record it as the document title |
|
131 |
*/ |
|
132 |
public void setTitleFromChildElement() { |
|
133 |
String title = null; |
|
134 |
long assigned_id=0; |
|
135 |
PreparedStatement pstmt; |
|
136 |
try { |
|
137 |
pstmt = conn.prepareStatement("SELECT nodedata " + |
|
138 |
"FROM xml_nodes " + |
|
139 |
"WHERE nodename = 'title' " + |
|
140 |
"START WITH nodeid = ? " + |
|
141 |
"CONNECT BY PRIOR nodeid = parentnodeid "); |
|
142 |
|
|
143 |
// Bind the values to the query |
|
144 |
pstmt.setLong(1, rootnodeid); |
|
145 |
|
|
146 |
pstmt.execute(); |
|
147 |
try { |
|
148 |
ResultSet rs = pstmt.getResultSet(); |
|
149 |
try { |
|
150 |
boolean tableHasRows = rs.next(); |
|
151 |
if (tableHasRows) { |
|
152 |
try { |
|
153 |
title = rs.getString(1); |
|
154 |
} catch (SQLException e) { |
|
155 |
System.out.println("Error with getString: " + e.getMessage()); } |
|
156 |
} |
|
157 |
} catch (SQLException e) { |
|
158 |
System.out.println("Error with next: " + e.getMessage()); |
|
159 |
} |
|
160 |
} catch (SQLException e) { |
|
161 |
System.out.println("Error with getrset: " + e.getMessage()); |
|
162 |
} |
|
163 |
pstmt.close(); |
|
164 |
} catch (SQLException e) { |
|
165 |
System.out.println("Error getting id: " + e.getMessage()); |
|
166 |
} |
|
167 |
|
|
168 |
// assign the new title |
|
169 |
this.setTitle(title); |
|
170 |
} |
|
171 |
|
|
172 | 71 |
} |
src/edu/ucsb/nceas/metacat/DBSAXHandler.java | ||
---|---|---|
33 | 33 |
{ |
34 | 34 |
|
35 | 35 |
static int elementNo = 0; |
36 |
static String docname; |
|
37 |
private DBSAXDocument currentDocument; |
|
36 |
static String docname = null; |
|
38 | 37 |
private String doctype; |
39 | 38 |
private String systemid; |
40 |
private long rootnodeid = 0; |
|
41 | 39 |
private boolean debug = false; |
42 | 40 |
private boolean stackCreated = false; |
43 | 41 |
private Stack elementStack; |
... | ... | |
69 | 67 |
/** SAX Handler that receives notification of end of the document */ |
70 | 68 |
public void endDocument() throws SAXException |
71 | 69 |
{ |
72 |
currentDocument.setTitleFromChildElement(); |
|
73 |
System.out.println(currentDocument.getTitle()); |
|
74 | 70 |
System.out.println("end Document"); |
75 | 71 |
} |
76 | 72 |
|
77 | 73 |
/** SAX Handler that receives notification of DTD. Sets the DTD */ |
78 | 74 |
public void setDoctype(DTD dtd) throws SAXException |
79 | 75 |
{ |
80 |
// here is a bug: dtd.getPublicId() and dtd.getSustemId() return null. |
|
76 |
// here is a bug: dtd.getPublicId() and dtd.getSustemId() always return null.
|
|
81 | 77 |
docname = dtd.getName(); |
82 | 78 |
doctype = dtd.getPublicId(); |
83 | 79 |
systemid = dtd.getSystemId(); |
84 | 80 |
System.out.println("DOCNAME: " + docname); |
85 | 81 |
System.out.println("DOCTYPE: " + doctype); |
86 | 82 |
System.out.println(" SYSID: " + systemid); |
83 |
|
|
84 |
// Create the document node represantation as root |
|
85 |
DBSAXElement documentNode = new DBSAXElement(conn, docname, 0, 0); |
|
86 |
// Add the element to the stack, so that any text data can be |
|
87 |
// added as it is encountered |
|
88 |
elementStack.push(documentNode); |
|
87 | 89 |
} |
88 | 90 |
|
89 | 91 |
/** SAX Handler that receives notification of end of DTD |
... | ... | |
93 | 95 |
public void endDoctype() throws SAXException |
94 | 96 |
{ |
95 | 97 |
System.out.println("end of DOCTYPE"); |
96 |
if (doctype == null) { |
|
97 |
doctype = DBEntityResolver.doctype; |
|
98 |
//if (doctype == null) |
|
99 |
// doctype = docname; |
|
100 |
} |
|
98 |
//if (doctype == null) |
|
99 |
// doctype = DBEntityResolver.doctype; |
|
100 |
//DBSAXElement documentNode = (DBSAXElement)elementStack.peek(); |
|
101 |
//new DBSAXDocument(conn, documentNode.getElementID(), docname, doctype); |
|
101 | 102 |
} |
102 | 103 |
|
103 | 104 |
/** SAX Handler that is called at the start of each XML element */ |
... | ... | |
121 | 122 |
expName = name.getExpandedName(); |
122 | 123 |
|
123 | 124 |
elementNo++; |
124 |
if (docname == null) |
|
125 |
throw new SAXException("No DOCTYPE declaration provided"); |
|
126 | 125 |
// Get a reference to the parent element for the id |
127 | 126 |
long parent_id; |
128 | 127 |
int nodeIndex; |
... | ... | |
135 | 134 |
nodeIndex = 0; |
136 | 135 |
} |
137 | 136 |
|
137 |
// Document representation that points to the root document node |
|
138 |
if (elementNo == 1) { |
|
139 |
// If no DOCTYPE declaration: docname = root element name |
|
140 |
if (docname == null) |
|
141 |
docname = localName; |
|
142 |
else if (doctype == null) |
|
143 |
doctype = DBEntityResolver.doctype; |
|
144 |
DBSAXElement documentNode = (DBSAXElement)elementStack.peek(); |
|
145 |
new DBSAXDocument(conn, documentNode.getElementID(), docname, doctype); |
|
146 |
} |
|
138 | 147 |
// Create the current element representation |
139 | 148 |
currentElement = new DBSAXElement(conn, localName, parent_id, nodeIndex); |
140 | 149 |
// go to create document definition in the db |
141 | 150 |
// call once after insertion of the first element |
142 | 151 |
if (parent_id == 0) { |
143 |
this.rootnodeid = currentElement.getElementID();
|
|
144 |
currentDocument = new DBSAXDocument(conn, rootnodeid, docname, doctype);
|
|
152 |
long rootnodeid = currentElement.getElementID();
|
|
153 |
new DBSAXDocument(conn, rootnodeid, docname, doctype); |
|
145 | 154 |
} |
146 | 155 |
|
147 | 156 |
// Add all of the attributes |
... | ... | |
192 | 201 |
* For now works only for comments after the root element. */ |
193 | 202 |
public void comment(String data) throws SAXException |
194 | 203 |
{ |
195 |
if (elementNo > 0) { |
|
204 |
//if (elementNo > 0) {
|
|
196 | 205 |
DBSAXElement currentElement = (DBSAXElement)elementStack.peek(); |
197 | 206 |
int nodeIndex = currentElement.incChildNum(); |
198 | 207 |
currentElement.writeCommentToDB(data, nodeIndex); |
199 |
} |
|
208 |
//}
|
|
200 | 209 |
} |
201 | 210 |
public void processingInstruction(String target, String data) throws SAXException |
202 | 211 |
{ |
203 |
if (elementNo == 0) |
|
204 |
System.out.println("target:" + target + "\ndata:" + data); |
|
205 |
else if (elementNo > 0) |
|
206 |
System.out.println("targetHere:" + target + "\ndata:" + data); |
|
212 |
DBSAXElement currentElement = (DBSAXElement)elementStack.peek(); |
|
213 |
int nodeIndex = currentElement.incChildNum(); |
|
214 |
currentElement.writePIToDB(target, data, nodeIndex); |
|
207 | 215 |
} |
208 | 216 |
|
209 | 217 |
/** SAX Handler that is called at the end of each XML element */ |
src/edu/ucsb/nceas/metacat/DBSAXDocument.java | ||
---|---|---|
20 | 20 |
*/ |
21 | 21 |
public class DBSAXDocument { |
22 | 22 |
|
23 |
private Connection conn;
|
|
23 |
private Connection conn;
|
|
24 | 24 |
private long rootnodeid; |
25 |
private long docid; |
|
26 | 25 |
private String docname; |
27 | 26 |
private String doctype; |
28 |
private String doctitle; |
|
29 | 27 |
|
30 | 28 |
/** |
31 | 29 |
* Construct a new document instance |
32 | 30 |
* |
33 | 31 |
* @param conn the JDBC Connection to which all information is written |
34 | 32 |
* @param rootnodeid - sequence id of the root node in the document |
35 |
* @param docname - the name of DTD, |
|
36 |
* i.e. the name immediately following the DOCTYPE keyword - |
|
37 |
* should be the root element name. |
|
38 |
* (Oracle's and IBM parsers are not aware if it is not the |
|
39 |
* root element name) |
|
40 |
* @param doctype - Public ID of the DTD, |
|
41 |
* i.e. the name immediately following the PUBLIC keyword in |
|
42 |
* DOCTYPE declaration. |
|
43 |
* @param doctitle - document title from first child element named "title" |
|
33 |
* @param docname - the name of DTD, i.e. the name immediately following the DOCTYPE keyword - should be the root element name. |
|
34 |
* (Oracle's and IBM parsers are not aware if it is not the root element name) |
|
35 |
* @param doctype - Public ID of the DTD, i.e. the name immediately following the PUBLIC keyword in DOCTYPE declaration. |
|
44 | 36 |
* |
45 | 37 |
*/ |
46 |
public DBSAXDocument (Connection conn, long rootnodeid, String docname, |
|
47 |
String doctype) |
|
38 |
public DBSAXDocument (Connection conn, long rootnodeid, String docname, String doctype) |
|
48 | 39 |
{ |
49 | 40 |
this.conn = conn; |
50 | 41 |
this.rootnodeid = rootnodeid; |
... | ... | |
60 | 51 |
conn.setAutoCommit(false); |
61 | 52 |
PreparedStatement pstmt; |
62 | 53 |
pstmt = conn.prepareStatement( |
63 |
"INSERT INTO xml_documents " + |
|
64 |
"(docid, rootnodeid, docname, doctype, doctitle) " + |
|
65 |
"VALUES (null, ?, ?, ?, ?)"); |
|
54 |
"INSERT INTO xml_documents (docid, rootnodeid, docname, doctype) " + |
|
55 |
"VALUES (null, ?, ?, ?)"); |
|
66 | 56 |
|
67 | 57 |
// Bind the values to the query |
68 | 58 |
pstmt.setLong(1, rootnodeid); |
69 | 59 |
pstmt.setString(2, docname); |
70 | 60 |
pstmt.setString(3, doctype); |
71 |
pstmt.setString(4, doctitle); |
|
72 | 61 |
// Do the insertion |
73 | 62 |
pstmt.execute(); |
74 | 63 |
pstmt.close(); |
75 |
|
|
76 |
long assigned_id=0; |
|
77 |
Statement stmt; |
|
78 |
stmt = conn.createStatement(); |
|
79 |
stmt.execute("SELECT xml_documents_id_seq.currval FROM dual"); |
|
80 |
ResultSet rs = stmt.getResultSet(); |
|
81 |
boolean tableHasRows = rs.next(); |
|
82 |
if (tableHasRows) { |
|
83 |
assigned_id = rs.getLong(1); |
|
84 |
this.docid = assigned_id; |
|
85 |
} |
|
86 |
stmt.close(); |
|
87 |
|
|
88 | 64 |
conn.commit(); |
89 | 65 |
conn.setAutoCommit(true); |
90 | 66 |
} catch (SQLException e) { |
... | ... | |
92 | 68 |
} |
93 | 69 |
} |
94 | 70 |
|
95 |
/** |
|
96 |
* Get the document title |
|
97 |
*/ |
|
98 |
public String getTitle() { |
|
99 |
return doctitle; |
|
100 |
} |
|
101 |
|
|
102 |
/** |
|
103 |
* Set the document title |
|
104 |
* |
|
105 |
* @param title the new title for the document |
|
106 |
*/ |
|
107 |
public void setTitle( String title ) { |
|
108 |
this.doctitle = title; |
|
109 |
try { |
|
110 |
PreparedStatement pstmt; |
|
111 |
pstmt = conn.prepareStatement( |
|
112 |
"UPDATE xml_documents " + |
|
113 |
" SET doctitle = ? " + |
|
114 |
"WHERE docid = ?"); |
|
115 |
|
|
116 |
// Bind the values to the query |
|
117 |
pstmt.setString(1, doctitle); |
|
118 |
pstmt.setLong(2, docid); |
|
119 |
|
|
120 |
// Do the insertion |
|
121 |
pstmt.execute(); |
|
122 |
pstmt.close(); |
|
123 |
} catch (SQLException e) { |
|
124 |
System.out.println(e.getMessage()); |
|
125 |
} |
|
126 |
} |
|
127 |
|
|
128 |
/** |
|
129 |
* Look up the title of the first child element named "title" |
|
130 |
* and record it as the document title |
|
131 |
*/ |
|
132 |
public void setTitleFromChildElement() { |
|
133 |
String title = null; |
|
134 |
long assigned_id=0; |
|
135 |
PreparedStatement pstmt; |
|
136 |
try { |
|
137 |
pstmt = conn.prepareStatement("SELECT nodedata " + |
|
138 |
"FROM xml_nodes " + |
|
139 |
"WHERE nodename = 'title' " + |
|
140 |
"START WITH nodeid = ? " + |
|
141 |
"CONNECT BY PRIOR nodeid = parentnodeid "); |
|
142 |
|
|
143 |
// Bind the values to the query |
|
144 |
pstmt.setLong(1, rootnodeid); |
|
145 |
|
|
146 |
pstmt.execute(); |
|
147 |
try { |
|
148 |
ResultSet rs = pstmt.getResultSet(); |
|
149 |
try { |
|
150 |
boolean tableHasRows = rs.next(); |
|
151 |
if (tableHasRows) { |
|
152 |
try { |
|
153 |
title = rs.getString(1); |
|
154 |
} catch (SQLException e) { |
|
155 |
System.out.println("Error with getString: " + e.getMessage()); } |
|
156 |
} |
|
157 |
} catch (SQLException e) { |
|
158 |
System.out.println("Error with next: " + e.getMessage()); |
|
159 |
} |
|
160 |
} catch (SQLException e) { |
|
161 |
System.out.println("Error with getrset: " + e.getMessage()); |
|
162 |
} |
|
163 |
pstmt.close(); |
|
164 |
} catch (SQLException e) { |
|
165 |
System.out.println("Error getting id: " + e.getMessage()); |
|
166 |
} |
|
167 |
|
|
168 |
// assign the new title |
|
169 |
this.setTitle(title); |
|
170 |
} |
|
171 |
|
|
172 | 71 |
} |
src/edu/ucsb/nceas/metacat/DBEntityResolver.java | ||
---|---|---|
65 | 65 |
// register publicId in db and use the provided systemId |
66 | 66 |
if (systemId != "") { |
67 | 67 |
new URL(systemId); |
68 |
registerDTDPublicID (conn, doctype, publicId, systemId);
|
|
68 |
registerDTDSystemID (conn, doctype, publicId, systemId);
|
|
69 | 69 |
return null; |
70 | 70 |
} |
71 | 71 |
new URL(dbSystemId); |
... | ... | |
89 | 89 |
if ( systemId != null) { |
90 | 90 |
if (currentElementNo == 0) { |
91 | 91 |
doctype = DBSAXHandler.docname; |
92 |
registerDTDPublicID (conn, doctype, "", systemId); |
|
92 |
dbSystemId = getDTDSystemID (conn, doctype); |
|
93 |
if (dbSystemId == "") { |
|
94 |
new URL(systemId); |
|
95 |
registerDTDSystemID (conn, doctype, doctype, systemId); |
|
96 |
return null; |
|
97 |
} |
|
98 |
new URL(dbSystemId); |
|
99 |
return new InputSource(dbSystemId); |
|
93 | 100 |
} |
94 |
new URL(systemId); |
|
95 | 101 |
} |
96 | 102 |
|
97 | 103 |
// use the default behaviour |
98 | 104 |
return null; |
99 | 105 |
} |
100 |
|
|
101 |
/** Look at db XML Catalog to get System ID (if any) for that Public ID.
|
|
106 |
|
|
107 |
/** Look at db XML Catalog to get System ID (if any) for that doctype.
|
|
102 | 108 |
* Return empty string if there are not */ |
103 |
private String getDTDSystemID (Connection conn, String publicId) |
|
104 |
{ |
|
109 |
private String getDTDSystemID (Connection conn, String doctype) { |
|
105 | 110 |
String system_id = ""; |
106 | 111 |
Statement stmt; |
107 | 112 |
try { |
108 | 113 |
stmt = conn.createStatement(); |
114 |
System.out.println("DOCTYPE:" + doctype); |
|
109 | 115 |
stmt.execute("SELECT system_id FROM xml_catalog " + |
110 |
"WHERE entity_type = 'DTD' AND source_doctype = '" + publicId + "'");
|
|
116 |
"WHERE entity_type = 'DTD' AND public_id = '" + doctype + "'");
|
|
111 | 117 |
try { |
112 | 118 |
ResultSet rs = stmt.getResultSet(); |
113 | 119 |
try { |
... | ... | |
135 | 141 |
return system_id; |
136 | 142 |
} |
137 | 143 |
|
138 |
/** Register Public ID in db XML Catalog */
|
|
139 |
private void registerDTDPublicID (Connection conn, String doctype, String publicId, String systemId)
|
|
144 |
/** Register DTD System ID in db XML Catalog */
|
|
145 |
private void registerDTDSystemID (Connection conn, String doctype, String publicId, String systemId)
|
|
140 | 146 |
{ |
141 | 147 |
try { |
148 |
java.net.URLConnection urlConn = (new URL(systemId)).openConnection(); |
|
149 |
urlConn.connect(); |
|
150 |
} catch (java.io.IOException e) { |
|
151 |
System.out.println("IOException: " + e.getMessage()); |
|
152 |
return; |
|
153 |
} |
|
154 |
try { |
|
142 | 155 |
conn.setAutoCommit(false); |
143 | 156 |
PreparedStatement pstmt; |
144 | 157 |
pstmt = conn.prepareStatement( |
src/edu/ucsb/nceas/metacat/DBSAXNode.java | ||
---|---|---|
57 | 57 |
// Bind the values to the query |
58 | 58 |
pstmt.setString(1, "ELEMENT"); |
59 | 59 |
pstmt.setString(2, getTagName()); |
60 |
//pstmt.setInt(4, null); |
|
61 | 60 |
if (getParentID() != 0) { |
62 | 61 |
pstmt.setLong(3, getParentID()); |
63 | 62 |
pstmt.setInt(4, getNodeIndex()); |
64 |
} |
|
63 |
} else |
|
64 |
pstmt.setString(1, "DOCUMENT"); |
|
65 | 65 |
// Do the insertion |
66 | 66 |
pstmt.execute(); |
67 | 67 |
pstmt.close(); |
... | ... | |
84 | 84 |
// Bind the values to the query |
85 | 85 |
pstmt.setString(1, "COMMENT"); |
86 | 86 |
pstmt.setLong(2, getElementID()); |
87 |
//System.out.println(data.length()); |
|
87 | 88 |
pstmt.setString(3, data); |
88 | 89 |
pstmt.setInt(4, nodeIndex); |
89 | 90 |
// Do the insertion |
90 | 91 |
pstmt.execute(); |
91 | 92 |
pstmt.close(); |
92 | 93 |
} catch (SQLException e) { |
94 |
System.out.println("Long Comment: " + e.getMessage()); |
|
95 |
} |
|
96 |
} |
|
97 |
|
|
98 |
/** creates SQL code and inserts PI into DB connection */ |
|
99 |
void writePIToDB(String target, String data, int nodeIndex) { |
|
100 |
try { |
|
101 |
PreparedStatement pstmt; |
|
102 |
pstmt = conn.prepareStatement( |
|
103 |
"INSERT INTO xml_nodes (nodeid, nodetype, " + |
|
104 |
"nodename, parentnodeid, nodedata, nodeindex) VALUES (null, ?, ?, ?, ?, ?)"); |
|
105 |
|
|
106 |
// Bind the values to the query |
|
107 |
pstmt.setString(1, "PI"); |
|
108 |
pstmt.setString(2, target); |
|
109 |
pstmt.setLong(3, getElementID()); |
|
110 |
pstmt.setString(4, data); |
|
111 |
pstmt.setInt(5, nodeIndex); |
|
112 |
// Do the insertion |
|
113 |
pstmt.execute(); |
|
114 |
pstmt.close(); |
|
115 |
} catch (SQLException e) { |
|
93 | 116 |
System.out.println(e.getMessage()); |
94 | 117 |
} |
95 | 118 |
} |
src/edu/ucsb/nceas/metacat/DBSAXElement.java | ||
---|---|---|
57 | 57 |
// Bind the values to the query |
58 | 58 |
pstmt.setString(1, "ELEMENT"); |
59 | 59 |
pstmt.setString(2, getTagName()); |
60 |
//pstmt.setInt(4, null); |
|
61 | 60 |
if (getParentID() != 0) { |
62 | 61 |
pstmt.setLong(3, getParentID()); |
63 | 62 |
pstmt.setInt(4, getNodeIndex()); |
64 |
} |
|
63 |
} else |
|
64 |
pstmt.setString(1, "DOCUMENT"); |
|
65 | 65 |
// Do the insertion |
66 | 66 |
pstmt.execute(); |
67 | 67 |
pstmt.close(); |
... | ... | |
84 | 84 |
// Bind the values to the query |
85 | 85 |
pstmt.setString(1, "COMMENT"); |
86 | 86 |
pstmt.setLong(2, getElementID()); |
87 |
//System.out.println(data.length()); |
|
87 | 88 |
pstmt.setString(3, data); |
88 | 89 |
pstmt.setInt(4, nodeIndex); |
89 | 90 |
// Do the insertion |
90 | 91 |
pstmt.execute(); |
91 | 92 |
pstmt.close(); |
92 | 93 |
} catch (SQLException e) { |
94 |
System.out.println("Long Comment: " + e.getMessage()); |
|
95 |
} |
|
96 |
} |
|
97 |
|
|
98 |
/** creates SQL code and inserts PI into DB connection */ |
|
99 |
void writePIToDB(String target, String data, int nodeIndex) { |
|
100 |
try { |
|
101 |
PreparedStatement pstmt; |
|
102 |
pstmt = conn.prepareStatement( |
|
103 |
"INSERT INTO xml_nodes (nodeid, nodetype, " + |
|
104 |
"nodename, parentnodeid, nodedata, nodeindex) VALUES (null, ?, ?, ?, ?, ?)"); |
|
105 |
|
|
106 |
// Bind the values to the query |
|
107 |
pstmt.setString(1, "PI"); |
|
108 |
pstmt.setString(2, target); |
|
109 |
pstmt.setLong(3, getElementID()); |
|
110 |
pstmt.setString(4, data); |
|
111 |
pstmt.setInt(5, nodeIndex); |
|
112 |
// Do the insertion |
|
113 |
pstmt.execute(); |
|
114 |
pstmt.close(); |
|
115 |
} catch (SQLException e) { |
|
93 | 116 |
System.out.println(e.getMessage()); |
94 | 117 |
} |
95 | 118 |
} |
src/edu/ucsb/nceas/metacat/MetaCatUtil.java | ||
---|---|---|
19 | 19 |
*/ |
20 | 20 |
public class MetaCatUtil { |
21 | 21 |
|
22 |
static String user = "jones"; |
|
23 |
static String password = "your-pw-goes-here"; |
|
24 |
static String defaultDB = "jdbc:oracle:thin:@localhost:1521:test"; |
|
25 |
|
|
22 | 26 |
/** |
23 | 27 |
* Utility method to establish a JDBC database connection |
24 | 28 |
* |
DBSAXElement.java | ||
---|---|---|
57 | 57 |
// Bind the values to the query |
58 | 58 |
pstmt.setString(1, "ELEMENT"); |
59 | 59 |
pstmt.setString(2, getTagName()); |
60 |
//pstmt.setInt(4, null); |
|
61 | 60 |
if (getParentID() != 0) { |
62 | 61 |
pstmt.setLong(3, getParentID()); |
63 | 62 |
pstmt.setInt(4, getNodeIndex()); |
64 |
} |
|
63 |
} else |
|
64 |
pstmt.setString(1, "DOCUMENT"); |
|
65 | 65 |
// Do the insertion |
66 | 66 |
pstmt.execute(); |
67 | 67 |
pstmt.close(); |
... | ... | |
84 | 84 |
// Bind the values to the query |
85 | 85 |
pstmt.setString(1, "COMMENT"); |
86 | 86 |
pstmt.setLong(2, getElementID()); |
87 |
//System.out.println(data.length()); |
|
87 | 88 |
pstmt.setString(3, data); |
88 | 89 |
pstmt.setInt(4, nodeIndex); |
89 | 90 |
// Do the insertion |
90 | 91 |
pstmt.execute(); |
91 | 92 |
pstmt.close(); |
92 | 93 |
} catch (SQLException e) { |
94 |
System.out.println("Long Comment: " + e.getMessage()); |
|
95 |
} |
|
96 |
} |
|
97 |
|
|
98 |
/** creates SQL code and inserts PI into DB connection */ |
|
99 |
void writePIToDB(String target, String data, int nodeIndex) { |
|
100 |
try { |
|
101 |
PreparedStatement pstmt; |
|
102 |
pstmt = conn.prepareStatement( |
|
103 |
"INSERT INTO xml_nodes (nodeid, nodetype, " + |
|
104 |
"nodename, parentnodeid, nodedata, nodeindex) VALUES (null, ?, ?, ?, ?, ?)"); |
|
105 |
|
|
106 |
// Bind the values to the query |
|
107 |
pstmt.setString(1, "PI"); |
|
108 |
pstmt.setString(2, target); |
|
109 |
pstmt.setLong(3, getElementID()); |
|
110 |
pstmt.setString(4, data); |
|
111 |
pstmt.setInt(5, nodeIndex); |
|
112 |
// Do the insertion |
|
113 |
pstmt.execute(); |
|
114 |
pstmt.close(); |
|
115 |
} catch (SQLException e) { |
|
93 | 116 |
System.out.println(e.getMessage()); |
94 | 117 |
} |
95 | 118 |
} |
MetaCatUtil.java | ||
---|---|---|
19 | 19 |
*/ |
20 | 20 |
public class MetaCatUtil { |
21 | 21 |
|
22 |
static String user = "jones"; |
|
23 |
static String password = "your-pw-goes-here"; |
|
24 |
static String defaultDB = "jdbc:oracle:thin:@localhost:1521:test"; |
|
25 |
|
|
22 | 26 |
/** |
23 | 27 |
* Utility method to establish a JDBC database connection |
24 | 28 |
* |
DBSAXHandler.java | ||
---|---|---|
33 | 33 |
{ |
34 | 34 |
|
35 | 35 |
static int elementNo = 0; |
36 |
static String docname; |
|
37 |
private DBSAXDocument currentDocument; |
|
36 |
static String docname = null; |
|
38 | 37 |
private String doctype; |
39 | 38 |
private String systemid; |
40 |
private long rootnodeid = 0; |
|
41 | 39 |
private boolean debug = false; |
42 | 40 |
private boolean stackCreated = false; |
43 | 41 |
private Stack elementStack; |
... | ... | |
69 | 67 |
/** SAX Handler that receives notification of end of the document */ |
70 | 68 |
public void endDocument() throws SAXException |
71 | 69 |
{ |
72 |
currentDocument.setTitleFromChildElement(); |
|
73 |
System.out.println(currentDocument.getTitle()); |
|
74 | 70 |
System.out.println("end Document"); |
75 | 71 |
} |
76 | 72 |
|
77 | 73 |
/** SAX Handler that receives notification of DTD. Sets the DTD */ |
78 | 74 |
public void setDoctype(DTD dtd) throws SAXException |
79 | 75 |
{ |
80 |
// here is a bug: dtd.getPublicId() and dtd.getSustemId() return null. |
|
76 |
// here is a bug: dtd.getPublicId() and dtd.getSustemId() always return null.
|
|
81 | 77 |
docname = dtd.getName(); |
82 | 78 |
doctype = dtd.getPublicId(); |
83 | 79 |
systemid = dtd.getSystemId(); |
84 | 80 |
System.out.println("DOCNAME: " + docname); |
85 | 81 |
System.out.println("DOCTYPE: " + doctype); |
86 | 82 |
System.out.println(" SYSID: " + systemid); |
83 |
|
|
84 |
// Create the document node represantation as root |
|
85 |
DBSAXElement documentNode = new DBSAXElement(conn, docname, 0, 0); |
|
86 |
// Add the element to the stack, so that any text data can be |
|
87 |
// added as it is encountered |
|
88 |
elementStack.push(documentNode); |
|
87 | 89 |
} |
88 | 90 |
|
89 | 91 |
/** SAX Handler that receives notification of end of DTD |
... | ... | |
93 | 95 |
public void endDoctype() throws SAXException |
94 | 96 |
{ |
95 | 97 |
System.out.println("end of DOCTYPE"); |
96 |
if (doctype == null) { |
|
97 |
doctype = DBEntityResolver.doctype; |
|
98 |
//if (doctype == null) |
|
99 |
// doctype = docname; |
|
100 |
} |
|
98 |
//if (doctype == null) |
|
99 |
// doctype = DBEntityResolver.doctype; |
|
100 |
//DBSAXElement documentNode = (DBSAXElement)elementStack.peek(); |
|
101 |
//new DBSAXDocument(conn, documentNode.getElementID(), docname, doctype); |
|
101 | 102 |
} |
102 | 103 |
|
103 | 104 |
/** SAX Handler that is called at the start of each XML element */ |
... | ... | |
121 | 122 |
expName = name.getExpandedName(); |
122 | 123 |
|
123 | 124 |
elementNo++; |
124 |
if (docname == null) |
|
125 |
throw new SAXException("No DOCTYPE declaration provided"); |
|
126 | 125 |
// Get a reference to the parent element for the id |
127 | 126 |
long parent_id; |
128 | 127 |
int nodeIndex; |
... | ... | |
135 | 134 |
nodeIndex = 0; |
136 | 135 |
} |
137 | 136 |
|
137 |
// Document representation that points to the root document node |
|
138 |
if (elementNo == 1) { |
|
139 |
// If no DOCTYPE declaration: docname = root element name |
|
140 |
if (docname == null) |
|
141 |
docname = localName; |
|
142 |
else if (doctype == null) |
|
143 |
doctype = DBEntityResolver.doctype; |
|
144 |
DBSAXElement documentNode = (DBSAXElement)elementStack.peek(); |
|
145 |
new DBSAXDocument(conn, documentNode.getElementID(), docname, doctype); |
|
146 |
} |
|
138 | 147 |
// Create the current element representation |
139 | 148 |
currentElement = new DBSAXElement(conn, localName, parent_id, nodeIndex); |
140 | 149 |
// go to create document definition in the db |
141 | 150 |
// call once after insertion of the first element |
142 | 151 |
if (parent_id == 0) { |
143 |
this.rootnodeid = currentElement.getElementID();
|
|
144 |
currentDocument = new DBSAXDocument(conn, rootnodeid, docname, doctype);
|
|
152 |
long rootnodeid = currentElement.getElementID();
|
|
153 |
new DBSAXDocument(conn, rootnodeid, docname, doctype); |
|
145 | 154 |
} |
146 | 155 |
|
147 | 156 |
// Add all of the attributes |
... | ... | |
192 | 201 |
* For now works only for comments after the root element. */ |
193 | 202 |
public void comment(String data) throws SAXException |
194 | 203 |
{ |
195 |
if (elementNo > 0) { |
|
204 |
//if (elementNo > 0) {
|
|
196 | 205 |
DBSAXElement currentElement = (DBSAXElement)elementStack.peek(); |
197 | 206 |
int nodeIndex = currentElement.incChildNum(); |
198 | 207 |
currentElement.writeCommentToDB(data, nodeIndex); |
199 |
} |
|
208 |
//}
|
|
200 | 209 |
} |
201 | 210 |
public void processingInstruction(String target, String data) throws SAXException |
202 | 211 |
{ |
203 |
if (elementNo == 0) |
|
204 |
System.out.println("target:" + target + "\ndata:" + data); |
|
205 |
else if (elementNo > 0) |
|
206 |
System.out.println("targetHere:" + target + "\ndata:" + data); |
|
212 |
DBSAXElement currentElement = (DBSAXElement)elementStack.peek(); |
|
213 |
int nodeIndex = currentElement.incChildNum(); |
|
214 |
currentElement.writePIToDB(target, data, nodeIndex); |
|
207 | 215 |
} |
208 | 216 |
|
209 | 217 |
/** SAX Handler that is called at the end of each XML element */ |
DBEntityResolver.java | ||
---|---|---|
65 | 65 |
// register publicId in db and use the provided systemId |
66 | 66 |
if (systemId != "") { |
67 | 67 |
new URL(systemId); |
68 |
registerDTDPublicID (conn, doctype, publicId, systemId);
|
|
68 |
registerDTDSystemID (conn, doctype, publicId, systemId);
|
|
69 | 69 |
return null; |
70 | 70 |
} |
71 | 71 |
new URL(dbSystemId); |
... | ... | |
89 | 89 |
if ( systemId != null) { |
90 | 90 |
if (currentElementNo == 0) { |
91 | 91 |
doctype = DBSAXHandler.docname; |
92 |
registerDTDPublicID (conn, doctype, "", systemId); |
|
92 |
dbSystemId = getDTDSystemID (conn, doctype); |
|
93 |
if (dbSystemId == "") { |
|
94 |
new URL(systemId); |
|
95 |
registerDTDSystemID (conn, doctype, doctype, systemId); |
|
96 |
return null; |
|
97 |
} |
|
98 |
new URL(dbSystemId); |
|
99 |
return new InputSource(dbSystemId); |
|
93 | 100 |
} |
94 |
new URL(systemId); |
|
95 | 101 |
} |
96 | 102 |
|
97 | 103 |
// use the default behaviour |
98 | 104 |
return null; |
99 | 105 |
} |
100 |
|
|
101 |
/** Look at db XML Catalog to get System ID (if any) for that Public ID.
|
|
106 |
|
|
107 |
/** Look at db XML Catalog to get System ID (if any) for that doctype.
|
|
102 | 108 |
* Return empty string if there are not */ |
103 |
private String getDTDSystemID (Connection conn, String publicId) |
|
104 |
{ |
|
109 |
private String getDTDSystemID (Connection conn, String doctype) { |
|
105 | 110 |
String system_id = ""; |
106 | 111 |
Statement stmt; |
107 | 112 |
try { |
108 | 113 |
stmt = conn.createStatement(); |
114 |
System.out.println("DOCTYPE:" + doctype); |
|
109 | 115 |
stmt.execute("SELECT system_id FROM xml_catalog " + |
110 |
"WHERE entity_type = 'DTD' AND source_doctype = '" + publicId + "'");
|
|
116 |
"WHERE entity_type = 'DTD' AND public_id = '" + doctype + "'");
|
|
111 | 117 |
try { |
112 | 118 |
ResultSet rs = stmt.getResultSet(); |
113 | 119 |
try { |
... | ... | |
135 | 141 |
return system_id; |
136 | 142 |
} |
137 | 143 |
|
138 |
/** Register Public ID in db XML Catalog */
|
|
139 |
private void registerDTDPublicID (Connection conn, String doctype, String publicId, String systemId)
|
|
144 |
/** Register DTD System ID in db XML Catalog */
|
|
145 |
private void registerDTDSystemID (Connection conn, String doctype, String publicId, String systemId)
|
|
140 | 146 |
{ |
141 | 147 |
try { |
148 |
java.net.URLConnection urlConn = (new URL(systemId)).openConnection(); |
|
149 |
urlConn.connect(); |
|
150 |
} catch (java.io.IOException e) { |
|
151 |
System.out.println("IOException: " + e.getMessage()); |
|
152 |
return; |
|
153 |
} |
|
154 |
try { |
|
142 | 155 |
conn.setAutoCommit(false); |
143 | 156 |
PreparedStatement pstmt; |
144 | 157 |
pstmt = conn.prepareStatement( |
Also available in: Unified diff
document root node, doctype changes