Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: net/jxta/ext/config/Conversion.java


1   /*
2    *  Copyright (c) 2001 Sun Microsystems, Inc.  All rights
3    *  reserved.
4    *
5    *  Redistribution and use in source and binary forms, with or without
6    *  modification, are permitted provided that the following conditions
7    *  are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright
10   *  notice, this list of conditions and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright
13   *  notice, this list of conditions and thproe following disclaimer in
14   *  the documentation and/or other materials provided with the
15   *  distribution.
16   *
17   *  3. The end-user documentation included with the redistribution,
18   *  if any, must include the following acknowledgment:
19   *  "This product includes software developed by the
20   *  Sun Microsystems, Inc. for Project JXTA."
21   *  Alternately, this acknowledgment may appear in the software itself,
22   *  if and wherever such third-party acknowledgments normally appear.
23   *
24   *  4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA"
25   *  must not be used to endorse or promote products derived from this
26   *  software without prior written permission. For written
27   *  permission, please contact Project JXTA at http://www.jxta.org.
28   *
29   *  5. Products derived from this software may not be called "JXTA",
30   *  nor may "JXTA" appear in their name, without prior written
31   *  permission of Sun.
32   *
33   *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
34   *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35   *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36   *  DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
37   *  ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38   *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39   *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
40   *  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
41   *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
42   *  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
43   *  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44   *  SUCH DAMAGE.
45   *  ====================================================================
46   *
47   *  This software consists of voluntary contributions made by many
48   *  individuals on behalf of Project JXTA.  For more
49   *  information on Project JXTA, please see
50   *  <http://www.jxta.org/>.
51   *
52   *  This license is based on the BSD license adopted by the Apache Foundation.
53   *
54   *  $Id: Conversion.java,v 1.12 2004/07/31 07:44:33 gonzo Exp $
55   */
56  package net.jxta.ext.config;
57  
58  import java.awt.Color;
59  import java.io.File;
60  import java.io.UnsupportedEncodingException;
61  import java.net.MalformedURLException;
62  import java.net.URI;
63  import java.net.URISyntaxException;
64  import java.net.URL;
65  import java.net.URLDecoder;
66  import java.net.URLEncoder;
67  import java.util.ArrayList;
68  import java.util.Iterator;
69  import java.util.List;
70  
71  /**
72   * @author     james todd [gonzo at jxta dot org]
73   * @version    $Id: Conversion.java,v 1.12 2004/07/31 07:44:33 gonzo Exp $
74   */
75  
76  public class Conversion {
77  
78      private final static String BOGUS = "bogus";
79      private final static char SLASH = '/';
80      private final static char PERCENT = '%';
81      private final static char COLON = ':';
82      private final static char[] ESCAPE = { SLASH, COLON, '?', '#', '@', PERCENT };
83      private final static String ENCODING_UTF8 = "UTF-8";
84      private final static String URI_FILE = "file" + COLON + SLASH;
85  
86      /**
87       *  Description of the Method
88       *
89       * @param  s                        Description of the Parameter
90       * @return                          Description of the Return Value
91       * @exception  ConversionException  Description of the Exception
92       */
93      public static char toChar(String s)
94      throws ConversionException {
95          char c = 0;
96  
97          try {
98              c = s != null && s.length() >= 1 ?
99                  s.charAt(0) : BOGUS.charAt(0);
100         } catch (IndexOutOfBoundsException ioobe) {
101             throw new ConversionException(ioobe);
102         }
103 
104         return c;
105     }
106 
107 
108     /**
109      *  Description of the Method
110      *
111      * @param  s                        Description of the Parameter
112      * @return                          Description of the Return Value
113      * @exception  ConversionException  Description of the Exception
114      */
115     public static int toInt(String s)
116     throws ConversionException {
117         int i = -1;
118 
119         try {
120             i = Integer.parseInt(s != null ? s : BOGUS);
121         } catch (NumberFormatException nfe) {
122             throw new ConversionException(nfe);
123         }
124 
125         return i;
126     }
127 
128     /**
129      *  Description of the Method
130      *
131      * @param  s                        Description of the Parameter
132      * @return                          Description of the Return Value
133      * @exception  ConversionException  Description of the Exception
134      */
135     public static long toLong(String s)
136     throws ConversionException {
137         long i = -1;
138 
139         try {
140             i = Long.parseLong(s != null ? s : BOGUS);
141         } catch (NumberFormatException nfe) {
142             throw new ConversionException(nfe);
143         }
144 
145         return i;
146     }
147 
148     /**
149      *  Description of the Method
150      *
151      * @param  s                        Description of the Parameter
152      * @return                          Description of the Return Value
153      * @exception  ConversionException  Description of the Exception
154      */
155     public static float toFloat(String s)
156     throws ConversionException {
157         float f = -1;
158 
159         try {
160             f = Float.parseFloat(s != null ? s : BOGUS);
161         } catch (NumberFormatException nfe) {
162             throw new ConversionException(nfe);
163         }
164 
165         return f;
166     }
167 
168 
169     /**
170      *  Description of the Method
171      *
172      * @param  s  Description of the Parameter
173      * @return    Description of the Return Value
174      */
175     public static boolean toBoolean(String s) {
176         return Boolean.valueOf(s).booleanValue();
177     }
178 
179 
180     /**
181      *  Description of the Method
182      *
183      * @param  s                        Description of the Parameter
184      * @return                          Description of the Return Value
185      * @exception  ConversionException  Description of the Exception
186      */
187     public static URL toURL(String s)
188     throws ConversionException {
189         URL u = null;
190 
191         try {
192             u = new URL(s);
193         } catch (MalformedURLException mue) {
194             throw new ConversionException(mue);
195         }
196 
197         return u;
198     }
199 
200 
201     /**
202      *  Description of the Method
203      *
204      * @param  f                        Description of the Parameter
205      * @return                          Description of the Return Value
206      * @exception  ConversionException  Description of the Exception
207      */
208     public static URL toURL(File f)
209     throws ConversionException {
210         String protocol = "file";
211         String delimiter = ":";
212         String prefix = "./";
213         String filePrefix = System.getProperty("file.separator", "/");
214         String s = f.toString();
215 
216         if (s.startsWith(filePrefix)) {
217             s = prefix + s;
218         }
219 
220         URL u = null;
221 
222         try {
223             u = new URL(protocol + delimiter + s);
224         } catch (MalformedURLException mue) {
225             throw new ConversionException(mue);
226         }
227 
228         return u;
229     }
230 
231 
232     /**
233      *  Description of the Method
234      *
235      * @param  l                        Description of the Parameter
236      * @return                          Description of the Return Value
237      * @exception  ConversionException  Description of the Exception
238      */
239     public static List toURLs(List l)
240     throws ConversionException {
241         List urls = new ArrayList();
242 
243         try {
244             for (Iterator i = l.iterator(); i.hasNext(); ) {
245                 urls.add(new URL((String)i.next()));
246             }
247         } catch (MalformedURLException mue) {
248             throw new ConversionException(mue);
249         }
250 
251         return urls;
252     }
253 
254     /**
255      *  Description of the Method
256      *
257      * @param  s                        Description of the Parameter
258      * @return                          Description of the Return Value
259      * @exception  ConversionException  Description of the Exception
260      */
261     public static URI toURI(String s)
262     throws ConversionException {
263         s = s != null ? s.trim() : s;
264 
265         URI u = null;
266 
267         if (s != null &&
268             s.length() > 0) {
269             s = s.replace(File.separatorChar, SLASH);
270 
271             try {
272                 s = URLEncoder.encode(s, ENCODING_UTF8);
273             } catch (UnsupportedEncodingException usee) {}
274             
275             for (int i = 0; i < ESCAPE.length; i++) {
276                    s = s.replaceAll(PERCENT +
277                                     Integer.toString(ESCAPE[i], 16).toUpperCase(),
278                                     String.valueOf(ESCAPE[i]));
279             }
280 
281             if (s.toLowerCase().startsWith(URI_FILE) &&
282                 s.length() > URI_FILE.length() + 1) {
283                 char c = s.charAt(URI_FILE.length());
284                 char d = s.charAt(URI_FILE.length() + 1);
285 
286                 if (c == SLASH &&
287                     d != SLASH) {
288                     s = s.replaceFirst(URI_FILE, URI_FILE + SLASH);
289                 }
290             }
291 
292             try {
293                 u = new URI(s);
294             } catch (URISyntaxException use) {
295                 throw new ConversionException(use);
296             }
297         }
298 
299         return u;
300     }
301 
302     /**
303      *  Description of the Method
304      *
305      * @param  l                        Description of the Parameter
306      * @return                          Description of the Return Value
307      * @exception  ConversionException  Description of the Exception
308      */
309     public static List toURIs(List l)
310     throws ConversionException {
311         List uris = new ArrayList();
312 
313         for (Iterator i = l.iterator(); i.hasNext(); ) {
314             uris.add(toURI((String)i.next()));
315         }
316 
317         return uris;
318     }
319 
320     /**
321      *  Description of the Method
322      *
323      * @param  u                        Description of the Parameter
324      * @return                          Description of the Return Value
325      * @exception  ConversionException  Description of the Exception
326      */
327     public static File toFile(URI u)
328     throws ConversionException {
329         File f = null;
330 
331         if (u != null) {
332             String s = u.getPath();
333 
334             try {
335                 s = URLDecoder.decode(s, ENCODING_UTF8);
336             } catch (UnsupportedEncodingException uee) {
337                 uee.printStackTrace();
338             }
339 
340             f = new File(s);
341         } else {
342             throw new ConversionException("invalid file");
343         }
344 
345         return f;
346     }
347 
348     /**
349      *  Description of the Method
350      *
351      * @param  s                        Description of the Parameter
352      * @return                          Description of the Return Value
353      * @exception  ConversionException  Description of the Exception
354      */
355     public static Color toColor(String s)
356     throws ConversionException {
357         String octalPrefix = "0";
358         String hex1Prefix = "0x";
359         String hex2Prefix = "#";
360         int decimalRadix = 10;
361         int octalRadix = 8;
362         int hexRadix = 16;
363         int radix = decimalRadix;
364         int i = -1;
365         Exception e = null;
366 
367         if (s != null) {
368             String t = s.toLowerCase();
369 
370             if (t.startsWith(hex1Prefix)) {
371                 s = s.substring(hex1Prefix.length());
372                 radix = hexRadix;
373             } else if (t.startsWith(hex2Prefix)) {
374                 s = s.substring(hex2Prefix.length());
375                 radix = hexRadix;
376             } else if (t.startsWith(octalPrefix)) {
377                 s = s.substring(octalPrefix.length());
378                 radix = octalRadix;
379             } else {
380                 radix = decimalRadix;
381             }
382 
383             try {
384                 i = Integer.parseInt(s, radix);
385             } catch (NumberFormatException nfe) {
386                 e = nfe;
387             }
388         }
389 
390         if (i == -1 ||
391             e != null) {
392             throw new ConversionException(e);
393         }
394 
395         return new Color(i);
396     }
397 
398 
399     /**
400      *  Description of the Method
401      *
402      * @param  s                        Description of the Parameter
403      * @return                          Description of the Return Value
404      * @exception  ConversionException  Description of the Exception
405      */
406     public static Class toClass(String s)
407     throws ConversionException {
408         Class c = null;
409 
410         try {
411             c = Class.forName(s);
412         } catch (ClassNotFoundException cnfe) {
413             throw new ConversionException(cnfe);
414         }
415 
416         return c;
417     }
418 
419 
420     /**
421      *  Description of the Method
422      *
423      * @param  l                        Description of the Parameter
424      * @return                          Description of the Return Value
425      * @exception  ConversionException  Description of the Exception
426      */
427     public static List toClasses(List l)
428     throws ConversionException {
429         List classes = new ArrayList();
430 
431         try {
432             for (Iterator i = l.iterator(); i.hasNext(); ) {
433                 classes.add(Class.forName((String)i.next()));
434             }
435         } catch (ClassNotFoundException cnfe) {
436             throw new ConversionException(cnfe);
437         }
438 
439         return classes;
440     }
441 }
442