1 package org.jbpm.bpel.xml.util; 2 3 import java.util.Calendar ; 4 import java.util.IdentityHashMap ; 5 import java.util.Map ; 6 import java.util.TimeZone ; 7 import java.util.regex.Matcher ; 8 import java.util.regex.Pattern ; 9 10 import org.apache.commons.logging.Log; 11 import org.apache.commons.logging.LogFactory; 12 import org.w3c.dom.Node ; 13 14 18 public class DatatypeUtil { 19 20 private static final Map booleanLiterals = new IdentityHashMap (6); 21 private static final Pattern dateTimePattern = Pattern.compile( 22 "(-?\\p{Digit}{4,})" + "-(\\p{Digit}{2})" + "-(\\p{Digit}{2})" + "(?:T" + "(\\p{Digit}{2})" + ":(\\p{Digit}{2})" + ":(\\p{Digit}{2})" + "(?:\\." + "(\\p{Digit}{1,3})" + "\\p{Digit}*" + ")?" + ")?" + "((?:(?:\\+|-)\\p{Digit}{2}:\\p{Digit}{2})|(Z))?"); private static final Log log = LogFactory.getLog(DatatypeUtil.class); 36 37 38 private DatatypeUtil() { 39 } 40 41 45 public static Boolean toBoolean(Object value) { 46 Boolean bool = null; 47 if (value instanceof Node ) { 48 value = NodeUtil.getValue((Node ) value); 49 } 50 if (value instanceof Boolean ) { 51 bool = (Boolean ) value; 52 } 53 else if (value instanceof String ) { 54 bool = parseBoolean((String ) value); 55 } 56 return bool; 57 } 58 59 60 64 public static Calendar toDateTime(Object value) { 65 Calendar dateTime = null; 66 if (value instanceof Node ) { 67 value = NodeUtil.getValue((Node ) value); 68 } 69 if (value instanceof Calendar ) { 70 dateTime = (Calendar ) value; 71 } 72 else if (value instanceof String ) { 73 dateTime = DatatypeUtil.parseDateTime((String ) value); 74 } 75 return dateTime; 76 } 77 78 82 public static Duration toDuration(Object value) { 83 Duration duration = null; 84 if (value instanceof Node ) { 85 value = NodeUtil.getValue((Node ) value); 86 } 87 if (value instanceof Duration) { 88 duration = (Duration) value; 89 } 90 else if (value instanceof String ) { 91 duration = Duration.parseDuration((String ) value); 92 } 93 return duration; 94 } 95 96 104 public static Boolean parseBoolean(String text) { 105 return (Boolean ) booleanLiterals.get(text.intern()); 106 } 107 108 116 public static Calendar parseDateTime(String text) { 117 Calendar dateTime = null; 118 119 Matcher matcher = dateTimePattern.matcher(text); 120 if (matcher.matches()) { 121 dateTime = Calendar.getInstance(); 122 dateTime.clear(); 124 dateTime.set(Calendar.YEAR, Integer.parseInt(matcher.group(1))); 126 dateTime.set(Calendar.MONTH, Integer.parseInt(matcher.group(2)) - 1); 128 dateTime.set(Calendar.DAY_OF_MONTH, Integer.parseInt(matcher.group(3))); 130 String group = matcher.group(4); 132 if (group != null) { 133 dateTime.set(Calendar.HOUR_OF_DAY, Integer.parseInt(group)); 135 dateTime.set(Calendar.MINUTE, Integer.parseInt(matcher.group(5))); 137 dateTime.set(Calendar.SECOND, Integer.parseInt(matcher.group(6))); 139 group = matcher.group(7); 141 if (group != null) { 142 dateTime.set(Calendar.MILLISECOND, Integer.parseInt(group)); 143 } 144 } 145 group = matcher.group(8); 147 if (group != null) { 148 TimeZone timeZone; 149 if (group.equals("Z")) { 150 timeZone = TimeZone.getTimeZone("GMT+00:00"); 152 } 153 else { 154 timeZone = TimeZone.getTimeZone("GMT" + group); 155 } 156 dateTime.setTimeZone(timeZone); 157 } 158 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 |