001: /*
002: * FormatIdentifier.java
003: *
004: * Version: $Revision: 1523 $
005: *
006: * Date: $Date: 2006-05-26 09:23:17 -0500 (Fri, 26 May 2006) $
007: *
008: * Copyright (c) 2002-2005, Hewlett-Packard Company and Massachusetts
009: * Institute of Technology. All rights reserved.
010: *
011: * Redistribution and use in source and binary forms, with or without
012: * modification, are permitted provided that the following conditions are
013: * met:
014: *
015: * - Redistributions of source code must retain the above copyright
016: * notice, this list of conditions and the following disclaimer.
017: *
018: * - Redistributions in binary form must reproduce the above copyright
019: * notice, this list of conditions and the following disclaimer in the
020: * documentation and/or other materials provided with the distribution.
021: *
022: * - Neither the name of the Hewlett-Packard Company nor the name of the
023: * Massachusetts Institute of Technology nor the names of their
024: * contributors may be used to endorse or promote products derived from
025: * this software without specific prior written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
030: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
032: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
033: * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
034: * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
035: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
036: * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
037: * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
038: * DAMAGE.
039: */
040: package org.dspace.content;
041:
042: import java.sql.SQLException;
043:
044: import org.dspace.core.Context;
045: import org.dspace.storage.rdbms.DatabaseManager;
046: import org.dspace.storage.rdbms.TableRowIterator;
047:
048: /**
049: * This class handles the recognition of bitstream formats, using the format
050: * registry in the database. For the moment, the format identifier simply uses
051: * file extensions stored in the "BitstreamFormatIdentifier" table. This
052: * probably isn't a particularly satisfactory long-term solution.
053: *
054: * @author Robert Tansley
055: * @version $Revision: 1523 $
056: */
057: public class FormatIdentifier {
058: /**
059: * Attempt to identify the format of a particular bitstream. If the format
060: * is unknown, null is returned.
061: *
062: * @param bitstream
063: * the bitstream to identify the format of
064: *
065: * @return a format from the bitstream format registry, or null
066: */
067: public static BitstreamFormat guessFormat(Context context,
068: Bitstream bitstream) throws SQLException {
069: // FIXME: Just setting format to first guess
070: // For now just get the file name
071: String filename = bitstream.getName().toLowerCase();
072:
073: // Gracefully handle the null case
074: if (filename == null) {
075: return null;
076: }
077:
078: // This isn't rocket science. We just get the name of the
079: // bitstream, get the extension, and see if we know the type.
080: String extension = filename;
081: int lastDot = filename.lastIndexOf('.');
082:
083: if (lastDot != -1) {
084: extension = filename.substring(lastDot + 1);
085: }
086:
087: // If the last character was a dot, then extension will now be
088: // an empty string. If this is the case, we don't know what
089: // file type it is.
090: if (extension.equals("")) {
091: return null;
092: }
093:
094: // See if the extension is in the fileextension table
095: TableRowIterator tri = DatabaseManager
096: .query(
097: context,
098: "SELECT bitstreamformatregistry.* FROM bitstreamformatregistry, "
099: + "fileextension WHERE fileextension.extension LIKE ? "
100: + "AND bitstreamformatregistry.bitstream_format_id="
101: + "fileextension.bitstream_format_id",
102: extension);
103:
104: BitstreamFormat retFormat = null;
105: if (tri.hasNext()) {
106: // Return first match
107: retFormat = new BitstreamFormat(context, tri.next());
108: } else {
109: retFormat = null;
110: }
111: // close the TableRowIterator to free up resources
112: tri.close();
113: return retFormat;
114: }
115: }
|