KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > bpel > xml > util > DatatypeUtil


1 package org.jbpm.bpel.xml.util;
2
3 import java.util.Calendar JavaDoc;
4 import java.util.IdentityHashMap JavaDoc;
5 import java.util.Map JavaDoc;
6 import java.util.TimeZone JavaDoc;
7 import java.util.regex.Matcher JavaDoc;
8 import java.util.regex.Pattern JavaDoc;
9
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12 import org.w3c.dom.Node JavaDoc;
13
14 /**
15  * @author Alejandro Guízar
16  * @version $Revision: 1.1 $ $Date: 2005/06/23 02:22:46 $
17  */

18 public class DatatypeUtil {
19   
20   private static final Map JavaDoc booleanLiterals = new IdentityHashMap JavaDoc(6);
21   private static final Pattern JavaDoc dateTimePattern = Pattern.compile(
22       "(-?\\p{Digit}{4,})" + // year
23
"-(\\p{Digit}{2})" + // month
24
"-(\\p{Digit}{2})" + // day
25
"(?:T" + // begin-time
26
"(\\p{Digit}{2})" + // hour
27
":(\\p{Digit}{2})" + // minute
28
":(\\p{Digit}{2})" + // whole seconds
29
"(?:\\." + // begin-fractional seconds
30
"(\\p{Digit}{1,3})" + // milliseconds
31
"\\p{Digit}*" + // time below milliseconds
32
")?" + // end-fractional seconds
33
")?" + // end-time
34
"((?:(?:\\+|-)\\p{Digit}{2}:\\p{Digit}{2})|(Z))?"); // timezone
35
private static final Log log = LogFactory.getLog(DatatypeUtil.class);
36
37   /* Suppresses default constructor, ensuring non-instantiability */
38   private DatatypeUtil() {
39   }
40   
41   /**
42    * Interprets a value as a XML Schema boolean.
43    * @see "WS-BPEL 2.0 §9.1.1"
44    */

45   public static Boolean JavaDoc toBoolean(Object JavaDoc value) {
46     Boolean JavaDoc bool = null;
47     if (value instanceof Node JavaDoc) {
48       value = NodeUtil.getValue((Node JavaDoc) value);
49     }
50     if (value instanceof Boolean JavaDoc) {
51       bool = (Boolean JavaDoc) value;
52     }
53     else if (value instanceof String JavaDoc) {
54       bool = parseBoolean((String JavaDoc) value);
55     }
56     return bool;
57   }
58   
59   
60   /**
61    * Interprets a value as a XML Schema date or dateTime
62    * @see "WS-BPEL 2.0 §9.1.2"
63    */

64   public static Calendar JavaDoc toDateTime(Object JavaDoc value) {
65     Calendar JavaDoc dateTime = null;
66     if (value instanceof Node JavaDoc) {
67       value = NodeUtil.getValue((Node JavaDoc) value);
68     }
69     if (value instanceof Calendar JavaDoc) {
70       dateTime = (Calendar JavaDoc) value;
71     }
72     else if (value instanceof String JavaDoc) {
73       dateTime = DatatypeUtil.parseDateTime((String JavaDoc) value);
74     }
75     return dateTime;
76   }
77   
78   /**
79    * Interprets a value as a XML Schema duration.
80    * @see "WS-BPEL 2.0 §9.1.3"
81    */

82   public static Duration toDuration(Object JavaDoc value) {
83     Duration duration = null;
84     if (value instanceof Node JavaDoc) {
85       value = NodeUtil.getValue((Node JavaDoc) value);
86     }
87     if (value instanceof Duration) {
88       duration = (Duration) value;
89     }
90     else if (value instanceof String JavaDoc) {
91       duration = Duration.parseDuration((String JavaDoc) value);
92     }
93     return duration;
94   }
95   
96   /**
97    * Parses a boolean lexical representation as per the XML Schema
98    * W3C recommendation.
99    * @param text the lexical representation of a boolean
100    * @return the boolean the text represents
101    * @see <a HREF="http://www.w3.org/TR/xmlschema-2/#boolean">
102    * XML Schema Part 2: Datatypes &sect;3.2.2</a>
103    */

104   public static Boolean JavaDoc parseBoolean(String JavaDoc text) {
105     return (Boolean JavaDoc) booleanLiterals.get(text.intern());
106   }
107   
108   /**
109    * Parses a date/dateTime lexical representation as per the XML Schema
110    * W3C recommendation.
111    * @param text the lexical representation of a date/dateTime
112    * @return the date/dateTime the text represents
113    * @see <a HREF="http://www.w3.org/TR/xmlschema-2/#dateTime">
114    * XML Schema Part 2: Datatypes &sect;3.2.7</a>
115    */

116   public static Calendar JavaDoc parseDateTime(String JavaDoc text) {
117     Calendar JavaDoc dateTime = null;
118     
119     Matcher JavaDoc matcher = dateTimePattern.matcher(text);
120     if (matcher.matches()) {
121       dateTime = Calendar.getInstance();
122       // calendar is initialized with the current time; must be cleared
123
dateTime.clear();
124       // group 1: year
125
dateTime.set(Calendar.YEAR, Integer.parseInt(matcher.group(1)));
126       // group 2: month; the month field is zero-based
127
dateTime.set(Calendar.MONTH, Integer.parseInt(matcher.group(2)) - 1);
128       // group 3: day
129
dateTime.set(Calendar.DAY_OF_MONTH, Integer.parseInt(matcher.group(3)));
130       // group 4: hour
131
String JavaDoc group = matcher.group(4);
132       if (group != null) {
133         // if the hour is present, so are minutes and seconds
134
dateTime.set(Calendar.HOUR_OF_DAY, Integer.parseInt(group));
135         // group 5: minute
136
dateTime.set(Calendar.MINUTE, Integer.parseInt(matcher.group(5)));
137         // group 6: second
138
dateTime.set(Calendar.SECOND, Integer.parseInt(matcher.group(6)));
139         // group 7: milliseconds
140
group = matcher.group(7);
141         if (group != null) {
142           dateTime.set(Calendar.MILLISECOND, Integer.parseInt(group));
143         }
144       }
145       // group 8: timezone
146
group = matcher.group(8);
147       if (group != null) {
148         TimeZone JavaDoc timeZone;
149         if (group.equals("Z")) {
150           // "Z" means "the zero-length duration timezone"
151
timeZone = TimeZone.getTimeZone("GMT+00:00");
152         }
153         else {
154           timeZone = TimeZone.getTimeZone("GMT" + group);
155         }
156         dateTime.setTimeZone(timeZone);
157       }
158       // the schema recommendation specifies strict date interpretation
159
dateTime.setLenient(false);
160     }
161     else {
162       log.error("Invalid dateTime lexical representation '" + text + "'");
163     }
164     return dateTime;
165   }
166   
167   static {
168     booleanLiterals.put("true", Boolean.TRUE);
169     booleanLiterals.put("1", Boolean.TRUE);
170     booleanLiterals.put("false", Boolean.FALSE);
171     booleanLiterals.put("0", Boolean.FALSE);
172   }
173 }
174
Popular Tags