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
|
*
|
9
|
* '$Author: daigle $'
|
10
|
* '$Date: 2009-08-04 14:32:58 -0700 (Tue, 04 Aug 2009) $'
|
11
|
* '$Revision: 5015 $'
|
12
|
*
|
13
|
* This program is free software; you can redistribute it and/or modify
|
14
|
* it under the terms of the GNU General Public License as published by
|
15
|
* the Free Software Foundation; either version 2 of the License, or
|
16
|
* (at your option) any later version.
|
17
|
*
|
18
|
* This program is distributed in the hope that it will be useful,
|
19
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
20
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
21
|
* GNU General Public License for more details.
|
22
|
*
|
23
|
* You should have received a copy of the GNU General Public License
|
24
|
* along with this program; if not, write to the Free Software
|
25
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
26
|
*/
|
27
|
|
28
|
package edu.ucsb.nceas.metacat;
|
29
|
|
30
|
import org.xml.sax.*;
|
31
|
|
32
|
import edu.ucsb.nceas.metacat.database.DBConnection;
|
33
|
|
34
|
import java.sql.*;
|
35
|
import java.net.URL;
|
36
|
import java.net.MalformedURLException;
|
37
|
import java.util.Stack;
|
38
|
import java.util.EmptyStackException;
|
39
|
|
40
|
/**
|
41
|
* A database aware Class implementing DTDHandler interface for the SAX
|
42
|
* parser to call when processing the XML stream and intercepting notations
|
43
|
* and unparsed entities
|
44
|
*/
|
45
|
public class DBDTDHandler implements DTDHandler
|
46
|
{
|
47
|
private DBConnection connection = null;
|
48
|
|
49
|
/** Construct an instance of the DBDTDHandler clas
|
50
|
*
|
51
|
* @param conn the JDBC connection to which information is written
|
52
|
*/
|
53
|
public DBDTDHandler(DBConnection conn)
|
54
|
{
|
55
|
this.connection = conn;
|
56
|
}
|
57
|
|
58
|
/** Notation declarations are not signaled */
|
59
|
public void notationDecl(String name, String publicId, String systemId)
|
60
|
throws SAXException
|
61
|
{
|
62
|
System.out.println("from DBDTDHandler.notationDecl");
|
63
|
System.out.print(name);
|
64
|
System.out.print(publicId);
|
65
|
System.out.print(systemId);
|
66
|
return;
|
67
|
}
|
68
|
|
69
|
/** All are reported after startDocument and before first
|
70
|
* startElement event
|
71
|
*/
|
72
|
public void unparsedEntityDecl(String name, String publicId,
|
73
|
String systemId, String notationName)
|
74
|
throws SAXException
|
75
|
{
|
76
|
System.out.println("from DBDTDHandler.unparsedEntityDecl");
|
77
|
System.out.print(name);
|
78
|
System.out.print(publicId);
|
79
|
System.out.print(systemId);
|
80
|
System.out.println(notationName);
|
81
|
//String doctype = DBEntityResolver.doctype;
|
82
|
//if ( getEntitySystemID(conn, doctype, publicId) == "" )
|
83
|
// registerEntityPublicID(conn, doctype, publicId, systemId);
|
84
|
return;
|
85
|
}
|
86
|
|
87
|
/**
|
88
|
* Look at db XML Catalog to get System ID (if any) for that Public ID
|
89
|
* and doctype.
|
90
|
* Return empty string if there are not
|
91
|
*/
|
92
|
/*
|
93
|
* If this ever gets uncommented, you will need to verify the
|
94
|
* server url on the front of the system ID.
|
95
|
*
|
96
|
private String getEntitySystemID (Connection conn, String doctype,
|
97
|
String publicId)
|
98
|
{
|
99
|
String system_id = "";
|
100
|
Statement stmt;
|
101
|
try {
|
102
|
stmt = conn.createStatement();
|
103
|
stmt.execute("SELECT system_id FROM xml_catalog " +
|
104
|
"WHERE entry_type = 'ENTITY' AND source_doctype = '" +
|
105
|
doctype + "' AND public_id = '" + publicId + "'");
|
106
|
try {
|
107
|
ResultSet rs = stmt.getResultSet();
|
108
|
try {
|
109
|
boolean tableHasRows = rs.next();
|
110
|
if (tableHasRows) {
|
111
|
try {
|
112
|
system_id = rs.getString(1);
|
113
|
} catch (SQLException e) {
|
114
|
System.out.println("DBDTDHandler.getEntitySystemID() " +
|
115
|
"- Error with getString: " + e.getMessage());
|
116
|
}
|
117
|
}
|
118
|
} catch (SQLException e) {
|
119
|
System.out.println("DBDTDHandler.getEntitySystemID() " +
|
120
|
"- Error with next: " + e.getMessage());
|
121
|
}
|
122
|
} catch (SQLException e) {
|
123
|
System.out.println("DBDTDHandler.getEntitySystemID() " +
|
124
|
"- Error with getrset: " + e.getMessage());
|
125
|
}
|
126
|
stmt.close();
|
127
|
} catch (SQLException e) {
|
128
|
System.out.println("DBDTDHandler.getEntitySystemID() " +
|
129
|
"- Error getting id: " + e.getMessage());
|
130
|
}
|
131
|
|
132
|
// return the selected System ID
|
133
|
return system_id;
|
134
|
}
|
135
|
*/
|
136
|
/**
|
137
|
* Register Public ID in db XML Catalog
|
138
|
*/
|
139
|
/*
|
140
|
private void registerEntityPublicID (Connection conn, String doctype,
|
141
|
String publicId, String systemId)
|
142
|
{
|
143
|
try {
|
144
|
PreparedStatement pstmt;
|
145
|
pstmt = conn.prepareStatement(
|
146
|
"INSERT INTO xml_catalog (entity_id, entity_name, " +
|
147
|
"entry_type, source_doctype, public_id, system_id) " +
|
148
|
"VALUES (null, null, 'ENTITY', ?, ?, ?)");
|
149
|
// Bind the values to the query
|
150
|
pstmt.setString(1, doctype);
|
151
|
pstmt.setString(2, publicId);
|
152
|
pstmt.setString(3, systemId);
|
153
|
// Do the insertion
|
154
|
pstmt.execute();
|
155
|
pstmt.close();
|
156
|
} catch (SQLException e) {
|
157
|
System.out.println(e.getMessage());
|
158
|
}
|
159
|
}
|
160
|
*/
|
161
|
|
162
|
}
|