KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > app > mediafilter > MediaFilter


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

40 package org.dspace.app.mediafilter;
41
42 import java.io.InputStream JavaDoc;
43
44 import org.dspace.content.Bitstream;
45 import org.dspace.content.BitstreamFormat;
46 import org.dspace.content.Bundle;
47 import org.dspace.content.Item;
48 import org.dspace.core.Context;
49
50 public abstract class MediaFilter
51 {
52     protected Item item = null;
53     
54     /* To create your own filter, implement the following virtual methods */
55
56     /**
57      * Get a filename for a newly created filtered bitstream
58      *
59      * @param sourceName
60      * name of source bitstream
61      * @return filename generated by the filter - for example, document.pdf
62      * becomes document.pdf.txt
63      */

64     public abstract String JavaDoc getFilteredName(String JavaDoc sourceName);
65
66     /**
67      * @return name of the bundle this filter will stick its generated
68      * Bitstreams
69      */

70     public abstract String JavaDoc getBundleName();
71
72     /**
73      * @return name of the bitstream format (say "HTML" or "Microsoft Word")
74      * returned by this filter look in the bitstream format registry or
75      * mediafilter.cfg for valid format strings.
76      */

77     public abstract String JavaDoc getFormatString();
78
79     /**
80      * @return string to describe the newly-generated Bitstream's - how it was
81      * produced is a good idea
82      */

83     public abstract String JavaDoc getDescription();
84
85     /**
86      * @param source
87      * input stream
88      *
89      * @return result of filter's transformation, written out to a bitstream
90      */

91     public abstract InputStream JavaDoc getDestinationStream(InputStream JavaDoc source)
92             throws Exception JavaDoc;
93
94     /* end of methods you need to implement! */
95
96     /**
97      * processBitstream is a utility class that calls the above virtual methods -
98      * it is unlikely that you will need to override it. It scans the bitstreams
99      * in an item, and decides if a bitstream has already been filtered, and if
100      * not or if overWrite is set, invokes the filter.
101      *
102      * @param c
103      * context
104      * @param item
105      * item containing bitstream to process
106      * @param source
107      * source bitstream to process
108      *
109      * @return true if new rendition is created, false if rendition already
110      * exists and overWrite is not set
111      */

112     public boolean processBitstream(Context c, Item item, Bitstream source)
113             throws Exception JavaDoc
114     {
115         boolean overWrite = MediaFilterManager.isForce;
116         
117         this.item = item;
118
119         // get bitstream filename, calculate destination filename
120
String JavaDoc newName = getFilteredName(source.getName());
121
122         Bitstream existingBitstream = null; // is there an existing rendition?
123
Bundle targetBundle = null; // bundle we're modifying
124

125         Bundle[] bundles = item.getBundles(getBundleName());
126
127         // check if destination bitstream exists
128
if (bundles.length > 0)
129         {
130             // only finds the last match (FIXME?)
131
for (int i = 0; i < bundles.length; i++)
132             {
133                 Bitstream[] bitstreams = bundles[i].getBitstreams();
134
135                 for (int j = 0; j < bitstreams.length; j++)
136                 {
137                     if (bitstreams[j].getName().equals(newName))
138                     {
139                         targetBundle = bundles[i];
140                         existingBitstream = bitstreams[j];
141                     }
142                 }
143             }
144         }
145
146         // if exists and overwrite = false, exit
147
if (!overWrite && (existingBitstream != null))
148         {
149             System.out.println("SKIPPED: bitstream " + source.getID()
150                     + " because '" + newName + "' already exists");
151
152             return false;
153         }
154
155         InputStream JavaDoc destStream = getDestinationStream(source.retrieve());
156
157         // create new bundle if needed
158
if (bundles.length < 1)
159         {
160             targetBundle = item.createBundle(getBundleName());
161         }
162         else
163         {
164             // take the first match
165
targetBundle = bundles[0];
166         }
167
168         Bitstream b = targetBundle.createBitstream(destStream);
169
170         // Now set the format and name of the bitstream
171
b.setName(newName);
172         b.setSource("Written by MediaFilter " + this.getClass().getName()); // or
173
// obj.getClass().getName();
174
b.setDescription(getDescription());
175
176         // Find the proper format
177
BitstreamFormat bf = BitstreamFormat.findByShortDescription(c,
178                 getFormatString());
179         b.setFormat(bf);
180         b.update();
181
182         // fixme - set date?
183
// we are overwriting, so remove old bitstream
184
if (existingBitstream != null)
185         {
186             targetBundle.removeBitstream(existingBitstream);
187         }
188
189         System.out.println("FILTERED: bitstream " + source.getID()
190                 + " and created '" + newName + "'");
191
192         return true;
193     }
194 }
195
Popular Tags