KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > util > magicfile > MagicFile


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.util.magicfile;
11
12 import java.io.File JavaDoc;
13 import java.io.FileInputStream JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import org.mmbase.util.logging.*;
18
19 /**
20  * Tries to determine the mime-type of a byte array (or a file).
21  *
22  * @author cjr@dds.nl
23  * @author Michiel Meeuwissen
24  * @version $Id: MagicFile.java,v 1.2 2006/07/21 11:32:35 michiel Exp $
25  */

26 public class MagicFile {
27     private static final Logger log = Logging.getLoggerInstance(MagicFile.class);
28
29     public static final String JavaDoc FAILED = "Failed to determine type";
30     // application/octet-stream?
31

32     protected static int BUFSIZE = 4598;
33     // Read a string of maximally this length from the file
34
// Is this garanteed to be big enough?
35

36     private static MagicFile instance;
37
38     protected DetectorProvider detectors;
39
40     /**
41      * Return the current instance of MagicFile. If no instance exists,
42      * one is created.
43      */

44     public static MagicFile getInstance() {
45         if (instance == null) {
46             instance = new MagicFile();
47         }
48         return instance;
49     }
50
51     private MagicFile() {
52         DetectorProvider d = MagicXMLReader.getInstance();
53         // default, read from XML
54
if (d == null) {
55             d = new MagicParser();
56         }
57         detectors = d;
58     }
59
60     /**
61      * Returns a list of detectors used by this MagicFile instance
62      */

63
64     public List JavaDoc getDetectors() {
65         return detectors.getDetectors();
66     }
67
68     /*
69      * @deprecated use getMimeType(File)
70      */

71     protected String JavaDoc test(String JavaDoc path) {
72         try {
73             return getMimeType(new File JavaDoc(path));
74         } catch (IOException JavaDoc e) {
75             return "File not found " + path;
76         }
77     }
78     /**
79      * @param file Location of file to be checked
80      * @return Type of the file as determined by the magic file
81      */

82     protected String JavaDoc getMimeType(File JavaDoc file) throws IOException JavaDoc {
83         byte[] lithmus = new byte[BUFSIZE];
84         //log.debug("path = "+path);
85
FileInputStream JavaDoc fir = new FileInputStream JavaDoc(file);
86         int res = fir.read(lithmus, 0, BUFSIZE);
87         log.debug("read " + res + " bytes from " + file.getAbsolutePath());
88         return getMimeType(lithmus);
89     }
90
91     /**
92      * Tests the byte[] array for the mime type.
93      *
94      * @return The found mime-type or FAILED
95      */

96     public String JavaDoc getMimeType(byte[] input) {
97         byte[] lithmus;
98
99         if (input.length > BUFSIZE) {
100             lithmus = new byte[BUFSIZE];
101             System.arraycopy(input, 0, lithmus, 0, BUFSIZE);
102             log.debug("getMimeType was called with big bytearray cutting to " + BUFSIZE + " bytes");
103         } else {
104             lithmus = input;
105         }
106
107         List JavaDoc list = getDetectors();
108         if (list == null) {
109             log.warn("No detectors found");
110             return FAILED;
111         }
112         Iterator JavaDoc i = list.iterator();
113         while (i.hasNext()) {
114             Detector detector = (Detector)i.next();
115             log.debug("Trying " + detector.getMimeType());
116             if (detector != null && detector.test(lithmus)) {
117                 //return detector.getDesignation();
118
return detector.getMimeType();
119             }
120         }
121         return FAILED;
122     }
123
124     /**
125      * @javadoc
126      */

127     public String JavaDoc extensionToMimeType(String JavaDoc extension) {
128         Iterator JavaDoc i = getDetectors().iterator();
129         while (i.hasNext()) {
130             Detector detector = (Detector)i.next();
131             Iterator JavaDoc j = detector.getExtensions().iterator();
132             while (j.hasNext()) {
133                 String JavaDoc ex = (String JavaDoc)j.next();
134                 if (ex.equalsIgnoreCase(extension)) {
135                     return detector.getMimeType();
136                 }
137             }
138         }
139         return FAILED;
140     }
141
142     /**
143      * Given a mime-type string, this function tries to create a common extension for it.
144      * @return An extension (without the dot), or an empty string if the mime-type is unknown.
145      * @since MMBase-1.7.1
146      */

147     public String JavaDoc mimeTypeToExtension(String JavaDoc mimeType) {
148         Iterator JavaDoc i = getDetectors().iterator();
149         while (i.hasNext()) {
150             Detector detector = (Detector)i.next();
151             if (mimeType.equalsIgnoreCase(detector.getMimeType())) {
152                 Iterator JavaDoc j = detector.getExtensions().iterator();
153                 if (j.hasNext()) {
154                     String JavaDoc ex = (String JavaDoc)j.next();
155                     return ex;
156                 }
157             }
158         }
159         return "";
160     }
161
162     /**
163      * @javadoc
164      */

165     public String JavaDoc getMimeType(byte[] data, String JavaDoc extension) {
166         String JavaDoc result;
167         result = getMimeType(data);
168         if (result.equals(FAILED)) {
169             result = extensionToMimeType(extension);
170         }
171         return result;
172     }
173
174     /**
175      * e.g.: java -Dmmbase.config=/home/mmbase/mmbase-app/WEB-INF/config org.mmbase.util.MagicFile test.doc
176      * @javadoc
177      */

178     public static void main(String JavaDoc[] argv) {
179         MagicFile magicFile = MagicFile.getInstance();
180
181         if (argv.length == 1) {
182             try {
183                 // one argument possible: a file name. Return the mime-type
184
log.info(magicFile.getMimeType(new File JavaDoc(argv[0])));
185             } catch (IOException JavaDoc e) {
186                 log.info(argv[0] + " cannot be opened or read: " + e.toString());
187             }
188         } else {
189             // show the known Detectors;
190
Iterator JavaDoc i = magicFile.getDetectors().iterator();
191             while (i.hasNext()) {
192                 Detector d = (Detector)i.next();
193                 log.info(d.toString());
194             }
195         }
196     }
197 }
198
Popular Tags