OSDN Git Service

Set optimal mime types and executable settings.
[mikumikustudio/MikuMikuStudio.git] / src / com / jmex / xml / types / SchemaDuration.java
1 /**
2  * SchemaDuration.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
18
19 public class SchemaDuration extends SchemaCalendarBase {
20   protected boolean bNegative;
21
22   // construction
23   public SchemaDuration() {
24     setEmpty();
25   }
26
27   public SchemaDuration(SchemaDuration newvalue) {
28     year = newvalue.year;
29     month = newvalue.month;
30     day = newvalue.day;
31     hour = newvalue.hour;
32     minute = newvalue.minute;
33     second = newvalue.second;
34     partsecond = newvalue.partsecond;
35     hasTZ = newvalue.hasTZ;
36     offsetTZ = newvalue.offsetTZ;
37         bNegative = newvalue.isNegative();
38
39     isempty = newvalue.isempty;
40   }
41
42   public SchemaDuration(int newyear, int newmonth, int newday, int newhour, int newminute, int newsecond, double newpartsecond, boolean newisnegative) {
43     setYear(newyear);
44     setMonth(newmonth);
45     setDay(newday);
46     setHour(newhour);
47     setMinute(newminute);
48     setSecond(newsecond);
49     setPartSecond(newpartsecond);
50     bNegative = newisnegative;
51
52     isempty = false;
53   }
54
55   public SchemaDuration(String newvalue) {
56     parse(newvalue);
57   }
58
59   public SchemaDuration(SchemaType newvalue) {
60     assign( newvalue );
61   }
62
63   public SchemaDuration(SchemaTypeCalendar newvalue) {
64     assign( (SchemaType)newvalue );
65   }
66
67   // setValue, getValue
68   public int getYear() {
69     return year;
70   }
71
72   public int getMonth() {
73     return month;
74   }
75
76   public int getDay() {
77     return day;
78   }
79
80   public int getHour() {
81     return hour;
82   }
83
84   public int getMinute() {
85     return minute;
86   }
87
88   public int getSecond() {
89     return second;
90   }
91
92   public double getPartSecond() {
93     return partsecond;
94   }
95
96   public int getMillisecond() {
97     return (int)java.lang.Math.round(partsecond*1000.0);
98   }
99
100   public boolean isNegative() {
101     return bNegative;
102   }
103
104   public void setYear(int newyear) {
105     year = newyear;
106     isempty = false;
107   }
108
109   public void setMonth(int newmonth) {
110     month = newmonth;
111     isempty = false;
112   }
113
114   public void setDay(int newday) {
115     day = newday;
116     isempty = false;
117   }
118
119   public void setHour(int newhour) {
120     hour = newhour;
121     isempty = false;
122   }
123
124   public void setMinute(int newminute) {
125     minute = newminute;
126     isempty = false;
127   }
128
129   public void setSecond(int newsecond) {
130     second = newsecond;
131     isempty = false;
132   }
133
134   public void setPartSecond(double newpartsecond) {
135     partsecond = newpartsecond;
136     isempty = false;
137   }
138
139   public void setMillisecond(int newmillisecond) {
140     int normalizedMSec = newmillisecond;
141     // must normallize, because 0 <= partseconds < 1
142     if (normalizedMSec < 0) {
143       int neededSeconds = newmillisecond / 1000 + 1;
144       normalizedMSec = neededSeconds * 1000 + newmillisecond;
145       if (!bNegative)
146         second -= neededSeconds;
147       else
148         second += neededSeconds - 1;
149     }
150     if (normalizedMSec >= 1000) {
151       int overflowSeconds = normalizedMSec / 1000;
152       normalizedMSec = normalizedMSec % 1000;
153       if (!bNegative)
154         second += overflowSeconds;
155       else
156         second -= overflowSeconds;
157     }
158     partsecond = (double) normalizedMSec / 1000;
159     isempty = false;
160   }
161
162   public void setNegative(boolean newisnegative) {
163     bNegative = newisnegative;
164     isempty = false;
165   }
166
167
168   public void assign(SchemaType newvalue) {
169     if( newvalue == null || newvalue.isNull() || newvalue.isEmpty() )
170       setEmpty();
171     else if (newvalue instanceof SchemaDuration) {
172       setInternalValues( ( (SchemaDuration) newvalue).year,
173                         ( (SchemaDuration) newvalue).month,
174                         ( (SchemaDuration) newvalue).day,
175                         ( (SchemaDuration) newvalue).hour,
176                         ( (SchemaDuration) newvalue).minute,
177                         ( (SchemaDuration) newvalue).second,
178                         ( (SchemaDuration) newvalue).partsecond, SchemaCalendarBase.TZ_MISSING, 0);
179       bNegative = ( (SchemaDuration) newvalue).bNegative;
180     }
181     else if (newvalue instanceof SchemaString)
182       parse(newvalue.toString());
183     else
184       throw new TypesIncompatibleException(newvalue, this);
185   }
186
187   public void setEmpty() {
188     setInternalValues( 0,0,0, 0,0,0, 0.0, SchemaCalendarBase.TZ_MISSING, 0 );
189     bNegative = false;
190         isempty = true;
191   }
192
193   // further
194   public Object clone() {
195     return new SchemaDuration( this );
196   }
197
198   public String toString() {
199     if( isempty )
200       return "";
201
202     StringBuffer s = new StringBuffer();
203     if (bNegative)
204       s.append("-");
205     s.append("P");
206     if (year != 0) {
207       s.append(new DecimalFormat("0").format( (long) year));
208       s.append("Y");
209     }
210     if (month != 0) {
211       s.append(new DecimalFormat("0").format( (long) month));
212       s.append("M");
213     }
214     if (day != 0) {
215       s.append(new DecimalFormat("0").format( (long) day));
216       s.append("D");
217     }
218         if (hour!=0 || minute!=0 || second!=0 || partsecond>0 ) {
219         s.append("T");
220             if (hour != 0) {
221                 s.append(new DecimalFormat("0").format( (long) hour));
222                 s.append("H");
223         }
224             if (minute != 0) {
225                 s.append(new DecimalFormat("0").format( (long) minute));
226                 s.append("M");
227         }
228             if (second != 0)
229                 s.append(new DecimalFormat("0").format( (long) second));
230             if (partsecond > 0 && partsecond < 1) {
231                 String sPartSecond = new DecimalFormat("0.0###############").format(partsecond);
232                 s.append(".");
233                 s.append(sPartSecond.substring(2, sPartSecond.length()));
234             }
235             if (second != 0 || (partsecond > 0 && partsecond < 1))
236                 s.append("S");
237         }
238     if (s.length() == 1)
239                 s.append("T0S");
240     return s.toString();
241   }
242
243   public boolean booleanValue() {
244     return true;
245   }
246
247   public void parse(String s) {
248           
249         String newvalue = SchemaNormalizedString.normalize(SchemaNormalizedString.WHITESPACE_COLLAPSE, s);
250
251         if ( newvalue == null )
252         {
253                 isempty = true;
254                 return;
255         }
256         else if ( newvalue.length() == 0 )
257         {
258                 isempty = true;
259                 return;
260         }
261
262         int pos = 0;
263         bNegative = false;
264         day = 0;
265         hour = 0;
266         minute = 0;
267         second = 0;
268         partsecond = 0.0;
269         month = 0;      
270         year = 0;
271                         
272         if (newvalue.charAt(pos) == '-') 
273         {
274                 bNegative = true;
275                 ++pos;
276         }
277
278         if (pos == newvalue.length() || newvalue.charAt(pos) != 'P') 
279                 throw new StringParseException("Duration must start with P or -P followed by a duration value.", 2);
280         ++pos;
281
282         int state = 0;  // year component
283         while ( pos != newvalue.length() )
284         {
285                 // no more data allowed?
286                 if (state == 8) 
287                         throw new StringParseException("Extra data after duration value.", 2);
288                                                 
289                 // check if ymd part is over
290                 if (newvalue.charAt(pos) == 'T') 
291                 {
292                         if (state >= 4) // hour
293                                 throw new StringParseException("Multiple Ts inside duration value.", 2);
294                         state = 4;
295                         ++pos;                  
296                 }
297
298                 if (state == 3) // 'T' separator
299                         throw new StringParseException("Extra data after duration value.", 2);
300
301                 // now a digit has to follow, and probably more than one
302                 if (pos == newvalue.length() || newvalue.charAt(pos) < '0' || newvalue.charAt(pos) > '9') 
303                         throw new StringParseException("Invalid numeric data in duration value.", 2);
304
305                 int val = 0;
306                 int digits = 0;
307                 while (pos != newvalue.length() && newvalue.charAt(pos) >= '0' && newvalue.charAt(pos) <= '9')
308                 {
309                         if (val >= 100000000) 
310                                 throw new StringParseException("Numeric overflow in duration value.", 2);
311                         val = val * 10 + (newvalue.charAt(pos) - '0');
312                         digits += 1;
313                         ++pos;
314                 }
315
316                 if (pos == newvalue.length()) 
317                         throw new StringParseException("Duration value missing component designator.", 2);
318                                 
319                 int foundState = 8;     // bad
320                 switch (newvalue.charAt(pos)) 
321                 {
322                 case 'Y': if (state >= 4) foundState = 8; else foundState = 0; break;
323                 case 'M': if (state >= 4) foundState = 5; else foundState = 1; break;
324                 case 'D': if (state >= 4) foundState = 8; else foundState = 2; break;
325                 case 'H': if (state >= 4) foundState = 4; else foundState = 8; break;
326                 case 'S': if (state >= 7) foundState = 7; else if (state >= 4) foundState = 6; else foundState = 8; break;
327                 case '.': if (state >= 4) foundState = 6; else foundState = 8; break;
328                 }
329
330                 if (foundState == 8 || foundState < state) 
331                         throw new StringParseException("Invalid or duplicate component designator.", 2);
332
333                 ++pos;
334
335                 switch ( foundState )
336                 {
337                 case 0: year = val; break;
338                 case 1: month = val; break;
339                 case 2: day = val; break;
340                 case 4: hour = val; break;
341                 case 5: minute = val; break;
342                 case 6: second = val; break;
343                 case 7: partsecond = val * Math.pow(0.1, digits); break;
344                 }
345
346                 state = foundState + 1;
347         }                                       
348         if (state == 0) 
349                 throw new StringParseException("No components given after P in duration value.", 2);
350
351         isempty = false;
352
353   }
354
355   // ---------- interface SchemaTypeCalendar ----------
356   public int calendarType() {
357     return CALENDAR_VALUE_DURATION;
358   }
359
360   public SchemaDuration durationValue() {
361     return new SchemaDuration( this );
362   }
363
364   public SchemaDateTime dateTimeValue() {
365     throw new TypesIncompatibleException( this, new SchemaDateTime( "2003-07-28T12:00:00" ) );
366   }
367
368   public SchemaDate dateValue() {
369     throw new TypesIncompatibleException( this, new SchemaDate( "2003-07-28" ) );
370   }
371
372   public SchemaTime timeValue() {
373     throw new TypesIncompatibleException( this, new SchemaTime( "12:00:00" ) );
374   }
375
376   public long getDayTimeValue() {
377     int sign = bNegative ? -1 : +1;
378     return (long)(((((long)day * 24 + hour) * 60 + minute) * 60 + second + partsecond) * 1000) * sign;
379   }
380
381   public int getYearMonthValue() {
382     int sign = bNegative ? -1 : +1;
383     return (year * 12 + month) * sign;
384   }
385
386   public void setYearMonthValue(int l) {
387         if (l < 0) {
388                 bNegative = true;
389                 l = -l;
390         } else {
391                 bNegative = false;
392         }
393
394     year = l / 12;
395     month = l % 12;
396   }
397
398   public void setDayTimeValue(long l) {
399         if (l < 0) {
400                 bNegative = true;
401                 l = -l;
402         } else {
403                 bNegative = false;
404         }
405     day = (int)(l / 86400000l); l %= 86400000l;
406     hour = (int)(l / 3600000l); l %=  3600000l;
407     minute = (int)(l / 60000l); l %=    60000l;
408     second =  (int)(l / 1000l); l %=     1000l;
409     partsecond = l / 1000.0;
410   }
411 }