1 57 58 package org.apache.soap.encoding.soapenc; 59 60 import org.apache.soap.util.xml.*; 61 import org.apache.soap.util.*; 62 import org.apache.soap.rpc.SOAPContext; 63 import java.io.*; 64 import org.w3c.dom.*; 65 import java.io.*; 66 import java.util.*; 67 import java.text.*; 68 69 82 public class DateSerializer implements Serializer, Deserializer 83 { 84 SimpleDateFormat sdf; 85 86 public DateSerializer() 87 { 88 sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); 89 sdf.setTimeZone(TimeZone.getTimeZone("GMT")); 91 } 92 93 public void marshall(String inScopeEncStyle, Class javaType, Object src, 94 Object context, Writer sink, NSStack nsStack, 95 XMLJavaMappingRegistry xjmr, SOAPContext ctx) 96 throws IllegalArgumentException , IOException 97 { 98 if(!javaType.equals(java.util.Date .class)) 99 { 100 throw new IllegalArgumentException ("Can only serialize java.util.Date instances"); 101 } 102 nsStack.pushScope(); 103 if(src!=null) 104 { 105 SoapEncUtils.generateStructureHeader(inScopeEncStyle, 106 javaType, 107 context, 108 sink, 109 nsStack,xjmr); 110 111 String fdate=null; 112 113 synchronized(sdf) 114 { 115 fdate=sdf.format((Date)src); 116 } 117 118 sink.write(fdate); 119 sink.write("</" + context + '>'); 120 } 121 else 122 { 123 SoapEncUtils.generateNullStructure(inScopeEncStyle, 124 javaType, 125 context, 126 sink, 127 nsStack,xjmr); 128 } 129 nsStack.popScope(); 130 } 131 132 public Bean unmarshall(String inScopeEncStyle, QName elementType, Node src, 133 XMLJavaMappingRegistry xjmr, SOAPContext ctx) 134 throws IllegalArgumentException 135 { 136 Date date=null; 137 Element root = (Element)src; 138 String value = DOMUtils.getChildCharacterData(root); 139 if(value!=null && !((value=value.trim()).equals(""))) 140 { 141 try 142 { 143 synchronized(sdf) 144 { 145 date=sdf.parse(value); 146 } 147 } 148 catch (ParseException pe) 149 { 150 try 151 { 152 if (value.length() < 19) 154 throw new ParseException("",0); 155 156 if (value.charAt(4) != '-' || value.charAt(7) != '-' || 157 value.charAt(10) != 'T') 158 throw new ParseException("",0); 159 160 if (value.charAt(13) != ':' || value.charAt(16) != ':') 161 throw new ParseException("",0); 162 163 try 165 { 166 synchronized(sdf) 167 { 168 date=sdf.parse(value.substring(0,19)+".000Z"); 169 } 170 } 171 catch (Exception e) 172 { 173 throw new ParseException("",0); 174 } 175 176 int pos = 19; 177 178 if (pos < value.length() && value.charAt(pos)=='.') { 180 int milliseconds = 0; 181 int start = ++pos; 182 while (pos<value.length() && Character.isDigit(value.charAt(pos))) 183 pos++; 184 185 String decimal=value.substring(start,pos); 186 if (decimal.length()==3) { 187 milliseconds=Integer.parseInt(decimal); 188 } else if (decimal.length() < 3) { 189 milliseconds=Integer.parseInt((decimal+"000").substring(0,3)); 190 } else { 191 milliseconds=Integer.parseInt(decimal.substring(0,3)); 192 if (decimal.charAt(3)>='5') ++milliseconds; 193 } 194 195 date.setTime(date.getTime()+milliseconds); 197 } 198 199 if (pos+5 < value.length() && 201 (value.charAt(pos)=='+' || (value.charAt(pos)=='-'))) 202 { 203 if (!Character.isDigit(value.charAt(pos+1)) || 204 !Character.isDigit(value.charAt(pos+2)) || 205 value.charAt(pos+3) != ':' || 206 !Character.isDigit(value.charAt(pos+4)) || 207 !Character.isDigit(value.charAt(pos+5))) 208 throw new ParseException("",0); 209 210 int hours = (value.charAt(pos+1)-'0')*10+value.charAt(pos+2)-'0'; 211 int mins = (value.charAt(pos+4)-'0')*10+value.charAt(pos+5)-'0'; 212 int milliseconds = (hours*60+mins)*60*1000; 213 214 if (value.charAt(pos)=='+') milliseconds=-milliseconds; 216 date.setTime(date.getTime()+milliseconds); 217 pos+=6; 218 } 219 220 if (pos < value.length() && value.charAt(pos)=='Z') pos++; 221 222 if (pos < value.length()) 223 throw new ParseException("",0); 224 } 225 catch (ParseException pe2) 226 { 227 synchronized(sdf) 228 { 229 throw new IllegalArgumentException ("String represents no valid " + 230 "Date for this Deserializer; " + 231 "try " + sdf.toPattern() + "."); 232 } 233 } 234 } 235 } 236 return new Bean(java.util.Date .class,date); 237 } 238 } 239 | Popular Tags |