1 57 58 61 62 package org.apache.soap.encoding.soapenc; 63 64 import org.apache.soap.util.xml.*; 65 import org.apache.soap.util.*; 66 import org.apache.soap.encoding.soapenc.SoapEncUtils; 67 import org.apache.soap.rpc.SOAPContext; 68 import java.io.*; 69 import org.w3c.dom.*; 70 import java.io.*; 71 import java.util.*; 72 import java.text.*; 73 74 85 public class CalendarSerializer implements Serializer, Deserializer 86 { 87 SimpleDateFormat sdf; 88 89 public CalendarSerializer() 90 { 91 sdf=new SimpleDateFormat("yyyy-MM-dd"); 92 } 95 96 public void marshall(String inScopeEncStyle, Class javaType, Object src, 97 Object context, Writer sink, NSStack nsStack, 98 XMLJavaMappingRegistry xjmr, SOAPContext ctx) 99 throws IllegalArgumentException , IOException 100 { 101 if(!javaType.equals(java.util.GregorianCalendar .class)) 102 { 103 throw new IllegalArgumentException ("Can only serialize GregorianCalendar instances"); 104 } 105 nsStack.pushScope(); 106 if(src!=null) 107 { 108 SoapEncUtils.generateStructureHeader(inScopeEncStyle, 109 javaType, 110 context, 111 sink, 112 nsStack,xjmr); 113 114 Date calDate = ((GregorianCalendar)src).getTime(); 115 String fdate = null; 116 117 synchronized(sdf) 118 { 119 fdate=sdf.format(calDate); 120 } 121 122 sink.write(fdate); 123 sink.write("</" + context + '>'); 124 } 125 else 126 { 127 SoapEncUtils.generateNullStructure(inScopeEncStyle, 128 javaType, 129 context, 130 sink, 131 nsStack,xjmr); 132 } 133 nsStack.popScope(); 134 } 135 136 public Bean unmarshall(String inScopeEncStyle, QName elementType, Node src, 137 XMLJavaMappingRegistry xjmr, SOAPContext ctx) 138 throws IllegalArgumentException 139 { 140 Date date=null; 141 Calendar calDate = new GregorianCalendar(); 142 Element root = (Element)src; 143 String value = DOMUtils.getChildCharacterData(root); 144 if(value!=null && !((value=value.trim()).equals(""))) 145 { 146 try 147 { 148 synchronized(sdf) 149 { 150 date=sdf.parse(value); 151 } 152 calDate.setTime(date); 153 } 154 catch (ParseException pe) 155 { 156 throw new IllegalArgumentException ("String represents no valid Date for this Deserializer"); 157 } 158 } 159 return new Bean(java.util.GregorianCalendar .class, calDate); 160 } 161 } 162 | Popular Tags |