home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 9 / IOPROG_9.ISO / contrib / iis4 / iis4_07.cab / VariantToJava.java < prev    next >
Encoding:
Java Source  |  1997-09-04  |  6.7 KB  |  251 lines

  1. /*    ********************************************************************************
  2.     VariantToJava.java *************************************************************
  3.     ********************************************************************************  */
  4.  
  5. package aspcomp;
  6.  
  7. import com.ms.com.*;
  8. import java.util.*;
  9. import com.ms.asp.*;
  10.  
  11. /**
  12.  * Conversion routines.
  13.  */
  14.  
  15. public class VariantToJava 
  16. {
  17.  
  18. /*    ********************************************************************************
  19.     Internal (Private) Variables ***************************************************
  20.     ********************************************************************************  */
  21.  
  22.     // Half a second, expressed in days
  23.     private static double HALF_SECOND = (1.0/172800.0);
  24.     private static int rgMonthDays[] =
  25.         {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
  26.  
  27.  
  28. /*    ********************************************************************************
  29.     External (Public) Methods ******************************************************
  30.     ********************************************************************************  */
  31.  
  32.     public static boolean getBoolean(Variant v)        
  33.         throws ClassCastException 
  34.     {
  35.         return v.getBoolean();
  36.     }
  37.  
  38.     public static byte getByte(Variant v)            
  39.         throws ClassCastException 
  40.     {
  41.         return v.getByte();
  42.     }
  43.  
  44.     public static short getShort(Variant v)            
  45.         throws ClassCastException 
  46.     {
  47.         return v.getShort();
  48.     }
  49.  
  50.     public static char getChar(Variant v)            
  51.         throws ClassCastException 
  52.     {
  53.         return (char)v.getShort();
  54.     }
  55.  
  56.     public static int getInt(Variant v)                
  57.         throws ClassCastException 
  58.     {
  59.         return v.getInt();
  60.     }
  61.  
  62.     public static long getLong(Variant v)            
  63.         throws ClassCastException 
  64.     {
  65.         return (long)v.getInt();
  66.     }
  67.  
  68.     public static float getFloat(Variant v)            
  69.         throws ClassCastException 
  70.     {
  71.         return v.getFloat();
  72.     }
  73.  
  74.     public static double getDouble(Variant v)        
  75.         throws ClassCastException 
  76.     {
  77.         return v.getDouble();
  78.     }
  79.  
  80.     public static String getString(Variant v)        
  81.         throws ClassCastException 
  82.     {
  83.         if (v.getvt() != v.VariantString) {
  84.             if (v.getvt() == v.VariantDispatch) {
  85.                 IStringList slist = (IStringList)v.getDispatch();
  86.                 Variant p = new Variant();
  87.                 p.noParam();
  88.                 v = slist.getItem(p);
  89.                 if (v.getvt() == v.VariantString)
  90.                     return v.getString();
  91.             }
  92.             throw new ClassCastException();
  93.         }
  94.         return v.getString();
  95.     }
  96.  
  97.     public static Object getObject(Variant v)        
  98.         throws ClassCastException 
  99.     {
  100.         switch(v.getvt()) {
  101.         case Variant.VariantString:
  102.             String s = v.getString();
  103.             return s;
  104.         case Variant.VariantObject:
  105.             return v.getObject();
  106.         default:
  107.             break;
  108.         }
  109.         throw new ClassCastException();
  110.     }
  111.  
  112.     public static Date getDate(Variant v)            
  113.         throws ClassCastException 
  114.     {
  115.         // source code copied from MFC 4.21 and modified
  116.  
  117.         long nDays;                    // Number of days since Dec. 30, 1899
  118.         long nDaysAbsolute;            // Number of days since 1/1/0
  119.         long nSecsInDay;            // Time in seconds since midnight
  120.         long nMinutesInDay;            // Minutes in day
  121.  
  122.         long n400Years;                // Number of 400 year increments since 1/1/0
  123.         long n400Century;            // Century within 400 year block (0,1,2 or 3)
  124.         long n4Years;                // Number of 4 year increments since 1/1/0
  125.         long n4Day;                    // Day within 4 year block
  126.                                     //  (0 is 1/1/yr1, 1460 is 12/31/yr4)
  127.         long n4Yr;                    // Year within 4 year block (0,1,2 or 3)
  128.         boolean bLeap4 = true;        // TRUE if 4 year block includes leap year
  129.  
  130.         // values in terms of year month date.
  131.         int tm_sec;   
  132.         int tm_min;   
  133.         int tm_hour;  
  134.         int tm_mday;  
  135.         int tm_mon;   
  136.         int tm_year;  
  137.         int tm_wday;  
  138.         int tm_yday;  
  139.  
  140.         if (v.getvt() != v.VariantDate)
  141.             throw new ClassCastException();
  142.  
  143.         double dtSrc = v.getDate();
  144.         double dblDate = dtSrc; // temporary serial date
  145.  
  146.         // If a valid date, then this conversion should not overflow
  147.         nDays = (long)dblDate;
  148.  
  149.         // Round to the second
  150.         dblDate += ((dtSrc > 0.0) ? HALF_SECOND : -HALF_SECOND);
  151.  
  152.         // Add days from 1/1/0 to 12/30/1899
  153.         nDaysAbsolute = (long)dblDate + 693959L; 
  154.  
  155.         dblDate = Math.abs(dblDate);
  156.         nSecsInDay = (long)((dblDate - Math.floor(dblDate)) * 86400.);
  157.  
  158.         // Leap years every 4 yrs except centuries not multiples of 400.
  159.         n400Years = (long)(nDaysAbsolute / 146097L);
  160.  
  161.         // Set nDaysAbsolute to day within 400-year block
  162.         nDaysAbsolute %= 146097L;
  163.  
  164.         // -1 because first century has extra day
  165.         n400Century = (long)((nDaysAbsolute - 1) / 36524L);
  166.  
  167.         // Non-leap century
  168.         if (n400Century != 0)
  169.         {
  170.             // Set nDaysAbsolute to day within century
  171.             nDaysAbsolute = (nDaysAbsolute - 1) % 36524L;
  172.  
  173.             // +1 because 1st 4 year increment has 1460 days
  174.             n4Years = (long)((nDaysAbsolute + 1) / 1461L);
  175.  
  176.             if (n4Years != 0)
  177.                 n4Day = (long)((nDaysAbsolute + 1) % 1461L);
  178.             else
  179.             {
  180.                 bLeap4 = false;
  181.                 n4Day = (long)nDaysAbsolute;
  182.             }
  183.         }
  184.         else
  185.         {
  186.             // Leap century - not special case!
  187.             n4Years = (long)(nDaysAbsolute / 1461L);
  188.             n4Day = (long)(nDaysAbsolute % 1461L);
  189.         }
  190.  
  191.         if (bLeap4)
  192.         {
  193.             // -1 because first year has 366 days
  194.             n4Yr = (n4Day - 1) / 365;
  195.  
  196.             if (n4Yr != 0)
  197.                 n4Day = (n4Day - 1) % 365;
  198.         }
  199.         else
  200.         {
  201.             n4Yr = n4Day / 365;
  202.             n4Day %= 365;
  203.         }
  204.  
  205.         tm_year = (int)(n400Years * 400 + n400Century * 100 + 
  206.                         n4Years * 4 + n4Yr);
  207.  
  208.         // Handle leap year: before, on, and after Feb. 29.
  209.         if (n4Yr == 0 && bLeap4 && n4Day == 59)
  210.         {
  211.             /* Feb. 29 */ 
  212.             tm_mon = 2;
  213.             tm_mday = 29;
  214.         }
  215.         else 
  216.         {
  217.             if (n4Yr == 0 && bLeap4 && n4Day >= 59)
  218.                 --n4Day;
  219.  
  220.             // Make n4DaY a 1-based day of non-leap year and compute
  221.             //  month/day for everything but Feb. 29.
  222.             ++n4Day;
  223.  
  224.             // Month number always >= n/32, so save some loop time */ 
  225.             for (tm_mon = (int)((n4Day >> 5) + 1);
  226.                  n4Day > rgMonthDays[tm_mon]; tm_mon++);
  227.  
  228.             tm_mday = (int)(n4Day - rgMonthDays[tm_mon-1]);
  229.         }
  230.  
  231.         if (nSecsInDay == 0)
  232.             tm_hour = tm_min = tm_sec = 0;
  233.         else
  234.         {
  235.             tm_sec = (int)(nSecsInDay % 60L);
  236.             nMinutesInDay = nSecsInDay / 60L;
  237.             tm_min = (int)(nMinutesInDay % 60);
  238.             tm_hour = (int)(nMinutesInDay / 60);
  239.         }
  240.  
  241.         String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
  242.         if (ids.length == 0) throw new AspComponentException(AspLocalizedStrings.ASP_E_INTERNAL_FAILURE);
  243.         SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);
  244.         pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
  245.         pdt.setEndRule  (Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
  246.         GregorianCalendar calendar = new GregorianCalendar(pdt);
  247.         calendar.set(tm_year, tm_mon - 1, tm_mday, tm_hour, tm_min, tm_sec);
  248.         return calendar.getTime();
  249.     }
  250. }
  251.