1 // FileUtil.java
2 
3 package net.sf.gogui.util;
4 
5 import java.io.BufferedReader;
6 import java.io.File;
7 import java.io.FileReader;
8 import java.io.IOException;
9 import java.net.URI;
10 import java.net.URISyntaxException;
11 import java.util.ArrayList;
12 
13 /** Static file utility functions. */
14 public final class FileUtil
15 {
16     /** Return the file extension of a file name.
17         @param file The file.
18         @return File extension or null if file name has no extension. */
getExtension(File file)19     public static String getExtension(File file)
20     {
21         String ext = null;
22         String s = file.getName();
23         int i = s.lastIndexOf('.');
24         if (i > 0 &&  i < s.length() - 1)
25             ext = s.substring(i + 1);
26         return ext;
27     }
28 
29     /** Returns relative URI between to files.
30         Can be used instead of URI.relativize(), which  does not compute
31         relative URI's correctly, if toFile is not a subdirectory of fromFile
32         (Sun's Java 1.5.0).
33         @todo Handle special charcters and file names containing slashes.
34         @param fromFile File to compute the URI relative to.
35         @param toFile Target file or directory.
36         @return Relative URI. */
getRelativeURI(File fromFile, File toFile)37     public static String getRelativeURI(File fromFile, File toFile)
38     {
39         assert ! fromFile.exists() || ! fromFile.isDirectory();
40         fromFile = fromFile.getAbsoluteFile().getParentFile();
41         assert fromFile != null;
42         ArrayList<String> fromList = splitFile(fromFile);
43         ArrayList<String> toList = splitFile(toFile);
44         int fromSize = fromList.size();
45         int toSize = toList.size();
46         int i = 0;
47         while (i < fromSize && i < toSize
48                && fromList.get(i).equals(toList.get(i)))
49             ++i;
50         StringBuilder result = new StringBuilder();
51         for (int j = i; j < fromSize; ++j)
52             result.append("../");
53         for (int j = i; j < toSize; ++j)
54         {
55             result.append(toList.get(j));
56             if (j < toSize - 1)
57                 result.append('/');
58         }
59         return result.toString();
60     }
61 
62     /** Return URI for file.
63         Replacement for File.toURI() with defined (empty) authority. */
getURI(File file)64     public static URI getURI(File file)
65     {
66         try
67         {
68             return new URI("file", "", file.toURI().getPath(), null, null);
69         }
70         catch (URISyntaxException e)
71         {
72             return null;
73         }
74     }
75 
76     /** Check for extension (case-insensitive). */
hasExtension(File f, String extension)77     public static boolean hasExtension(File f, String extension)
78     {
79         String ext = getExtension(f);
80         if (ext == null)
81             return false;
82         return ext.equalsIgnoreCase(extension);
83     }
84 
85     /** Read a list of strings from a file.
86         The file is expected to contain one string per line; leading and
87         trailing whitespaces are removed. Empty lines or lines beginning
88         with the comment character '#' are ignored. */
readStringListFromFile(File file)89     public static ArrayList<String> readStringListFromFile(File file)
90         throws IOException
91     {
92         ArrayList<String> result = new ArrayList<String>();
93         FileReader reader = new FileReader(file);
94             BufferedReader in = new BufferedReader(reader);
95         try
96         {
97             while (true)
98             {
99                 String line = in.readLine();
100                 if (line == null)
101                     break;
102                 line = line.trim();
103                 if (! line.equals("") && ! line.startsWith("#"))
104                     result.add(line);
105             }
106             return result;
107         }
108         finally
109         {
110             in.close();
111         }
112     }
113 
114     /** Remove extension in file name.
115         If the file does not have the extension oldExtension,
116         the extension will not be removed. */
removeExtension(File file, String oldExtension)117     public static String removeExtension(File file, String oldExtension)
118     {
119         String name = file.toString();
120         if (hasExtension(file, oldExtension))
121         {
122             int index = name.lastIndexOf('.');
123             assert index >= 0;
124             return name.substring(0, index);
125         }
126         return name;
127     }
128 
129     /** Replace extension in file name.
130         If the file does not have the extension oldExtension,
131         the extension will not be replaced but the new extension will be
132         appended. */
replaceExtension(File file, String oldExtension, String newExtension)133     public static String replaceExtension(File file, String oldExtension,
134                                           String newExtension)
135     {
136         String name = file.toString();
137         if (hasExtension(file, oldExtension))
138         {
139             int index = name.lastIndexOf('.');
140             assert index >= 0;
141             return name.substring(0, index) + "." + newExtension;
142         }
143         return name + "." + newExtension;
144     }
145 
replaceExtension(String file, String oldExtension, String newExtension)146     public static String replaceExtension(String file, String oldExtension,
147                                           String newExtension)
148     {
149         return replaceExtension(new File(file), oldExtension, newExtension);
150     }
151 
152     /** Make constructor unavailable; class is for namespace only. */
FileUtil()153     private FileUtil()
154     {
155     }
156 
splitFile(File file)157     private static ArrayList<String> splitFile(File file)
158     {
159         ArrayList<String> list = new ArrayList<String>();
160         file = file.getAbsoluteFile();
161         try
162         {
163             file = file.getCanonicalFile();
164         }
165         catch (IOException e)
166         {
167         }
168         while (file != null)
169         {
170             list.add(0, file.getName());
171             file = file.getParentFile();
172         }
173         return list;
174     }
175 }
176