4 * This file was generated by XMLSpy 2007sp2 Enterprise Edition.
6 * YOU SHOULD NOT MODIFY THIS FILE, BECAUSE IT WILL BE
7 * OVERWRITTEN WHEN YOU RE-RUN CODE GENERATION.
9 * Refer to the XMLSpy Documentation for further details.
10 * http://www.altova.com/xmlspy
14 package com.jmex.xml.types;
16 import java.text.DecimalFormat;
19 public class SchemaDuration extends SchemaCalendarBase {
20 protected boolean bNegative;
23 public SchemaDuration() {
27 public SchemaDuration(SchemaDuration newvalue) {
29 month = newvalue.month;
32 minute = newvalue.minute;
33 second = newvalue.second;
34 partsecond = newvalue.partsecond;
35 hasTZ = newvalue.hasTZ;
36 offsetTZ = newvalue.offsetTZ;
37 bNegative = newvalue.isNegative();
39 isempty = newvalue.isempty;
42 public SchemaDuration(int newyear, int newmonth, int newday, int newhour, int newminute, int newsecond, double newpartsecond, boolean newisnegative) {
49 setPartSecond(newpartsecond);
50 bNegative = newisnegative;
55 public SchemaDuration(String newvalue) {
59 public SchemaDuration(SchemaType newvalue) {
63 public SchemaDuration(SchemaTypeCalendar newvalue) {
64 assign( (SchemaType)newvalue );
68 public int getYear() {
72 public int getMonth() {
80 public int getHour() {
84 public int getMinute() {
88 public int getSecond() {
92 public double getPartSecond() {
96 public int getMillisecond() {
97 return (int)java.lang.Math.round(partsecond*1000.0);
100 public boolean isNegative() {
104 public void setYear(int newyear) {
109 public void setMonth(int newmonth) {
114 public void setDay(int newday) {
119 public void setHour(int newhour) {
124 public void setMinute(int newminute) {
129 public void setSecond(int newsecond) {
134 public void setPartSecond(double newpartsecond) {
135 partsecond = newpartsecond;
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;
146 second -= neededSeconds;
148 second += neededSeconds - 1;
150 if (normalizedMSec >= 1000) {
151 int overflowSeconds = normalizedMSec / 1000;
152 normalizedMSec = normalizedMSec % 1000;
154 second += overflowSeconds;
156 second -= overflowSeconds;
158 partsecond = (double) normalizedMSec / 1000;
162 public void setNegative(boolean newisnegative) {
163 bNegative = newisnegative;
168 public void assign(SchemaType newvalue) {
169 if( newvalue == null || newvalue.isNull() || newvalue.isEmpty() )
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;
181 else if (newvalue instanceof SchemaString)
182 parse(newvalue.toString());
184 throw new TypesIncompatibleException(newvalue, this);
187 public void setEmpty() {
188 setInternalValues( 0,0,0, 0,0,0, 0.0, SchemaCalendarBase.TZ_MISSING, 0 );
194 public Object clone() {
195 return new SchemaDuration( this );
198 public String toString() {
202 StringBuffer s = new StringBuffer();
207 s.append(new DecimalFormat("0").format( (long) year));
211 s.append(new DecimalFormat("0").format( (long) month));
215 s.append(new DecimalFormat("0").format( (long) day));
218 if (hour!=0 || minute!=0 || second!=0 || partsecond>0 ) {
221 s.append(new DecimalFormat("0").format( (long) hour));
225 s.append(new DecimalFormat("0").format( (long) minute));
229 s.append(new DecimalFormat("0").format( (long) second));
230 if (partsecond > 0 && partsecond < 1) {
231 String sPartSecond = new DecimalFormat("0.0###############").format(partsecond);
233 s.append(sPartSecond.substring(2, sPartSecond.length()));
235 if (second != 0 || (partsecond > 0 && partsecond < 1))
243 public boolean booleanValue() {
247 public void parse(String s) {
249 String newvalue = SchemaNormalizedString.normalize(SchemaNormalizedString.WHITESPACE_COLLAPSE, s);
251 if ( newvalue == null )
256 else if ( newvalue.length() == 0 )
272 if (newvalue.charAt(pos) == '-')
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);
282 int state = 0; // year component
283 while ( pos != newvalue.length() )
285 // no more data allowed?
287 throw new StringParseException("Extra data after duration value.", 2);
289 // check if ymd part is over
290 if (newvalue.charAt(pos) == 'T')
292 if (state >= 4) // hour
293 throw new StringParseException("Multiple Ts inside duration value.", 2);
298 if (state == 3) // 'T' separator
299 throw new StringParseException("Extra data after duration value.", 2);
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);
307 while (pos != newvalue.length() && newvalue.charAt(pos) >= '0' && newvalue.charAt(pos) <= '9')
309 if (val >= 100000000)
310 throw new StringParseException("Numeric overflow in duration value.", 2);
311 val = val * 10 + (newvalue.charAt(pos) - '0');
316 if (pos == newvalue.length())
317 throw new StringParseException("Duration value missing component designator.", 2);
319 int foundState = 8; // bad
320 switch (newvalue.charAt(pos))
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;
330 if (foundState == 8 || foundState < state)
331 throw new StringParseException("Invalid or duplicate component designator.", 2);
335 switch ( foundState )
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;
346 state = foundState + 1;
349 throw new StringParseException("No components given after P in duration value.", 2);
355 // ---------- interface SchemaTypeCalendar ----------
356 public int calendarType() {
357 return CALENDAR_VALUE_DURATION;
360 public SchemaDuration durationValue() {
361 return new SchemaDuration( this );
364 public SchemaDateTime dateTimeValue() {
365 throw new TypesIncompatibleException( this, new SchemaDateTime( "2003-07-28T12:00:00" ) );
368 public SchemaDate dateValue() {
369 throw new TypesIncompatibleException( this, new SchemaDate( "2003-07-28" ) );
372 public SchemaTime timeValue() {
373 throw new TypesIncompatibleException( this, new SchemaTime( "12:00:00" ) );
376 public long getDayTimeValue() {
377 int sign = bNegative ? -1 : +1;
378 return (long)(((((long)day * 24 + hour) * 60 + minute) * 60 + second + partsecond) * 1000) * sign;
381 public int getYearMonthValue() {
382 int sign = bNegative ? -1 : +1;
383 return (year * 12 + month) * sign;
386 public void setYearMonthValue(int l) {
398 public void setDayTimeValue(long l) {
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;