KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > app > oai > DIDLCrosswalk


1 /*
2  * Copyright (c) 2004-2005, Hewlett-Packard Company and Massachusetts
3  * Institute of Technology. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * - Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * - Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * - Neither the name of the Hewlett-Packard Company nor the name of the
17  * Massachusetts Institute of Technology nor the names of their
18  * contributors may be used to endorse or promote products derived from
19  * this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
32  * DAMAGE.
33  */

34
35 package org.dspace.app.oai;
36
37 import java.util.Properties JavaDoc;
38 import java.util.Date JavaDoc;
39 import java.io.*;
40 import java.lang.*;
41 import java.sql.SQLException JavaDoc;
42 import ORG.oclc.oai.server.crosswalk.Crosswalk;
43 import ORG.oclc.oai.server.verb.CannotDisseminateFormatException;
44 import ORG.oclc.oai.server.verb.ServerVerb;
45 import org.dspace.content.DCDate;
46 import org.dspace.content.DCValue;
47 import org.dspace.content.Item;
48 import org.dspace.content.Bundle;
49 import org.dspace.content.Bitstream;
50 import org.dspace.content.BitstreamFormat;
51 import org.dspace.storage.bitstore.BitstreamStorageManager;
52 import org.dspace.app.didl.UUIDFactory;
53 import org.dspace.core.Utils;
54 import org.dspace.search.HarvestedItemInfo;
55 import sun.misc.BASE64Encoder;
56 import org.dspace.core.*;
57
58 /**
59  * DSpace Item DIDL crosswalk.
60  *
61  * Development of this code was part of the aDORe repository project
62  * by the Research Library of the Los Alamos National Laboratory.
63  *
64  * @author Henry Jerez
65  * @author Los Alamos National Laboratory
66  */

