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

Last change on this file since 9874 was 9874, checked in by kjdon, 19 years ago

merged from branch ant-install-branch: merge 1

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