View Javadoc

1   /*
2   jMimeMagic(TM) is a Java library for determining the content type of files or
3   streams.
4   
5   Copyright (C) 2004 David Castro
6   
7   This library is free software; you can redistribute it and/or
8   modify it under the terms of the GNU Lesser General Public
9   License as published by the Free Software Foundation; either
10  version 2.1 of the License, or (at your option) any later version.
11  
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  Lesser General Public License for more details.
16  
17  You should have received a copy of the GNU Lesser General Public
18  License along with this library; if not, write to the Free Software
19  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  
21  For more information, please email arimus@users.sourceforge.net
22  */
23  package net.sf.jmimemagic.detectors;
24  
25  import net.sf.jmimemagic.MagicDetector;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.apache.oro.text.perl.Perl5Util;
30  
31  import java.io.File;
32  import java.io.UnsupportedEncodingException;
33  
34  import java.util.Map;
35  
36  
37  /***
38   * DOCUMENT ME!
39   *
40   * @author $Author$
41   * @version $Revision$
42    */
43  public class TextFileDetector implements MagicDetector
44  {
45      private static Log log = LogFactory.getLog(TextFileDetector.class);
46  
47      /***
48       * Creates a new TextFileDetector object.
49       */
50      public TextFileDetector()
51      {
52          super();
53      }
54  
55      /***
56       * DOCUMENT ME!
57       *
58       * @return DOCUMENT ME!
59       */
60      public String getDisplayName()
61      {
62          return "Text File Detector";
63      }
64  
65      /***
66       * DOCUMENT ME!
67       *
68       * @return DOCUMENT ME!
69       */
70      public String getVersion()
71      {
72          return "0.1";
73      }
74  
75      /***
76       * DOCUMENT ME!
77       *
78       * @return DOCUMENT ME!
79       */
80      public String[] getHandledExtensions()
81      {
82          return new String[] { "txt", "text" };
83      }
84  
85      /***
86       * DOCUMENT ME!
87       *
88       * @return DOCUMENT ME!
89       */
90      public String[] getHandledTypes()
91      {
92          return new String[] { "text/plain" };
93      }
94  
95      /***
96       * DOCUMENT ME!
97       *
98       * @return DOCUMENT ME!
99       */
100     public String getName()
101     {
102         return "textfiledetector";
103     }
104 
105     /***
106      * DOCUMENT ME!
107      *
108      * @param data DOCUMENT ME!
109      * @param offset DOCUMENT ME!
110      * @param length DOCUMENT ME!
111      * @param bitmask DOCUMENT ME!
112      * @param comparator DOCUMENT ME!
113      * @param mimeType DOCUMENT ME!
114      * @param params DOCUMENT ME!
115      *
116      * @return DOCUMENT ME!
117      */
118     public String[] process(byte[] data, int offset, int length, long bitmask, char comparator,
119         String mimeType, Map params)
120     {
121         log.debug("processing stream data");
122 
123         Perl5Util util = new Perl5Util();
124 
125         try {
126             String s = new String(data, "UTF-8");
127 
128             if (!util.match("/[^[:ascii:][:space:]]/", s)) {
129                 return new String[] { "text/plain" };
130             }
131 
132             return null;
133         } catch (UnsupportedEncodingException e) {
134             log.error("TextFileDetector: failed to process data");
135 
136             return null;
137         }
138     }
139 
140     /***
141      * DOCUMENT ME!
142      *
143      * @param file DOCUMENT ME!
144      * @param offset DOCUMENT ME!
145      * @param length DOCUMENT ME!
146      * @param bitmask DOCUMENT ME!
147      * @param comparator DOCUMENT ME!
148      * @param mimeType DOCUMENT ME!
149      * @param params DOCUMENT ME!
150      *
151      * @return DOCUMENT ME!
152      */
153     public String[] process(File file, int offset, int length, long bitmask, char comparator,
154         String mimeType, Map params)
155     {
156         log.debug("processing file data");
157 
158         return new String[] {  };
159     }
160 }