KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > content > FormatIdentifier


1 /*
2  * FormatIdentifier.java
3  *
4  * Version: $Revision: 1.9 $
5  *
6  * Date: $Date: 2006/05/26 14:16:33 $
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.content;
41
42 import java.sql.SQLException JavaDoc;
43
44 import org.dspace.core.Context;
45 import org.dspace.storage.rdbms.DatabaseManager;
46 import org.dspace.storage.rdbms.TableRowIterator;
47
48 /**
49  * This class handles the recognition of bitstream formats, using the format
50  * registry in the database. For the moment, the format identifier simply uses
51  * file extensions stored in the "BitstreamFormatIdentifier" table. This
52  * probably isn't a particularly satisfactory long-term solution.
53  *
54  * @author Robert Tansley
55  * @version $Revision: 1.9 $
56  */

57 public class FormatIdentifier
58 {
59     /**
60      * Attempt to identify the format of a particular bitstream. If the format
61      * is unknown, null is returned.
62      *
63      * @param bitstream
64      * the bitstream to identify the format of
65      *
66      * @return a format from the bitstream format registry, or null
67      */

68     public static BitstreamFormat guessFormat(Context context,
69             Bitstream bitstream) throws SQLException JavaDoc
70     {
71         // FIXME: Just setting format to first guess
72
// For now just get the file name
73
String JavaDoc filename = bitstream.getName().toLowerCase();
74
75         // Gracefully handle the null case
76
if (filename == null)
77         {
78             return null;
79         }
80
81         // This isn't rocket science. We just get the name of the
82
// bitstream, get the extension, and see if we know the type.
83
String JavaDoc extension = filename;
84         int lastDot = filename.lastIndexOf('.');
85
86         if (lastDot != -1)
87         {
88             extension = filename.substring(lastDot + 1);
89         }
90
91         // If the last character was a dot, then extension will now be
92
// an empty string. If this is the case, we don't know what
93
// file type it is.
94
if (extension.equals(""))
95         {
96             return null;
97         }
98
99         // See if the extension is in the fileextension table
100
TableRowIterator tri = DatabaseManager.query(context,
101                 "SELECT bitstreamformatregistry.* FROM bitstreamformatregistry, " +
102                 "fileextension WHERE fileextension.extension LIKE ? " +
103                 "AND bitstreamformatregistry.bitstream_format_id=" +
104                 "fileextension.bitstream_format_id",
105                 extension);
106
107         BitstreamFormat retFormat = null;
108         if (tri.hasNext())
109         {
110             // Return first match
111
retFormat = new BitstreamFormat(context, tri.next());
112         }
113         else
114         {
115             retFormat = null;
116         }
117         // close the TableRowIterator to free up resources
118
tri.close();
119         return retFormat;
120     }
121 }
122
Popular Tags