home *** CD-ROM | disk | FTP | other *** search
/ Internet Magazine 2003 October / INTERNET108.ISO / pc / software / windows / building / xmlspy / xmlspyentcomplete5.exe / Data1.cab / _5F9AE45164CC4784AC82F030915FD1EF < prev    next >
Encoding:
Text File  |  2003-04-03  |  3.5 KB  |  143 lines

  1. /**
  2.  * SchemaDateTime.java
  3.  *
  4.  * This file was generated by XMLSPY 5 Enterprise Edition.
  5.  *
  6.  * YOU SHOULD NOT MODIFY THIS FILE, BECAUSE IT WILL BE
  7.  * OVERWRITTEN WHEN YOU RE-RUN CODE GENERATION.
  8.  *
  9.  * Refer to the XMLSPY Documentation for further details.
  10.  * http://www.altova.com/xmlspy
  11.  */
  12.  
  13. package com.altova.xml.types;
  14.  
  15. import java.util.Date;
  16. import java.util.Calendar;
  17. import java.text.SimpleDateFormat;
  18. import java.text.ParseException;
  19.  
  20. public class SchemaDateTime implements SchemaType {
  21.     protected static final String format = "yyyy-MM-dd'T'HH:mm:ss";
  22.     protected static final String formatTZ = "yyyy-MM-dd'T'HH:mm:ssZ";
  23.  
  24.     protected Calendar value;
  25.     protected boolean hasTZ;
  26.  
  27.     public static SchemaDateTime now() {
  28.         return new SchemaDateTime(Calendar.getInstance());
  29.     }
  30.  
  31.     protected static int stripMsec(StringBuffer sb) {
  32.         if (sb.length() == 0 || sb.charAt(0) != '.')
  33.             return 0;
  34.         int i = 1;
  35.         while (i < sb.length() && "0123456789".indexOf(sb.charAt(i)) != -1)
  36.             i++;
  37.         int msec = Math.round(Float.parseFloat(sb.substring(0, i)) * 1000);
  38.         sb.delete(0, i);
  39.         return msec;
  40.     }
  41.  
  42.     protected static String parseTZ(StringBuffer sb) {
  43.         if (sb.length() == 1 && sb.charAt(0) == 'Z')
  44.             return "+0000";
  45.         else if (sb.length() == 6) // like "+01:00"
  46.             return sb.substring(0, 3) + sb.substring(4, 6); // like "+0100"
  47.         else
  48.             return null;
  49.     }
  50.  
  51.     protected void parse(String newvalue) throws ParseException {
  52.         int msec = 0;
  53.         hasTZ = false;
  54.         if (newvalue.length() > 19) {
  55.             StringBuffer s = new StringBuffer(newvalue.substring(19));
  56.             newvalue = newvalue.substring(0, 19);
  57.  
  58.             msec = stripMsec(s);
  59.             String sTZ = parseTZ(s);
  60.             if (sTZ != null) {
  61.                 hasTZ = true;
  62.                 newvalue += sTZ;
  63.             }
  64.         }
  65.         value.setTime(new SimpleDateFormat(hasTZ ? formatTZ : format).parse(newvalue));
  66.         if (msec != 0)
  67.             value.set(Calendar.MILLISECOND, msec);
  68.     }
  69.  
  70.     public SchemaDateTime(SchemaDateTime newvalue) {
  71.         value = newvalue.value;
  72.         hasTZ = newvalue.hasTZ;
  73.     }
  74.  
  75.     public SchemaDateTime(Calendar newvalue) {
  76.         value = newvalue;
  77.         hasTZ = true;
  78.     }
  79.  
  80.     public SchemaDateTime(String newvalue) {
  81.         try {
  82.             value = Calendar.getInstance();
  83.             parse(newvalue);
  84.         } catch (ParseException e) {
  85.             throw new com.altova.xml.XmlException(e);
  86.         }
  87.     }
  88.  
  89.     public Calendar getValue() {
  90.         return value;
  91.     }
  92.  
  93.     public void setValue(Calendar newvalue) {
  94.         value = newvalue;
  95.         hasTZ = true;
  96.     }
  97.  
  98.     public void setValue(String newvalue) throws ParseException {
  99.         parse(newvalue);
  100.     }
  101.  
  102.     public int hashCode() {
  103.         return value.hashCode();
  104.     }
  105.  
  106.     public boolean equals(Object obj) {
  107.         if (!(obj instanceof SchemaDateTime))
  108.             return false;
  109.         return value.equals(((SchemaDateTime)obj).value);
  110.     }
  111.  
  112.     public Object clone() {
  113.         return new SchemaDateTime((Calendar)value.clone());
  114.     }
  115.  
  116.     public String toString() {
  117.         StringBuffer s = new StringBuffer(
  118.                 new SimpleDateFormat(format).format(value.getTime())
  119.                 );
  120.         if (value.get(Calendar.MILLISECOND) != 0)
  121.             s.append('.').append(new SimpleDateFormat("SSS").format(value.getTime()));
  122.         if (hasTZ) {
  123.             String tz = new SimpleDateFormat("Z").format(value.getTime()); // "+0100"
  124.             s.append(tz.substring(0, 3));
  125.             s.append(':');
  126.             s.append(tz.substring(3, 5));
  127.         }
  128.         return s.toString();
  129.     }
  130.  
  131.     public String asString() {
  132.         return toString();
  133.     }
  134.  
  135.     public int compareTo(Object obj) {
  136.         return compareTo((SchemaDateTime)obj);
  137.     }
  138.  
  139.     public int compareTo(SchemaDateTime obj) {
  140.         return value.getTime().compareTo(obj.value.getTime());
  141.     }
  142. }
  143.