67
68 public class DIDLCrosswalk extends Crosswalk
69 {
70     public DIDLCrosswalk(Properties JavaDoc properties)
71     {
72         super("urn:mpeg:mpeg21:2002:02-DIDL-NS http://standards.iso.org/ittf/PubliclyAvailableStandards/MPEG-21_schema_files/did/didl.xsd ");
73     }
74     
75     
76     public boolean isAvailableFor(Object JavaDoc nativeItem)
77     {
78         // We have DC for everything
79
return true;
80     }
81     
82     
83     public String JavaDoc createMetadata(Object JavaDoc nativeItem)
84         throws CannotDisseminateFormatException
85     {
86         Item item = ((HarvestedItemInfo) nativeItem).item;
87        
88         Date JavaDoc d = ((HarvestedItemInfo) nativeItem).datestamp;
89         String JavaDoc ITEMDATE = new DCDate(d).toString();
90         
91         // Get all the DC
92
DCValue[] allDC = item.getDC(Item.ANY, Item.ANY, Item.ANY);
93         
94         StringBuffer JavaDoc metadata = new StringBuffer JavaDoc();
95         StringBuffer JavaDoc metadata1 = new StringBuffer JavaDoc();
96         String JavaDoc itemhandle=item.getHandle();
97         int maxsize= Integer.parseInt(ConfigurationManager.getProperty("oai.didl.maxresponse"));
98         String JavaDoc currdate=ServerVerb.createResponseDate(new Date JavaDoc());
99         
100         metadata.append("<didl:DIDL ")
101             .append(" xmlns:didl=\"urn:mpeg:mpeg21:2002:02-DIDL-NS\" ")
102             .append(" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ")
103             .append("xsi:schemaLocation=\"urn:mpeg:mpeg21:2002:02-DIDL-NS http://standards.iso.org/ittf/PubliclyAvailableStandards/MPEG-21_schema_files/did/didl.xsd \">")
104             .append ("<didl:DIDLInfo>")
105             .append ("<dcterms:created xmlns:dcterms=\"http://purl.org/dc/terms/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://purl.org/dc/terms/ \">")
106             .append (currdate)
107             .append ("</dcterms:created> </didl:DIDLInfo>" )
108             .append("<didl:Item id=\"")
109             .append("uuid-" + UUIDFactory.generateUUID().toString()+"\">");
110         metadata.append("<didl:Descriptor>")
111             .append("<didl:Statement mimeType=\"application/xml; charset=utf-8\">")
112             .append("<dii:Identifier xmlns:dii=\"urn:mpeg:mpeg21:2002:01-DII-NS\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"urn:mpeg:mpeg21:2002:01-DII-NS http://standards.iso.org/ittf/PubliclyAvailableStandards/MPEG-21_schema_files/dii/dii.xsd\">")
113             .append("urn:hdl:" + itemhandle)
114             .append("</dii:Identifier>")
115             .append("</didl:Statement>")
116             .append("</didl:Descriptor>");
117         metadata.append("<didl:Descriptor>")
118             .append("<didl:Statement mimeType=\"application/xml; charset=utf-8\">");
119                     
120         for (int i = 0; i < allDC.length; i++)
121         {
122             // Do not include description.provenance
123
boolean description = allDC[i].element.equals("description");
124             boolean provenance = allDC[i].qualifier != null &&
125                                  allDC[i].qualifier.equals("provenance");
126
127             if (!(description && provenance))
128             {
129                 // Escape XML chars <, > and &
130
String JavaDoc value = allDC[i].value;
131
132                 // First do &'s - need to be careful not to replace the
133
// & in "&amp;" again!
134
int c = -1;
135                 while ((c = value.indexOf("&", c + 1)) > -1)
136                 {
137                     value = value.substring(0, c) +
138                         "&amp;" +
139                         value.substring(c + 1);
140                 }
141
142                 while ((c = value.indexOf("<")) > -1)
143                 {
144                     value = value.substring(0, c) +
145                         "&lt;" +
146                         value.substring(c + 1);
147                 }
148                 
149                 while ((c = value.indexOf(">")) > -1)
150                 {
151                     value = value.substring(0, c) +
152                         "&gt;" +
153                         value.substring(c + 1);
154                 }
155
156                 metadata1.append("<dc:")
157                     .append(allDC[i].element)
158                     .append(">")
159                     .append(value)
160                     .append("</dc:")
161                     .append(allDC[i].element)
162                     .append(">");
163             }
164         }
165         
166         metadata.append("<oai_dc:dc xmlns:oai_dc=\"http://www.openarchives.org/OAI/2.0/oai_dc/\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd\">");
167                         
168         metadata.append(metadata1);
169         
170         metadata.append("</oai_dc:dc>")
171             .append("</didl:Statement>")
172             .append("</didl:Descriptor>");
173         
174         /**putfirst item here**/
175         
176         
177         //**CYCLE HERE!!!!**//
178

179         try
180         {
181             Bundle[] bundles= item.getBundles("ORIGINAL");
182             
183             if (bundles.length == 0)
184             {
185                 metadata.append("<P>There are no files associated with this item.</P>");
186             }
187             else
188             {
189                 /**cycle bundles**/
190                 for (int i = 0; i < bundles.length; i++)
191                 {
192                     int flag=0;
193                     Bitstream[] bitstreams = bundles[i].getBitstreams();
194                     
195                     /**cycle bitstreams**/
196                     for (int k = 0; k < bitstreams.length ; k++)
197                     {
198                         // Skip internal types
199
if (!bitstreams[k].getFormat().isInternal())
200                         {
201                             if (flag==0)
202                             {
203                                 flag=1;
204                             }
205                             
206                             metadata.append("<didl:Component id=" + "\"uuid-"+ UUIDFactory.generateUUID().toString() + "\">");
207                            
208                            if (bitstreams[k].getSize()> maxsize)
209                            {
210                                metadata.append("<didl:Resource ref=\""+ConfigurationManager.getProperty("dspace.url")+"/bitstream/"+itemhandle+"/"+bitstreams[k].getSequenceID()+"/"+bitstreams[k].getName() );
211                                metadata.append("\" mimeType=\"");
212                                metadata.append(bitstreams[k].getFormat().getMIMEType());
213                                metadata.append("\">");
214                                metadata.append("</didl:Resource>");
215                            }
216                            else
217                            {
218                             
219                                 try
220                                 {
221                                     metadata.append("<didl:Resource mimeType=\"");
222                                     metadata.append(bitstreams[k].getFormat().getMIMEType());
223                                     metadata.append("\" encoding=\"base64\">");
224                                                                        
225                                     /*
226                                      * Assume that size of in-line bitstreams will always be
227                                      * smaller than MAXINT bytes
228                                      */

229                                     int intSize = (int) bitstreams[k].getSize();
230                                     
231                                     byte[] buffer = new byte[intSize];
232                                     
233                                     Context contextl= new Context();
234                                     BufferedInputStream bis=new BufferedInputStream(BitstreamStorageManager.retrieve(contextl,bitstreams[k].getID()));
235                                     int size=bis.read(buffer);
236                                     contextl.complete();
237                                     
238                                     BASE64Encoder encoder=(BASE64Encoder) Class.forName("sun.misc.BASE64Encoder").newInstance();
239                                     String JavaDoc encoding = encoder.encodeBuffer(buffer);
240                                     metadata.append(encoding);
241                                 }
242                                 catch (Exception JavaDoc ex)
243                                 {
244                                     ex.printStackTrace();
245                                     
246                                     metadata.append("<didl:Resource ref=\""+ConfigurationManager.getProperty("dspace.url")+"/bitstream/"+itemhandle+"/"+bitstreams[k].getSequenceID()+"/"+bitstreams[k].getName() );
247                                     metadata.append("\" mimeType=\"");
248                                     metadata.append(bitstreams[k].getFormat().getMIMEType());
249                                     metadata.append("\">");
250                                 }
251
252                                 metadata.append("</didl:Resource>");
253                             }
254                             metadata.append("</didl:Component>");
255                         }
256                         /*end bitstream cycle*/
257                     }
258                     /*end bundle cycle*/
259                 }
260             }
261         }
262         catch (SQLException JavaDoc sqle)
263         {
264             System.err.println("Caught exception:"+sqle.getCause());
265             sqle.printStackTrace();
266         }
267             
268         //**END CYCLE HERE **//
269

270         metadata.append("</didl:Item>")
271                 .append("</didl:DIDL>");
272     
273         return metadata.toString();
274     }
275 }
Popular Tags