Revision 72
Added by bojilova over 24 years ago
DBSAXDocument.java | ||
---|---|---|
1 |
/** |
|
2 |
* Name: DBSAXDocument.java |
|
3 |
* Purpose: A Class that writes an XML document main data |
|
4 |
* Copyright: 2000 Regents of the University of California and the |
|
5 |
* National Center for Ecological Analysis and Synthesis |
|
6 |
* Authors: Jivka Bojilova |
|
7 |
* |
|
8 |
* Version: '$Id$' |
|
9 |
*/ |
|
10 |
|
|
11 |
//package edu.ucsb.nceas.metacat; |
|
12 |
|
|
13 |
import java.sql.*; |
|
14 |
import java.io.IOException; |
|
15 |
import java.util.Hashtable; |
|
16 |
import java.util.Enumeration; |
|
17 |
|
|
18 |
/** |
|
19 |
* A Class that writes an XML document main data to a database connection |
|
20 |
*/ |
|
21 |
public class DBSAXDocument { |
|
22 |
|
|
23 |
private Connection conn; |
|
24 |
private long rootnodeid; |
|
25 |
private String docname; |
|
26 |
private String doctype; |
|
27 |
|
|
28 |
/** |
|
29 |
* Construct a new document instance |
|
30 |
* |
|
31 |
* @param conn the JDBC Connection to which all information is written |
|
32 |
* @param rootnodeid - sequence id of the root node in the document |
|
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. |
|
36 |
* |
|
37 |
*/ |
|
38 |
public DBSAXDocument (Connection conn, long rootnodeid, String docname, String doctype) |
|
39 |
{ |
|
40 |
this.conn = conn; |
|
41 |
this.rootnodeid = rootnodeid; |
|
42 |
this.docname = docname; |
|
43 |
this.doctype = doctype; |
|
44 |
writeDocumentToDB(); |
|
45 |
System.out.println("DBSAXDocument.writeDocumentToDB"); |
|
46 |
} |
|
47 |
|
|
48 |
/** creates SQL code and inserts new document into DB connection */ |
|
49 |
private void writeDocumentToDB() { |
|
50 |
try { |
|
51 |
conn.setAutoCommit(false); |
|
52 |
PreparedStatement pstmt; |
|
53 |
pstmt = conn.prepareStatement( |
|
54 |
"INSERT INTO xml_documents (docid, rootnodeid, docname, doctype) " + |
|
55 |
"VALUES (null, ?, ?, ?)"); |
|
56 |
|
|
57 |
// Bind the values to the query |
|
58 |
pstmt.setLong(1, rootnodeid); |
|
59 |
pstmt.setString(2, docname); |
|
60 |
pstmt.setString(3, doctype); |
|
61 |
// Do the insertion |
|
62 |
pstmt.execute(); |
|
63 |
pstmt.close(); |
|
64 |
conn.commit(); |
|
65 |
conn.setAutoCommit(true); |
|
66 |
} catch (SQLException e) { |
|
67 |
System.out.println(e.getMessage()); |
|
68 |
} |
|
69 |
} |
|
70 |
|
|
71 |
} |
|
0 | 72 |
src/xmltables.sql | ||
---|---|---|
1 |
/* |
|
1 |
source_doctype, public_id, /*
|
|
2 | 2 |
* xmltables.sql -- Create or replace tables for storing XML in the db |
3 | 3 |
* |
4 | 4 |
* Purpose: creates tables needed for XML database |
... | ... | |
13 | 13 |
/* |
14 | 14 |
* Drop all of the objects in proper order |
15 | 15 |
*/ |
16 |
set echo off |
|
17 |
|
|
16 | 18 |
DROP SEQUENCE xml_nodes_id_seq; |
19 |
DROP SEQUENCE xml_entity_id_seq; |
|
20 |
DROP SEQUENCE xml_documents_id_seq; |
|
21 |
|
|
17 | 22 |
DROP TRIGGER xml_nodes_before_insert; |
23 |
DROP TRIGGER xml_documents_before_insert; |
|
24 |
DROP TRIGGER xml_cat_entities_before_insert; |
|
25 |
|
|
26 |
DROP TABLE xml_catalog_entities; |
|
27 |
DROP TABLE xml_catalog; |
|
18 | 28 |
DROP TABLE xml_documents; |
19 | 29 |
DROP TABLE xml_nodes; |
20 | 30 |
|
... | ... | |
24 | 34 |
CREATE TABLE xml_nodes ( |
25 | 35 |
nodeid NUMBER(20), |
26 | 36 |
parentnodeid NUMBER(20), |
27 |
nodetype VARCHAR2(2000), |
|
28 |
nodename VARCHAR2(2000), |
|
29 |
nodedata VARCHAR2(2000), |
|
37 |
nodeindex NUMBER(10), |
|
38 |
nodetype VARCHAR2(20), |
|
39 |
nodename VARCHAR2(100), |
|
40 |
nodedata LONG, |
|
30 | 41 |
date_created DATE, |
31 | 42 |
date_updated DATE, |
32 | 43 |
CONSTRAINT xml_nodes_pk PRIMARY KEY (nodeid), |
... | ... | |
46 | 57 |
/ |
47 | 58 |
|
48 | 59 |
/* |
49 |
* Documents -- table to store XML document catalog
|
|
60 |
* Documents -- table to store XML documents
|
|
50 | 61 |
*/ |
51 | 62 |
CREATE TABLE xml_documents ( |
52 | 63 |
docid NUMBER(20), |
53 | 64 |
rootnodeid NUMBER(20), |
54 |
docname VARCHAR2(1000),
|
|
55 |
doctype VARCHAR2(1000),
|
|
65 |
docname VARCHAR2(100), |
|
66 |
doctype VARCHAR2(100), |
|
56 | 67 |
date_created DATE, |
57 | 68 |
date_updated DATE, |
58 | 69 |
CONSTRAINT xml_documents_pk PRIMARY KEY (docid), |
... | ... | |
60 | 71 |
FOREIGN KEY (rootnodeid) REFERENCES xml_nodes |
61 | 72 |
); |
62 | 73 |
|
74 |
CREATE SEQUENCE xml_documents_id_seq; |
|
75 |
|
|
76 |
CREATE TRIGGER xml_documents_before_insert |
|
77 |
BEFORE INSERT ON xml_documents FOR EACH ROW |
|
78 |
BEGIN |
|
79 |
SELECT xml_documents_id_seq.nextval |
|
80 |
INTO :new.docid |
|
81 |
FROM dual; |
|
82 |
END; |
|
83 |
/ |
|
84 |
|
|
85 |
/* |
|
86 |
* XML Catalog -- tables to store all external sources for XML documents |
|
87 |
*/ |
|
88 |
CREATE TABLE xml_catalog ( |
|
89 |
doctype VARCHAR2(100), |
|
90 |
date_created DATE, |
|
91 |
date_updated DATE, |
|
92 |
CONSTRAINT xml_catalog_pk PRIMARY KEY (doctype) |
|
93 |
); |
|
94 |
|
|
95 |
CREATE TABLE xml_catalog_entities ( |
|
96 |
entity_id NUMBER(20), |
|
97 |
entity_name VARCHAR2(100), |
|
98 |
entity_type VARCHAR2(20), |
|
99 |
source_doctype VARCHAR2(100), |
|
100 |
target_doctype VARCHAR2(100), |
|
101 |
public_id VARCHAR2(100), |
|
102 |
system_id VARCHAR2(1000), |
|
103 |
date_created DATE, |
|
104 |
date_updated DATE, |
|
105 |
CONSTRAINT xml_catalog_entities_pk PRIMARY KEY (entity_id), |
|
106 |
CONSTRAINT xml_catalog_entities_uk UNIQUE (entity_type, source_doctype, target_doctype, public_id), |
|
107 |
CONSTRAINT xml_source_catalog_fk FOREIGN KEY (source_doctype) REFERENCES xml_catalog, |
|
108 |
CONSTRAINT xml_target_catalog_fk FOREIGN KEY (target_doctype) REFERENCES xml_catalog |
|
109 |
); |
|
110 |
|
|
111 |
CREATE SEQUENCE xml_entity_id_seq; |
|
112 |
|
|
113 |
CREATE TRIGGER xml_cat_entities_before_insert |
|
114 |
BEFORE INSERT ON xml_catalog_entities FOR EACH ROW |
|
115 |
BEGIN |
|
116 |
SELECT xml_entity_id_seq.nextval |
|
117 |
INTO :new.entity_id |
|
118 |
FROM dual; |
|
119 |
END; |
|
120 |
/ |
src/edu/ucsb/nceas/metacat/DBSAXHandler.java | ||
---|---|---|
9 | 9 |
* Version: '$Id$' |
10 | 10 |
*/ |
11 | 11 |
|
12 |
package edu.ucsb.nceas.metacat; |
|
12 |
//package edu.ucsb.nceas.metacat;
|
|
13 | 13 |
|
14 | 14 |
import org.xml.sax.*; |
15 | 15 |
|
... | ... | |
23 | 23 |
import oracle.xml.parser.v2.SAXAttrList; |
24 | 24 |
|
25 | 25 |
import oracle.xml.parser.v2.SAXParser; |
26 |
import oracle.xml.parser.v2.DTD; |
|
26 | 27 |
|
27 | 28 |
/** |
28 | 29 |
* A database aware Class implementing callback bethods for the SAX parser to |
... | ... | |
31 | 32 |
public class DBSAXHandler extends DefaultXMLDocumentHandler |
32 | 33 |
{ |
33 | 34 |
|
35 |
static int elementNo = 0; |
|
36 |
private String docname; |
|
37 |
private String doctype; |
|
38 |
private String systemid; |
|
34 | 39 |
private boolean debug = false; |
35 | 40 |
private boolean stackCreated = false; |
36 | 41 |
private Stack elementStack; |
... | ... | |
53 | 58 |
|
54 | 59 |
} |
55 | 60 |
|
61 |
/** SAX Handler that receives notification of beginning of the document */ |
|
62 |
public void startDocument() throws SAXException |
|
63 |
{ |
|
64 |
System.out.println("start Document"); |
|
65 |
} |
|
66 |
|
|
67 |
/** SAX Handler that receives notification of end of the document */ |
|
68 |
public void endDocument() throws SAXException |
|
69 |
{ |
|
70 |
System.out.println("end Document"); |
|
71 |
} |
|
72 |
|
|
73 |
/** SAX Handler that receives notification of DTD. Sets the DTD */ |
|
74 |
public void setDoctype(DTD dtd) throws SAXException |
|
75 |
{ |
|
76 |
// here is a bug: dtd.getPublicId() and dtd.getSustemId() return null. |
|
77 |
docname = dtd.getName(); |
|
78 |
doctype = dtd.getPublicId(); |
|
79 |
systemid = dtd.getSystemId(); |
|
80 |
System.out.println("DOCTYPE: " + docname); |
|
81 |
System.out.println("DOCTYPE: " + doctype); |
|
82 |
System.out.println("DOCTYPE: " + systemid); |
|
83 |
} |
|
84 |
|
|
85 |
/** SAX Handler that receives notification of end of DTD |
|
86 |
* All events in DTDHandler about all unparsed entities and the event in EntityResolver for the DTD file declaration |
|
87 |
* appear between setDoctype and endDoctype. |
|
88 |
* The rest of parsable external entities inside DTD file appear later in the elements from where they are referred to. */ |
|
89 |
public void endDoctype() throws SAXException |
|
90 |
{ |
|
91 |
System.out.println("end of DOCTYPE"); |
|
92 |
if (doctype == null) |
|
93 |
doctype = DBEntityResolver.doctype; |
|
94 |
} |
|
95 |
|
|
56 | 96 |
/** SAX Handler that is called at the start of each XML element */ |
57 | 97 |
public void startElement(NSName name, SAXAttrList atts) throws SAXException |
58 | 98 |
{ |
... | ... | |
73 | 113 |
nsName = name.getNamespace(); |
74 | 114 |
expName = name.getExpandedName(); |
75 | 115 |
|
116 |
elementNo++; |
|
117 |
if ((elementNo == 1) && (doctype == null)) |
|
118 |
throw new SAXException("No DOCTYPE declaration or PUBLIC ID provided"); |
|
76 | 119 |
// Get a reference to the parent element for the id |
77 | 120 |
long parent_id; |
121 |
int nodeIndex; |
|
78 | 122 |
try { |
79 | 123 |
parentElement = (DBSAXElement)elementStack.peek(); |
80 | 124 |
parent_id = parentElement.getElementID(); |
125 |
nodeIndex = parentElement.incChildNum(); |
|
81 | 126 |
} catch (EmptyStackException e) { |
82 | 127 |
parent_id = 0; |
128 |
nodeIndex = 0; |
|
83 | 129 |
} |
84 | 130 |
|
85 | 131 |
// Create the current element representation |
86 |
currentElement = new DBSAXElement(conn, localName, parent_id); |
|
132 |
currentElement = new DBSAXElement(conn, localName, parent_id, nodeIndex); |
|
133 |
// go to create document definition in the db |
|
134 |
// call once after insertion of the first element |
|
135 |
if (parent_id == 0) { |
|
136 |
long rootnodeid = currentElement.getElementID(); |
|
137 |
new DBSAXDocument(conn, rootnodeid, docname, doctype); |
|
138 |
} |
|
87 | 139 |
|
88 | 140 |
// Add all of the attributes |
89 | 141 |
for (int i=0; i<atts.getLength(); i++) |
... | ... | |
128 | 180 |
{ |
129 | 181 |
} |
130 | 182 |
|
183 |
/** SAX Handler called once for each comment found: |
|
184 |
* node that comment may occur before or after the root element. |
|
185 |
* For now works only for comments after the root element. */ |
|
186 |
public void comment(String data) throws SAXException |
|
187 |
{ |
|
188 |
if (elementNo > 0) { |
|
189 |
DBSAXElement currentElement = (DBSAXElement)elementStack.peek(); |
|
190 |
currentElement.writeCommentToDB(data); |
|
191 |
} |
|
192 |
} |
|
193 |
|
|
131 | 194 |
/** SAX Handler that is called at the end of each XML element */ |
132 | 195 |
public void endElement(NSName name) throws SAXException |
133 | 196 |
{ |
src/edu/ucsb/nceas/metacat/DBSAXDocument.java | ||
---|---|---|
1 |
/** |
|
2 |
* Name: DBSAXDocument.java |
|
3 |
* Purpose: A Class that writes an XML document main data |
|
4 |
* Copyright: 2000 Regents of the University of California and the |
|
5 |
* National Center for Ecological Analysis and Synthesis |
|
6 |
* Authors: Jivka Bojilova |
|
7 |
* |
|
8 |
* Version: '$Id$' |
|
9 |
*/ |
|
10 |
|
|
11 |
//package edu.ucsb.nceas.metacat; |
|
12 |
|
|
13 |
import java.sql.*; |
|
14 |
import java.io.IOException; |
|
15 |
import java.util.Hashtable; |
|
16 |
import java.util.Enumeration; |
|
17 |
|
|
18 |
/** |
|
19 |
* A Class that writes an XML document main data to a database connection |
|
20 |
*/ |
|
21 |
public class DBSAXDocument { |
|
22 |
|
|
23 |
private Connection conn; |
|
24 |
private long rootnodeid; |
|
25 |
private String docname; |
|
26 |
private String doctype; |
|
27 |
|
|
28 |
/** |
|
29 |
* Construct a new document instance |
|
30 |
* |
|
31 |
* @param conn the JDBC Connection to which all information is written |
|
32 |
* @param rootnodeid - sequence id of the root node in the document |
|
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. |
|
36 |
* |
|
37 |
*/ |
|
38 |
public DBSAXDocument (Connection conn, long rootnodeid, String docname, String doctype) |
|
39 |
{ |
|
40 |
this.conn = conn; |
|
41 |
this.rootnodeid = rootnodeid; |
|
42 |
this.docname = docname; |
|
43 |
this.doctype = doctype; |
|
44 |
writeDocumentToDB(); |
|
45 |
System.out.println("DBSAXDocument.writeDocumentToDB"); |
|
46 |
} |
|
47 |
|
|
48 |
/** creates SQL code and inserts new document into DB connection */ |
|
49 |
private void writeDocumentToDB() { |
|
50 |
try { |
|
51 |
conn.setAutoCommit(false); |
|
52 |
PreparedStatement pstmt; |
|
53 |
pstmt = conn.prepareStatement( |
|
54 |
"INSERT INTO xml_documents (docid, rootnodeid, docname, doctype) " + |
|
55 |
"VALUES (null, ?, ?, ?)"); |
|
56 |
|
|
57 |
// Bind the values to the query |
|
58 |
pstmt.setLong(1, rootnodeid); |
|
59 |
pstmt.setString(2, docname); |
|
60 |
pstmt.setString(3, doctype); |
|
61 |
// Do the insertion |
|
62 |
pstmt.execute(); |
|
63 |
pstmt.close(); |
|
64 |
conn.commit(); |
|
65 |
conn.setAutoCommit(true); |
|
66 |
} catch (SQLException e) { |
|
67 |
System.out.println(e.getMessage()); |
|
68 |
} |
|
69 |
} |
|
70 |
|
|
71 |
} |
|
0 | 72 |
src/edu/ucsb/nceas/metacat/DBEntityResolver.java | ||
---|---|---|
1 |
/** |
|
2 |
* Name: DBSAXHandler.java |
|
3 |
* Purpose: A Class that implements org.xml.sax.EntityResolver interface |
|
4 |
* for resolving external entities |
|
5 |
* Copyright: 2000 Regents of the University of California and the |
|
6 |
* National Center for Ecological Analysis and Synthesis |
|
7 |
* Authors: Jivka Bojilova |
|
8 |
* |
|
9 |
* Version: '$Id$' |
|
10 |
*/ |
|
11 |
|
|
12 |
//package edu.ucsb.nceas.metacat; |
|
13 |
|
|
14 |
import org.xml.sax.*; |
|
15 |
|
|
16 |
import java.sql.*; |
|
17 |
import java.net.URL; |
|
18 |
import java.net.MalformedURLException; |
|
19 |
import java.util.Stack; |
|
20 |
import java.util.EmptyStackException; |
|
21 |
|
|
22 |
/** |
|
23 |
* A database aware Class implementing EntityResolver interface for the SAX parser to |
|
24 |
* call when processing the XML stream and intercepting any external entities |
|
25 |
* (including the external DTD subset and external parameter entities, if any) before including them. |
|
26 |
*/ |
|
27 |
public class DBEntityResolver implements EntityResolver |
|
28 |
{ |
|
29 |
|
|
30 |
static String doctype = null; |
|
31 |
static Connection conn = null; |
|
32 |
private int pIdCounter = 0; |
|
33 |
private long currentElementNo; |
|
34 |
|
|
35 |
/** Construct an instance of the DBEntityResolver clas |
|
36 |
* |
|
37 |
* @param conn the JDBC connection to which information is written |
|
38 |
*/ |
|
39 |
public DBEntityResolver(Connection conn) |
|
40 |
{ |
|
41 |
this.conn = conn; |
|
42 |
} |
|
43 |
|
|
44 |
|
|
45 |
/** The Parser call this method before opening any external entity |
|
46 |
* except the top-level document entity (including the external DTD subset, |
|
47 |
* external entities referenced within the DTD, and external entities referenced |
|
48 |
* within the document element)*/ |
|
49 |
public InputSource resolveEntity (String publicId, String systemId) |
|
50 |
throws MalformedURLException |
|
51 |
{ |
|
52 |
String dbSystemId; |
|
53 |
|
|
54 |
if (publicId != null) { |
|
55 |
pIdCounter += 1; |
|
56 |
currentElementNo = DBSAXHandler.elementNo; |
|
57 |
System.out.println("from DBEntityResolver: current element is " + DBSAXHandler.elementNo); |
|
58 |
System.out.println("from DBEntityResolver: " + pIdCounter + " " + publicId); |
|
59 |
// look at the db XML Catalog and get dbSystemId by this publicId |
|
60 |
if (currentElementNo == 0) { |
|
61 |
doctype = publicId; |
|
62 |
dbSystemId = getDTDSystemID (conn, publicId); |
|
63 |
if (dbSystemId == "") |
|
64 |
// register publicId in db and use the provided systemId |
|
65 |
if (systemId != "") { |
|
66 |
System.out.println("from DBEntityResolver: " + systemId); |
|
67 |
new URL(systemId); |
|
68 |
registerDTDPublicID (conn, publicId, systemId); |
|
69 |
return null; |
|
70 |
} |
|
71 |
new URL(dbSystemId); |
|
72 |
System.out.println("from DBEntityResolver: db System ID: " + dbSystemId); |
|
73 |
return new InputSource(dbSystemId); |
|
74 |
} |
|
75 |
else { |
|
76 |
// look at the db XML Catalog and get dbSystemId by this publicId for a given doctype |
|
77 |
dbSystemId = getEntitySystemID (conn, doctype, publicId); |
|
78 |
if (dbSystemId == "") |
|
79 |
// register publicId in db for a given doctype and use the provided systemId |
|
80 |
if (systemId != "") { |
|
81 |
System.out.println("from DBEntityResolver: " + systemId); |
|
82 |
new URL(systemId); |
|
83 |
registerEntityPublicID (conn, doctype, publicId, systemId); |
|
84 |
return null; |
|
85 |
} |
|
86 |
new URL(dbSystemId); |
|
87 |
System.out.println("from DBEntityResolver: db System ID: " + dbSystemId); |
|
88 |
return new InputSource(dbSystemId); |
|
89 |
} |
|
90 |
} |
|
91 |
if ( systemId != null) { |
|
92 |
System.out.println("from DBEntityResolver: " + systemId); |
|
93 |
new URL(systemId); |
|
94 |
} |
|
95 |
|
|
96 |
// use the default behaviour |
|
97 |
return null; |
|
98 |
} |
|
99 |
|
|
100 |
/** Look at db XML Catalog to get System ID (if any) for that Public ID. |
|
101 |
* Return empty string if there are not */ |
|
102 |
private String getDTDSystemID (Connection conn, String publicId) |
|
103 |
{ |
|
104 |
String system_id = ""; |
|
105 |
Statement stmt; |
|
106 |
try { |
|
107 |
stmt = conn.createStatement(); |
|
108 |
stmt.execute("SELECT system_id FROM xml_catalog_entities a, xml_catalog b " + |
|
109 |
"WHERE a.entity_type = 'DTD' AND a.source_doctype = b.doctype AND b.doctype = '" + publicId + "'"); |
|
110 |
try { |
|
111 |
ResultSet rs = stmt.getResultSet(); |
|
112 |
try { |
|
113 |
boolean tableHasRows = rs.next(); |
|
114 |
if (tableHasRows) { |
|
115 |
try { |
|
116 |
system_id = rs.getString(1); |
|
117 |
} catch (SQLException e) { |
|
118 |
System.out.println("DBEntityResolver.getDTDSystemID() - Error with getString: " + e.getMessage()); |
|
119 |
} |
|
120 |
} |
|
121 |
} catch (SQLException e) { |
|
122 |
System.out.println("DBEntityResolver.getDTDSystemID() - Error with next: " + e.getMessage()); |
|
123 |
} |
|
124 |
} catch (SQLException e) { |
|
125 |
System.out.println("DBEntityResolver.getDTDSystemID() - Error with getrset: " + e.getMessage()); |
|
126 |
} |
|
127 |
stmt.close(); |
|
128 |
} catch (SQLException e) { |
|
129 |
System.out.println("DBEntityResolver.getDTDSystemID() - Error getting id: " + e.getMessage()); |
|
130 |
} |
|
131 |
|
|
132 |
// return the selected System ID |
|
133 |
return system_id; |
|
134 |
} |
|
135 |
|
|
136 |
/** Register Public ID in db XML Catalog */ |
|
137 |
private void registerDTDPublicID (Connection conn, String publicId, String systemId) |
|
138 |
{ |
|
139 |
try { |
|
140 |
conn.setAutoCommit(false); |
|
141 |
PreparedStatement pstmt; |
|
142 |
pstmt = conn.prepareStatement( |
|
143 |
"INSERT INTO xml_catalog (doctype) VALUES (?)"); |
|
144 |
// Bind the values to the query |
|
145 |
pstmt.setString(1, publicId); |
|
146 |
// Do the insertion |
|
147 |
pstmt.execute(); |
|
148 |
pstmt.close(); |
|
149 |
pstmt = conn.prepareStatement( |
|
150 |
"INSERT INTO xml_catalog_entities (entity_id, entity_type, source_doctype, system_id) " + |
|
151 |
"VALUES (null, 'DTD', ?, ?)"); |
|
152 |
// Bind the values to the query |
|
153 |
pstmt.setString(1, publicId); |
|
154 |
pstmt.setString(2, systemId); |
|
155 |
// Do the insertion |
|
156 |
pstmt.execute(); |
|
157 |
pstmt.close(); |
|
158 |
conn.commit(); |
|
159 |
conn.setAutoCommit(true); |
|
160 |
} catch (SQLException e) { |
|
161 |
System.out.println(e.getMessage()); |
|
162 |
} |
|
163 |
} |
|
164 |
|
|
165 |
/** Look at db XML Catalog to get System ID (if any) for that Public ID and doctype. |
|
166 |
* Return empty string if there are not */ |
|
167 |
private String getEntitySystemID (Connection conn, String doctype, String publicId) |
|
168 |
{ |
|
169 |
String system_id = ""; |
|
170 |
Statement stmt; |
|
171 |
try { |
|
172 |
stmt = conn.createStatement(); |
|
173 |
stmt.execute("SELECT system_id FROM xml_catalog_entities " + |
|
174 |
"WHERE entity_type = 'ENTITY' AND source_doctype = '" + doctype + "' AND public_id = '" + publicId + "'"); |
|
175 |
try { |
|
176 |
ResultSet rs = stmt.getResultSet(); |
|
177 |
try { |
|
178 |
boolean tableHasRows = rs.next(); |
|
179 |
if (tableHasRows) { |
|
180 |
try { |
|
181 |
system_id = rs.getString(1); |
|
182 |
} catch (SQLException e) { |
|
183 |
System.out.println("DBEntityResolver.getEntitySystemID() - Error with getString: " + e.getMessage()); |
|
184 |
} |
|
185 |
} |
|
186 |
} catch (SQLException e) { |
|
187 |
System.out.println("DBEntityResolver.getEntitySystemID() - Error with next: " + e.getMessage()); |
|
188 |
} |
|
189 |
} catch (SQLException e) { |
|
190 |
System.out.println("DBEntityResolver.getEntitySystemID() - Error with getrset: " + e.getMessage()); |
|
191 |
} |
|
192 |
stmt.close(); |
|
193 |
} catch (SQLException e) { |
|
194 |
System.out.println("DBEntityResolver.getEntitySystemID() - Error getting id: " + e.getMessage()); |
|
195 |
} |
|
196 |
|
|
197 |
// return the selected System ID number |
|
198 |
return system_id; |
|
199 |
} |
|
200 |
|
|
201 |
/** Register Public ID in db XML Catalog */ |
|
202 |
private void registerEntityPublicID (Connection conn, String doctype, String publicId, String systemId) |
|
203 |
{ |
|
204 |
try { |
|
205 |
conn.setAutoCommit(false); |
|
206 |
PreparedStatement pstmt; |
|
207 |
pstmt = conn.prepareStatement( |
|
208 |
"INSERT INTO xml_catalog_entities (entity_id, entity_name, entity_type, source_doctype, public_id, system_id) " + |
|
209 |
"VALUES (null, null, 'ENTITY', ?, ?, ?)"); |
|
210 |
// Bind the values to the query |
|
211 |
pstmt.setString(1, doctype); |
|
212 |
pstmt.setString(2, publicId); |
|
213 |
pstmt.setString(3, systemId); |
|
214 |
// Do the insertion |
|
215 |
pstmt.execute(); |
|
216 |
pstmt.close(); |
|
217 |
conn.commit(); |
|
218 |
conn.setAutoCommit(true); |
|
219 |
} catch (SQLException e) { |
|
220 |
System.out.println(e.getMessage()); |
|
221 |
} |
|
222 |
} |
|
223 |
|
|
224 |
} |
|
0 | 225 |
src/edu/ucsb/nceas/metacat/DBSAXNode.java | ||
---|---|---|
8 | 8 |
* Version: '$Id$' |
9 | 9 |
*/ |
10 | 10 |
|
11 |
package edu.ucsb.nceas.metacat; |
|
11 |
//package edu.ucsb.nceas.metacat;
|
|
12 | 12 |
|
13 | 13 |
import java.sql.*; |
14 | 14 |
import java.io.IOException; |
... | ... | |
31 | 31 |
* @param parent_id the parent id number for this element |
32 | 32 |
*/ |
33 | 33 |
public DBSAXElement (Connection conn, String tagname, |
34 |
long parent_id) { |
|
34 |
long parent_id, int nodeIndex) {
|
|
35 | 35 |
|
36 |
super(tagname, parent_id); |
|
36 |
super(tagname, parent_id, nodeIndex);
|
|
37 | 37 |
|
38 | 38 |
this.conn = conn; |
39 | 39 |
writeElementToDB(); |
... | ... | |
47 | 47 |
if (getParentID() != 0) { |
48 | 48 |
pstmt = conn.prepareStatement( |
49 | 49 |
"INSERT INTO xml_nodes (nodeid, nodetype, " + |
50 |
"nodename, parentnodeid) VALUES (null, ?, ?, ?)");
|
|
50 |
"nodename, parentnodeid, nodeindex) VALUES (null, ?, ?, ?, ?)");
|
|
51 | 51 |
} else { |
52 | 52 |
pstmt = conn.prepareStatement( |
53 | 53 |
"INSERT INTO xml_nodes (nodeid, nodetype, nodename) " + |
... | ... | |
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); |
|
60 | 61 |
if (getParentID() != 0) { |
61 | 62 |
pstmt.setLong(3, getParentID()); |
63 |
pstmt.setInt(4, getNodeIndex()); |
|
62 | 64 |
} |
63 | 65 |
// Do the insertion |
64 | 66 |
pstmt.execute(); |
... | ... | |
71 | 73 |
} |
72 | 74 |
} |
73 | 75 |
|
76 |
/** creates SQL code and inserts comment into DB connection */ |
|
77 |
void writeCommentToDB(String data) { |
|
78 |
try { |
|
79 |
PreparedStatement pstmt; |
|
80 |
pstmt = conn.prepareStatement( |
|
81 |
"INSERT INTO xml_nodes (nodeid, nodetype, " + |
|
82 |
"nodename, parentnodeid, nodedata) VALUES (null, ?, null, ?, ?)"); |
|
83 |
|
|
84 |
// Bind the values to the query |
|
85 |
pstmt.setString(1, "COMMENT"); |
|
86 |
pstmt.setLong(2, getElementID()); |
|
87 |
pstmt.setString(3, data); |
|
88 |
// Do the insertion |
|
89 |
pstmt.execute(); |
|
90 |
pstmt.close(); |
|
91 |
} catch (SQLException e) { |
|
92 |
System.out.println(e.getMessage()); |
|
93 |
} |
|
94 |
} |
|
95 |
|
|
74 | 96 |
/** look up the assigned element id from DB connection */ |
75 | 97 |
private long getAssignedElementID() { |
76 | 98 |
long assigned_id=0; |
src/edu/ucsb/nceas/metacat/DBSAXElement.java | ||
---|---|---|
8 | 8 |
* Version: '$Id$' |
9 | 9 |
*/ |
10 | 10 |
|
11 |
package edu.ucsb.nceas.metacat; |
|
11 |
//package edu.ucsb.nceas.metacat;
|
|
12 | 12 |
|
13 | 13 |
import java.sql.*; |
14 | 14 |
import java.io.IOException; |
... | ... | |
31 | 31 |
* @param parent_id the parent id number for this element |
32 | 32 |
*/ |
33 | 33 |
public DBSAXElement (Connection conn, String tagname, |
34 |
long parent_id) { |
|
34 |
long parent_id, int nodeIndex) {
|
|
35 | 35 |
|
36 |
super(tagname, parent_id); |
|
36 |
super(tagname, parent_id, nodeIndex);
|
|
37 | 37 |
|
38 | 38 |
this.conn = conn; |
39 | 39 |
writeElementToDB(); |
... | ... | |
47 | 47 |
if (getParentID() != 0) { |
48 | 48 |
pstmt = conn.prepareStatement( |
49 | 49 |
"INSERT INTO xml_nodes (nodeid, nodetype, " + |
50 |
"nodename, parentnodeid) VALUES (null, ?, ?, ?)");
|
|
50 |
"nodename, parentnodeid, nodeindex) VALUES (null, ?, ?, ?, ?)");
|
|
51 | 51 |
} else { |
52 | 52 |
pstmt = conn.prepareStatement( |
53 | 53 |
"INSERT INTO xml_nodes (nodeid, nodetype, nodename) " + |
... | ... | |
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); |
|
60 | 61 |
if (getParentID() != 0) { |
61 | 62 |
pstmt.setLong(3, getParentID()); |
63 |
pstmt.setInt(4, getNodeIndex()); |
|
62 | 64 |
} |
63 | 65 |
// Do the insertion |
64 | 66 |
pstmt.execute(); |
... | ... | |
71 | 73 |
} |
72 | 74 |
} |
73 | 75 |
|
76 |
/** creates SQL code and inserts comment into DB connection */ |
|
77 |
void writeCommentToDB(String data) { |
|
78 |
try { |
|
79 |
PreparedStatement pstmt; |
|
80 |
pstmt = conn.prepareStatement( |
|
81 |
"INSERT INTO xml_nodes (nodeid, nodetype, " + |
|
82 |
"nodename, parentnodeid, nodedata) VALUES (null, ?, null, ?, ?)"); |
|
83 |
|
|
84 |
// Bind the values to the query |
|
85 |
pstmt.setString(1, "COMMENT"); |
|
86 |
pstmt.setLong(2, getElementID()); |
|
87 |
pstmt.setString(3, data); |
|
88 |
// Do the insertion |
|
89 |
pstmt.execute(); |
|
90 |
pstmt.close(); |
|
91 |
} catch (SQLException e) { |
|
92 |
System.out.println(e.getMessage()); |
|
93 |
} |
|
94 |
} |
|
95 |
|
|
74 | 96 |
/** look up the assigned element id from DB connection */ |
75 | 97 |
private long getAssignedElementID() { |
76 | 98 |
long assigned_id=0; |
src/edu/ucsb/nceas/metacat/DBDTDHandler.java | ||
---|---|---|
1 |
/** |
|
2 |
* Name: DBDTDHandler.java |
|
3 |
* Purpose: A Class that implements org.xml.sax.DTDHandler interface |
|
4 |
* for resolving external entities |
|
5 |
* Copyright: 2000 Regents of the University of California and the |
|
6 |
* National Center for Ecological Analysis and Synthesis |
|
7 |
* Authors: Jivka Bojilova |
|
8 |
* |
|
9 |
* Version: '$Id$' |
|
10 |
*/ |
|
11 |
|
|
12 |
//package edu.ucsb.nceas.metacat; |
|
13 |
|
|
14 |
import org.xml.sax.*; |
|
15 |
|
|
16 |
import java.sql.*; |
|
17 |
import java.net.URL; |
|
18 |
import java.net.MalformedURLException; |
|
19 |
import java.util.Stack; |
|
20 |
import java.util.EmptyStackException; |
|
21 |
|
|
22 |
/** |
|
23 |
* A database aware Class implementing DTDHandler interface for the SAX parser to |
|
24 |
* call when processing the XML stream and intercepting notations and unparsed entities |
|
25 |
*/ |
|
26 |
public class DBDTDHandler implements DTDHandler |
|
27 |
{ |
|
28 |
private Connection conn = null; |
|
29 |
|
|
30 |
/** Construct an instance of the DBDTDHandler clas |
|
31 |
* |
|
32 |
* @param conn the JDBC connection to which information is written |
|
33 |
*/ |
|
34 |
public DBDTDHandler(Connection conn) |
|
35 |
{ |
|
36 |
this.conn = conn; |
|
37 |
} |
|
38 |
|
|
39 |
/** Notation declarations are not signaled */ |
|
40 |
public void notationDecl(String name, String publicId, String systemId) |
|
41 |
throws SAXException |
|
42 |
{ |
|
43 |
System.out.println("from DBDTDHandler.notationDecl"); |
|
44 |
System.out.println(name); |
|
45 |
System.out.println(publicId); |
|
46 |
System.out.println(systemId); |
|
47 |
return; |
|
48 |
} |
|
49 |
|
|
50 |
/** All are reported after startDocument and before startElement event*/ |
|
51 |
public void unparsedEntityDecl(String name, String publicId, String systemId, String notationName) |
|
52 |
throws SAXException |
|
53 |
{ |
|
54 |
System.out.println("from DBDTDHandler.unparsedEntityDecl"); |
|
55 |
System.out.print(name); |
|
56 |
System.out.print(publicId); |
|
57 |
System.out.print(systemId); |
|
58 |
System.out.println(notationName); |
|
59 |
Connection conn = DBEntityResolver.conn; |
|
60 |
String doctype = DBEntityResolver.doctype; |
|
61 |
if ( getEntitySystemID(conn, doctype, publicId) == "" ) |
|
62 |
registerEntityPublicID(conn, doctype, publicId, systemId); |
|
63 |
return; |
|
64 |
} |
|
65 |
|
|
66 |
/** Look at db XML Catalog to get System ID (if any) for that Public ID and doctype. |
|
67 |
* Return empty string if there are not */ |
|
68 |
private String getEntitySystemID (Connection conn, String doctype, String publicId) |
|
69 |
{ |
|
70 |
String system_id = ""; |
|
71 |
Statement stmt; |
|
72 |
try { |
|
73 |
stmt = conn.createStatement(); |
|
74 |
stmt.execute("SELECT system_id FROM xml_catalog_entities " + |
|
75 |
"WHERE entity_type = 'ENTITY' AND source_doctype = '" + doctype + "' AND public_id = '" + publicId + "'"); |
|
76 |
try { |
|
77 |
ResultSet rs = stmt.getResultSet(); |
|
78 |
try { |
|
79 |
boolean tableHasRows = rs.next(); |
|
80 |
if (tableHasRows) { |
|
81 |
try { |
|
82 |
system_id = rs.getString(1); |
|
83 |
} catch (SQLException e) { |
|
84 |
System.out.println("DBEntityResolver.getEntitySystemID() - Error with getString: " + e.getMessage()); |
|
85 |
} |
|
86 |
} |
|
87 |
} catch (SQLException e) { |
|
88 |
System.out.println("DBEntityResolver.getEntitySystemID() - Error with next: " + e.getMessage()); |
|
89 |
} |
|
90 |
} catch (SQLException e) { |
|
91 |
System.out.println("DBEntityResolver.getEntitySystemID() - Error with getrset: " + e.getMessage()); |
|
92 |
} |
|
93 |
stmt.close(); |
|
94 |
} catch (SQLException e) { |
|
95 |
System.out.println("DBEntityResolver.getEntitySystemID() - Error getting id: " + e.getMessage()); |
|
96 |
} |
|
97 |
|
|
98 |
// return the selected System ID |
|
99 |
return system_id; |
|
100 |
} |
|
101 |
|
|
102 |
/** Register Public ID in db XML Catalog */ |
|
103 |
private void registerEntityPublicID (Connection conn, String doctype, String publicId, String systemId) |
|
104 |
{ |
|
105 |
try { |
|
106 |
conn.setAutoCommit(false); |
|
107 |
PreparedStatement pstmt; |
|
108 |
pstmt = conn.prepareStatement( |
|
109 |
"INSERT INTO xml_catalog_entities (entity_id, entity_name, entity_type, source_doctype, public_id, system_id) " + |
|
110 |
"VALUES (null, null, 'ENTITY', ?, ?, ?)"); |
|
111 |
// Bind the values to the query |
|
112 |
pstmt.setString(1, doctype); |
|
113 |
pstmt.setString(2, publicId); |
|
114 |
pstmt.setString(3, systemId); |
|
115 |
// Do the insertion |
|
116 |
pstmt.execute(); |
|
117 |
pstmt.close(); |
|
118 |
conn.commit(); |
|
119 |
conn.setAutoCommit(true); |
|
120 |
} catch (SQLException e) { |
|
121 |
System.out.println(e.getMessage()); |
|
122 |
} |
|
123 |
} |
|
124 |
|
|
125 |
} |
|
0 | 126 |
src/edu/ucsb/nceas/metacat/DBWriter.java | ||
---|---|---|
9 | 9 |
* Version: '$Id$' |
10 | 10 |
*/ |
11 | 11 |
|
12 |
package edu.ucsb.nceas.metacat; |
|
12 |
//package edu.ucsb.nceas.metacat;
|
|
13 | 13 |
|
14 | 14 |
import org.xml.sax.*; |
15 | 15 |
|
... | ... | |
33 | 33 |
*/ |
34 | 34 |
public class DBSAXWriter { |
35 | 35 |
|
36 |
static String defaultDB = "jdbc:oracle:thin:@localhost:1521:test";
|
|
36 |
static String defaultDB = "jdbc:oracle:thin:@penelope.nceas.ucsb.edu:1526:DEV8";
|
|
37 | 37 |
private Connection conn = null; |
38 | 38 |
|
39 | 39 |
/** |
... | ... | |
97 | 97 |
{ |
98 | 98 |
this.conn = conn; |
99 | 99 |
|
100 |
//try { |
|
101 |
|
|
102 |
try { |
|
100 |
try { |
|
103 | 101 |
SAXParser parser = initializeParser(conn); |
104 | 102 |
parser.parse(xml); |
105 | 103 |
} catch (SAXParseException e) { |
106 | 104 |
System.err.println(e.getMessage()); |
107 | 105 |
} catch (SAXException e) { |
108 | 106 |
System.err.println(e.getMessage()); |
107 |
} catch (Exception e) { |
|
108 |
System.err.println(e.toString()); |
|
109 | 109 |
} |
110 |
//} catch (Exception e) { |
|
111 |
//System.err.println(e.toString()); |
|
112 |
//} |
|
113 | 110 |
} |
114 | 111 |
|
115 | 112 |
public DBSAXWriter( String filename, Connection conn) |
... | ... | |
126 | 123 |
System.err.println(e.getMessage()); |
127 | 124 |
} catch (SAXException e) { |
128 | 125 |
System.err.println(e.getMessage()); |
126 |
} catch (Exception e) { |
|
127 |
System.err.println("here is the URL exception"); |
|
128 |
System.err.println(e.toString()); |
|
129 | 129 |
} |
130 | 130 |
} |
131 | 131 |
|
... | ... | |
138 | 138 |
// Use the XMLDocumentHandler interface for namespace support |
139 | 139 |
// instead of org.xml.sax.DocumentHandler |
140 | 140 |
XMLDocumentHandler xmlDocHandler = new DBSAXHandler(conn); |
141 |
EntityResolver xmlEntityResolver = new DBEntityResolver(conn); |
|
142 |
DTDHandler xmlDTDHandler = new DBDTDHandler(conn); |
|
141 | 143 |
|
142 | 144 |
// For all the other interface use the default provided by |
143 | 145 |
// Handler base |
... | ... | |
149 | 151 |
// Set Handlers in the parser |
150 | 152 |
// Set the DocumentHandler to XMLDocumentHandler |
151 | 153 |
parser.setDocumentHandler(xmlDocHandler); |
154 |
parser.setEntityResolver(xmlEntityResolver); |
|
155 |
parser.setDTDHandler(xmlDTDHandler); |
|
152 | 156 |
|
153 | 157 |
// Set the other Handler to the defHandler |
154 | 158 |
parser.setErrorHandler(defHandler); |
155 |
parser.setEntityResolver(defHandler); |
|
156 |
parser.setDTDHandler(defHandler); |
|
157 | 159 |
|
158 | 160 |
} catch (Exception e) { |
159 | 161 |
System.err.println(e.toString()); |
src/edu/ucsb/nceas/metacat/BasicNode.java | ||
---|---|---|
8 | 8 |
* Version: '$Id$' |
9 | 9 |
*/ |
10 | 10 |
|
11 |
package edu.ucsb.nceas.metacat; |
|
11 |
//package edu.ucsb.nceas.metacat;
|
|
12 | 12 |
|
13 | 13 |
import java.io.IOException; |
14 | 14 |
import java.util.Hashtable; |
... | ... | |
21 | 21 |
private String tagname; |
22 | 22 |
private StringBuffer content; |
23 | 23 |
private long parent_id; |
24 |
private Hashtable attributes; |
|
24 |
private Hashtable attributes; |
|
25 |
private int childNum; |
|
26 |
private int nodeIndex; |
|
25 | 27 |
|
26 | 28 |
/** Construct a Basic Element */ |
27 | 29 |
public BasicElement () { |
... | ... | |
33 | 35 |
* |
34 | 36 |
* @param tagname the name of the element |
35 | 37 |
* @param parent_id the id number of the parent element |
38 |
* @param nodeIndex - number of the element in the order for a given parent node |
|
39 |
* Every element initializes childNum to 0 when created and has interface incChildNum |
|
40 |
* when new child is created |
|
36 | 41 |
*/ |
37 |
public BasicElement (String tagname, long parent_id) { |
|
42 |
public BasicElement (String tagname, long parent_id, int nodeIndex) {
|
|
38 | 43 |
this(); |
39 | 44 |
this.tagname = tagname; |
40 | 45 |
this.parent_id = parent_id; |
46 |
this.nodeIndex = nodeIndex; |
|
47 |
this.childNum = 0; |
|
41 | 48 |
} |
42 | 49 |
|
43 | 50 |
/** Construct a Basic Element |
... | ... | |
46 | 53 |
* @param tagname the name of the element |
47 | 54 |
* @param parent_id the id number of the parent element |
48 | 55 |
*/ |
49 |
public BasicElement (long element_id, String tagname, long parent_id) { |
|
50 |
this(tagname,parent_id); |
|
56 |
public BasicElement (long element_id, String tagname, long parent_id, int nodeIndex) {
|
|
57 |
this(tagname,parent_id,nodeIndex);
|
|
51 | 58 |
this.element_id = element_id; |
52 | 59 |
} |
53 | 60 |
|
... | ... | |
149 | 156 |
public String getContent() { |
150 | 157 |
return this.content.toString(); |
151 | 158 |
} |
159 |
|
|
160 |
/** Get nodeIndex of the element */ |
|
161 |
public int getNodeIndex() { |
|
162 |
return this.nodeIndex; |
|
163 |
} |
|
164 |
|
|
165 |
/** increase childNum when new child for the element is created */ |
|
166 |
public int incChildNum() { |
|
167 |
return ++this.childNum; |
|
168 |
} |
|
152 | 169 |
} |
src/edu/ucsb/nceas/metacat/BasicElement.java | ||
---|---|---|
8 | 8 |
* Version: '$Id$' |
9 | 9 |
*/ |
10 | 10 |
|
11 |
package edu.ucsb.nceas.metacat; |
|
11 |
//package edu.ucsb.nceas.metacat;
|
|
12 | 12 |
|
13 | 13 |
import java.io.IOException; |
14 | 14 |
import java.util.Hashtable; |
... | ... | |
21 | 21 |
private String tagname; |
22 | 22 |
private StringBuffer content; |
23 | 23 |
private long parent_id; |
24 |
private Hashtable attributes; |
|
24 |
private Hashtable attributes; |
|
25 |
private int childNum; |
|
26 |
private int nodeIndex; |
|
25 | 27 |
|
26 | 28 |
/** Construct a Basic Element */ |
27 | 29 |
public BasicElement () { |
... | ... | |
33 | 35 |
* |
34 | 36 |
* @param tagname the name of the element |
35 | 37 |
* @param parent_id the id number of the parent element |
38 |
* @param nodeIndex - number of the element in the order for a given parent node |
|
39 |
* Every element initializes childNum to 0 when created and has interface incChildNum |
|
40 |
* when new child is created |
|
36 | 41 |
*/ |
37 |
public BasicElement (String tagname, long parent_id) { |
|
42 |
public BasicElement (String tagname, long parent_id, int nodeIndex) {
|
|
38 | 43 |
this(); |
39 | 44 |
this.tagname = tagname; |
40 | 45 |
this.parent_id = parent_id; |
46 |
this.nodeIndex = nodeIndex; |
|
47 |
this.childNum = 0; |
|
41 | 48 |
} |
42 | 49 |
|
43 | 50 |
/** Construct a Basic Element |
... | ... | |
46 | 53 |
* @param tagname the name of the element |
47 | 54 |
* @param parent_id the id number of the parent element |
48 | 55 |
*/ |
49 |
public BasicElement (long element_id, String tagname, long parent_id) { |
|
50 |
this(tagname,parent_id); |
|
56 |
public BasicElement (long element_id, String tagname, long parent_id, int nodeIndex) {
|
|
57 |
this(tagname,parent_id,nodeIndex);
|
|
51 | 58 |
this.element_id = element_id; |
52 | 59 |
} |
53 | 60 |
|
... | ... | |
149 | 156 |
public String getContent() { |
150 | 157 |
return this.content.toString(); |
151 | 158 |
} |
159 |
|
|
160 |
/** Get nodeIndex of the element */ |
|
161 |
public int getNodeIndex() { |
|
162 |
return this.nodeIndex; |
|
163 |
} |
|
164 |
|
|
165 |
/** increase childNum when new child for the element is created */ |
|
166 |
public int incChildNum() { |
|
167 |
return ++this.childNum; |
|
168 |
} |
|
152 | 169 |
} |
src/edu/ucsb/nceas/metacat/DBSAXWriter.java | ||
---|---|---|
9 | 9 |
* Version: '$Id$' |
10 | 10 |
*/ |
11 | 11 |
|
12 |
package edu.ucsb.nceas.metacat; |
|
12 |
//package edu.ucsb.nceas.metacat;
|
|
13 | 13 |
|
14 | 14 |
import org.xml.sax.*; |
15 | 15 |
|
... | ... | |
33 | 33 |
*/ |
34 | 34 |
public class DBSAXWriter { |
35 | 35 |
|
36 |
static String defaultDB = "jdbc:oracle:thin:@localhost:1521:test";
|
|
36 |
static String defaultDB = "jdbc:oracle:thin:@penelope.nceas.ucsb.edu:1526:DEV8";
|
|
37 | 37 |
private Connection conn = null; |
38 | 38 |
|
39 | 39 |
/** |
... | ... | |
97 | 97 |
{ |
98 | 98 |
this.conn = conn; |
99 | 99 |
|
100 |
//try { |
|
101 |
|
|
102 |
try { |
|
100 |
try { |
|
103 | 101 |
SAXParser parser = initializeParser(conn); |
104 | 102 |
parser.parse(xml); |
105 | 103 |
} catch (SAXParseException e) { |
106 | 104 |
System.err.println(e.getMessage()); |
107 | 105 |
} catch (SAXException e) { |
108 | 106 |
System.err.println(e.getMessage()); |
107 |
} catch (Exception e) { |
|
108 |
System.err.println(e.toString()); |
|
109 | 109 |
} |
110 |
//} catch (Exception e) { |
|
111 |
//System.err.println(e.toString()); |
|
112 |
//} |
|
113 | 110 |
} |
114 | 111 |
|
115 | 112 |
public DBSAXWriter( String filename, Connection conn) |
... | ... | |
126 | 123 |
System.err.println(e.getMessage()); |
127 | 124 |
} catch (SAXException e) { |
128 | 125 |
System.err.println(e.getMessage()); |
126 |
} catch (Exception e) { |
|
127 |
System.err.println("here is the URL exception"); |
|
128 |
System.err.println(e.toString()); |
|
129 | 129 |
} |
130 | 130 |
} |
131 | 131 |
|
... | ... | |
138 | 138 |
// Use the XMLDocumentHandler interface for namespace support |
139 | 139 |
// instead of org.xml.sax.DocumentHandler |
140 | 140 |
XMLDocumentHandler xmlDocHandler = new DBSAXHandler(conn); |
141 |
EntityResolver xmlEntityResolver = new DBEntityResolver(conn); |
|
142 |
DTDHandler xmlDTDHandler = new DBDTDHandler(conn); |
|
141 | 143 |
|
142 | 144 |
// For all the other interface use the default provided by |
143 | 145 |
// Handler base |
... | ... | |
149 | 151 |
// Set Handlers in the parser |
150 | 152 |
// Set the DocumentHandler to XMLDocumentHandler |
151 | 153 |
parser.setDocumentHandler(xmlDocHandler); |
154 |
parser.setEntityResolver(xmlEntityResolver); |
|
155 |
parser.setDTDHandler(xmlDTDHandler); |
|
152 | 156 |
|
153 | 157 |
// Set the other Handler to the defHandler |
154 | 158 |
parser.setErrorHandler(defHandler); |
155 |
parser.setEntityResolver(defHandler); |
|
156 |
parser.setDTDHandler(defHandler); |
|
157 | 159 |
|
158 | 160 |
} catch (Exception e) { |
159 | 161 |
System.err.println(e.toString()); |
DBSAXElement.java | ||
---|---|---|
8 | 8 |
* Version: '$Id$' |
9 | 9 |
*/ |
10 | 10 |
|
11 |
package edu.ucsb.nceas.metacat; |
|
11 |
//package edu.ucsb.nceas.metacat;
|
|
12 | 12 |
|
13 | 13 |
import java.sql.*; |
14 | 14 |
import java.io.IOException; |
... | ... | |
31 | 31 |
* @param parent_id the parent id number for this element |
32 | 32 |
*/ |
33 | 33 |
public DBSAXElement (Connection conn, String tagname, |
34 |
long parent_id) { |
|
34 |
long parent_id, int nodeIndex) {
|
|
35 | 35 |
|
36 |
super(tagname, parent_id); |
|
36 |
super(tagname, parent_id, nodeIndex);
|
|
37 | 37 |
|
38 | 38 |
this.conn = conn; |
39 | 39 |
writeElementToDB(); |
... | ... | |
47 | 47 |
if (getParentID() != 0) { |
48 | 48 |
pstmt = conn.prepareStatement( |
49 | 49 |
"INSERT INTO xml_nodes (nodeid, nodetype, " + |
50 |
"nodename, parentnodeid) VALUES (null, ?, ?, ?)");
|
|
50 |
"nodename, parentnodeid, nodeindex) VALUES (null, ?, ?, ?, ?)");
|
|
51 | 51 |
} else { |
52 | 52 |
pstmt = conn.prepareStatement( |
53 | 53 |
"INSERT INTO xml_nodes (nodeid, nodetype, nodename) " + |
... | ... | |
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); |
|
60 | 61 |
if (getParentID() != 0) { |
61 | 62 |
pstmt.setLong(3, getParentID()); |
63 |
pstmt.setInt(4, getNodeIndex()); |
|
62 | 64 |
} |
63 | 65 |
// Do the insertion |
64 | 66 |
pstmt.execute(); |
... | ... | |
71 | 73 |
} |
72 | 74 |
} |
73 | 75 |
|
76 |
/** creates SQL code and inserts comment into DB connection */ |
|
77 |
void writeCommentToDB(String data) { |
|
78 |
try { |
|
79 |
PreparedStatement pstmt; |
|
80 |
pstmt = conn.prepareStatement( |
|
81 |
"INSERT INTO xml_nodes (nodeid, nodetype, " + |
|
82 |
"nodename, parentnodeid, nodedata) VALUES (null, ?, null, ?, ?)"); |
|
83 |
|
|
84 |
// Bind the values to the query |
|
85 |
pstmt.setString(1, "COMMENT"); |
|
86 |
pstmt.setLong(2, getElementID()); |
|
87 |
pstmt.setString(3, data); |
|
88 |
// Do the insertion |
|
89 |
pstmt.execute(); |
|
90 |
pstmt.close(); |
|
91 |
} catch (SQLException e) { |
|
92 |
System.out.println(e.getMessage()); |
|
93 |
} |
|
94 |
} |
|
95 |
|
|
74 | 96 |
/** look up the assigned element id from DB connection */ |
75 | 97 |
private long getAssignedElementID() { |
76 | 98 |
long assigned_id=0; |
BasicElement.java | ||
---|---|---|
8 | 8 |
* Version: '$Id$' |
9 | 9 |
*/ |
10 | 10 |
|
11 |
package edu.ucsb.nceas.metacat; |
|
11 |
//package edu.ucsb.nceas.metacat;
|
|
12 | 12 |
|
13 | 13 |
import java.io.IOException; |
14 | 14 |
import java.util.Hashtable; |
... | ... | |
21 | 21 |
private String tagname; |
22 | 22 |
private StringBuffer content; |
23 | 23 |
private long parent_id; |
24 |
private Hashtable attributes; |
|
24 |
private Hashtable attributes; |
|
25 |
private int childNum; |
|
26 |
private int nodeIndex; |
|
25 | 27 |
|
26 | 28 |
/** Construct a Basic Element */ |
27 | 29 |
public BasicElement () { |
... | ... | |
33 | 35 |
* |
34 | 36 |
* @param tagname the name of the element |
35 | 37 |
* @param parent_id the id number of the parent element |
38 |
* @param nodeIndex - number of the element in the order for a given parent node |
|
39 |
* Every element initializes childNum to 0 when created and has interface incChildNum |
|
40 |
* when new child is created |
|
36 | 41 |
*/ |
37 |
public BasicElement (String tagname, long parent_id) { |
|
42 |
public BasicElement (String tagname, long parent_id, int nodeIndex) {
|
|
38 | 43 |
this(); |
39 | 44 |
this.tagname = tagname; |
40 | 45 |
this.parent_id = parent_id; |
46 |
this.nodeIndex = nodeIndex; |
|
47 |
this.childNum = 0; |
|
41 | 48 |
} |
42 | 49 |
|
43 | 50 |
/** Construct a Basic Element |
... | ... | |
46 | 53 |
* @param tagname the name of the element |
47 | 54 |
* @param parent_id the id number of the parent element |
48 | 55 |
*/ |
49 |
public BasicElement (long element_id, String tagname, long parent_id) { |
|
50 |
this(tagname,parent_id); |
|
56 |
public BasicElement (long element_id, String tagname, long parent_id, int nodeIndex) {
|
|
57 |
this(tagname,parent_id,nodeIndex);
|
|
51 | 58 |
this.element_id = element_id; |
52 | 59 |
} |
53 | 60 |
|
... | ... | |
149 | 156 |
public String getContent() { |
150 | 157 |
return this.content.toString(); |
151 | 158 |
} |
159 |
|
|
160 |
/** Get nodeIndex of the element */ |
|
161 |
public int getNodeIndex() { |
|
162 |
return this.nodeIndex; |
|
163 |
} |
|
164 |
|
|
165 |
/** increase childNum when new child for the element is created */ |
|
166 |
public int incChildNum() { |
|
167 |
return ++this.childNum; |
|
168 |
} |
|
152 | 169 |
} |
DBSAXWriter.java | ||
---|---|---|
9 | 9 |
* Version: '$Id$' |
10 | 10 |
*/ |
11 | 11 |
|
12 |
package edu.ucsb.nceas.metacat; |
|
12 |
//package edu.ucsb.nceas.metacat;
|
|
13 | 13 |
|
14 | 14 |
import org.xml.sax.*; |
15 | 15 |
|
... | ... | |
33 | 33 |
*/ |
34 | 34 |
public class DBSAXWriter { |
35 | 35 |
|
36 |
static String defaultDB = "jdbc:oracle:thin:@localhost:1521:test";
|
|
36 |
static String defaultDB = "jdbc:oracle:thin:@penelope.nceas.ucsb.edu:1526:DEV8";
|
|
37 | 37 |
private Connection conn = null; |
38 | 38 |
|
39 | 39 |
/** |
... | ... | |
97 | 97 |
{ |
98 | 98 |
this.conn = conn; |
99 | 99 |
|
100 |
//try { |
|
101 |
|
|
102 |
try { |
|
100 |
try { |
|
103 | 101 |
SAXParser parser = initializeParser(conn); |
104 | 102 |
parser.parse(xml); |
105 | 103 |
} catch (SAXParseException e) { |
106 | 104 |
System.err.println(e.getMessage()); |
107 | 105 |
} catch (SAXException e) { |
108 | 106 |
System.err.println(e.getMessage()); |
107 |
} catch (Exception e) { |
|
108 |
System.err.println(e.toString()); |
|
109 | 109 |
} |
110 |
//} catch (Exception e) { |
|
111 |
//System.err.println(e.toString()); |
|
112 |
//} |
|
113 | 110 |
} |
114 | 111 |
|
115 | 112 |
public DBSAXWriter( String filename, Connection conn) |
... | ... | |
126 | 123 |
System.err.println(e.getMessage()); |
127 | 124 |
} catch (SAXException e) { |
128 | 125 |
System.err.println(e.getMessage()); |
126 |
} catch (Exception e) { |
|
127 |
System.err.println("here is the URL exception"); |
|
128 |
System.err.println(e.toString()); |
|
129 | 129 |
} |
130 | 130 |
} |
131 | 131 |
|
... | ... | |
138 | 138 |
// Use the XMLDocumentHandler interface for namespace support |
139 | 139 |
// instead of org.xml.sax.DocumentHandler |
140 | 140 |
XMLDocumentHandler xmlDocHandler = new DBSAXHandler(conn); |
141 |
EntityResolver xmlEntityResolver = new DBEntityResolver(conn); |
|
142 |
DTDHandler xmlDTDHandler = new DBDTDHandler(conn); |
|
141 | 143 |
|
142 | 144 |
// For all the other interface use the default provided by |
143 | 145 |
// Handler base |
... | ... | |
149 | 151 |
// Set Handlers in the parser |
150 | 152 |
// Set the DocumentHandler to XMLDocumentHandler |
151 | 153 |
parser.setDocumentHandler(xmlDocHandler); |
154 |
parser.setEntityResolver(xmlEntityResolver); |
|
155 |
parser.setDTDHandler(xmlDTDHandler); |
|
152 | 156 |
|
153 | 157 |
// Set the other Handler to the defHandler |
154 | 158 |
parser.setErrorHandler(defHandler); |
155 |
parser.setEntityResolver(defHandler); |
|
156 |
parser.setDTDHandler(defHandler); |
|
157 | 159 |
|
158 | 160 |
} catch (Exception e) { |
159 | 161 |
System.err.println(e.toString()); |
DBSAXHandler.java | ||
---|---|---|
9 | 9 |
* Version: '$Id$' |
10 | 10 |
*/ |
11 | 11 |
|
12 |
package edu.ucsb.nceas.metacat; |
|
12 |
//package edu.ucsb.nceas.metacat;
|
|
13 | 13 |
|
14 | 14 |
import org.xml.sax.*; |
15 | 15 |
|
... | ... | |
23 | 23 |
import oracle.xml.parser.v2.SAXAttrList; |
24 | 24 |
|
25 | 25 |
import oracle.xml.parser.v2.SAXParser; |
26 |
import oracle.xml.parser.v2.DTD; |
|
26 | 27 |
|
27 | 28 |
/** |
28 | 29 |
* A database aware Class implementing callback bethods for the SAX parser to |
... | ... | |
31 | 32 |
public class DBSAXHandler extends DefaultXMLDocumentHandler |
32 | 33 |
{ |
33 | 34 |
|
35 |
static int elementNo = 0; |
|
36 |
private String docname; |
|
37 |
private String doctype; |
|
38 |
private String systemid; |
|
34 | 39 |
private boolean debug = false; |
35 | 40 |
private boolean stackCreated = false; |
36 | 41 |
private Stack elementStack; |
... | ... | |
53 | 58 |
|
54 | 59 |
} |
55 | 60 |
|
61 |
/** SAX Handler that receives notification of beginning of the document */ |
|
62 |
public void startDocument() throws SAXException |
|
63 |
{ |
Also available in: Unified diff
Included new features about writing XML documents into db
This includes writing data into db XML Catalog, document data into xml_documents,
comments into xml_nodes, added nodeindex for the order of elements by given parent element.