1
|
/**
|
2
|
* '$RCSfile$'
|
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
|
* Release: @release@
|
9
|
*
|
10
|
* '$Author: jones $'
|
11
|
* '$Date: 2000-08-14 13:53:34 -0700 (Mon, 14 Aug 2000) $'
|
12
|
* '$Revision: 349 $'
|
13
|
*/
|
14
|
|
15
|
package edu.ucsb.nceas.metacat;
|
16
|
|
17
|
import org.xml.sax.*;
|
18
|
|
19
|
import java.sql.*;
|
20
|
import java.net.URL;
|
21
|
import java.net.MalformedURLException;
|
22
|
import java.util.Stack;
|
23
|
import java.util.EmptyStackException;
|
24
|
|
25
|
/**
|
26
|
* A database aware Class implementing DTDHandler interface for the SAX
|
27
|
* parser to call when processing the XML stream and intercepting notations
|
28
|
* and unparsed entities
|
29
|
*/
|
30
|
public class DBDTDHandler implements DTDHandler
|
31
|
{
|
32
|
private Connection conn = null;
|
33
|
|
34
|
/** Construct an instance of the DBDTDHandler clas
|
35
|
*
|
36
|
* @param conn the JDBC connection to which information is written
|
37
|
*/
|
38
|
public DBDTDHandler(Connection conn)
|
39
|
{
|
40
|
this.conn = conn;
|
41
|
}
|
42
|
|
43
|
/** Notation declarations are not signaled */
|
44
|
public void notationDecl(String name, String publicId, String systemId)
|
45
|
throws SAXException
|
46
|
{
|
47
|
System.out.println("from DBDTDHandler.notationDecl");
|
48
|
System.out.print(name);
|
49
|
System.out.print(publicId);
|
50
|
System.out.print(systemId);
|
51
|
return;
|
52
|
}
|
53
|
|
54
|
/** All are reported after startDocument and before first
|
55
|
* startElement event
|
56
|
*/
|
57
|
public void unparsedEntityDecl(String name, String publicId,
|
58
|
String systemId, String notationName)
|
59
|
throws SAXException
|
60
|
{
|
61
|
System.out.println("from DBDTDHandler.unparsedEntityDecl");
|
62
|
System.out.print(name);
|
63
|
System.out.print(publicId);
|
64
|
System.out.print(systemId);
|
65
|
System.out.println(notationName);
|
66
|
//String doctype = DBEntityResolver.doctype;
|
67
|
//if ( getEntitySystemID(conn, doctype, publicId) == "" )
|
68
|
// registerEntityPublicID(conn, doctype, publicId, systemId);
|
69
|
return;
|
70
|
}
|
71
|
|
72
|
/**
|
73
|
* Look at db XML Catalog to get System ID (if any) for that Public ID
|
74
|
* and doctype.
|
75
|
* Return empty string if there are not
|
76
|
*/
|
77
|
/*
|
78
|
private String getEntitySystemID (Connection conn, String doctype,
|
79
|
String publicId)
|
80
|
{
|
81
|
String system_id = "";
|
82
|
Statement stmt;
|
83
|
try {
|
84
|
stmt = conn.createStatement();
|
85
|
stmt.execute("SELECT system_id FROM xml_catalog " +
|
86
|
"WHERE entry_type = 'ENTITY' AND source_doctype = '" +
|
87
|
doctype + "' AND public_id = '" + publicId + "'");
|
88
|
try {
|
89
|
ResultSet rs = stmt.getResultSet();
|
90
|
try {
|
91
|
boolean tableHasRows = rs.next();
|
92
|
if (tableHasRows) {
|
93
|
try {
|
94
|
system_id = rs.getString(1);
|
95
|
} catch (SQLException e) {
|
96
|
System.out.println("DBDTDHandler.getEntitySystemID() " +
|
97
|
"- Error with getString: " + e.getMessage());
|
98
|
}
|
99
|
}
|
100
|
} catch (SQLException e) {
|
101
|
System.out.println("DBDTDHandler.getEntitySystemID() " +
|
102
|
"- Error with next: " + e.getMessage());
|
103
|
}
|
104
|
} catch (SQLException e) {
|
105
|
System.out.println("DBDTDHandler.getEntitySystemID() " +
|
106
|
"- Error with getrset: " + e.getMessage());
|
107
|
}
|
108
|
stmt.close();
|
109
|
} catch (SQLException e) {
|
110
|
System.out.println("DBDTDHandler.getEntitySystemID() " +
|
111
|
"- Error getting id: " + e.getMessage());
|
112
|
}
|
113
|
|
114
|
// return the selected System ID
|
115
|
return system_id;
|
116
|
}
|
117
|
*/
|
118
|
/**
|
119
|
* Register Public ID in db XML Catalog
|
120
|
*/
|
121
|
/*
|
122
|
private void registerEntityPublicID (Connection conn, String doctype,
|
123
|
String publicId, String systemId)
|
124
|
{
|
125
|
try {
|
126
|
PreparedStatement pstmt;
|
127
|
pstmt = conn.prepareStatement(
|
128
|
"INSERT INTO xml_catalog (entity_id, entity_name, " +
|
129
|
"entry_type, source_doctype, public_id, system_id) " +
|
130
|
"VALUES (null, null, 'ENTITY', ?, ?, ?)");
|
131
|
// Bind the values to the query
|
132
|
pstmt.setString(1, doctype);
|
133
|
pstmt.setString(2, publicId);
|
134
|
pstmt.setString(3, systemId);
|
135
|
// Do the insertion
|
136
|
pstmt.execute();
|
137
|
pstmt.close();
|
138
|
} catch (SQLException e) {
|
139
|
System.out.println(e.getMessage());
|
140
|
}
|
141
|
}
|
142
|
*/
|
143
|
|
144
|
}
|
145
|
|
146
|
/**
|
147
|
* '$Log$
|
148
|
* 'Revision 1.10 2000/06/27 04:31:07 jones
|
149
|
* 'Fixed bugs associated with the new UPDATE and DELETE functions of
|
150
|
* 'DBWriter. There were problematic interactions between some static
|
151
|
* 'variables used in DBEntityResolver and the way in which the
|
152
|
* 'Servlet objects are re-used across multiple client invocations.
|
153
|
* '
|
154
|
* 'Generally cleaned up error reporting. Now all errors and success
|
155
|
* 'results are reported as XML documents from MetaCatServlet. Need
|
156
|
* 'to make the command line tools do the same.
|
157
|
* '
|
158
|
* 'Revision 1.9 2000/06/26 10:35:04 jones
|
159
|
* 'Merged in substantial changes to DBWriter and associated classes and to
|
160
|
* 'the MetaCatServlet in order to accomodate the new UPDATE and DELETE
|
161
|
* 'functions. The command line tools and the parameters for the
|
162
|
* 'servlet have changed substantially.
|
163
|
* '
|
164
|
* 'Revision 1.8.2.2 2000/06/25 23:38:16 jones
|
165
|
* 'Added RCSfile keyword
|
166
|
* '
|
167
|
* 'Revision 1.8.2.1 2000/06/25 23:34:17 jones
|
168
|
* 'Changed documentation formatting, added log entries at bottom of source files
|
169
|
* ''
|
170
|
*/
|