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: DBDTDHandler.java 168 2000-06-16 03:20:41Z jones $'
|
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
|
24
|
* parser to call when processing the XML stream and intercepting notations
|
25
|
* and unparsed entities
|
26
|
*/
|
27
|
public class DBDTDHandler implements DTDHandler
|
28
|
{
|
29
|
private Connection conn = null;
|
30
|
|
31
|
/** Construct an instance of the DBDTDHandler clas
|
32
|
*
|
33
|
* @param conn the JDBC connection to which information is written
|
34
|
*/
|
35
|
public DBDTDHandler(Connection conn)
|
36
|
{
|
37
|
this.conn = conn;
|
38
|
}
|
39
|
|
40
|
/** Notation declarations are not signaled */
|
41
|
public void notationDecl(String name, String publicId, String systemId)
|
42
|
throws SAXException
|
43
|
{
|
44
|
System.out.println("from DBDTDHandler.notationDecl");
|
45
|
System.out.print(name);
|
46
|
System.out.print(publicId);
|
47
|
System.out.print(systemId);
|
48
|
return;
|
49
|
}
|
50
|
|
51
|
/** All are reported after startDocument and before first
|
52
|
* startElement event
|
53
|
*/
|
54
|
public void unparsedEntityDecl(String name, String publicId,
|
55
|
String systemId, String notationName)
|
56
|
throws SAXException
|
57
|
{
|
58
|
System.out.println("from DBDTDHandler.unparsedEntityDecl");
|
59
|
System.out.print(name);
|
60
|
System.out.print(publicId);
|
61
|
System.out.print(systemId);
|
62
|
System.out.println(notationName);
|
63
|
String doctype = DBEntityResolver.doctype;
|
64
|
//if ( getEntitySystemID(conn, doctype, publicId) == "" )
|
65
|
// registerEntityPublicID(conn, doctype, publicId, systemId);
|
66
|
return;
|
67
|
}
|
68
|
|
69
|
/**
|
70
|
* Look at db XML Catalog to get System ID (if any) for that Public ID
|
71
|
* and doctype.
|
72
|
* Return empty string if there are not
|
73
|
*/
|
74
|
/*
|
75
|
private String getEntitySystemID (Connection conn, String doctype,
|
76
|
String publicId)
|
77
|
{
|
78
|
String system_id = "";
|
79
|
Statement stmt;
|
80
|
try {
|
81
|
stmt = conn.createStatement();
|
82
|
stmt.execute("SELECT system_id FROM xml_catalog " +
|
83
|
"WHERE entry_type = 'ENTITY' AND source_doctype = '" +
|
84
|
doctype + "' AND public_id = '" + publicId + "'");
|
85
|
try {
|
86
|
ResultSet rs = stmt.getResultSet();
|
87
|
try {
|
88
|
boolean tableHasRows = rs.next();
|
89
|
if (tableHasRows) {
|
90
|
try {
|
91
|
system_id = rs.getString(1);
|
92
|
} catch (SQLException e) {
|
93
|
System.out.println("DBDTDHandler.getEntitySystemID() " +
|
94
|
"- Error with getString: " + e.getMessage());
|
95
|
}
|
96
|
}
|
97
|
} catch (SQLException e) {
|
98
|
System.out.println("DBDTDHandler.getEntitySystemID() " +
|
99
|
"- Error with next: " + e.getMessage());
|
100
|
}
|
101
|
} catch (SQLException e) {
|
102
|
System.out.println("DBDTDHandler.getEntitySystemID() " +
|
103
|
"- Error with getrset: " + e.getMessage());
|
104
|
}
|
105
|
stmt.close();
|
106
|
} catch (SQLException e) {
|
107
|
System.out.println("DBDTDHandler.getEntitySystemID() " +
|
108
|
"- Error getting id: " + e.getMessage());
|
109
|
}
|
110
|
|
111
|
// return the selected System ID
|
112
|
return system_id;
|
113
|
}
|
114
|
*/
|
115
|
/**
|
116
|
* Register Public ID in db XML Catalog
|
117
|
*/
|
118
|
/*
|
119
|
private void registerEntityPublicID (Connection conn, String doctype,
|
120
|
String publicId, String systemId)
|
121
|
{
|
122
|
try {
|
123
|
PreparedStatement pstmt;
|
124
|
pstmt = conn.prepareStatement(
|
125
|
"INSERT INTO xml_catalog (entity_id, entity_name, " +
|
126
|
"entry_type, source_doctype, public_id, system_id) " +
|
127
|
"VALUES (null, null, 'ENTITY', ?, ?, ?)");
|
128
|
// Bind the values to the query
|
129
|
pstmt.setString(1, doctype);
|
130
|
pstmt.setString(2, publicId);
|
131
|
pstmt.setString(3, systemId);
|
132
|
// Do the insertion
|
133
|
pstmt.execute();
|
134
|
pstmt.close();
|
135
|
} catch (SQLException e) {
|
136
|
System.out.println(e.getMessage());
|
137
|
}
|
138
|
}
|
139
|
*/
|
140
|
|
141
|
}
|