source: trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/metadata/METSFile.java@ 8699

Last change on this file since 8699 was 8461, checked in by kjdon, 20 years ago

added in Chi's changes for METS documents. mostly the addition of new/improved parseXML methods

  • Property svn:keywords set to Author Date Id Revision
File size: 10.9 KB
Line 
1package org.greenstone.gsdl3.gs3build.metadata;
2
3import java.io.PrintWriter;
4
5import java.util.List;
6import java.util.ArrayList;
7
8import java.net.URL;
9import java.net.URLConnection;
10
11import java.sql.ResultSet;
12import java.sql.SQLException;
13
14import org.w3c.dom.Document;
15import org.w3c.dom.Element;
16import org.w3c.dom.NamedNodeMap;
17import org.w3c.dom.Node;
18import org.w3c.dom.NodeList;
19import org.w3c.dom.Text;
20
21import org.greenstone.gsdl3.gs3build.util.HTTPTools;
22import org.greenstone.gsdl3.gs3build.util.XMLTools;
23import org.greenstone.gsdl3.gs3build.database.*;
24import org.greenstone.gsdl3.gs3build.util.GS3SQLConnection;
25
26/**
27 * An individual METS component file for a document. Many features of METS are not
28 * currently fully supported/used here, but member variables exist for all of the
29 * standard members.
30 */
31
32public class METSFile
33{
34 METSFileID id;
35 METSFilePos location;
36 String MIMEType;
37 int sequenceNo;
38 long size;
39 METSDate created;
40 long checkSum;
41 List adminRefs;
42 List describeRefs;
43 METSFileGroup group;
44
45 /**
46 * Create a new METSFile object, with the given properties. The remaining
47 * fields are filled in with default values.
48 *
49 * @param <code>String</code> the id of the file
50 * @param <code>METSFilePos</code> the location of the file
51 * @param <code>String</code> the MIME type of the file
52 */
53 public METSFile(METSFileID id, METSFilePos location, String mimeType)
54 { this.id = id; // required
55 this.location = location; // the location
56 this.MIMEType = mimeType; // the MIME type;
57 this.sequenceNo = -1; // -1 indicates unset
58 this.size = -1; // -1 indicates unset
59 this.checkSum = -1; // -1 indicates unset
60 this.created = null;
61 this.adminRefs = new ArrayList();
62 this.describeRefs = new ArrayList();
63 this.group = null;
64 }
65
66 public boolean equals(URL url)
67 { return this.location.equals(url);
68 }
69
70 /**
71 * Set the parent group
72 *
73 * @param <code>METSFileGroup</code> the new holding file group
74 */
75 public void setGroup(METSFileGroup group)
76 { this.group = group;
77 }
78
79 /**
80 * Get the parent group of this file
81 *
82 * @return <code>METSFileGroup</code> the holding file group
83 */
84 public METSFileGroup getGroup()
85 { return this.group;
86 }
87
88 /**
89 * Get the identifier of this file
90 *
91 * @return <code>METSFileID</code> the file identifier
92 */
93 public METSFileID getID()
94 { return this.id;
95 }
96
97 /**
98 * Set the identifier of this file
99 *
100 * @param <code>METSFileID</code> the file identifier
101 */
102 public void setID(METSFileID id)
103 { this.id = id;
104 }
105
106 /**
107 * @return <code>String</code> the MIME (content) type of the file
108 */
109 public String getMIMEType()
110 { return this.MIMEType;
111 }
112
113 /**
114 * @return <code>URL</code> the location of the file. This will often be in
115 * "file://" form.
116 */
117 public URL getLocation()
118 { return this.location.getLocation();
119 }
120
121 /**
122 * Take a url and make a METSFile object out of it - works out
123 * details such as the Mime type, identifiers and other necessary
124 * information to call the constructor for METSFile
125 *
126 * @param <code>URL</code> the url of the object - this may be a
127 * reference to a file on the local filestore
128 *
129 * @return <code>METSFile</code> the METS file object for the corresponding
130 * document component.
131 */
132 public static METSFile makeMETSFile(URL url, String mimeType)
133 {
134 METSFile reply = new METSFile(new METSFileID(), new METSFilePos(url), mimeType);
135 return reply;
136 }
137
138 public static METSFile makeMETSFile(URL url)
139 { String mimeType;
140
141 if (url.getProtocol().equals("file"))
142 { // TODO: Work out the MIME type
143 mimeType = URLConnection.getFileNameMap().getContentTypeFor(url.toString().substring(7));
144 if (mimeType == null)
145 { mimeType = "text/plain";
146 }
147 }
148 else
149 { // TODO: look up the MIME type through the pertinent connection
150 mimeType = HTTPTools.getMIMEType(url);
151 }
152 // TODO: make the identifier...
153
154 return makeMETSFile(url, mimeType);
155 }
156
157 /**
158 * Write the METS file in an XML to a text-output sink
159 *
160 * @param <code>PrintWriter</code> the destination of the output
161 */
162 public void write(PrintWriter writer)
163 { String tag = XMLTools.getOpenTag("mets", "file");
164 tag = XMLTools.addAttribute(tag, "MIMETYPE", this.MIMEType);
165 tag = XMLTools.addAttribute(tag, "ID", this.id.toString());
166 writer.println(tag);
167
168 tag = XMLTools.getOpenTag("mets", "FLocat");
169 tag = XMLTools.addAttribute(tag, "LOCTYPE", this.location.getType());
170 tag = XMLTools.addAttribute(tag, "xlink:href", this.location.getLocation().toString());
171 tag = XMLTools.addAttribute(tag, "ID", this.id.toString());
172 tag = XMLTools.makeSingleton(tag);
173 writer.println(tag);
174
175 writer.println(XMLTools.getCloseTag("mets", "file"));
176 }
177
178
179 public boolean writeSQL(int groupReference, GS3SQLConnection connection)
180 { // check if this file is in the table already...
181 GS3SQLAction action;
182
183 GS3SQLSelect select = new GS3SQLSelect("files");
184 select.addField("*");
185 GS3SQLWhereItem whereItem = new GS3SQLWhereItem("FileGroupRef", "=", Integer.toString(groupReference),
186 GS3SQLField.INTEGER_TYPE);
187 GS3SQLWhere where = new GS3SQLWhere(whereItem);
188 whereItem = new GS3SQLWhereItem("FileID","=", this.id.toString());
189 where.add(whereItem);
190 select.setWhere(where);
191 connection.execute(select.toString());
192
193 // if not, then make an insert action
194 try {
195 ResultSet results = connection.getResultSet();
196 if (results == null ||
197 !results.first())
198 { GS3SQLInsert insert = new GS3SQLInsert("files");
199
200 insert.addValue("FileGroupRef", Integer.toString(groupReference), GS3SQLField.INTEGER_TYPE);
201 insert.addValue("FileID", this.id.toString());
202
203 action = insert;
204
205 }
206 else {
207 GS3SQLUpdate update = new GS3SQLUpdate("files");
208
209 update.setWhere(where);
210 action = update;
211 }
212 }
213 catch (SQLException ex)
214 { System.err.println(ex);
215 return false;
216 }
217 action.addValue("FileLocType", this.location.getType());
218 action.addValue("FileLocation", this.location.getLocation().toString());
219 action.addValue("MIMEType", this.MIMEType);
220
221 return connection.execute(action.toString());
222 }
223
224 public static METSFile readSQL(GS3SQLConnection connection, ResultSet parentSet)
225 {
226 try {
227 String locType = parentSet.getString("FileLocType");
228 String location = parentSet.getString("FileLocation");
229 String mimeType = parentSet.getString("MIMEType");
230 String id = parentSet.getString("FileID");
231
232 METSFileID metsID = new METSFileID(id);
233 METSFilePos metsFilePos = new METSFilePos(locType, location);
234
235 METSFile reply = new METSFile(metsID, metsFilePos, mimeType);
236 return reply;
237 }
238 catch (SQLException ex)
239 { System.out.println(ex);
240 }
241 catch (java.net.MalformedURLException urlEx)
242 { System.out.println(urlEx);
243 }
244 return null;
245 }
246
247 /**
248 * Parse an XML Element as a METS File
249 */
250 /*
251 public static METSFile parseXML(Element element, METSFileGroup parentGroup)
252 { METSFile file = null;
253 NodeList children = element.getChildNodes();
254
255 for (int c = 0; c < children.getLength(); c ++)
256 { if (children.item(c).getNodeType() != org.w3c.dom.Node.ELEMENT_NODE) {
257 continue;
258 }
259
260 Element childElement = (Element) children.item(c);
261 if (childElement.getNodeName().equals("mets:FLocat"))
262 { METSFilePos filePos;
263
264 // get most of the information from the child FLocat node
265 String locationType = childElement.getAttribute("LOCTYPE");
266 String href = childElement.getAttribute("xlink:href");
267 String id = childElement.getAttribute("ID");
268
269 // some more data from the parent node
270 String mimeType = element.getAttribute("MIMETYPE");
271
272 try
273 { filePos = new METSFilePos(href, locationType);
274 }
275 catch (java.net.MalformedURLException ex)
276 { // TODO: raise error
277 continue;
278 }
279 file = new METSFile(new METSFileID(id), filePos, mimeType);
280 }
281 else if (childElement.getNodeName().equals("mets:FContent"))
282 {
283 }
284 else
285 { // TODO: raise an error!
286 }
287 }
288 return file;
289 }
290 */
291
292
293 public static METSFile parse_file(Element element, METSFileGroup group, String parseFilePath)
294 {
295 NodeList children = element.getChildNodes();
296
297 METSFile thisFile = null;
298
299 for (int c = 0; c < children.getLength(); c++){
300 if (children.item(c).getNodeType() != org.w3c.dom.Node.ELEMENT_NODE){
301 continue;
302 }
303 Element child = (Element) children.item(c);
304 String childName = child.getNodeName();
305
306 if (childName.equals("mets:file")){
307 METSFile.parse_file(child, group, parseFilePath);
308 } else if (childName.equals("mets:FLocat")) {
309 String mimeType = element.getAttribute("MIMETYPE");
310 String fileId = element.getAttribute("ID");
311
312 METSFilePos thisFilePos = METSFile.parse_flocateXML(child, parseFilePath);
313 thisFile = new METSFile(new METSFileID(fileId), thisFilePos, mimeType);
314 group.addFile(thisFile);
315 } else {
316 System.err.println("Warning: unrecognised tag " +childName);
317 }
318 }
319 return thisFile;
320 }
321
322 public static METSFilePos parse_flocateXML(Element element, String parseFilePath)
323 {
324 METSFilePos thisFilePos = null;
325
326 // get most of the information from the child FLocat node
327 String locationType = element.getAttribute("LOCTYPE");
328 String href = element.getAttribute("xlink:href");
329 String fileId = element.getAttribute("ID");
330
331 try {
332 thisFilePos = new METSFilePos(locationType, href);
333
334 if (fileId.startsWith("default.")) {
335 //retrieve the string after ..import/
336 int importPosition = href.indexOf("import/")+7;
337 href.substring(importPosition);
338 if (fileId.equals("default.1")) {
339 href = "file:" + parseFilePath+ "doctxt.xml";
340 } else {
341 href = "file:" + parseFilePath + href.substring(importPosition);
342 }
343 }
344 thisFilePos = new METSFilePos(locationType, href);
345 } catch (java.net.MalformedURLException ex) {
346 // TODO: raise error
347 System.err.println(ex);
348 }
349 return thisFilePos;
350// file = new METSFile(new METSFileID(id), filePos, mimeType);
351// }
352// else if (childElement.getNodeName().equals("mets:FContent"))
353// {
354// }
355// else
356// { // TODO: raise an error!
357// }
358// }
359// return file;
360// }
361
362 }
363
364
365 /**
366 * Get the location of this file as a URL
367 */
368 public URL getURL()
369 { return this.location.getLocation();
370 }
371
372 /**
373 * Get modified file date
374 */
375 public long getModifiedDatestamp()
376 { return this.location.getModifiedDatestamp();
377 }
378
379 /**
380 * Overridden <code>toString</code> for convenience - returns the location of the file.
381 *
382 * @return <code>String</code> the location of the file as a string
383 */
384 public String toString()
385 { return this.location.toString();
386 }
387}
Note: See TracBrowser for help on using the repository browser.