Revision 151
Added by bojilova over 24 years ago
src/edu/ucsb/nceas/metacat/DBEntityResolver.java | ||
---|---|---|
15 | 15 |
|
16 | 16 |
import java.sql.*; |
17 | 17 |
import java.net.URL; |
18 |
import java.net.URLConnection; |
|
18 | 19 |
import java.net.MalformedURLException; |
20 |
import java.io.IOException; |
|
19 | 21 |
import java.util.Stack; |
20 | 22 |
import java.util.EmptyStackException; |
21 | 23 |
|
... | ... | |
50 | 52 |
* referenced within the document element) |
51 | 53 |
*/ |
52 | 54 |
public InputSource resolveEntity (String publicId, String systemId) |
53 |
throws MalformedURLException
|
|
55 |
throws MalformedURLException, IOException
|
|
54 | 56 |
{ |
55 | 57 |
String dbSystemId; |
56 | 58 |
|
57 | 59 |
currentElementNo = DBSAXHandler.elementNo; |
58 | 60 |
|
59 |
if (publicId != null) { |
|
60 |
pIdCounter += 1; |
|
61 |
/* |
|
62 |
System.out.println("from DBEntityResolver: current element is " + |
|
63 |
DBSAXHandler.elementNo); |
|
64 |
System.out.println("from DBEntityResolver: " + pIdCounter + " " + |
|
65 |
publicId); |
|
66 |
*/ |
|
67 |
// look at the db XML Catalog and get dbSystemId by this publicId |
|
68 |
if (currentElementNo == 0) { |
|
61 |
if (currentElementNo == 0) { |
|
62 |
if (publicId != null) { |
|
63 |
pIdCounter += 1; |
|
69 | 64 |
doctype = publicId; |
70 |
dbSystemId = getDTDSystemID (conn, publicId); |
|
71 |
if (dbSystemId == "") |
|
72 |
// register publicId in db and use the provided systemId |
|
73 |
if (systemId != "") { |
|
74 |
new URL(systemId); |
|
75 |
registerDTDSystemID (conn, doctype, publicId, systemId); |
|
76 |
return null; |
|
77 |
} |
|
78 |
new URL(dbSystemId); |
|
79 |
return new InputSource(dbSystemId); |
|
80 |
} |
|
81 |
/* |
|
82 |
else { |
|
83 |
// look at the db XML Catalog and get dbSystemId by this |
|
84 |
// publicId for a given doctype |
|
85 |
dbSystemId = getEntitySystemID (conn, doctype, publicId); |
|
86 |
if (dbSystemId == "") |
|
87 |
// register publicId in db for a given doctype and |
|
88 |
// use the provided systemId |
|
89 |
if (systemId != "") { |
|
90 |
new URL(systemId); |
|
91 |
registerEntityPublicID (conn, doctype, publicId, systemId); |
|
92 |
return null; |
|
93 |
} |
|
94 |
new URL(dbSystemId); |
|
95 |
return new InputSource(dbSystemId); |
|
96 |
} |
|
97 |
*/ |
|
98 |
} |
|
99 |
// publicId is null => doctype is null => doctype = docname |
|
100 |
if ( systemId != null) { |
|
101 |
if (currentElementNo == 0) { |
|
65 |
} else if ( systemId != null) { |
|
102 | 66 |
doctype = DBSAXHandler.docname; |
103 |
dbSystemId = getDTDSystemID (conn, doctype); |
|
104 |
if (dbSystemId == "") { |
|
105 |
new URL(systemId); |
|
67 |
} |
|
68 |
// look at the db XML Catalog and get dbSystemId by this doctype |
|
69 |
dbSystemId = getDTDSystemID (conn, doctype); |
|
70 |
if (dbSystemId == "") { |
|
71 |
// register publicId in db and use the provided systemId |
|
72 |
if (systemId != "") { |
|
73 |
checkURLConnection(systemId); |
|
106 | 74 |
registerDTDSystemID (conn, doctype, doctype, systemId); |
107 | 75 |
return null; |
108 |
} |
|
109 |
new URL(dbSystemId); |
|
76 |
} |
|
77 |
} else { |
|
78 |
checkURLConnection(dbSystemId); |
|
110 | 79 |
return new InputSource(dbSystemId); |
111 |
} |
|
80 |
}
|
|
112 | 81 |
} |
113 | 82 |
|
114 | 83 |
// use the default behaviour |
84 |
checkURLConnection(systemId); |
|
115 | 85 |
return null; |
116 | 86 |
} |
117 | 87 |
|
... | ... | |
165 | 135 |
String publicId, String systemId) |
166 | 136 |
{ |
167 | 137 |
try { |
168 |
java.net.URLConnection urlConn = |
|
169 |
(new URL(systemId)).openConnection(); |
|
170 |
urlConn.connect(); |
|
171 |
} catch (java.io.IOException e) { |
|
172 |
System.out.println(e.getMessage()); |
|
173 |
return; |
|
174 |
} |
|
175 |
try { |
|
176 | 138 |
PreparedStatement pstmt; |
177 | 139 |
pstmt = conn.prepareStatement( |
178 | 140 |
"INSERT INTO xml_catalog " + |
... | ... | |
190 | 152 |
System.out.println(e.getMessage()); |
191 | 153 |
} |
192 | 154 |
} |
193 |
|
|
194 | 155 |
/** |
195 |
* Look at db XML Catalog to get System ID (if any) for that Public ID |
|
196 |
* and doctype. Return empty string if there are not |
|
156 |
* Check URL Connection for systemId |
|
197 | 157 |
*/ |
198 |
/* |
|
199 |
private String getEntitySystemID (Connection conn, String doctype, |
|
200 |
String publicId) |
|
158 |
private void checkURLConnection (String systemId) |
|
159 |
throws MalformedURLException, IOException |
|
201 | 160 |
{ |
202 |
String system_id = ""; |
|
203 |
Statement stmt; |
|
204 | 161 |
try { |
205 |
stmt = conn.createStatement(); |
|
206 |
stmt.execute("SELECT system_id FROM xml_catalog " + |
|
207 |
"WHERE entry_type = 'ENTITY' AND source_doctype = '" + |
|
208 |
doctype + "' AND public_id = '" + publicId + "'"); |
|
209 |
try { |
|
210 |
ResultSet rs = stmt.getResultSet(); |
|
211 |
try { |
|
212 |
boolean tableHasRows = rs.next(); |
|
213 |
if (tableHasRows) { |
|
214 |
try { |
|
215 |
system_id = rs.getString(1); |
|
216 |
} catch (SQLException e) { |
|
217 |
System.out.println("DBEntityResolver.getEntitySystemID() " + |
|
218 |
"- Error with getString: " + e.getMessage()); |
|
219 |
} |
|
220 |
} |
|
221 |
} catch (SQLException e) { |
|
222 |
System.out.println("DBEntityResolver.getEntitySystemID() " + |
|
223 |
"- Error with next: " + e.getMessage()); |
|
224 |
} |
|
225 |
} catch (SQLException e) { |
|
226 |
System.out.println("DBEntityResolver.getEntitySystemID() " + |
|
227 |
"- Error with getrset: " + e.getMessage()); |
|
228 |
} |
|
229 |
stmt.close(); |
|
230 |
} catch (SQLException e) { |
|
231 |
System.out.println("DBEntityResolver.getEntitySystemID() " + |
|
232 |
"- Error getting id: " + e.getMessage()); |
|
233 |
} |
|
234 |
|
|
235 |
// return the selected System ID number |
|
236 |
return system_id; |
|
237 |
} |
|
238 |
*/ |
|
239 |
/** |
|
240 |
* Register Public ID in db XML Catalog |
|
241 |
*/ |
|
242 |
/* |
|
243 |
private void registerEntityPublicID (Connection conn, String doctype, |
|
244 |
String publicId, String systemId) |
|
245 |
{ |
|
246 |
try { |
|
247 |
PreparedStatement pstmt; |
|
248 |
pstmt = conn.prepareStatement( |
|
249 |
"INSERT INTO xml_catalog (catalog_id, entry_type, " + |
|
250 |
"source_doctype, public_id, system_id) " + |
|
251 |
"VALUES (null, 'ENTITY', ?, ?, ?)"); |
|
252 |
// Bind the values to the query |
|
253 |
pstmt.setString(1, doctype); |
|
254 |
pstmt.setString(2, publicId); |
|
255 |
pstmt.setString(3, systemId); |
|
256 |
// Do the insertion |
|
257 |
pstmt.execute(); |
|
258 |
pstmt.close(); |
|
259 |
} catch (SQLException e) { |
|
260 |
System.out.println(e.getMessage()); |
|
261 |
} |
|
262 |
} |
|
263 |
*/ |
|
162 |
URLConnection urlConn = (new URL(systemId)).openConnection(); |
|
163 |
urlConn.connect(); |
|
164 |
} catch (MalformedURLException e) { |
|
165 |
System.out.println("from checkURLConnection(): " + e.getMessage()); |
|
166 |
throw e; |
|
167 |
} catch (IOException e) { |
|
168 |
System.out.println("from checkURLConnection(): " + e.getMessage()); |
|
169 |
throw e; |
|
170 |
} |
|
171 |
} |
|
264 | 172 |
} |
Also available in: Unified diff
put checkURLConnection(systemId) before
its register in db or using from the parser