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