1 17 package org.apache.ws.jaxme.generator.types; 18 19 import java.text.ParsePosition ; 20 import java.util.Calendar ; 21 import java.util.Date ; 22 23 import org.apache.ws.jaxme.generator.sg.SGFactory; 24 import org.apache.ws.jaxme.generator.sg.SGlet; 25 import org.apache.ws.jaxme.generator.sg.SchemaSG; 26 import org.apache.ws.jaxme.generator.sg.SimpleTypeSG; 27 import org.apache.ws.jaxme.impl.DatatypeConverterImpl; 28 import org.apache.ws.jaxme.impl.JMUnmarshallerImpl; 29 import org.apache.ws.jaxme.js.DirectAccessible; 30 import org.apache.ws.jaxme.js.JavaMethod; 31 import org.apache.ws.jaxme.js.JavaQName; 32 import org.apache.ws.jaxme.js.JavaQNameImpl; 33 import org.apache.ws.jaxme.js.JavaSource; 34 import org.apache.ws.jaxme.js.LocalJavaField; 35 import org.apache.ws.jaxme.js.TypedValue; 36 import org.apache.ws.jaxme.js.impl.TypedValueImpl; 37 import org.apache.ws.jaxme.xs.XSType; 38 import org.apache.ws.jaxme.xs.parser.impl.LocSAXException; 39 import org.apache.ws.jaxme.xs.util.XsDateTimeFormat; 40 import org.xml.sax.SAXException ; 41 42 45 public class DateTimeSG extends AtomicTypeSGImpl { 46 public static final JavaQName CALENDAR_TYPE = JavaQNameImpl.getInstance(Calendar .class); 47 private static final JavaQName OBJECT_TYPE = JavaQNameImpl.getInstance(Object .class); 48 49 51 public DateTimeSG(SGFactory pFactory, SchemaSG pSchema, XSType pType) throws SAXException { 52 super(pFactory, pSchema, pType); 53 } 54 55 protected String getDatatypeName() { return "DateTime"; } 56 protected JavaQName getDatatypeType() { return CALENDAR_TYPE; } 57 protected Class getFormatClass() { return XsDateTimeFormat.class; } 58 public JavaQName getRuntimeType(SimpleTypeSG pController) { return CALENDAR_TYPE; } 59 60 public TypedValue getCastFromString(SimpleTypeSG pController, String pValue) 61 throws SAXException { 62 try { 63 Calendar calendar = new DatatypeConverterImpl().parseDate(pValue); 64 return new TypedValueImpl( 65 new Object [] { "new java.util.GregorianCalendar(" 66 + calendar.get(Calendar.YEAR) + "," 67 + calendar.get(Calendar.MONTH) + "," 68 + calendar.get(Calendar.DAY_OF_MONTH) + ")" }, 69 getDatatypeType()); 70 } catch (RuntimeException e) { 71 try { 72 throw new LocSAXException("Failed to convert string value to " 73 + getDatatypeName() + " instance: " + pValue, getLocator()); 74 } catch (Exception e1) { 75 throw new SAXException("Failed to convert string value to " 76 + getDatatypeName() + " instance: " + pValue); 77 } 78 } 79 } 80 81 public TypedValue getCastFromString(SimpleTypeSG pController, JavaMethod pMethod, Object pValue, Object pData) throws SAXException { 82 boolean mayBeDate; 83 Object format; 84 if (pData == null) { 85 format = new Object []{"new ", getFormatClass(), "()"}; 86 mayBeDate = false; 87 } else { 88 format = new Object []{"((", JMUnmarshallerImpl.class, ") ", pData, 89 ".getJMUnmarshaller()).get" + getDatatypeName(), "Format()"}; 90 mayBeDate = true; 91 } 92 if (!(pValue instanceof DirectAccessible)) { 93 LocalJavaField v = pMethod.newJavaField(String .class); 94 v.addLine(pValue); 95 pValue = v; 96 } 97 LocalJavaField pos = pMethod.newJavaField(ParsePosition .class); 98 pos.addLine("new ", ParsePosition .class, "(0)"); 99 LocalJavaField cal = pMethod.newJavaField(mayBeDate ? OBJECT_TYPE : pController.getRuntimeType()); 100 cal.addLine(format, ".parseObject(", pValue, ", ", pos, ");"); 101 pMethod.addIf(cal, " == null"); 102 pMethod.addThrowNew(IllegalArgumentException .class, 103 JavaSource.getQuoted("Failed to parse dateTime "), 104 " + ", pValue, " + ", JavaSource.getQuoted(" at: "), 105 " + ", pValue, ".substring(", pos, ".getErrorIndex())"); 106 pMethod.addEndIf(); 107 if (mayBeDate) { 108 LocalJavaField result = pMethod.newJavaField(pController.getRuntimeType()); 109 pMethod.addIf(cal, " instanceof ", Calendar .class); 110 pMethod.addLine(result, " = (", Calendar .class, ") ", cal, ";"); 111 pMethod.addElse(); 112 pMethod.addLine(result, " = ", Calendar .class, ".getInstance();"); 113 pMethod.addLine(result, ".setTime((", Date .class, ") ", cal, ");"); 114 pMethod.addEndIf(); 115 return result; 116 } else { 117 return cal; 118 } 119 } 120 121 public void forAllNonNullValues(SimpleTypeSG pController, JavaMethod pMethod, Object pValue, SGlet pSGlet) throws SAXException { 122 LocalJavaField f = pMethod.newJavaField(CALENDAR_TYPE); 123 f.addLine(pValue); 124 pMethod.addIf(f, " != null"); 125 pSGlet.generate(pMethod, pValue); 126 pMethod.addEndIf(); 127 } 128 129 public void forAllValues(SimpleTypeSG pController, JavaMethod pMethod, Object pValue, SGlet pSGlet) throws SAXException { 130 pSGlet.generate(pMethod, pValue); 131 } 132 } 133 | Popular Tags |