KickJava   Java API By Example, From Geeks To Geeks.

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


1 package prefuse.data.parser;
2
3 import java.sql.Time JavaDoc;
4 import java.text.DateFormat JavaDoc;
5
6 /**
7  * DataParser instance that parses Date values as java.util.Time instances,
8  * representing a particular time (but no specific date).
9  * This class uses a backing {@link java.text.DateFormat} instance to
10  * perform parsing. The DateFormat instance to use can be passed in to the
11  * constructor, or by default the DateFormat returned by
12  * {@link java.text.DateFormat#getTimeInstance(int)} with an
13  * argument of {@link java.text.DateFormat#SHORT} is used.
14  *
15  * @author <a HREF="http://jheer.org">jeffrey heer</a>
16  */

17 public class TimeParser extends DateParser {
18
19     /**
20      * Create a new TimeParser.
21      */

22     public TimeParser() {
23         this(DateFormat.getTimeInstance(DateFormat.SHORT));
24     }
25     
26     /**
27      * Create a new TimeParser.
28      * @param dateFormat the DateFormat instance to use for parsing
29      */

30     public TimeParser(DateFormat JavaDoc dateFormat) {
31         super(dateFormat);
32     }
33     
34     /**
35      * Returns java.sql.Time.class.
36      * @see prefuse.data.parser.DataParser#getType()
37      */

38     public Class JavaDoc getType() {
39         return Time JavaDoc.class;
40     }
41     
42     /**
43      * @see prefuse.data.parser.DataParser#canParse(java.lang.String)
44      */

45     public boolean canParse(String JavaDoc val) {
46         try {
47             parseTime(val);
48             return true;
49         } catch ( DataParseException e ) {
50             return false;
51         }
52     }
53     
54     /**
55      * @see prefuse.data.parser.DataParser#parse(java.lang.String)
56      */

57     public Object JavaDoc parse(String JavaDoc val) throws DataParseException {
58         return parseTime(val);
59     }
60     
61     /**
62      * Parse a Time value from a text string.
63      * @param text the text string to parse
64      * @return the parsed Time value
65      * @throws DataParseException if an error occurs during parsing
66      */

67     public Time JavaDoc parseTime(String JavaDoc text) throws DataParseException {
68         m_pos.setErrorIndex(0);
69         m_pos.setIndex(0);
70         
71         // parse the data value, convert to the wrapper type
72
Time JavaDoc t = null;
73         try {
74             t = Time.valueOf(text);
75             m_pos.setIndex(text.length());
76         } catch ( IllegalArgumentException JavaDoc e ) {
77             t = null;
78         }
79         if ( t == null ) {
80             java.util.Date JavaDoc d1 = m_dfmt.parse(text, m_pos);
81             if ( d1 != null ) {
82                 t = new Time JavaDoc(d1.getTime());
83             }
84         }
85         
86         // date format will parse substrings successfully, so we need
87
// to check the position to make sure the whole value was used
88
if ( t == null || m_pos.getIndex() < text.length() ) {
89             throw new DataParseException("Could not parse Date: "+text);
90         } else {
91             return t;
92         }
93     }
94         
95 } // end of class TimeParser
96
Popular Tags