1 22 23 package org.xquark.xpath.datamodel; 24 25 import java.text.DateFormat ; 26 import java.text.ParseException ; 27 import java.text.SimpleDateFormat ; 28 import java.util.Date ; 29 30 import org.xquark.schema.SchemaException; 31 import org.xquark.schema.datatypes.PrimitiveType; 32 33 51 public class SchemaFormater { 52 public static final String RCSRevision = "$Revision: 1.1 $"; 53 public static final String RCSName = "$Name: $"; 54 55 58 protected static DateFormat [] dateFormats; 59 62 protected static DateFormat ufDateFormat = DateFormat.getDateInstance(DateFormat.LONG); 63 64 67 protected static DateFormat [] timeFormats; 68 71 protected static DateFormat ufTimeFormat = DateFormat.getTimeInstance(DateFormat.LONG); 72 73 76 protected static DateFormat [] dateTimeFormats; 77 80 protected static DateFormat ufDateTimeFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG); 81 82 85 protected static DateFormat [] gMonthFormats; 86 89 protected static DateFormat ufGMonthFormat = new SimpleDateFormat ("MMMM"); 90 91 94 protected static DateFormat [] gDayFormats; 95 98 protected static DateFormat ufGDayFormat = new SimpleDateFormat ("d"); 99 100 103 protected static DateFormat [] gYearFormats; 104 105 106 113 public static String toSchemaForm(String input, PrimitiveType type) { 114 switch (type.getType()) { 115 case PrimitiveType.DATE: 116 for (int i = 0; i < dateFormats.length; i++) { 117 try { 118 return type.toXMLString(dateFormats[i].parse(input), null); 119 } 120 catch (ParseException e) { 121 } 122 } 123 break; 124 case PrimitiveType.TIME: 125 for (int i = 0; i < timeFormats.length; i++) { 126 try { 127 return type.toXMLString(timeFormats[i].parse(input), null); 128 } 129 catch (ParseException e) { 130 } 131 } 132 break; 133 case PrimitiveType.DATE_TIME: 134 for (int i = 0; i < dateTimeFormats.length; i++) { 135 try { 136 return type.toXMLString(dateTimeFormats[i].parse(input), null); 137 } 138 catch (ParseException e) { 139 } 140 } 141 break; 142 case PrimitiveType.GMONTH: 143 for (int i = 0; i < gMonthFormats.length; i++) { 144 try { 145 return type.toXMLString(gMonthFormats[i].parse(input), null); 146 } 147 catch (ParseException e) { 148 } 149 } 150 break; 151 case PrimitiveType.GDAY: 152 for (int i = 0; i < gDayFormats.length; i++) { 153 try { 154 return type.toXMLString(gDayFormats[i].parse(input), null); 155 } 156 catch (ParseException e) { 157 } 158 } 159 break; 160 case PrimitiveType.GYEAR: 161 for (int i = 0; i < gYearFormats.length; i++) { 162 try { 163 return type.toXMLString(gYearFormats[i].parse(input), null); 164 } 165 catch (ParseException e) { 166 } 167 } 168 break; 169 } 170 return input; 171 } 172 173 174 181 public static String toUserFriendlyForm(String input, PrimitiveType type) { 182 switch (type.getType()) { 183 case PrimitiveType.DATE: 184 try { 185 return ufDateFormat.format((Date ) type.convert(input, false, null)); 186 } 187 catch (SchemaException e) { 188 } 189 break; 190 case PrimitiveType.TIME: 191 try { 192 return ufTimeFormat.format((Date ) type.convert(input, false, null)); 193 } 194 catch (SchemaException e) { 195 } 196 break; 197 case PrimitiveType.DATE_TIME: 198 try { 199 return ufDateTimeFormat.format((Date ) type.convert(input, false, null)); 200 } 201 catch (SchemaException e) { 202 } 203 break; 204 case PrimitiveType.GMONTH: 205 try { 206 return ufGMonthFormat.format((Date ) type.convert(input, false, null)); 207 } 208 catch (SchemaException e) { 209 } 210 break; 211 case PrimitiveType.GDAY: 212 try { 213 return ufGDayFormat.format((Date ) type.convert(input, false, null)); 214 } 215 catch (SchemaException e) { 216 } 217 break; 218 } 219 return input; 220 } 221 222 static { 223 int[] styles = new int[]{DateFormat.FULL, DateFormat.LONG, DateFormat.MEDIUM, DateFormat.SHORT}; 224 int numStyles = styles.length; 225 int i; 226 int j; 227 228 dateFormats = new DateFormat [numStyles]; 230 for (i = 0; i < numStyles; i++) { 231 dateFormats[i] = DateFormat.getDateInstance(styles[i]); 232 } 233 234 timeFormats = new DateFormat [numStyles]; 236 for (i = 0; i < numStyles; i++) { 237 timeFormats[i] = DateFormat.getTimeInstance(styles[i]); 238 } 239 240 dateTimeFormats = new DateFormat [numStyles * numStyles]; 242 for (i = 0; i < numStyles; i++) { 243 for (j = 0; j < numStyles; j++) { 244 dateTimeFormats[i + j * numStyles] = DateFormat.getDateTimeInstance(styles[i], styles[j]); 245 } 246 } 247 248 gMonthFormats = new DateFormat []{new SimpleDateFormat ("M"), new SimpleDateFormat ("MMM"), new SimpleDateFormat ("MMMM")}; 250 251 gDayFormats = new DateFormat []{new SimpleDateFormat ("d")}; 253 254 gYearFormats = new DateFormat []{new SimpleDateFormat ("y")}; 256 257 } 258 259 } 260 | Popular Tags |