KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > applet > upload > FileUploadUtils


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src-components/org/opencms/applet/upload/FileUploadUtils.java,v $
3  * Date : $Date: 2006/10/17 13:33:11 $
4  * Version: $Revision: 1.10 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.applet.upload;
33
34 import java.io.File JavaDoc;
35
36 /**
37  * Util class for the FileUpload applet, collects various util methods.<p>
38  *
39  * Based on the Java 1.4 example.
40  *
41  * @author Michael Emmerich
42  *
43  * @version $Revision: 1.10 $
44  *
45  * @since 6.0.0
46  */

47 public final class FileUploadUtils {
48
49     /**
50      * Empty Constructor.<p>
51      */

52     private FileUploadUtils() {
53
54         // noop
55
}
56
57     /**
58      * Gets the extension of a file.<p>
59      *
60      * @param f The file to get the extension from
61      * @return The file extension
62      */

63     public static String JavaDoc getExtension(File JavaDoc f) {
64
65         String JavaDoc ext = null;
66         if (f != null) {
67             String JavaDoc s = f.getName();
68             int i = s.lastIndexOf('.');
69             if ((i > 0) && (i < s.length() - 1)) {
70                 ext = s.substring(i).toLowerCase();
71             }
72         }
73         return ext;
74     }
75
76     /**
77      * Returns <code>true</code> in case the given extension is one of the known image extensions.<p>
78      *
79      * Known extensions are: <code>.gif, .tiff, .jpeg, .jpg, .bmp, .png, .pnm, .pgm, .ppm, .pbm</code>.<p>
80      *
81      * @param extension the extension to check, must start with a dot '.'
82      * @return <code>true</code> in case the given extension is one of the known image extensions
83      */

84     public static boolean isImageExtension(String JavaDoc extension) {
85
86         if (extension != null) {
87             return (extension.equals(".gif")
88                 || extension.equals(".tiff")
89                 || extension.equals(".tif")
90                 || extension.equals(".jpeg")
91                 || extension.equals(".jpg")
92                 || extension.equals(".bmp")
93                 || extension.equals(".pnm")
94                 || extension.equals(".pbm")
95                 || extension.equals(".pgm")
96                 || extension.equals(".ppm") || extension.equals(".png"));
97         } else {
98             return false;
99         }
100     }
101
102     /**
103      * Returns <code>true</code> in case the given extension is one of the known text file extensions.<p>
104      *
105      * Known extensions are: <code>.txt, .ini, .bat, .cmd, .sh, .java, .log, .xml, .html, .sys</code>.<p>
106      *
107      * @param extension the extension to check, must start with a dot '.'
108      * @return <code>true</code> in case the given extension is one of the known text file extensions
109      */

110     public static boolean isTextExtension(String JavaDoc extension) {
111
112         if (extension != null) {
113             return (extension.equals(".txt")
114                 || extension.equals(".ini")
115                 || extension.equals(".bat")
116                 || extension.equals(".cmd")
117                 || extension.equals(".sh")
118                 || extension.equals(".java")
119                 || extension.equals(".log")
120                 || extension.equals(".xml")
121                 || extension.equals(".html") || extension.equals(".sys"));
122         } else {
123             return false;
124         }
125     }
126
127     /**
128      * Returns <code>true</code> in case the given extension is one of the known office file extensions.<p>
129      *
130      * Known extensions are: <code>.pdf, .odt, .ods, .odp, .odg, .doc, .xls, .ppt, .vsd</code>.<p>
131      *
132      * @param extension the extension to check, must start with a dot '.'
133      * @return <code>true</code> in case the given extension is one of the known office file extensions
134      */

135     public static boolean isOfficeExtension(String JavaDoc extension) {
136
137         if (extension != null) {
138             return (extension.equals(".odt")
139                 || extension.equals(".ods")
140                 || extension.equals(".odp")
141                 || extension.equals(".odg")
142                 || extension.equals(".pdf")
143                 || extension.equals(".doc")
144                 || extension.equals(".xls")
145                 || extension.equals(".vsd") || extension.equals(".ppt"));
146         } else {
147             return false;
148         }
149     }
150 }
Popular Tags