OSDN Git Service

Set optimal mime types and executable settings.
[mikumikustudio/MikuMikuStudio.git] / src / com / jmex / xml / types / SchemaCalendarBase.java
1 /**
2  * SchemaCalendarBase.java
3  *
4  * This file was generated by XMLSpy 2007sp2 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
14 package com.jmex.xml.types;
15
16 import java.text.DecimalFormat;
17 import java.text.SimpleDateFormat;
18 import java.util.Date;
19
20
21 public abstract class SchemaCalendarBase implements SchemaTypeCalendar {
22   public static final int TZ_MISSING = 0;
23   public static final int TZ_UTC = 1;
24   public static final int TZ_OFFSET = 2;
25
26   protected int year;
27   protected int month;
28   protected int day;
29   protected int hour;
30   protected int minute;
31   protected int second;
32   protected double partsecond; // 0.0 <= partsecond < 1.0
33   protected int hasTZ;
34   protected int offsetTZ; // offset in minutes
35   protected boolean isempty;
36     
37   protected final int DateTimePart_Year = 1 << 0;
38   protected final int DateTimePart_Month = 1 << 1;
39   protected final int DateTimePart_Day = 1 << 2;
40   protected final int DateTimePart_Date = DateTimePart_Year | DateTimePart_Month | DateTimePart_Day;
41   protected final int DateTimePart_Time = 1 << 3;
42   
43   // helper classes - int by reference
44   class RefInt
45   {
46           RefInt(int i) {value = i;}
47           public int value;
48   }
49   
50   // helper classes - parse context
51   class ParseContext
52   {
53     String source;
54     int index;
55     
56     public ParseContext(String source) 
57     {
58         this.source = source;
59         this.index = 0;
60     }
61     
62     public boolean isValid()
63     {
64         return index < source.length();
65     }
66     
67     public boolean check(char expect)
68     {
69         if (!isValid())
70                 return false;
71         return source.charAt(index) == expect;
72     }
73     
74     public boolean checkAndAdvance(char expect)
75     {
76         if (!check(expect))
77                 return false;
78         advance();
79         return true;
80     }
81     
82     public void advance()
83     {
84         ++index;
85     }
86     
87     public boolean readDigitAndAdvance(RefInt i, int scale, int maxdigit)
88     {
89         if (!isValid()) return false;
90         char c = source.charAt(index);
91         if (c<'0' || c>'9')
92                 return false;
93         int val = (int)c - (int)'0';
94         if (val > maxdigit)
95                 return false;
96         i.value += (val * scale);
97         advance();
98         return true;
99     }
100   }
101   
102   public SchemaCalendarBase() {
103     setEmpty();
104   }
105
106   public void setNull() {
107     setEmpty();
108   }
109
110   public void setEmpty() {
111     setInternalValues( 1,1,1, 0,0,0, 0.0, TZ_MISSING ,0 );
112           isempty = true;
113   }
114
115   public boolean equals(Object obj) {
116     if (! (obj instanceof SchemaCalendarBase))
117       return false;
118     SchemaCalendarBase dt = (SchemaCalendarBase)obj;
119     if( !(year == dt.year) )
120       return false;
121     if( !(month == dt.month) )
122       return false;
123     if( !(day == dt.day) )
124       return false;
125     if( !(hour == dt.hour) )
126       return false;
127     if( !(minute == dt.minute) )
128       return false;
129     if( !(second == dt.second) )
130       return false;
131     if( !(partsecond == dt.partsecond) )
132       return false;
133     if( !(hasTZ == dt.hasTZ) )
134       return false;
135     if( !(offsetTZ == dt.offsetTZ) )
136       return false;
137     return true;
138   }
139
140   public int hashCode() {
141     return (int) Double.doubleToLongBits(getApproximatedTotal());
142   }
143
144   public String toDateString() {
145     StringBuffer s = new StringBuffer();
146     s.append( new DecimalFormat("0000").format( (long)year ) );
147     s.append("-");
148     s.append( new DecimalFormat("00").format( (long)month) );
149     s.append("-");
150     s.append( new DecimalFormat("00").format( (long)day) );
151     return s.toString();
152   }
153
154   public String toTimeString() {
155     StringBuffer s = new StringBuffer();
156     s.append( new DecimalFormat("00").format( (long)hour) );
157     s.append(":");
158     s.append( new DecimalFormat("00").format( (long)minute) );
159     s.append(":");
160     s.append( new DecimalFormat("00").format( (long)second) );
161     if( partsecond > 0 && partsecond < 1) {
162       String sPartSecond = new DecimalFormat("0.0###############").format( partsecond );
163       s.append( "." );
164       s.append( sPartSecond.substring( 2, sPartSecond.length() ) );
165     }
166         if( hasTZ == TZ_UTC ) {
167       s.append("Z");
168         }
169     else if( hasTZ == TZ_OFFSET ) {
170       int absOffsetTZ = offsetTZ;
171       if (offsetTZ < 0) {
172         s.append("-");
173         absOffsetTZ = -offsetTZ;
174       }
175       else
176         s.append("+");
177       s.append(new DecimalFormat("00").format(absOffsetTZ / 60));
178       s.append(":");
179       s.append(new DecimalFormat("00").format(absOffsetTZ % 60));
180     }
181     return s.toString();
182   }
183
184   public int length() {
185     return toString().length();
186   }
187
188   public boolean booleanValue() {
189     return true;
190   }
191
192   public boolean isEmpty() {
193     return isempty;
194   }
195
196   public boolean isNull() {
197     return isEmpty();
198   }
199
200   public int compareTo(Object obj) {
201     return compareTo( (SchemaCalendarBase) obj);
202   }
203
204   public int compareTo(SchemaCalendarBase obj) {
205     return (int)(getApproximatedTotal()-obj.getApproximatedTotal());
206   }
207
208   protected void parseDate(String newvalue) throws StringParseException {
209         if (newvalue.length() == 0 )
210                 isempty = true;
211     else if (newvalue.length() < 10)
212       throw new StringParseException("date-part of string is too short", 0);
213     else {
214             try {
215               int nStart = 0;
216               if( newvalue.substring(0,1).equals("-") )
217                 nStart = 1;
218               year = Integer.parseInt(newvalue.substring(0, nStart+4));
219               if( !newvalue.substring(nStart+4, nStart+5).equals("-"))
220                 throw new StringParseException("invalid date format", 2);
221               month = Integer.parseInt(newvalue.substring(nStart+5, nStart+7));
222               if( !newvalue.substring(nStart+7, nStart+8).equals("-"))
223                 throw new StringParseException("invalid date format", 2);
224               day = Integer.parseInt(newvalue.substring(nStart+8, newvalue.length()));
225             }
226             catch (NumberFormatException e) {
227               throw new StringParseException("invalid date format", 2);
228             }
229             isempty = false;
230         }
231   }
232
233   protected void parseTime(String newvalue) throws StringParseException {
234     if (newvalue.length() < 8)
235       throw new StringParseException("time-part of string is too short", 0);
236     try {
237       int nStart = 0;
238       hour = Integer.parseInt(newvalue.substring(nStart, nStart+2));
239       if( !newvalue.substring(nStart+2, nStart+3).equals(":"))
240         throw new StringParseException("invalid date format", 2);
241       minute = Integer.parseInt(newvalue.substring(nStart+3, nStart+5));
242       if( !newvalue.substring(nStart+5, nStart+6).equals(":"))
243         throw new StringParseException("invalid date format", 2);
244       second = Integer.parseInt(newvalue.substring(nStart+6, nStart+8));
245       int nTZStartPosition = nStart+8;
246           partsecond = 0;
247       if (newvalue.length()>(nStart+8) ) {
248         nStart = nTZStartPosition;
249         int nEnd = newvalue.length();
250         int nMSecEnd = newvalue.indexOf("Z", nStart);
251         if( nMSecEnd > -1  &&  nMSecEnd < nEnd )
252           nEnd = nMSecEnd;
253         nMSecEnd = newvalue.indexOf("+", nStart);
254         if( nMSecEnd > -1  &&  nMSecEnd < nEnd )
255           nEnd = nMSecEnd;
256         nMSecEnd = newvalue.indexOf("-", nStart);
257         if( nMSecEnd > -1  &&  nMSecEnd < nEnd )
258           nEnd = nMSecEnd;
259         nTZStartPosition = nEnd;
260         partsecond = Double.parseDouble( "0" + newvalue.substring(nStart, nEnd));
261       }
262       hasTZ = TZ_MISSING;
263           offsetTZ = 0;
264       if (newvalue.length()>nTZStartPosition && newvalue.substring(nTZStartPosition, nTZStartPosition + 1).equals("Z"))
265         hasTZ = TZ_UTC;
266       else if (newvalue.length() == nTZStartPosition + 6) {
267         hasTZ = TZ_OFFSET;
268         offsetTZ = Integer.parseInt(newvalue.substring(nTZStartPosition+1, nTZStartPosition + 3)) * 60 +
269             Integer.parseInt(newvalue.substring(nTZStartPosition+4, nTZStartPosition + 6));
270         if( newvalue.substring(nTZStartPosition, nTZStartPosition+1).equals("-"))
271           offsetTZ = -offsetTZ;
272       }
273     }
274     catch (NumberFormatException e) {
275       throw new StringParseException("invalid number format", 2);
276     }
277     isempty = false;
278   }
279
280   protected boolean parseDateTime(String s, int part)
281   {
282           ParseContext context = new ParseContext(s);
283           
284           boolean bDatePart = ( part & DateTimePart_Date ) != 0;
285       boolean bTimePart = ( part & DateTimePart_Time ) != 0;
286         
287           RefInt year = new RefInt(0);
288           RefInt month = new RefInt(0);
289           RefInt day = new RefInt(0);
290           RefInt hour = new RefInt(0); 
291           RefInt minute = new RefInt(0);
292           double second = 0;
293         
294           hasTZ = TZ_MISSING;
295           offsetTZ = 0;
296         
297           if (bDatePart)
298           {
299                 // parse date
300                 boolean bNegative = context.checkAndAdvance('-');
301                 
302                 if ((part & DateTimePart_Year) != 0)
303                 {
304                         int digits = 0;
305                         RefInt temp = new RefInt(0);;
306                         while (context.readDigitAndAdvance(temp, 1, 9))
307                         {
308                                 year.value = year.value * 10 + temp.value;
309                                 digits += 1;
310                                 temp.value = 0;
311                                 if (digits >= 8) // overflow
312                                         return false;
313                         }
314                         if (digits < 4) // inalid
315                                 return false;
316                         if (digits > 4 && year.value < 10000)
317                                 return false;
318                         if (bNegative)
319                                 year.value = -year.value;
320                 }
321                 
322                 if ((part & (DateTimePart_Month | DateTimePart_Day)) != 0)
323                 {
324                         if ( !context.checkAndAdvance( '-' ) ) 
325                                 return false;
326
327                         if ( ( part & DateTimePart_Month ) != 0 )
328                         {
329                                 if ( !context.readDigitAndAdvance(month, 10, 1 ) ) return false;
330                                 if ( !context.readDigitAndAdvance(month, 1, month.value < 10 ? 9 : 2 ) ) return false;
331                                 if ( month.value == 0 ) return false;                                           
332                         }
333
334                         if ( ( part & DateTimePart_Day ) != 0 ) 
335                         {
336                                 if ( !context.checkAndAdvance('-') ) return false;
337
338                                 int maxFirstDigit = month.value != 2 ? 3 : 2;
339
340                                 // complicate things by making them complicated.
341                                 if ( !context.readDigitAndAdvance( day, 10, maxFirstDigit ) ) return false;
342                                 if ( !context.readDigitAndAdvance( day, 1, 9 ) ) return false;
343                                 if ( day.value == 0 || day.value > 31 ) return false;
344                                 
345                                 if ( ( part & DateTimePart_Month ) != 0 )
346                                 {
347                                         boolean b1 = month.value <= 7;
348                                         boolean b2 = ( month.value & 1 ) == 0;
349
350                                         // month 1, 3, 5, 7, 8, 10, 12
351                                         if ( b1 == b2 && day.value > 30 )
352                                                 return false;
353
354                                         // february.
355                                         if ( month.value == 2 && day.value > 29 )
356                                                 return false;
357
358                                         // leap years.
359                                         if (month.value == 2 && (part & DateTimePart_Year) != 0 && 
360                                                   (year.value % 4 != 0 || year.value % 100 == 0) && 
361                                                   year.value % 400 != 0 && day.value > 28)
362                                                 return false;
363                                 }
364                         }
365                 }               
366                 
367                 if ( bTimePart )
368                 {
369                         // a 'T' must follow
370                         if ( !context.checkAndAdvance('T') ) return false;
371                 }
372           }
373           
374           if ( bTimePart )
375                 {
376                         // check format here
377                         
378                         // hour from 0 to 2
379                         if ( !context.readDigitAndAdvance( hour, 10, 2 ) ) return false;
380                         if ( !context.readDigitAndAdvance( hour, 1, hour.value < 20 ? 9 : 4 ) ) return false;
381                         if ( !context.checkAndAdvance( ':' ) ) return false;
382                         int maxFirstDigit = hour.value == 24 ? 0 : 5;
383                         int maxSecondDigit = hour.value == 24 ? 0 : 9;
384                         if ( !context.readDigitAndAdvance( minute, 10, maxFirstDigit ) ) return false;
385                         if ( !context.readDigitAndAdvance( minute, 1, maxSecondDigit ) ) return false;
386                         if ( !context.checkAndAdvance( ':' ) ) return false;
387                         RefInt secondInt = new RefInt (0);
388                         if ( !context.readDigitAndAdvance( secondInt, 10, maxFirstDigit ) ) return false;
389                         if ( !context.readDigitAndAdvance( secondInt, 1, maxSecondDigit ) ) return false;
390
391                         second = secondInt.value;
392
393                         if ( context.checkAndAdvance( '.' ) )
394                         {
395                                 // fraction. do whatever seems fit.
396                                 RefInt val = new RefInt(0);
397                                 int digits = 0;
398                                 while ( context.readDigitAndAdvance( val, 1, 9) )
399                                 {
400                                         val.value *= 10;
401                                         digits += 1;
402                                         if ( digits >= 8 ) // precision loss - ignore
403                                                 break;
404                                 }
405
406                                 if ( digits == 0 )
407                                         return false;
408
409                                 second += val.value * Math.pow( 10.0, -digits - 1 );
410
411                                 // skip any further digits.
412                                 while ( context.readDigitAndAdvance( val, 0, 9) ) 
413                                         ;               
414                         }
415                 }
416           
417             //   timezone
418                 if ( context.checkAndAdvance('Z') )
419                 {
420                         // timezone specified, it is UTC.
421                         hasTZ = TZ_UTC;
422                         offsetTZ = 0;                           
423                 }
424                 else if ( context.check('+') || context.check('-' ) )
425                 {
426                         // timezone offset, in hour:minute format
427                         boolean bNegative = context.check('-');
428                         context.advance();
429                         
430                         // do not check the hour part, for those who are obscure.
431                         RefInt temp = new RefInt(0);
432                         if ( !context.readDigitAndAdvance( temp, 600, 9 ) ) return false;
433                         if ( !context.readDigitAndAdvance( temp, 60, 9 ) ) return false;
434                         if ( !context.checkAndAdvance( ':' ) ) return false;
435                         if ( !context.readDigitAndAdvance( temp, 10, 5 ) ) return false;
436                         if ( !context.readDigitAndAdvance( temp, 1, 9 ) ) return false;
437
438                         hasTZ = TZ_OFFSET;
439                         offsetTZ = bNegative ? -temp.value : temp.value;
440                 }
441                 
442                 if ( context.isValid() )
443                         return false;
444
445         setInternalValues(year.value, month.value, day.value, hour.value, minute.value, (int)second, ((second * 1000) % 1000)/1000, hasTZ, offsetTZ);
446                 
447                 return true;
448         }
449   
450   public double getApproximatedTotal() {
451     // approximated because month is approximated to have always 31 days.
452     return (double)second + 60.0*((double)minute + 60.0*((double)hour + 24.0*((double)day + 31.0*((double)month + 12.0*(double)year)))) + partsecond;
453   }
454
455   static int[] monthStart = new int[]
456         {
457                 0,      // 1st of Jan
458                 31,     // 1st of Feb
459                 59,     // 1st of Mar
460                 90,     // 1st of Apr
461                 120, // 1st of May
462                 151, // 1st of Jun
463                 181, // 1st of Jul
464                 212, // 1st of Aug
465                 243, // 1st of Sep
466                 273, // 1st of Oct
467                 304, // 1st of Nov
468                 334, // 1st of Dec
469                 365, // 1st of next year
470         };
471
472   static int[] monthStartLeap = new int[]
473         {
474                 0,      // 1st of Jan
475                 31,     // 1st of Feb
476                 60,     // 1st of Mar
477                 91,     // 1st of Apr
478                 121, // 1st of May
479                 152, // 1st of Jun
480                 182, // 1st of Jul
481                 213, // 1st of Aug
482                 244, // 1st of Sep
483                 274, // 1st of Oct
484                 305, // 1st of Nov
485                 335, // 1st of Dec
486                 366, // 1st of next year
487         };
488
489   public long getTimeValue()
490   {
491         long value = 0;
492
493     int realYear = year;
494     int monthRem = ( month - 1 ) % 12;
495         if ( monthRem < 0 ) monthRem += 12;
496     realYear += ( month - monthRem - 1 ) / 12;  
497         
498         int yearRed = realYear - 1;
499     int yearRem = yearRed % 400;
500         if ( yearRem < 0 ) yearRem += 400;
501     value += ( ( yearRed - yearRem ) / 400 ) * (303 * 365 + 97 * 366);
502         value += ( yearRem / 100 ) * (76 * 365 + 24 * 366);
503         yearRem %= 100;
504         value += ( yearRem / 4 ) * (365 * 3 + 366);
505         yearRem %= 4;
506         value += yearRem * 365; 
507
508         if ( ( realYear % 4 == 0 ) && ( ( realYear % 100 != 0 ) || ( realYear % 400 == 0 ) ) )
509                 value += monthStartLeap[ monthRem ];
510         else
511                 value += monthStart[ monthRem ];
512
513         value += day - 1;
514
515         value = value * 24 + hour;
516         value = value * 60 + minute - offsetTZ;
517         value = ( long )( ( value * 60 + second + partsecond ) * 1000 );
518         return value;
519   }
520
521   public void setTimeFromTimeValue(long tv)
522   {
523         tv %= (86400 * 1000);
524         if ( tv < 0 )
525                 tv += 86400 * 1000;
526
527     partsecond = (tv % 1000) / 1000.0;
528     tv /= 1000;
529         second = (int)(tv % 60);
530         tv /= 60;
531         minute = (int) (tv % 60);
532         tv /= 60;
533         hour = (int) (tv % 24);
534   }
535
536   public void setDateFromTimeValue(long tv)
537   {
538         long dayTime = tv % ( 86400 * 1000 );
539         int days = (int)(tv / (86400 * 1000));
540
541         if ( dayTime < 0 )              
542                 days -= 1;
543         
544         year = days < 0 ? -1 : +1; // there is no year 0
545         month = 1;
546         day = 1;
547
548         year += 400 * ( days / (303 * 365 + 97 * 366) );
549         days %= (303 * 365 + 97 * 366);
550         if ( days == (303 * 365 + 97 * 366) - 1 )
551         {
552                 year += 399; 
553                 days = 365;
554         }
555         else
556         {
557                 year += 100 * ( days / (76 * 365 + 24 * 366) );
558                 days %= (76 * 365 + 24 * 366);
559                 year += 4 * ( days / (365 * 3 + 366) );
560                 days %= (365 * 3 + 366);
561                 if ( days == (365 * 3 + 366) - 1 ) // last day of leap year
562                 {
563                         year += 3; 
564                         days = 365;
565                 }
566                 else
567                 {
568                         year += days / 365;
569                         days %= 365;
570                 }
571         }
572
573         if ( days < 0 )
574                 days += 365;    
575
576         int[] monthStarts;
577         if ( ( year % 4 == 0 ) && ( ( year % 100 != 0 ) || ( year % 400 == 0 ) ) )
578                 monthStarts = monthStartLeap;
579         else
580                 monthStarts = monthStart;
581
582         while ( days >= monthStarts[ month ] )
583                 ++month;
584         
585         day = days - monthStarts[ month - 1 ] + 1;
586   }
587
588   public Date getDate() {
589         String s = toDateString() + " " + toTimeString();
590         try {
591                 return (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(s));
592         } catch( java.text.ParseException e ) {
593                 throw new StringParseException("Could not convert to date.", 0);
594         }
595   }
596
597   protected void setInternalValues(int newyear, int newmonth, int newday, int newhour, int newminute, int newsecond, double newpartsecond, int newhasTZ, int newoffsetTZ) {
598     year = newyear;
599     month = newmonth;
600     day = newday;
601     hour = newhour;
602     minute = newminute;
603     second = newsecond;
604     partsecond = newpartsecond;
605     hasTZ = newhasTZ;
606     offsetTZ = newoffsetTZ;
607     isempty = false;
608   }
609
610   // ---------- interface SchemaTypeCalendar ----------
611   public SchemaDuration durationValue() {
612     throw new TypesIncompatibleException(this, new SchemaDuration( "PT" ) );
613   }
614
615   
616   
617 }