KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > jdic > filetypes > internal > AppUtility


1 /*
2  * Copyright (C) 2004 Sun Microsystems, Inc. All rights reserved. Use is
3  * subject to license terms.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the Lesser GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  */

20
21 package org.jdesktop.jdic.filetypes.internal;
22
23 import java.net.URL JavaDoc;
24
25 /**
26  * Utility class containing shared methods.
27  */

28 public class AppUtility {
29
30     /**
31      * Suppress default constructor for noninstantiability.
32      */

33     private AppUtility() {}
34     
35     /**
36      * Returns the file extension from the file part of the URL.
37      * The returned file extension include the leading '.' character.
38      * <P>
39      * For example: if the URL is http://www.sun.com/index.html, the
40      * returned file extension is ".html".
41      *
42      * @param url the specified URL
43      * @return the file extension of the file part of the URL.
44      */

45     public static String JavaDoc getFileExtensionByURL(URL JavaDoc url) {
46         String JavaDoc trimFile = url.getFile().trim();
47
48         if (trimFile == null || trimFile == "" || trimFile == "/") {
49             return null;
50         }
51          
52         int strIndex = trimFile.lastIndexOf("/");
53         String JavaDoc filePart = trimFile.substring(strIndex + 1, trimFile.length());
54
55         strIndex = filePart.lastIndexOf(".");
56         if (strIndex == -1 || strIndex == filePart.length() - 1) {
57             return null;
58         } else {
59             String JavaDoc fileExt = filePart.substring(strIndex, filePart.length());
60
61             return fileExt;
62         }
63     }
64
65     /**
66      * Removes the leading '.' character from the specified file extension.
67      *
68      * @param fileExt the specified file extension.
69      * @return file extension without a leading '.' character.
70      */

71     public static String JavaDoc removeDotFromFileExtension(String JavaDoc fileExt) {
72         String JavaDoc temFileExt = fileExt;
73         if (fileExt.charAt(0) == '.') {
74             temFileExt = fileExt.substring(1, fileExt.length());
75         }
76         
77         return temFileExt;
78     }
79
80     /**
81      * Adds one leading '.' character for the specified file extension.
82      * If the leading '.' character already exists, it just returns.
83      *
84      * @param fileExt the specified file extension.
85      * @return file extension with a leading '.' character.
86      */

87     public static String JavaDoc addDotToFileExtension(String JavaDoc fileExt) {
88         String JavaDoc temFileExt = fileExt;
89         if (fileExt.charAt(0) != '.') {
90             String JavaDoc dotStr = ".";
91             temFileExt = dotStr.concat(fileExt);
92         }
93        
94         return temFileExt;
95     }
96 }
97
98     
99
Popular Tags