Revision 1550
Added by Jing Tao over 21 years ago
src/edu/ucsb/nceas/metacat/ContentTypeProvider.java | ||
---|---|---|
1 |
/** |
|
2 |
* '$RCSfile$' |
|
3 |
* Purpose: A class to asyncronously do delta-T replication checking |
|
4 |
* Copyright: 2000 Regents of the University of California and the |
|
5 |
* National Center for Ecological Analysis and Synthesis |
|
6 |
* Authors: Chad Berkley |
|
7 |
* Release: @release@ |
|
8 |
* |
|
9 |
* '$Author$' |
|
10 |
* '$Date$' |
|
11 |
* '$Revision$' |
|
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 java.io.StringReader; |
|
31 |
import java.sql.PreparedStatement; |
|
32 |
import java.sql.ResultSet; |
|
33 |
import java.sql.SQLException; |
|
34 |
import java.util.Hashtable; |
|
35 |
import java.util.Vector; |
|
36 |
|
|
37 |
import org.apache.xpath.XPathAPI; |
|
38 |
import org.apache.xerces.parsers.DOMParser; |
|
39 |
import org.w3c.dom.Attr; |
|
40 |
import org.w3c.dom.NamedNodeMap; |
|
41 |
import org.w3c.dom.NodeList; |
|
42 |
import org.w3c.dom.Document; |
|
43 |
import org.w3c.dom.Node; |
|
44 |
import org.w3c.dom.NodeList; |
|
45 |
import org.w3c.dom.DocumentType; |
|
46 |
import org.apache.xerces.dom.DocumentTypeImpl; |
|
47 |
import org.apache.xpath.objects.XObject; |
|
48 |
|
|
49 |
import org.ecoinformatics.eml.EMLParser; |
|
50 |
/** |
|
51 |
* This class will figure out which content type it is for a given data file. |
|
52 |
* First, from xml_relation to get all relative files to this data file. |
|
53 |
* Then from xml_documents to get physical files. From physical file pull out |
|
54 |
* the content type |
|
55 |
*/ |
|
56 |
public class ContentTypeProvider |
|
57 |
{ |
|
58 |
private String dataFileId = null; |
|
59 |
private String contentType = null; |
|
60 |
private String packageType = null; |
|
61 |
private Hashtable contentTypeHash = new Hashtable(); |
|
62 |
|
|
63 |
//Constant |
|
64 |
private String BETA = "beta"; |
|
65 |
private String EML2 = "eml2"; |
|
66 |
private String DEFAULTCONTENTTYPE = MetaCatUtil. |
|
67 |
getOption("defaultcontenttype"); |
|
68 |
private String FORMATPATH = "//format"; |
|
69 |
private String TEXT = "text"; |
|
70 |
private String TEXTYPE ="text/plain"; |
|
71 |
private String XML = "xml"; |
|
72 |
private String XMLTYPE = "text/xml"; |
|
73 |
private String HTML = "HTML"; |
|
74 |
private String HTMLTYPE = "text/html"; |
|
75 |
private String GIF = "gif"; |
|
76 |
private String GIFTYPE = "image/gif"; |
|
77 |
private String BMP = "bmp"; |
|
78 |
private String BMPTYPE = "image/bmp"; |
|
79 |
private String TAR = "tar"; |
|
80 |
private String TARTYPE ="application/x-tar"; |
|
81 |
private String ZIP = "zip"; |
|
82 |
private String ZIPTYPE = "application/x-zip-compressed"; |
|
83 |
private String BINARY = "binary"; |
|
84 |
private String BINARYTYPE = "application/octet-stream"; |
|
85 |
|
|
86 |
private String ENTITYDOCTYPE = "entitydoctype"; |
|
87 |
private String PHYSICALDOCTYPE = "physicaldoctype"; |
|
88 |
|
|
89 |
|
|
90 |
/** |
|
91 |
* Constructor of ContentTypeProvider |
|
92 |
*/ |
|
93 |
public ContentTypeProvider(String docIdWithRevision) |
|
94 |
{ |
|
95 |
dataFileId = MetaCatUtil.getDocIdFromString(docIdWithRevision); |
|
96 |
//get relative doclist for data file and package type |
|
97 |
Vector docLists = null; |
|
98 |
docLists = getRelativeDocIdList(dataFileId); |
|
99 |
|
|
100 |
if ( packageType == null) |
|
101 |
{ |
|
102 |
// other situation, contenetype is default value |
|
103 |
contentType = DEFAULTCONTENTTYPE; |
|
104 |
} |
|
105 |
else if (packageType.equals(BETA)) |
|
106 |
{ |
|
107 |
// for beta package and get entity docid for the data file |
|
108 |
String entityDocid = getTargetDocIdForBeta(docLists, ENTITYDOCTYPE); |
|
109 |
// get physical docid for data file |
|
110 |
docLists = getRelativeDocIdList(entityDocid); |
|
111 |
String physicalDocId = getTargetDocIdForBeta(docLists, PHYSICALDOCTYPE); |
|
112 |
// if no physical docid assign to this data file, content type is default |
|
113 |
if (physicalDocId == null) |
|
114 |
{ |
|
115 |
|
|
116 |
contentType = DEFAULTCONTENTTYPE; |
|
117 |
} |
|
118 |
else |
|
119 |
{ |
|
120 |
|
|
121 |
parsePhysicalDocumentForBeta(physicalDocId); |
|
122 |
} |
|
123 |
} |
|
124 |
else if (packageType.equals(EML2)) |
|
125 |
{ |
|
126 |
// for eml2 package |
|
127 |
|
|
128 |
} |
|
129 |
|
|
130 |
} |
|
131 |
|
|
132 |
/** Method to get content type */ |
|
133 |
public String getContentType() |
|
134 |
{ |
|
135 |
return contentType; |
|
136 |
}//getContentType |
|
137 |
|
|
138 |
/* Get relative docid list and packagetype */ |
|
139 |
private Vector getRelativeDocIdList(String id) |
|
140 |
{ |
|
141 |
Vector docList = new Vector(); |
|
142 |
String sql = "SELECT packagetype, subject from xml_relation " + |
|
143 |
"where object = ?"; |
|
144 |
ResultSet rs = null; |
|
145 |
PreparedStatement pStmt=null; |
|
146 |
DBConnection conn = null; |
|
147 |
int serialNumber = -1; |
|
148 |
try |
|
149 |
{ |
|
150 |
//check out DBConnection |
|
151 |
conn=DBConnectionPool.getDBConnection |
|
152 |
("ContentTypeProvider.getRelativeDocIdlist"); |
|
153 |
serialNumber=conn.getCheckOutSerialNumber(); |
|
154 |
pStmt = conn.prepareStatement(sql); |
|
155 |
// binding value |
|
156 |
pStmt.setString(1, id); |
|
157 |
//execute query |
|
158 |
pStmt.execute(); |
|
159 |
rs = pStmt.getResultSet(); |
|
160 |
// get result list |
|
161 |
String packType = null; |
|
162 |
while (rs.next()) |
|
163 |
{ |
|
164 |
packType = rs.getString(1); |
|
165 |
String subject = rs.getString(2); |
|
166 |
|
|
167 |
// get rid of duplicate record and add the docid into vector |
|
168 |
if (!docList.contains(subject)) |
|
169 |
{ |
|
170 |
|
|
171 |
docList.add(subject); |
|
172 |
} |
|
173 |
}//while |
|
174 |
|
|
175 |
// set up data package type |
|
176 |
if ((MetaCatUtil.getOptionList(MetaCatUtil.getOption("packagedoctype"))). |
|
177 |
contains(packType)) |
|
178 |
{ |
|
179 |
//this is beta4 or beta6 version |
|
180 |
MetaCatUtil.debugMessage("This is beta package", 30); |
|
181 |
packageType = BETA; |
|
182 |
} |
|
183 |
else if ((MetaCatUtil.getOptionList |
|
184 |
(MetaCatUtil.getOption("eml2namespace"))).contains(packType)) |
|
185 |
{ |
|
186 |
// this eml 2 document |
|
187 |
MetaCatUtil.debugMessage("This is EML2 package", 30); |
|
188 |
packageType = EML2; |
|
189 |
} |
|
190 |
|
|
191 |
|
|
192 |
}//try |
|
193 |
catch(SQLException e) |
|
194 |
{ |
|
195 |
|
|
196 |
MetaCatUtil.debugMessage("ContenTypProvider.getRelativeDoclist1 " + |
|
197 |
e.getMessage(), 30); |
|
198 |
}//catch |
|
199 |
finally |
|
200 |
{ |
|
201 |
try |
|
202 |
{ |
|
203 |
pStmt.close(); |
|
204 |
} |
|
205 |
catch (SQLException ee) |
|
206 |
{ |
|
207 |
MetaCatUtil.debugMessage("ContenTypProvider.getRelativeDoclist2 " + |
|
208 |
ee.getMessage(), 30); |
|
209 |
} |
|
210 |
finally |
|
211 |
{ |
|
212 |
DBConnectionPool.returnDBConnection(conn, serialNumber); |
|
213 |
} |
|
214 |
}//finally |
|
215 |
|
|
216 |
return docList; |
|
217 |
}// getRelativeDocIdList |
|
218 |
|
|
219 |
/* Method to get physical document for data file in xml_documents table for |
|
220 |
* beta eml package |
|
221 |
*/ |
|
222 |
private String getTargetDocIdForBeta(Vector list, String targetType) |
|
223 |
{ |
|
224 |
String docId = null; |
|
225 |
// make sure list is not empty |
|
226 |
if (list.isEmpty()) |
|
227 |
{ |
|
228 |
|
|
229 |
return docId; |
|
230 |
} |
|
231 |
// get sql command |
|
232 |
String sql = "SELECT doctype, docid from xml_documents where docid in ("; |
|
233 |
// the first element |
|
234 |
sql = sql + "'"+(String)list.elementAt(0) + "'"; |
|
235 |
for (int i=1; i<list.size(); i++) |
|
236 |
{ |
|
237 |
String docid = (String) list.elementAt(i); |
|
238 |
sql = sql + ", '" + docid + "'"; |
|
239 |
}//for |
|
240 |
// add parensis |
|
241 |
sql = sql + ")"; |
|
242 |
MetaCatUtil.debugMessage("SQL for select doctype: "+ sql, 35); |
|
243 |
ResultSet rs = null; |
|
244 |
PreparedStatement pStmt=null; |
|
245 |
DBConnection conn = null; |
|
246 |
int serialNumber = -1; |
|
247 |
try |
|
248 |
{ |
|
249 |
//check out DBConnection |
|
250 |
conn=DBConnectionPool.getDBConnection |
|
251 |
("ContentTypeProvider.setPhycialDocIdForBeta"); |
|
252 |
serialNumber=conn.getCheckOutSerialNumber(); |
|
253 |
pStmt = conn.prepareStatement(sql); |
|
254 |
//execute query |
|
255 |
pStmt.execute(); |
|
256 |
rs = pStmt.getResultSet(); |
|
257 |
// get result list |
|
258 |
while (rs.next()) |
|
259 |
{ |
|
260 |
String packType = rs.getString(1); |
|
261 |
String targetId = rs.getString(2); |
|
262 |
// find physical document |
|
263 |
if ((MetaCatUtil.getOptionList(MetaCatUtil.getOption(targetType))). |
|
264 |
contains(packType)) |
|
265 |
{ |
|
266 |
// assign physical document and jump out the while loop |
|
267 |
docId = targetId; |
|
268 |
break; |
|
269 |
} |
|
270 |
}//while |
|
271 |
|
|
272 |
}//try |
|
273 |
catch(SQLException e) |
|
274 |
{ |
|
275 |
|
|
276 |
MetaCatUtil.debugMessage("ContenTypProvider.setPhysicalDocIdForBeta1 " + |
|
277 |
e.getMessage(), 30); |
|
278 |
}//catch |
|
279 |
finally |
|
280 |
{ |
|
281 |
try |
|
282 |
{ |
|
283 |
pStmt.close(); |
|
284 |
} |
|
285 |
catch(SQLException ee) |
|
286 |
{ |
|
287 |
MetaCatUtil.debugMessage("ContenTypProvider.setPhysicalDocIdForBeta2 " + |
|
288 |
ee.getMessage(), 30); |
|
289 |
}//catch |
|
290 |
finally |
|
291 |
{ |
|
292 |
DBConnectionPool.returnDBConnection(conn, serialNumber); |
|
293 |
} |
|
294 |
}//finally |
|
295 |
MetaCatUtil.debugMessage("!!!!!!!!!target docid is: "+ docId + " "+ |
|
296 |
"for target doctype: "+targetType, 25); |
|
297 |
return docId; |
|
298 |
} |
|
299 |
|
|
300 |
|
|
301 |
|
|
302 |
|
|
303 |
/* Parser the beta physical document and find the value in format element*/ |
|
304 |
private void parsePhysicalDocumentForBeta(String physicalDocid) |
|
305 |
{ |
|
306 |
String xmlDoc = null; |
|
307 |
try |
|
308 |
{ |
|
309 |
DocumentImpl doc = new DocumentImpl(physicalDocid); |
|
310 |
xmlDoc = doc.toString(); |
|
311 |
} |
|
312 |
catch (Exception e) |
|
313 |
{ |
|
314 |
contentType = DEFAULTCONTENTTYPE; |
|
315 |
MetaCatUtil.debugMessage("Error in ContentTypeProvider." + |
|
316 |
"parsePhysicalDocumentForBeta()" + e.getMessage(), 30); |
|
317 |
return; |
|
318 |
} |
|
319 |
// get format element's text value |
|
320 |
String format = getTextValueFromPath(new StringReader(xmlDoc), FORMATPATH); |
|
321 |
|
|
322 |
if (format == null) |
|
323 |
{ |
|
324 |
// if couldn't find the format, set contentype default value; |
|
325 |
contentType = DEFAULTCONTENTTYPE; |
|
326 |
} |
|
327 |
else |
|
328 |
{ |
|
329 |
// if can find a format and look up from hash to get value |
|
330 |
contentType = lookUpContentType(format); |
|
331 |
// couldn't find the content type for this format in hash table |
|
332 |
if (contentType == null) |
|
333 |
{ |
|
334 |
//set default vlaue |
|
335 |
contentType = DEFAULTCONTENTTYPE; |
|
336 |
}//if |
|
337 |
}//else |
|
338 |
}//parsePhysicalDocumentForBeta |
|
339 |
|
|
340 |
private String getTextValueFromPath(StringReader xml, String xPath) |
|
341 |
{ |
|
342 |
String textValue = null; |
|
343 |
// get nodelist from doc by path |
|
344 |
try |
|
345 |
{ |
|
346 |
NodeList list = EMLParser.getPathContent(xml, xPath); |
|
347 |
Node elementNode = list.item(0); |
|
348 |
Node textNode = elementNode.getFirstChild(); |
|
349 |
if (textNode.getNodeType() == Node.TEXT_NODE) |
|
350 |
{ |
|
351 |
textValue = textNode.getNodeValue();// get value |
|
352 |
} |
|
353 |
|
|
354 |
} |
|
355 |
catch (Exception e) |
|
356 |
{ |
|
357 |
MetaCatUtil.debugMessage("error in ContentTypeProvider."+ |
|
358 |
"getTextValueFromPath: "+e.getMessage(), 30); |
|
359 |
} |
|
360 |
MetaCatUtil.debugMessage("The text value for " + xPath + " is: "+ |
|
361 |
textValue, 30); |
|
362 |
return textValue; |
|
363 |
}//getTextValueFromPath |
|
364 |
|
|
365 |
/* A method to look up contentype */ |
|
366 |
private String lookUpContentType(String format) |
|
367 |
{ |
|
368 |
String newFormat = null; |
|
369 |
constructContentHashTable(); |
|
370 |
newFormat = format.toLowerCase().trim(); |
|
371 |
String type = null; |
|
372 |
type = (String)contentTypeHash.get(newFormat); |
|
373 |
MetaCatUtil.debugMessage("contentType looked from hashtalbe is: " + |
|
374 |
type, 30); |
|
375 |
return type; |
|
376 |
}// lookupcontentypes |
|
377 |
|
|
378 |
/* Construct content type hashtable */ |
|
379 |
private void constructContentHashTable() |
|
380 |
{ |
|
381 |
contentTypeHash.put(TEXT, TEXTYPE); |
|
382 |
contentTypeHash.put(XML, XMLTYPE); |
|
383 |
contentTypeHash.put(HTML,HTMLTYPE); |
|
384 |
contentTypeHash.put(GIF, GIFTYPE); |
|
385 |
contentTypeHash.put(BMP, BMPTYPE); |
|
386 |
contentTypeHash.put(TAR, TARTYPE); |
|
387 |
contentTypeHash.put(ZIP, ZIPTYPE); |
|
388 |
contentTypeHash.put(BINARY, BINARYTYPE); |
|
389 |
|
|
390 |
}//constructrContentHashTable(); |
|
391 |
|
|
392 |
public static void main(String[] argus) |
|
393 |
{ |
|
394 |
try |
|
395 |
{ |
|
396 |
DBConnectionPool pool = DBConnectionPool.getInstance(); |
|
397 |
ContentTypeProvider provider = new ContentTypeProvider("tao.9830"); |
|
398 |
String str = provider.getContentType(); |
|
399 |
MetaCatUtil.debugMessage("content type is : " + str, 20); |
|
400 |
|
|
401 |
} |
|
402 |
catch(Exception e) |
|
403 |
{ |
|
404 |
MetaCatUtil.debugMessage("erorr in Schemalocation.main: " + |
|
405 |
e.getMessage(), 30); |
|
406 |
} |
|
407 |
} |
|
408 |
}//ContentTypeProvider |
|
0 | 409 |
Also available in: Unified diff
Add a new class to handle contenttyp setting for data file.