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

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

changed the import statements for GS3SQLConnection and GS3SQLConnectionFactory to reflect their move to the database package

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