KickJava   Java API By Example, From Geeks To Geeks.

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


1 package prefuse.data.parser;
2
3 import prefuse.util.ColorLib;
4
5 /**
6  * DataParser instance the parses int color values from a
7  * hexadecimal text string.
8  *
9  * @author <a HREF="http://jheer.org">jeffrey heer</a>
10  */

11 public class ColorIntParser implements DataParser {
12     
13     /**
14      * Returns int.class.
15      * @see prefuse.data.parser.DataParser#getType()
16      */

17     public Class JavaDoc getType() {
18         return int.class;
19     }
20     
21     /**
22      * @see prefuse.data.parser.DataParser#format(java.lang.Object)
23      */

24     public String JavaDoc format(Object JavaDoc value) {
25         if ( value == null ) return null;
26         if ( !(value instanceof Number JavaDoc) )
27             throw new IllegalArgumentException JavaDoc(
28               "This class can only format Objects of type Number.");
29         return String.valueOf(((Number JavaDoc)value).intValue());
30     }
31     
32     /**
33      * @see prefuse.data.parser.DataParser#canParse(java.lang.String)
34      */

35     public boolean canParse(String JavaDoc text) {
36         try {
37             if ( text.charAt(0) == ColorLib.HEX_PREFIX ) {
38                 parseInt(text);
39             } else {
40                 return false;
41             }
42             return true;
43         } catch ( Exception JavaDoc e ) {
44             return false;
45         }
46     }
47     
48     /**
49      * @see prefuse.data.parser.DataParser#parse(java.lang.String)
50      */

51     public Object JavaDoc parse(String JavaDoc text) throws DataParseException {
52         return new Integer JavaDoc(parseInt(text));
53     }
54     
55     /**
56      * Parse an int value from a text string.
57      * @param text the text string to parse
58      * @return the parsed int value
59      * @throws DataParseException if an error occurs during parsing
60      */

61     public static int parseInt(String JavaDoc text) throws DataParseException {
62         try {
63             return ColorLib.hex(text);
64         } catch ( Exception JavaDoc e ) {
65             throw new DataParseException(e);
66         }
67     }
68     
69 } // end of class IntParser
70
Popular Tags