KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > coerce > FileHandler


1 /***************************************
2  * *
3  * JBoss: The OpenSource J2EE WebOS *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  ***************************************/

9
10 package org.jboss.util.coerce;
11
12 import org.jboss.util.CoercionException;
13 import org.jboss.util.NotCoercibleException;
14
15 import java.io.File JavaDoc;
16
17 /**
18  * A <tt>java.io.File</tt> coercion handler.
19  *
20  * @version <tt>$Revision: 1.3 $</tt>
21  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
22  */

23 public class FileHandler
24    extends BoundCoercionHandler
25 {
26    /**
27     * Get the target class type for this <tt>CoercionHandler</tt>.
28     *
29     * @return <tt>Class</tt> type.
30     */

31    public Class JavaDoc getType() {
32       return File JavaDoc.class;
33    }
34
35    /**
36     * Coerces the given value into the given type (which should be
37     * <tt>File</tt>).
38     *
39     * <p>This currently only support coercion from a <tt>String</tt>
40     *
41     * @param value Value to coerce.
42     * @param type <tt>File</tt>.
43     * @return Value coerced into a <tt>File</tt>.
44     *
45     * @throws CoercionException Failed to coerce.
46     */

47    public Object JavaDoc coerce(Object JavaDoc value, Class JavaDoc type) throws CoercionException {
48       if (value.getClass().equals(String JavaDoc.class)) {
49          return coerce((String JavaDoc)value);
50       }
51       
52       throw new NotCoercibleException(value);
53    }
54
55    /**
56     * Coerces the given String into a <tt>File</tt> by creating attempting
57     * to create a new file for the given filename.
58     *
59     * @param value The name of the file to create.
60     * @return <tt>File</tt>
61     *
62     * @throws NotCoercibleException Failed to create file.
63     */

64    public Object JavaDoc coerce(String JavaDoc value) {
65       return new File JavaDoc(value);
66    }
67 }
68
69
Popular Tags