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

Last change on this file since 5945 was 5945, checked in by cs025, 20 years ago

Extensive additions to metadata

  • Property svn:keywords set to Author Date Id Revision
File size: 8.5 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.toString().startsWith("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("ID","=", 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("ID", 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("ID");
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 public static METSFile parseXML(Element element, METSFileGroup parentGroup)
251 { METSFile file = null;
252 NodeList children = element.getChildNodes();
253
254 for (int c = 0; c < children.getLength(); c ++)
255 { if (children.item(c).getNodeType() != org.w3c.dom.Node.ELEMENT_NODE) {
256 continue;
257 }
258
259 Element childElement = (Element) children.item(c);
260 if (childElement.getNodeName().equals("mets:FLocat"))
261 { METSFilePos filePos;
262
263 // get most of the information from the child FLocat node
264 String locationType = childElement.getAttribute("LOCTYPE");
265 String href = childElement.getAttribute("xlink:href");
266 String id = childElement.getAttribute("ID");
267
268 // some more data from the parent node
269 String mimeType = element.getAttribute("MIMETYPE");
270
271 try
272 { filePos = new METSFilePos(href, locationType);
273 }
274 catch (java.net.MalformedURLException ex)
275 { // TODO: raise error
276 continue;
277 }
278 file = new METSFile(new METSFileID(id), filePos, mimeType);
279 }
280 else if (childElement.getNodeName().equals("mets:FContent"))
281 {
282 }
283 else
284 { // TODO: raise an error!
285 }
286 }
287 return file;
288 }
289
290
291 /**
292 * Overridden <code>toString</code> for convenience - returns the location of the file.
293 *
294 * @return <code>String</code> the location of the file as a string
295 */
296 public String toString()
297 { return this.location.toString();
298 }
299}
Note: See TracBrowser for help on using the repository browser.