KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > data > parser > BooleanParser


1 package prefuse.data.parser;
2
3 /**
4  * DataParser instance that parses boolean values. The string "true" is
5  * parsed to true values, "false" to false values. Both are case
6  * insensitive.
7  *
8  * @author <a HREF="http://jheer.org">jeffrey heer</a>
9  */

10 public class BooleanParser implements DataParser {
11     
12     /** Text string indicating a "true" value. */
13     public static final String JavaDoc TRUE = "true";
14     /** Text string indicating a "false" value. */
15     public static final String JavaDoc FALSE = "false";
16     
17     /**
18      * Returns boolean.class.
19      * @see prefuse.data.parser.DataParser#getType()
20      */

21     public Class JavaDoc getType() {
22         return boolean.class;
23     }
24     
25     /**
26      * @see prefuse.data.parser.DataParser#format(java.lang.Object)
27      */

28     public String JavaDoc format(Object JavaDoc value) {
29         if ( value == null ) return null;
30         if ( !(value instanceof Boolean JavaDoc) )
31             throw new IllegalArgumentException JavaDoc(
32               "This class can only format Objects of type Boolean.");
33         return ((Boolean JavaDoc)value).toString();
34     }
35     
36     /**
37      * @see prefuse.data.parser.DataParser#canParse(java.lang.String)
38      */

39     public boolean canParse(String JavaDoc text) {
40         return TRUE.equalsIgnoreCase(text) || FALSE.equalsIgnoreCase(text);
41     }
42     
43     /**
44      * @see prefuse.data.parser.DataParser#parse(java.lang.String)
45      */

46     public Object JavaDoc parse(String JavaDoc text) throws DataParseException {
47         return parseBoolean(text) ? Boolean.TRUE : Boolean.FALSE;
48     }
49     
50     /**
51      * Parse a boolean value from a text string
52      * @param text the text string to parse
53      * @return the parsed boolean value
54      * @throws DataParseException if an error occurs during parsing
55      */

56     public boolean parseBoolean(String JavaDoc text) throws DataParseException {
57         if ( TRUE.equalsIgnoreCase(text) ) {
58             return true;
59         } else if ( FALSE.equalsIgnoreCase(text) ) {
60             return false;
61         } else {
62             throw new DataParseException(
63                 "Input does not represent a boolean value: "+ text);
64         }
65     }
66     
67 } // end of class BooleanParser
68
Popular Tags