001 /*
002 *
003 * $Revision: 1.6 $ $Date: 2009/01/19 10:06:12 $
004 *
005 * This file is part of *** M y C o R e ***
006 * See http://www.mycore.de/ for details.
007 *
008 * This program is free software; you can use it, redistribute it
009 * and / or modify it under the terms of the GNU General Public License
010 * (GPL) as published by the Free Software Foundation; either version 2
011 * of the License or (at your option) any later version.
012 *
013 * This program is distributed in the hope that it will be useful, but
014 * WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016 * GNU General Public License for more details.
017 *
018 * You should have received a copy of the GNU General Public License
019 * along with this program, in a file called gpl.txt or license.txt.
020 * If not, write to the Free Software Foundation Inc.,
021 * 59 Temple Place - Suite 330, Boston, MA 02111-1307 USA
022 */
023
024 package org.mycore.frontend.metsmods;
025
026 import java.io.IOException;
027 import java.util.ArrayList;
028 import java.util.Iterator;
029 import java.util.List;
030
031 import org.jdom.*;
032 import org.jdom.input.SAXBuilder;
033 import org.jdom.output.XMLOutputter;
034 import org.mycore.common.MCRConstants;
035
036 /**
037 * @author Stefan Freitag
038 * @version $Revision: 1.6 $ $Date: 2009/01/19 10:06:12 $
039 */
040
041 public class MCRMetsModsUtil {
042
043
044
045 Element fileSec = new Element("fileSec", MCRConstants.METS_NAMESPACE);
046 Element structMap = new Element("structMap", MCRConstants.METS_NAMESPACE);
047
048 /**
049 * Gets a list containing picture names including directories.
050 *
051 * @param list
052 * of pictures
053 * @return JDOM element with METS
054 */
055 public Element createMetsElement(ArrayList<String> list, Element mets, String default_url) {
056 Iterator<String> iter = list.iterator();
057 int i = 1;
058 while (iter.hasNext()) {
059 String pic = iter.next();
060 String fn = pic.substring(pic.lastIndexOf("/") + 1, pic.lastIndexOf("."));
061 pic = default_url + "/" + pic;
062 add_file(mets, fn, pic, i);
063 i++;
064 }
065
066 return mets;
067 }
068
069 private Element create_mets_head() {
070 // <mets:mets xmlns:mets="http://www.loc.gov/METS/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xlink="http://www.w3.org/1999/xlink"
071 // xsi:schemaLocation="http://www.loc.gov/METS/ http://www.loc.gov/mets/mets.xsd">
072 // String mets_head =
073 // "<mets:mets xmlns:mets=\"http://www.loc.gov/METS/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" xsi:schemaLocation=\"http://www.loc.gov/METS/ http://www.loc.gov/mets/mets.xsd\"></mets:mets>";
074 String empty_mets_url = new String("../webapps/empty.mets.xml");
075
076 try {
077 Document doc = new SAXBuilder().build(empty_mets_url);
078
079 doc.getRootElement().removeContent();
080
081 return doc.getRootElement();
082
083 // XMLOutputter xmlout = new XMLOutputter();
084 // xmlout.output(doc.getRootElement(), System.out);
085 } catch (JDOMException e) {
086 // TODO Auto-generated catch block
087 e.printStackTrace();
088 } catch (IOException e) {
089 // TODO Auto-generated catch block
090 e.printStackTrace();
091 }
092
093 return null;
094 }
095
096 public Element init_mets(String derivate_id) {
097 Element mets = create_mets_head();
098 if (mets == null)
099 mets = new Element("mets", MCRConstants.METS_NAMESPACE);
100 mets.addContent(init_fileSec(derivate_id));
101 mets.addContent(init_structMap(derivate_id));
102
103 return mets;
104 }
105
106 public Element init_mods(String derivate_id, String title, String display, String place, String date) {
107 Element dmdSec = new Element("dmdSec", MCRConstants.METS_NAMESPACE);
108 dmdSec.setAttribute("ID", "dmd_" + derivate_id);
109
110 Element mdWrap = new Element("mdWrap", MCRConstants.METS_NAMESPACE);
111 mdWrap.setAttribute("MIMETYPE", "text/xml");
112 mdWrap.setAttribute("MDTYPE", "MODS");
113
114 Element xmlData = new Element("xmlData", MCRConstants.METS_NAMESPACE);
115 Element mods = new Element("mods", MCRConstants.MODS_NAMESPACE);
116
117 Element titleInfo = new Element("titleInfo", MCRConstants.MODS_NAMESPACE);
118 titleInfo.addContent(new Element("title", MCRConstants.MODS_NAMESPACE).setText(title));
119
120 Element name = new Element("name", MCRConstants.MODS_NAMESPACE);
121 name.setAttribute("type", "personal");
122 name.addContent(new Element("displayForm", MCRConstants.MODS_NAMESPACE).setText(display));
123
124 Element originInfo = new Element("originInfo", MCRConstants.MODS_NAMESPACE);
125 originInfo.addContent(new Element("place", MCRConstants.MODS_NAMESPACE).addContent(new Element("placeTerm", MCRConstants.MODS_NAMESPACE).setAttribute(
126 "type", "text").setText(place)));
127 originInfo.addContent(new Element("dateIssued", MCRConstants.MODS_NAMESPACE).setAttribute("keyDate", "yes").setAttribute("encoding", "w3cdtf").setText(
128 date));
129
130 mods.addContent(titleInfo);
131 mods.addContent(name);
132 mods.addContent(originInfo);
133
134 xmlData.addContent(mods);
135 mdWrap.addContent(xmlData);
136 dmdSec.addContent(mdWrap);
137
138 return dmdSec;
139 }
140
141 public Element init_amdSec(String derivate_id, String owner, String ownerLogo, String ownerSiteURL, String reference, String presentation) {
142 Element amdSec = new Element("amdSec", MCRConstants.METS_NAMESPACE);
143 amdSec.setAttribute("ID", "amd_" + derivate_id);
144
145 Element rightsMD = new Element("rightsMD", MCRConstants.METS_NAMESPACE);
146 rightsMD.setAttribute("ID", "rights_" + derivate_id);
147
148 Element mdWrap = new Element("mdWrap", MCRConstants.METS_NAMESPACE);
149 mdWrap.setAttribute("MIMETYPE", "text/xml");
150 mdWrap.setAttribute("MDTYPE", "OTHER");
151 mdWrap.setAttribute("OTHERMDTYPE", "DVRIGHTS");
152
153 Element xmlData = new Element("xmlData", MCRConstants.METS_NAMESPACE);
154 Element rights = new Element("rights", MCRConstants.DV_NAMESPACE);
155 rights.addContent(new Element("owner", MCRConstants.DV_NAMESPACE).setText(owner));
156 rights.addContent(new Element("ownerLogo", MCRConstants.DV_NAMESPACE).setText(ownerLogo));
157 rights.addContent(new Element("ownerSiteURL", MCRConstants.DV_NAMESPACE).setText(ownerSiteURL));
158
159 xmlData.addContent(rights);
160 mdWrap.addContent(xmlData);
161 rightsMD.addContent(mdWrap);
162
163 amdSec.addContent(rightsMD);
164 amdSec.addContent(init_digiprov(derivate_id, reference, presentation));
165
166 return amdSec;
167 }
168
169 private Element init_digiprov(String derivate_id, String reference, String presentation) {
170 Element digiprovMD = new Element("digiprovMD", MCRConstants.METS_NAMESPACE);
171 digiprovMD.setAttribute("ID", "digiprov_" + derivate_id);
172
173 Element mdWrap = new Element("mdWrap", MCRConstants.METS_NAMESPACE);
174 mdWrap.setAttribute("MIMETYPE", "text/html");
175 mdWrap.setAttribute("MDTYPE", "OTHER");
176 mdWrap.setAttribute("OTHERMDTYPE", "DVLINKS");
177
178 Element xmlData = new Element("xmlData", MCRConstants.METS_NAMESPACE);
179 Element dvlinks = new Element("links", MCRConstants.DV_NAMESPACE);
180 dvlinks.addContent(new Element("reference", MCRConstants.DV_NAMESPACE).setText(reference));
181 dvlinks.addContent(new Element("presentation", MCRConstants.DV_NAMESPACE).setText(presentation));
182
183 xmlData.addContent(dvlinks);
184 mdWrap.addContent(xmlData);
185 digiprovMD.addContent(mdWrap);
186
187 return digiprovMD;
188 }
189
190 public void showElement(Element e) {
191 try {
192 XMLOutputter xmlout = new XMLOutputter();
193 xmlout.output(e, System.out);
194 } catch (Exception p) {
195 p.printStackTrace();
196 }
197 }
198
199 public Element init_fileSec(String derivate_id) {
200 fileSec.setAttribute("ID", "fileSec_" + derivate_id);
201 Element fileGrp = new Element("fileGrp", MCRConstants.METS_NAMESPACE);
202 fileGrp.setAttribute("USE", "DEFAULT");
203
204 fileSec.addContent(fileGrp);
205
206 return fileSec;
207 }
208
209 public Element init_structMap(String derivate_id) {
210 structMap.setAttribute("TYPE", "PHYSICAL");
211 Element div = new Element("div", MCRConstants.METS_NAMESPACE);
212 div.setAttribute("ID", "phys_" + derivate_id);
213 div.setAttribute("DMDID", "dmd_" + derivate_id);
214 div.setAttribute("ADMID", "amd_" + derivate_id);
215 div.setAttribute("TYPE", "physSequence");
216
217 structMap.addContent(div);
218
219 return structMap;
220 }
221
222 public Element add_file(Element mets, String file_id, String url, int order) {
223 Element fileGrp = mets.getChild("fileSec", MCRConstants.METS_NAMESPACE).getChild("fileGrp", MCRConstants.METS_NAMESPACE);
224 Element div = mets.getChild("structMap", MCRConstants.METS_NAMESPACE).getChild("div", MCRConstants.METS_NAMESPACE);
225
226 // <fileSec>
227 Element file = new Element("file", MCRConstants.METS_NAMESPACE);
228 file.setAttribute("MIMETYPE", "image/jpeg");
229 file.setAttribute("ID", file_id + "_default");
230 Element flocat = new Element("FLocat", MCRConstants.METS_NAMESPACE);
231 flocat.setAttribute("LOCTYPE", "URL");
232 flocat.setAttribute("href", url, MCRConstants.XLINK_NAMESPACE);
233 file.addContent(flocat);
234 fileGrp.addContent(file);
235 // </fileSec>
236
237 // <structMap>
238 Element div_ = new Element("div", MCRConstants.METS_NAMESPACE);
239 div_.setAttribute("ID", file_id);
240 div_.setAttribute("ORDER", String.valueOf(order));
241 //The following line has changed in order to set the filename as orderlabel...
242 div_.setAttribute("ORDERLABEL", file_id);//String.valueOf(order));
243 // The next line is commented out because of a changed behavior of the mets format.
244 // div_.setAttribute("type","page");
245 Element fptr = new Element("fptr", MCRConstants.METS_NAMESPACE);
246 fptr.setAttribute("FILEID", file_id + "_default");
247 div_.addContent(fptr);
248 div.addContent(div_);
249 // </structMap>
250
251 return mets;
252 }
253
254 public static ArrayList<MCRMetsModsPicture> getFileList(Document metsfile) {
255 ArrayList<MCRMetsModsPicture> piclist = new ArrayList<MCRMetsModsPicture>();
256 Element root = metsfile.getRootElement();
257 Element structMap = root.getChild("structMap", MCRConstants.METS_NAMESPACE);
258 Element div = structMap.getChild("div", MCRConstants.METS_NAMESPACE);
259 List elements = div.getChildren("div", MCRConstants.METS_NAMESPACE);
260
261 for (int i = 0; i < elements.size(); i++)
262 piclist.add(new MCRMetsModsPicture("dummy", 0, "0"));
263
264 for (int i = 0; i < elements.size(); i++) {
265 Element e = (Element) elements.get(i);
266 String fileid = e.getAttributeValue("ID");
267 String order = e.getAttributeValue("ORDER");
268 String orderlabel = e.getAttributeValue("ORDERLABEL");
269
270 piclist.set(Integer.parseInt(order) - 1, new MCRMetsModsPicture(fileid, Integer.parseInt(order), orderlabel));
271
272 }
273
274 return piclist;
275 }
276
277 public static Document getMetsFile(ArrayList<MCRMetsModsPicture> piclist, Document metsfile) {
278 Element root = metsfile.getRootElement();
279 Element structMap = root.getChild("structMap", MCRConstants.METS_NAMESPACE);
280 Element div = structMap.getChild("div", MCRConstants.METS_NAMESPACE);
281 List elements = div.getChildren("div", MCRConstants.METS_NAMESPACE);
282 for (int i = 0; i < elements.size(); i++) {
283 Element e = (Element) elements.get(i);
284 String fileid = e.getAttributeValue("ID");
285
286 for (int j = 0; j < piclist.size(); j++) {
287 MCRMetsModsPicture mmp = piclist.get(j);
288 if (mmp.getPicture().compareTo(fileid) == 0) {
289
290 // System.out.println("-fileid: "+fileid+" -j: "+j+" -mmp.getPicture: "+mmp.getPicture()+" -mmp.getOrder: "+mmp.getOrder());
291
292 e.setAttribute("ORDER", String.valueOf(mmp.getOrder()));
293 e.setAttribute("ORDERLABEL", mmp.getOrderlabel());
294 piclist.remove(j); // to get a little bit more speed
295 }
296 }
297
298 }
299
300 return metsfile;
301 }
302
303
304 }