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