1 | package org.greenstone.gsdl3.gs3build.metadata;
|
---|
2 |
|
---|
3 | import java.io.PrintWriter;
|
---|
4 |
|
---|
5 | import java.util.List;
|
---|
6 | import java.util.ArrayList;
|
---|
7 |
|
---|
8 | import java.net.URL;
|
---|
9 | import java.net.URLConnection;
|
---|
10 |
|
---|
11 | import org.w3c.dom.Document;
|
---|
12 | import org.w3c.dom.Element;
|
---|
13 | import org.w3c.dom.NamedNodeMap;
|
---|
14 | import org.w3c.dom.Node;
|
---|
15 | import org.w3c.dom.NodeList;
|
---|
16 | import org.w3c.dom.Text;
|
---|
17 |
|
---|
18 | import org.greenstone.gsdl3.gs3build.util.HTTPTools;
|
---|
19 | import org.greenstone.gsdl3.gs3build.util.XMLTools;
|
---|
20 | import org.greenstone.gsdl3.gs3build.database.*;
|
---|
21 | import org.greenstone.gsdl3.gs3build.util.GS3SQLConnection;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * An individual METS component file for a document. Many features of METS are not
|
---|
25 | * currently fully supported/used here, but member variables exist for all of the
|
---|
26 | * standard members.
|
---|
27 | */
|
---|
28 |
|
---|
29 | public class METSFile
|
---|
30 | {
|
---|
31 | METSFileID id;
|
---|
32 | METSFilePos location;
|
---|
33 | String MIMEType;
|
---|
34 | int sequenceNo;
|
---|
35 | long size;
|
---|
36 | METSDate created;
|
---|
37 | long checkSum;
|
---|
38 | List adminRefs;
|
---|
39 | List describeRefs;
|
---|
40 | METSFileGroup group;
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Create a new METSFile object, with the given properties. The remaining
|
---|
44 | * fields are filled in with default values.
|
---|
45 | *
|
---|
46 | * @param <code>String</code> the id of the file
|
---|
47 | * @param <code>METSFilePos</code> the location of the file
|
---|
48 | * @param <code>String</code> the MIME type of the file
|
---|
49 | */
|
---|
50 | public METSFile(METSFileID id, METSFilePos location, String mimeType)
|
---|
51 | { this.id = id; // required
|
---|
52 | this.location = location; // the location
|
---|
53 | this.MIMEType = mimeType; // the MIME type;
|
---|
54 | this.sequenceNo = -1; // -1 indicates unset
|
---|
55 | this.size = -1; // -1 indicates unset
|
---|
56 | this.checkSum = -1; // -1 indicates unset
|
---|
57 | this.created = null;
|
---|
58 | this.adminRefs = new ArrayList();
|
---|
59 | this.describeRefs = new ArrayList();
|
---|
60 | this.group = null;
|
---|
61 | }
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Set the parent group
|
---|
65 | *
|
---|
66 | * @param <code>METSFileGroup</code> the new holding file group
|
---|
67 | */
|
---|
68 | public void setGroup(METSFileGroup group)
|
---|
69 | { this.group = group;
|
---|
70 | }
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Get the parent group of this file
|
---|
74 | *
|
---|
75 | * @return <code>METSFileGroup</code> the holding file group
|
---|
76 | */
|
---|
77 | public METSFileGroup getGroup()
|
---|
78 | { return this.group;
|
---|
79 | }
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Get the identifier of this file
|
---|
83 | *
|
---|
84 | * @return <code>METSFileID</code> the file identifier
|
---|
85 | */
|
---|
86 | public METSFileID getID()
|
---|
87 | { return this.id;
|
---|
88 | }
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Set the identifier of this file
|
---|
92 | *
|
---|
93 | * @param <code>METSFileID</code> the file identifier
|
---|
94 | */
|
---|
95 | public void setID(METSFileID id)
|
---|
96 | { this.id = id;
|
---|
97 | }
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * @return <code>String</code> the MIME (content) type of the file
|
---|
101 | */
|
---|
102 | public String getMIMEType()
|
---|
103 | { return this.MIMEType;
|
---|
104 | }
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * @return <code>URL</code> the location of the file. This will often be in
|
---|
108 | * "file://" form.
|
---|
109 | */
|
---|
110 | public URL getLocation()
|
---|
111 | { return this.location.getLocation();
|
---|
112 | }
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Take a url and make a METSFile object out of it - works out
|
---|
116 | * details such as the Mime type, identifiers and other necessary
|
---|
117 | * information to call the constructor for METSFile
|
---|
118 | *
|
---|
119 | * @param <code>URL</code> the url of the object - this may be a
|
---|
120 | * reference to a file on the local filestore
|
---|
121 | *
|
---|
122 | * @return <code>METSFile</code> the METS file object for the corresponding
|
---|
123 | * document component.
|
---|
124 | */
|
---|
125 | public static METSFile makeMETSFile(URL url, String mimeType)
|
---|
126 | {
|
---|
127 | METSFile reply = new METSFile(new METSFileID(), new METSFilePos(url), mimeType);
|
---|
128 | return reply;
|
---|
129 | }
|
---|
130 |
|
---|
131 | public static METSFile makeMETSFile(URL url)
|
---|
132 | { String mimeType;
|
---|
133 |
|
---|
134 | if (url.toString().startsWith("file://"))
|
---|
135 | { // TODO: Work out the MIME type
|
---|
136 | mimeType = URLConnection.getFileNameMap().getContentTypeFor(url.toString().substring(7));
|
---|
137 | if (mimeType == null)
|
---|
138 | { mimeType = "text/plain";
|
---|
139 | }
|
---|
140 | }
|
---|
141 | else
|
---|
142 | { // TODO: look up the MIME type through the pertinent connection
|
---|
143 | mimeType = HTTPTools.getMIMEType(url);
|
---|
144 | }
|
---|
145 | // TODO: make the identifier...
|
---|
146 |
|
---|
147 | return makeMETSFile(url, mimeType);
|
---|
148 | }
|
---|
149 |
|
---|
150 | /**
|
---|
151 | * Write the METS file in an XML to a text-output sink
|
---|
152 | *
|
---|
153 | * @param <code>PrintWriter</code> the destination of the output
|
---|
154 | */
|
---|
155 | public void write(PrintWriter writer)
|
---|
156 | { String tag = XMLTools.getOpenTag("mets", "file");
|
---|
157 | tag = XMLTools.addAttribute(tag, "MIMETYPE", this.MIMEType);
|
---|
158 | tag = XMLTools.addAttribute(tag, "ID", this.id.toString());
|
---|
159 | writer.println(tag);
|
---|
160 |
|
---|
161 | tag = XMLTools.getOpenTag("mets", "FLocat");
|
---|
162 | tag = XMLTools.addAttribute(tag, "LOCTYPE", this.location.getType());
|
---|
163 | tag = XMLTools.addAttribute(tag, "xlink:href", this.location.getLocation().toString());
|
---|
164 | tag = XMLTools.addAttribute(tag, "ID", this.id.toString());
|
---|
165 | tag = XMLTools.makeSingleton(tag);
|
---|
166 | writer.println(tag);
|
---|
167 |
|
---|
168 | writer.println(XMLTools.getCloseTag("mets", "file"));
|
---|
169 | }
|
---|
170 |
|
---|
171 |
|
---|
172 | public void writeSQL(GS3SQLConnection connection)
|
---|
173 | { // check if this node is in the
|
---|
174 | GS3SQLInsert insert = new GS3SQLInsert("files");
|
---|
175 |
|
---|
176 | insert.addValue("FileLocType", this.location.getType());
|
---|
177 | insert.addValue("FileLocation", this.location.getLocation().toString());
|
---|
178 | insert.addValue("MIMEType", this.MIMEType);
|
---|
179 | insert.addValue("ID", this.id.toString());
|
---|
180 |
|
---|
181 | connection.execute(insert.toString());
|
---|
182 | }
|
---|
183 |
|
---|
184 | /**
|
---|
185 | * Parse an XML Element as a METS File
|
---|
186 | */
|
---|
187 | public static METSFile parseXML(Element element, METSFileGroup parentGroup)
|
---|
188 | { METSFile file = null;
|
---|
189 | NodeList children = element.getChildNodes();
|
---|
190 |
|
---|
191 | for (int c = 0; c < children.getLength(); c ++)
|
---|
192 | { if (children.item(c).getNodeType() != org.w3c.dom.Node.ELEMENT_NODE) {
|
---|
193 | continue;
|
---|
194 | }
|
---|
195 |
|
---|
196 | Element childElement = (Element) children.item(c);
|
---|
197 | if (childElement.getNodeName().equals("mets:FLocat"))
|
---|
198 | { METSFilePos filePos;
|
---|
199 |
|
---|
200 | // get most of the information from the child FLocat node
|
---|
201 | String locationType = childElement.getAttribute("LOCTYPE");
|
---|
202 | String href = childElement.getAttribute("xlink:href");
|
---|
203 | String id = childElement.getAttribute("ID");
|
---|
204 |
|
---|
205 | // some more data from the parent node
|
---|
206 | String mimeType = element.getAttribute("MIMETYPE");
|
---|
207 |
|
---|
208 | try
|
---|
209 | { filePos = new METSFilePos(href, locationType);
|
---|
210 | }
|
---|
211 | catch (java.net.MalformedURLException ex)
|
---|
212 | { // TODO: raise error
|
---|
213 | continue;
|
---|
214 | }
|
---|
215 | file = new METSFile(new METSFileID(id), filePos, mimeType);
|
---|
216 | }
|
---|
217 | else if (childElement.getNodeName().equals("mets:FContent"))
|
---|
218 | {
|
---|
219 | }
|
---|
220 | else
|
---|
221 | { // TODO: raise an error!
|
---|
222 | }
|
---|
223 | }
|
---|
224 | return file;
|
---|
225 | }
|
---|
226 |
|
---|
227 |
|
---|
228 | /**
|
---|
229 | * Overridden <code>toString</code> for convenience - returns the location of the file.
|
---|
230 | *
|
---|
231 | * @return <code>String</code> the location of the file as a string
|
---|
232 | */
|
---|
233 | public String toString()
|
---|
234 | { return this.location.toString();
|
---|
235 | }
|
---|
236 | }
|
---|