1 55 56 package org.jboss.axis.encoding.ser; 57 58 import org.jboss.axis.Constants; 59 import org.jboss.axis.encoding.SerializationContext; 60 import org.jboss.axis.wsdl.fromJava.Types; 61 import org.w3c.dom.Element ; 62 import org.xml.sax.Attributes ; 63 64 import javax.xml.namespace.QName ; 65 import java.io.IOException ; 66 import java.text.DecimalFormat ; 67 import java.text.SimpleDateFormat ; 68 import java.util.Calendar ; 69 import java.util.Date ; 70 import java.util.GregorianCalendar ; 71 import java.util.TimeZone ; 72 73 80 public class DateSerializer extends SimpleSerializer 81 { 82 83 private static SimpleDateFormat zuluDate = 84 new SimpleDateFormat ("yyyy-MM-dd"); 85 86 private static SimpleDateFormat zuluDateTime = 87 new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss.SSS"); 88 89 private static Calendar calendar = Calendar.getInstance(); 90 91 protected QName defaultXmlType = Constants.XSD_DATE; 92 93 public DateSerializer(Class javaType, QName xmlType) 94 { 95 super(javaType, xmlType); 96 } 97 98 private String convertToZoneString(long offset) 99 { 100 if (offset == 0) return "Z"; 101 102 StringBuffer buffer = new StringBuffer (); 103 DecimalFormat hourFormat = new DecimalFormat ("'+'00;-00"); 104 DecimalFormat minuteFormat = new DecimalFormat ("00"); 105 106 int minutes = (int)offset / (1000 * 60); 107 int hours = minutes / 60; 108 109 minutes -= (hours * 60); 110 111 return hourFormat.format(hours) + ":" + minuteFormat.format(minutes); 112 } 113 114 private SimpleDateFormat getDateFormatForType(QName type) 115 { 116 117 if (type.equals(Constants.XSD_DATE)) 118 { 119 return zuluDate; 120 } 121 122 if (type.equals(Constants.XSD_DATETIME)) 123 { 124 return zuluDateTime; 125 } 126 127 return null; 128 } 129 130 133 public void serialize(QName name, Attributes attributes, 134 Object value, SerializationContext context) 135 throws IOException 136 { 137 context.startElement(name, attributes); 138 context.writeString(getValueAsString(value, context)); 139 context.endElement(); 140 } 141 142 public String getValueAsString(Object value, SerializationContext context) 143 { 144 StringBuffer buf = new StringBuffer (); 145 SimpleDateFormat zulu; 146 147 zulu = getDateFormatForType(super.xmlType); 148 149 if (zulu == null) 150 { 151 zulu = getDateFormatForType(defaultXmlType); 152 } 153 154 if (zulu == null) 155 { 156 zulu = zuluDate; 158 } 159 160 if (value instanceof Calendar ) 161 { 162 Calendar calendarValue = (Calendar )value; 163 164 if (calendarValue.get(Calendar.ERA) == GregorianCalendar.BC) 165 { 166 buf.append("-"); 167 } 168 169 synchronized (zulu) 170 { 171 zulu.setCalendar(calendarValue); 172 buf.append(zulu.format(calendarValue.getTime())); 173 } 174 175 176 buf.append(convertToZoneString(calendarValue.get(Calendar.ZONE_OFFSET) 177 + calendarValue.get(Calendar.DST_OFFSET))); 178 } 179 else 180 { 181 synchronized (calendar) 182 { 183 calendar.setTime((Date )value); 184 if (calendar.get(Calendar.ERA) == GregorianCalendar.BC) 185 { 186 buf.append("-"); 187 calendar.set(Calendar.ERA, GregorianCalendar.AD); 188 value = calendar.getTime(); 189 } 190 synchronized (zulu) 191 { 192 zulu.setCalendar(calendar); 193 zulu.setTimeZone(TimeZone.getTimeZone("GMT")); 194 buf.append(zulu.format((Date )value)); 195 } 196 buf.append("Z"); 197 198 } 199 } 200 return buf.toString(); 201 } 202 203 public String getMechanismType() 204 { 205 return Constants.AXIS_SAX; 206 } 207 208 219 public Element writeSchema(Class javaType, Types types) throws Exception 220 { 221 return null; 222 } 223 } 224 | Popular Tags |