KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > resource > Utils


1 package org.sapia.resource;
2
3 import java.io.BufferedReader JavaDoc;
4 import java.io.File JavaDoc;
5 import java.io.IOException JavaDoc;
6 import java.io.InputStream JavaDoc;
7 import java.io.InputStreamReader JavaDoc;
8 import java.io.StringWriter JavaDoc;
9 import java.net.URI JavaDoc;
10 import java.net.URISyntaxException JavaDoc;
11 import java.util.ArrayList JavaDoc;
12 import java.util.List JavaDoc;
13
14 /**
15  * This class holds various utility methods.
16  *
17  * @author Yanick Duchesne
18  * <dl>
19  * <dt><b>Copyright: </b>
20  * <dd>Copyright &#169; 2002-2007 <a
21  * HREF="http://www.sapia-oss.org">Sapia Open Source Software </a>. All
22  * Rights Reserved.</dd>
23  * </dt>
24  * <dt><b>License: </b>
25  * <dd>Read the license.txt file of the jar or visit the <a
26  * HREF="http://www.sapia-oss.org/license.html">license page </a> at the
27  * Sapia OSS web site</dd>
28  * </dt>
29  * </dl>
30  */

31 public class Utils {
32   /**
33    * Splits the given string into parts delimited by the given "split"
34    * character.
35    *
36    * @param toSplit
37    * the <code>String</code> to split.
38    * @param splitChar
39    * the delimiting character.
40    * @param trim
41    * if <code>true</code>, the tokens resulting from the split will
42    * be trimed.
43    * @return an array of <code>String</code> s corresponding to the tokens
44    * that resulted from the split.
45    */

46   public static String JavaDoc[] split(String JavaDoc toSplit, char splitChar, boolean trim) {
47     List JavaDoc tokens = new ArrayList JavaDoc();
48
49     StringBuffer JavaDoc token = new StringBuffer JavaDoc();
50
51     for(int i = 0; i < toSplit.length(); i++) {
52       if(toSplit.charAt(i) == splitChar) {
53         if(trim) {
54           tokens.add(token.toString().trim());
55         } else {
56           tokens.add(token.toString());
57         }
58
59         token.delete(0, token.length());
60       } else {
61         token.append(toSplit.charAt(i));
62       }
63     }
64
65     if(token.length() > 0) {
66       if(trim) {
67         tokens.add(token.toString().trim());
68       } else {
69         tokens.add(token.toString());
70       }
71     }
72
73     return (String JavaDoc[]) tokens.toArray(new String JavaDoc[tokens.size()]);
74   }
75
76   /**
77    * Tests if the given path has a protocol/scheme.
78    *
79    * @param path
80    * the path on which to perform the test.
81    * @return <code>true</code> if path has a scheme.
82    */

83   public static boolean hasScheme(String JavaDoc path) {
84     if(path == null) {
85       return false;
86     }
87
88     return path.indexOf(":") >= 0;
89   }
90
91   /**
92    * Chops the scheme/protocol from the given URL path and returns the path
93    * without the scheme.
94    *
95    * @param path
96    * a URL path.
97    * @return the path without the scheme, or the given path, if it has no
98    * scheme.
99    */

100   public static String JavaDoc chopScheme(String JavaDoc path) {
101     int idx = path.indexOf(":");
102
103     if(idx >= 0) {
104       String JavaDoc toReturn = path.substring(idx + 1);
105       if(toReturn.startsWith("//")){
106         toReturn = toReturn.substring(1);
107       }
108       return toReturn;
109     }
110
111     return path;
112   }
113
114   /**
115    * Returns the scheme of the given URL path.
116    *
117    * @param path
118    * a URL path.
119    * @return the scheme of the given URL path, or <code>null</code> if URL is
120    * not valid (i.e.: does not have a scheme/protocol).
121    */

122   public static String JavaDoc getScheme(String JavaDoc path) {
123     int idx = path.indexOf(":");
124
125     if(idx >= 0) {
126       return path.substring(0, idx);
127     } else {
128       return null;
129     }
130   }
131
132   /**
133    * Returns the path corresponding to the given base path, and the given
134    * relative path - results in "concatenation" of both, sort of.
135    *
136    * <pre>
137    *
138    * // will print: /opt/some/path/relative/path
139    * System.out.println(&quot;/opt/some/path&quot;, &quot;relative/path&quot;, false);
140    *
141    * // will print: /opt/some/relative/path
142    * System.out.println(&quot;/opt/some/file.txt&quot;, &quot;relative/path&quot;, true);
143    * </pre>
144    *
145    * @param base
146    * a base path.
147    * @param relative
148    * the path to evaluate relatively to the given path.
149    * @param isBaseFile
150    * if <code>true</code>, indicates that the base path corresponds
151    * to a file.
152    * @return the evaluated path.
153    */

154   public static String JavaDoc getRelativePath(String JavaDoc base, String JavaDoc relative,
155       boolean isBaseFile) {
156     String JavaDoc toReturn;
157     String JavaDoc compared = base.replace('\\', '/');
158     relative = Utils.chopScheme(relative);
159     if(isBaseFile) {
160       int idx;
161
162       if((idx = compared.lastIndexOf('/')) >= 0) {
163         toReturn = base.substring(0, idx) + File.separator + relative;
164       } else {
165         toReturn = base + File.separator + relative;
166       }
167     } else {
168       if(compared.endsWith("//")) {
169         toReturn = base + relative;
170       } else {
171         toReturn = base + File.separator + relative;
172       }
173     }
174
175     return toReturn;
176   }
177   
178   public static URI JavaDoc toURIObject(String JavaDoc uri) throws IOException JavaDoc{
179     try{
180       return new URI JavaDoc(uri);
181     }catch(URISyntaxException JavaDoc e){
182       throw new IOException JavaDoc("Could not parse uri: " + uri + " - " + e.getMessage());
183     }
184   }
185   
186   public static boolean isAbsolute(URI JavaDoc uri){
187     return (uri.isAbsolute() && (uri.getSchemeSpecificPart().charAt(0) == '/' ||
188         uri.getSchemeSpecificPart().charAt(0) == '\\'));
189   }
190   
191   /**
192    * Returns the data of the given text stream as a string.
193    * <p>
194    * IMPORTANT: the input stream is not closed by this method.
195    *
196    * @param is
197    * an <code>InputStream</code> of character data.
198    * @return the data of the given test stream.
199    * @throws IOException
200    * if a problem occurs.
201    */

202   public static String JavaDoc textStreamToString(InputStream JavaDoc is) throws IOException JavaDoc {
203     BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(is), 1024);
204     String JavaDoc line;
205     StringWriter JavaDoc writer = new StringWriter JavaDoc();
206
207     while((line = reader.readLine()) != null) {
208       writer.write(line);
209       writer.write(System.getProperty("line.separator"));
210     }
211
212     return writer.getBuffer().toString();
213   }
214 }
215
Popular Tags