axis-java-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ru...@apache.org
Subject cvs commit: xml-axis/java/test/encoding TestDeser2001.java
Date Mon, 21 May 2001 00:44:43 GMT
rubys       01/05/20 17:44:43

  Modified:    java/src/org/apache/axis/encoding DateSerializer.java
               java/test/encoding TestDeser2001.java
  Log:
  Handle optional miliseconds and timezone info on the way in
  
  Revision  Changes    Path
  1.4       +69 -1     xml-axis/java/src/org/apache/axis/encoding/DateSerializer.java
  
  Index: DateSerializer.java
  ===================================================================
  RCS file: /home/cvs/xml-axis/java/src/org/apache/axis/encoding/DateSerializer.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- DateSerializer.java	2001/05/20 22:14:05	1.3
  +++ DateSerializer.java	2001/05/21 00:44:43	1.4
  @@ -59,6 +59,7 @@
   import java.text.SimpleDateFormat;
   import java.util.TimeZone;
   import java.util.Date;
  +import java.util.Calendar;
   import java.text.DateFormat;
   
   import org.apache.axis.encoding.*;
  @@ -70,11 +71,13 @@
    * General purpose serializer/deserializerFactory for an arbitrary java bean.
    *
    * @author Sam Ruby <rubys@us.ibm.com>
  + * @see <a href="http://www.w3.org/TR/2001/PR-xmlschema-2-20010330/#dateTime">XML
Schema 3.2.7</a>
    */
   public class DateSerializer implements Serializer {
   
       private static SimpleDateFormat zulu = 
         new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
  +                        //  0123456789 0 123456789
   
       static {
         zulu.setTimeZone(TimeZone.getTimeZone("GMT"));
  @@ -82,11 +85,76 @@
   
       static class DateDeser extends SOAPTypeMappingRegistry.BasicDeser {
           Object makeValue(String source) { 
  +          Date result;
  +
  +          // validate fixed portion of format
  +          if (source.length() < 19) 
  +            throw new NumberFormatException("invalid dateTime");
  +
  +          if (source.charAt(4) != '-' || source.charAt(7) != '-' ||
  +              source.charAt(10) != 'T')
  +            throw new NumberFormatException("invalid date");
  +
  +          if (source.charAt(13) != ':' || source.charAt(16) != ':')
  +            throw new NumberFormatException("invalid time");
  +
  +          // convert what we have validated so far
             try {
  -            return zulu.parse(source); 
  +            result=zulu.parse(source.substring(0,19)+".000Z"); 
             } catch (Exception e) {
               throw new NumberFormatException(e.toString());
             }
  +
  +          int pos = 19;
  +
  +          // parse optional milliseconds
  +          if (pos < source.length() && source.charAt(pos)=='.') {
  +            int milliseconds = 0;
  +            int start = ++pos;
  +            while (pos<source.length() && Character.isDigit(source.charAt(pos)))
  +              pos++;
  +
  +            String decimal=source.substring(start,pos);
  +            if (decimal.length()==3) {
  +              milliseconds=Integer.parseInt(decimal);
  +            } else if (decimal.length() < 3) {
  +              milliseconds=Integer.parseInt((decimal+"000").substring(0,3));
  +            } else {
  +              milliseconds=Integer.parseInt(decimal.substring(0,3));
  +              if (decimal.charAt(3)>='5') ++milliseconds;
  +            }
  +
  +            // add milliseconds to the current result
  +            result.setTime(result.getTime()+milliseconds);
  +          }
  +
  +          // parse optional timezone
  +          if (pos+5 < source.length() &&
  +               (source.charAt(pos)=='+' || (source.charAt(pos)=='-')))
  +          {
  +            if (!Character.isDigit(source.charAt(pos+1)) || 
  +                !Character.isDigit(source.charAt(pos+2)) || 
  +                source.charAt(pos+3) != ':'              || 
  +                !Character.isDigit(source.charAt(pos+4)) || 
  +                !Character.isDigit(source.charAt(pos+5)))
  +               throw new NumberFormatException("invalid timezone");
  +
  +            int hours = (source.charAt(pos+1)-'0')*10+source.charAt(pos+2)-'0';
  +            int mins  = (source.charAt(pos+4)-'0')*10+source.charAt(pos+5)-'0';
  +            int milliseconds = (hours*60+mins)*60*1000;
  +
  +            // subtract milliseconds from the current result to obtain GMT
  +            if (source.charAt(pos)=='+') milliseconds=-milliseconds;
  +            result.setTime(result.getTime()+milliseconds);
  +            pos+=6;  
  +          }
  +
  +          if (pos < source.length() && source.charAt(pos)=='Z') pos++;
  +
  +          if (pos < source.length())
  +            throw new NumberFormatException("unexpected characters");
  +
  +          return result;
           }
       }
   
  
  
  
  1.3       +23 -1     xml-axis/java/test/encoding/TestDeser2001.java
  
  Index: TestDeser2001.java
  ===================================================================
  RCS file: /home/cvs/xml-axis/java/test/encoding/TestDeser2001.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- TestDeser2001.java	2001/05/20 22:14:06	1.2
  +++ TestDeser2001.java	2001/05/21 00:44:43	1.3
  @@ -18,13 +18,35 @@
       /** 
        * Test deserialization of Date responses
        */
  -    public void testDate() {
  +    public void testMinDate() {
           Calendar date = Calendar.getInstance();
           date.set(1999,04,31,12,01,30);
           date.setTimeZone(TimeZone.getTimeZone("GMT"));
  +        date.set(Calendar.MILLISECOND,0);
  +        deserialize("<result xsi:type=\"xsd:dateTime\">" + 
  +                       "1999-05-31T12:01:30" + 
  +                     "</result>",
  +                     date.getTime());
  +    }
  +
  +    public void testDateZ() {
  +        Calendar date = Calendar.getInstance();
  +        date.set(1999,04,31,12,01,30);
  +        date.setTimeZone(TimeZone.getTimeZone("GMT"));
           date.set(Calendar.MILLISECOND,150);
           deserialize("<result xsi:type=\"xsd:dateTime\">" + 
                          "1999-05-31T12:01:30.150Z" + 
  +                     "</result>",
  +                     date.getTime());
  +    }
  +
  +    public void testDateTZ() {
  +        Calendar date = Calendar.getInstance();
  +        date.set(1999,04,31,12,01,30);
  +        date.setTimeZone(TimeZone.getTimeZone("GMT-05:00"));
  +        date.set(Calendar.MILLISECOND,150);
  +        deserialize("<result xsi:type=\"xsd:dateTime\">" + 
  +                       "1999-05-31T12:01:30.150-05:00" + 
                        "</result>",
                        date.getTime());
       }
  
  
  

Mime
View raw message