Debian Codegraph @

net/sf/saxon/value/SaxonXMLGregorianCalendar.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.value;
9
10 import net.sf.saxon.functions.AccessorFn;
11 import net.sf.saxon.trans.XPathException;
12
13 import javax.xml.datatype.DatatypeConstants;
14 import javax.xml.datatype.Duration;
15 import javax.xml.datatype.XMLGregorianCalendar;
16 import javax.xml.namespace.QName;
17 import java.math.BigDecimal;
18 import java.math.BigInteger;
19 import java.util.*;
20
21 /**
22 * Saxon implementation of the JAXP class javax.xml.datatype.XMLGregorianCalendar.
23 * This is currently used only by the XQJ interface for XQuery: the normal representation of a
24 * date, time, or dateTime value in Saxon is with a subclass of {@link CalendarValue}
25 * <p>The JAXP specification for this class defines it in terms of XML Schema 1.0 semantics.
26 * This implementation is more aligned to the XPath 2.0 semantics of the data types.</p>
27 * <p>Note that this class, unlike the representations of all other data types, is mutable.</p>
28 */
29 public class SaxonXMLGregorianCalendar extends XMLGregorianCalendar {
30
31 /*@Nullable*/ private CalendarValue calendarValue;
32 /*@Nullable*/ private BigInteger year;
33 private int month = DatatypeConstants.FIELD_UNDEFINED;
34 private int day = DatatypeConstants.FIELD_UNDEFINED;
35 private int hour = DatatypeConstants.FIELD_UNDEFINED;
36 private int minute = DatatypeConstants.FIELD_UNDEFINED;
37 private int second = DatatypeConstants.FIELD_UNDEFINED;
38 private int microsecond = DatatypeConstants.FIELD_UNDEFINED;
39 private int tzOffset = DatatypeConstants.FIELD_UNDEFINED;
40
41 /**
42 * Create a SaxonXMLGregorianCalendar from a Saxon CalendarValue object
43 *
44 * @param value the CalendarValue
45 */
46
47 public SaxonXMLGregorianCalendar(/*@NotNull*/ CalendarValue value) {
48 clear();
49 setCalendarValue(value);
50 }
51
52 private SaxonXMLGregorianCalendar() {
53 }
54
55 /**
56 * Set the calendar value of this object
57 *
58 * @param value the calendar value
59 */
60
61 public void setCalendarValue(/*@NotNull*/ CalendarValue value) {
62 calendarValue = value;
63 try {
64 if (value instanceof GYearValue) {
65 year = BigInteger.valueOf(((Int64Value) value.getComponent(AccessorFn.Component.YEAR)).longValue());
66 } else if (value instanceof GYearMonthValue) {
67 year = BigInteger.valueOf(((Int64Value) value.getComponent(AccessorFn.Component.YEAR)).longValue());
68 month = (int) ((Int64Value) value.getComponent(AccessorFn.Component.MONTH)).longValue();
69 } else if (value instanceof GMonthValue) {
70 month = (int) ((Int64Value) value.getComponent(AccessorFn.Component.MONTH)).longValue();
71 } else if (value instanceof GMonthDayValue) {
72 month = (int) ((Int64Value) value.getComponent(AccessorFn.Component.MONTH)).longValue();
73 day = (int) ((Int64Value) value.getComponent(AccessorFn.Component.DAY)).longValue();
74 } else if (value instanceof GDayValue) {
75 day = (int) ((Int64Value) value.getComponent(AccessorFn.Component.DAY)).longValue();
76 } else if (value instanceof DateValue) {
77 year = BigInteger.valueOf(((Int64Value) value.getComponent(AccessorFn.Component.YEAR)).longValue());
78 month = (int) ((Int64Value) value.getComponent(AccessorFn.Component.MONTH)).longValue();
79 day = (int) ((Int64Value) value.getComponent(AccessorFn.Component.DAY)).longValue();
80 } else if (value instanceof TimeValue) {
81 hour = (int) ((Int64Value) value.getComponent(AccessorFn.Component.HOURS)).longValue();
82 minute = (int) ((Int64Value) value.getComponent(AccessorFn.Component.MINUTES)).longValue();
83 second = (int) ((Int64Value) value.getComponent(AccessorFn.Component.WHOLE_SECONDS)).longValue();
84 microsecond = (int) ((Int64Value) value.getComponent(AccessorFn.Component.MICROSECONDS)).longValue();
85 } else {
86 year = BigInteger.valueOf(((Int64Value) value.getComponent(AccessorFn.Component.YEAR)).longValue());
87 month = (int) ((Int64Value) value.getComponent(AccessorFn.Component.MONTH)).longValue();
88 day = (int) ((Int64Value) value.getComponent(AccessorFn.Component.DAY)).longValue();
89 hour = (int) ((Int64Value) value.getComponent(AccessorFn.Component.HOURS)).longValue();
90 minute = (int) ((Int64Value) value.getComponent(AccessorFn.Component.MINUTES)).longValue();
91 second = (int) ((Int64Value) value.getComponent(AccessorFn.Component.WHOLE_SECONDS)).longValue();
92 microsecond = (int) ((Int64Value) value.getComponent(AccessorFn.Component.MICROSECONDS)).longValue();
93 }
94 } catch (XPathException e) {
95 throw new IllegalArgumentException(e.getMessage());
96 }
97 }
98
99 /**
100 * <p>Unset all fields to undefined.</p>
101 * <p>Set all int fields to {@link javax.xml.datatype.DatatypeConstants#FIELD_UNDEFINED} and reference fields
102 * to null.</p>
103 */
104 public void clear() {
105 year = null;
106 month = DatatypeConstants.FIELD_UNDEFINED;
107 day = DatatypeConstants.FIELD_UNDEFINED;
108 hour = DatatypeConstants.FIELD_UNDEFINED;
109 minute = DatatypeConstants.FIELD_UNDEFINED;
110 second = DatatypeConstants.FIELD_UNDEFINED;
111 microsecond = DatatypeConstants.FIELD_UNDEFINED;
112 tzOffset = DatatypeConstants.FIELD_UNDEFINED;
113 }
114
115 /**
116 * <p>Reset this <code>XMLGregorianCalendar</code> to its original values.</p>
117 * <p>Saxon does not attempt to reset to the initial value as defined in the specification of
118 * the superclass, because it cannot distinguish the initial setting from subsequent changes.
119 * This method is therefore synonymous with {@link #clear()}</p>
120 */
121 public void reset() {
122 clear();
123 }
124
125 /**
126 * <p>Set low and high order component of XSD <code>dateTime</code> year field.</p>
127 * <p>Unset this field by invoking the setter with a parameter value of <code>null</code>.</p>
128 *
129 * @param year value constraints summarized in <a href="#datetimefield-year">year field of date/time field mapping table</a>.
130 * @throws IllegalArgumentException if <code>year</code> parameter is
131 * outside value constraints for the field as specified in
132 * <a href="#datetimefieldmapping">date/time field mapping table</a>.
133 */
134 public void setYear(BigInteger year) {
135 calendarValue = null;
136 this.year = year;
137 }
138
139 /**
140 * <p>Set year of XSD <code>dateTime</code> year field.</p>
141 * <p>Unset this field by invoking the setter with a parameter value of
142 * {@link javax.xml.datatype.DatatypeConstants#FIELD_UNDEFINED}.</p>
143 * <p>Note: if the absolute value of the <code>year</code> parameter
144 * is less than 10^9, the eon component of the XSD year field is set to
145 * <code>null</code> by this method.</p>
146 *
147 * @param year value constraints are summarized in <a href="#datetimefield-year">year field of date/time field mapping table</a>.
148 * If year is {@link javax.xml.datatype.DatatypeConstants#FIELD_UNDEFINED}, then eon is set to <code>null</code>.
149 */
150 public void setYear(int year) {
151 calendarValue = null;
152 this.year = BigInteger.valueOf(year);
153 }
154
155 /**
156 * <p>Set month.</p>
157 * <p>Unset this field by invoking the setter with a parameter value of {@link javax.xml.datatype.DatatypeConstants#FIELD_UNDEFINED}.</p>
158 *
159 * @param month value constraints summarized in <a href="#datetimefield-month">month field of date/time field mapping table</a>.
160 * @throws IllegalArgumentException if <code>month</code> parameter is
161 * outside value constraints for the field as specified in
162 * <a href="#datetimefieldmapping">date/time field mapping table</a>.
163 */
164 public void setMonth(int month) {
165 calendarValue = null;
166 this.month = month;
167 }
168
169 /**
170 * <p>Set days in month.</p>
171 * <p>Unset this field by invoking the setter with a parameter value of {@link javax.xml.datatype.DatatypeConstants#FIELD_UNDEFINED}.</p>
172 *
173 * @param day value constraints summarized in <a href="#datetimefield-day">day field of date/time field mapping table</a>.
174 * @throws IllegalArgumentException if <code>day</code> parameter is
175 * outside value constraints for the field as specified in
176 * <a href="#datetimefieldmapping">date/time field mapping table</a>.
177 */
178 public void setDay(int day) {
179 calendarValue = null;
180 this.day = day;
181 }
182
183 /**
184 * <p>Set the number of minutes in the timezone offset.</p>
185 * <p>Unset this field by invoking the setter with a parameter value of {@link javax.xml.datatype.DatatypeConstants#FIELD_UNDEFINED}.</p>
186 *
187 * @param offset value constraints summarized in <a href="#datetimefield-timezone">
188 * timezone field of date/time field mapping table</a>.
189 * @throws IllegalArgumentException if <code>offset</code> parameter is
190 * outside value constraints for the field as specified in
191 * <a href="#datetimefieldmapping">date/time field mapping table</a>.
192 */
193 public void setTimezone(int offset) {
194 calendarValue = null;
195 tzOffset = offset;
196 }
197
198 /**
199 * <p>Set hours.</p>
200 * <p>Unset this field by invoking the setter with a parameter value of {@link javax.xml.datatype.DatatypeConstants#FIELD_UNDEFINED}.</p>
201 *
202 * @param hour value constraints summarized in <a href="#datetimefield-hour">hour field of date/time field mapping table</a>.
203 * @throws IllegalArgumentException if <code>hour</code> parameter is outside value constraints for the field as specified in
204 * <a href="#datetimefieldmapping">date/time field mapping table</a>.
205 */
206 public void setHour(int hour) {
207 calendarValue = null;
208 this.hour = hour;
209 }
210
211 /**
212 * <p>Set minutes.</p>
213 * <p>Unset this field by invoking the setter with a parameter value of {@link javax.xml.datatype.DatatypeConstants#FIELD_UNDEFINED}.</p>
214 *
215 * @param minute value constraints summarized in <a href="#datetimefield-minute">minute field of date/time field mapping table</a>.
216 * @throws IllegalArgumentException if <code>minute</code> parameter is outside value constraints for the field as specified in
217 * <a href="#datetimefieldmapping">date/time field mapping table</a>.
218 */
219 public void setMinute(int minute) {
220 calendarValue = null;
221 this.minute = minute;
222 }
223
224 /**
225 * <p>Set seconds.</p>
226 * <p>Unset this field by invoking the setter with a parameter value of {@link javax.xml.datatype.DatatypeConstants#FIELD_UNDEFINED}.</p>
227 *
228 * @param second value constraints summarized in <a href="#datetimefield-second">second field of date/time field mapping table</a>.
229 * @throws IllegalArgumentException if <code>second</code> parameter is outside value constraints for the field as specified in
230 * <a href="#datetimefieldmapping">date/time field mapping table</a>.
231 */
232 public void setSecond(int second) {
233 calendarValue = null;
234 this.second = second;
235 }
236
237 /**
238 * <p>Set milliseconds.</p>
239 * <p>Unset this field by invoking the setter with a parameter value of {@link javax.xml.datatype.DatatypeConstants#FIELD_UNDEFINED}.</p>
240 *
241 * @param millisecond value constraints summarized in
242 * <a href="#datetimefield-millisecond">millisecond field of date/time field mapping table</a>.
243 * @throws IllegalArgumentException if <code>millisecond</code> parameter is outside value constraints for the field as specified
244 * in <a href="#datetimefieldmapping">date/time field mapping table</a>.
245 */
246 public void setMillisecond(int millisecond) {
247 calendarValue = null;
248 microsecond = millisecond * 1000;
249 }
250
251 /**
252 * <p>Set fractional seconds.</p>
253 * <p>Unset this field by invoking the setter with a parameter value of <code>null</code>.</p>
254 *
255 * @param fractional value constraints summarized in
256 * <a href="#datetimefield-fractional">fractional field of date/time field mapping table</a>.
257 * @throws IllegalArgumentException if <code>fractional</code> parameter is outside value constraints for the field as specified
258 * in <a href="#datetimefieldmapping">date/time field mapping table</a>.
259 */
260 public void setFractionalSecond(/*@NotNull*/ BigDecimal fractional) {
261 calendarValue = null;
262 second = fractional.intValue();
263 BigInteger micros = fractional.movePointRight(6).toBigInteger();
264 micros = micros.remainder(BigInteger.valueOf(1000000));
265 microsecond = micros.intValue();
266 }
267
268 /**
269 * <p>Return high order component for XML Schema 1.0 dateTime datatype field for
270 * <code>year</code>.
271 * <code>null</code> if this optional part of the year field is not defined.</p>
272 * <p>Value constraints for this value are summarized in
273 * <a href="#datetimefield-year">year field of date/time field mapping table</a>.</p>
274 *
275 * @return eon of this <code>XMLGregorianCalendar</code>. The value
276 * returned is an integer multiple of 10^9.
277 * @see #getYear()
278 * @see #getEonAndYear()
279 */
280 public BigInteger getEon() {
281 return year.divide(BigInteger.valueOf(1000000000));
282 }
283
284 /**
285 * <p>Return low order component for XML Schema 1.0 dateTime datatype field for
286 * <code>year</code> or {@link javax.xml.datatype.DatatypeConstants#FIELD_UNDEFINED}.</p>
287 * <p>Value constraints for this value are summarized in
288 * <a href="#datetimefield-year">year field of date/time field mapping table</a>.</p>
289 *
290 * @return year of this <code>XMLGregorianCalendar</code>.
291 * @see #getEon()
292 * @see #getEonAndYear()
293 */
294 public int getYear() {
295 return year.intValue();
296 }
297
298 /**
299 * <p>Return XML Schema 1.0 dateTime datatype field for
300 * <code>year</code>.</p>
301 * <p>Value constraints for this value are summarized in
302 * <a href="#datetimefield-year">year field of date/time field mapping table</a>.</p>
303 *
304 * @return sum of <code>eon</code> and <code>BigInteger.valueOf(year)</code>
305 * when both fields are defined. When only <code>year</code> is defined,
306 * return it. When both <code>eon</code> and <code>year</code> are not
307 * defined, return <code>null</code>.
308 * @see #getEon()
309 * @see #getYear()
310 */
311 /*@Nullable*/
312 public BigInteger getEonAndYear() {
313 return year;
314 }
315
316 /**
317 * <p>Return number of month or {@link javax.xml.datatype.DatatypeConstants#FIELD_UNDEFINED}.</p>
318 * <p>Value constraints for this value are summarized in
319 * <a href="#datetimefield-month">month field of date/time field mapping table</a>.</p>
320 *
321 * @return year of this <code>XMLGregorianCalendar</code>.
322 */
323 public int getMonth() {
324 return month;
325 }
326
327 /**
328 * Return day in month or {@link javax.xml.datatype.DatatypeConstants#FIELD_UNDEFINED}.
329 * <p>Value constraints for this value are summarized in
330 * <a href="#datetimefield-day">day field of date/time field mapping table</a>.</p>
331 *
332 * @see #setDay(int)
333 */
334 public int getDay() {
335 return day;
336 }
337
338 /**
339 * Return timezone offset in minutes or
340 * {@link javax.xml.datatype.DatatypeConstants#FIELD_UNDEFINED} if this optional field is not defined.
341 * <p>Value constraints for this value are summarized in
342 * <a href="#datetimefield-timezone">timezone field of date/time field mapping table</a>.</p>
343 *
344 * @see #setTimezone(int)
345 */
346 public int getTimezone() {
347 return tzOffset;
348 }
349
350 /**
351 * Return hours or {@link javax.xml.datatype.DatatypeConstants#FIELD_UNDEFINED}.
352 * Returns {@link javax.xml.datatype.DatatypeConstants#FIELD_UNDEFINED} if this field is not defined.
353 * <p>Value constraints for this value are summarized in
354 * <a href="#datetimefield-hour">hour field of date/time field mapping table</a>.</p>
355 *
356 * @see #setTime(int, int, int)
357 */
358 public int getHour() {
359 return hour;
360 }
361
362 /**
363 * Return minutes or {@link javax.xml.datatype.DatatypeConstants#FIELD_UNDEFINED}.
364 * <p>Returns {@link javax.xml.datatype.DatatypeConstants#FIELD_UNDEFINED} if this field is not defined.</p>
365 * <p>Value constraints for this value are summarized in
366 * <a href="#datetimefield-minute">minute field of date/time field mapping table</a>.</p>
367 *
368 * @see #setTime(int, int, int)
369 */
370 public int getMinute() {
371 return minute;
372 }
373
374 /**
375 * <p>Return seconds or {@link javax.xml.datatype.DatatypeConstants#FIELD_UNDEFINED}.</p>
376 * <p>Returns {@link javax.xml.datatype.DatatypeConstants#FIELD_UNDEFINED} if this field is not defined.
377 * When this field is not defined, the optional xs:dateTime
378 * fractional seconds field, represented by
379 * {@link #getFractionalSecond()} and {@link #getMillisecond()},
380 * must not be defined.</p>
381 * <p>Value constraints for this value are summarized in
382 * <a href="#datetimefield-second">second field of date/time field mapping table</a>.</p>
383 *
384 * @return Second of this <code>XMLGregorianCalendar</code>.
385 * @see #getFractionalSecond()
386 * @see #getMillisecond()
387 * @see #setTime(int, int, int)
388 */
389 public int getSecond() {
390 return second;
391 }
392
393 /**
394 * <p>Return microsecond precision of {@link #getFractionalSecond()}.</p>
395 * <p>This method represents a convenience accessor to infinite
396 * precision fractional second value returned by
397 * {@link #getFractionalSecond()}. The returned value is the rounded
398 * down to microseconds value of
399 * {@link #getFractionalSecond()}. When {@link #getFractionalSecond()}
400 * returns <code>null</code>, this method must return
401 * {@link DatatypeConstants#FIELD_UNDEFINED}.</p>
402 * <p>Value constraints for this value are summarized in
403 * <a href="#datetimefield-second">second field of date/time field mapping table</a>.</p>
404 *
405 * @return Millisecond of this <code>XMLGregorianCalendar</code>.
406 * @see #getFractionalSecond()
407 * @see #setTime(int, int, int)
408 */
409 public int getMicrosecond() {
410
411 BigDecimal fractionalSeconds = getFractionalSecond();
412
413 // is field undefined?
414 if (fractionalSeconds == null) {
415 return DatatypeConstants.FIELD_UNDEFINED;
416 }
417
418 return getFractionalSecond().movePointRight(6).intValue();
419 }
420
421
422 /**
423 * <p>Return fractional seconds.</p>
424 * <p><code>null</code> is returned when this optional field is not defined.</p>
425 * <p>Value constraints are detailed in
426 * <a href="#datetimefield-second">second field of date/time field mapping table</a>.</p>
427 * <p>This optional field can only have a defined value when the
428 * xs:dateTime second field, represented by {@link #getSecond()},
429 * does not return {@link javax.xml.datatype.DatatypeConstants#FIELD_UNDEFINED}.</p>
430 *
431 * @return fractional seconds of this <code>XMLGregorianCalendar</code>.
432 * @see #getSecond()
433 * @see #setTime(int, int, int, java.math.BigDecimal)
434 */
435 /*@Nullable*/
436 public BigDecimal getFractionalSecond() {
437 if (second == DatatypeConstants.FIELD_UNDEFINED) {
438 return null;
439 }
440 return BigDecimal.valueOf(microsecond).movePointLeft(6);
441 }
442
443 /**
444 * <p>Compare two instances of W3C XML Schema 1.0 date/time datatypes
445 * according to partial order relation defined in
446 * <a href="http://www.w3.org/TR/xmlschema-2/#dateTime-order">W3C XML Schema 1.0 Part 2, Section 3.2.7.3,
447 * <i>Order relation on dateTime</i></a>.</p>
448 * <p><code>xsd:dateTime</code> datatype field mapping to accessors of
449 * this class are defined in
450 * <a href="#datetimefieldmapping">date/time field mapping table</a>.</p>
451 *
452 * @param xmlGregorianCalendar Instance of <code>XMLGregorianCalendar</code> to compare
453 * @return The relationship between <code>this</code> <code>XMLGregorianCalendar</code> and
454 * the specified <code>xmlGregorianCalendar</code> as
455 * {@link javax.xml.datatype.DatatypeConstants#LESSER},
456 * {@link javax.xml.datatype.DatatypeConstants#EQUAL},
457 * {@link javax.xml.datatype.DatatypeConstants#GREATER} or
458 * {@link javax.xml.datatype.DatatypeConstants#INDETERMINATE}.
459 * @throws NullPointerException if <code>xmlGregorianCalendar</code> is null.
460 */
461 public int compare(/*@NotNull*/ XMLGregorianCalendar xmlGregorianCalendar) {
462 return toCalendarValue().getSchemaComparable().compareTo(
463 ((SaxonXMLGregorianCalendar) xmlGregorianCalendar).toCalendarValue().getSchemaComparable());
464 }
465
466 /**
467 * <p>Normalize this instance to UTC.</p>
468 * <p>2000-03-04T23:00:00+03:00 normalizes to 2000-03-04T20:00:00Z</p>
469 * <p>Implements W3C XML Schema Part 2, Section 3.2.7.3 (A).</p>
470 *
471 * @return a copy of this <code>XMLGregorianCalendar</code> normalized to UTC.
472 */
473 /*@NotNull*/
474 public XMLGregorianCalendar normalize() {
475 return new SaxonXMLGregorianCalendar(toCalendarValue().adjustTimezone(0));
476 }
477
478 /**
479 * <p>Return the lexical representation of <code>this</code> instance.
480 * The format is specified in
481 * <a href="http://www.w3.org/TR/xmlschema-2/#dateTime-order">XML Schema 1.0 Part 2, Section 3.2.[7-14].1,
482 * <i>Lexical Representation</i>".</a></p>
483 * <p>Specific target lexical representation format is determined by
484 * {@link #getXMLSchemaType()}.</p>
485 *
486 * @return XML, as <code>String</code>, representation of this <code>XMLGregorianCalendar</code>
487 * @throws IllegalStateException if the combination of set fields
488 * does not match one of the eight defined XML Schema builtin date/time datatypes.
489 */
490 public String toXMLFormat() {
491 return toCalendarValue().getStringValue();
492 }
493
494 /**
495 * <p>Return the name of the XML Schema date/time type that this instance
496 * maps to. Type is computed based on fields that are set.</p>
497 *
498 * @return One of the following class constants:
499 * {@link javax.xml.datatype.DatatypeConstants#DATETIME},
500 * {@link javax.xml.datatype.DatatypeConstants#TIME},
501 * {@link javax.xml.datatype.DatatypeConstants#DATE},
502 * {@link javax.xml.datatype.DatatypeConstants#GYEARMONTH},
503 * {@link javax.xml.datatype.DatatypeConstants#GMONTHDAY},
504 * {@link javax.xml.datatype.DatatypeConstants#GYEAR},
505 * {@link javax.xml.datatype.DatatypeConstants#GMONTH} or
506 * {@link javax.xml.datatype.DatatypeConstants#GDAY}.
507 * @throws IllegalStateException if the combination of set fields
508 * does not match one of the eight defined XML Schema builtin
509 * date/time datatypes.
510 */
511 public QName getXMLSchemaType() {
512 if (second == DatatypeConstants.FIELD_UNDEFINED) {
513 if (year == null) {
514 if (month == DatatypeConstants.FIELD_UNDEFINED) {
515 return DatatypeConstants.GDAY;
516 } else if (day == DatatypeConstants.FIELD_UNDEFINED) {
517 return DatatypeConstants.GMONTH;
518 } else {
519 return DatatypeConstants.GMONTHDAY;
520 }
521 } else if (day == DatatypeConstants.FIELD_UNDEFINED) {
522 if (month == DatatypeConstants.FIELD_UNDEFINED) {
523 return DatatypeConstants.GYEAR;
524 } else {
525 return DatatypeConstants.GYEARMONTH;
526 }
527 }
528 return DatatypeConstants.DATE;
529 } else if (year == null) {
530 return DatatypeConstants.TIME;
531 } else {
532 return DatatypeConstants.DATETIME;
533 }
534 }
535
536 /**
537 * Validate instance by <code>getXMLSchemaType()</code> constraints.
538 *
539 * @return true if data values are valid.
540 */
541 public boolean isValid() {
542 return true;
543 }
544
545 /**
546 * <p>Add <code>duration</code> to this instance.</p>
547 * <p>The computation is specified in
548 * <a href="http://www.w3.org/TR/xmlschema-2/#adding-durations-to-dateTimes">XML Schema 1.0 Part 2, Appendix E,
549 * <i>Adding durations to dateTimes</i></a>.
550 * <a href="#datetimefieldsmapping">date/time field mapping table</a>
551 * defines the mapping from XML Schema 1.0 <code>dateTime</code> fields
552 * to this class' representation of those fields.</p>
553 *
554 * @param duration Duration to add to this <code>XMLGregorianCalendar</code>.
555 * @throws NullPointerException when <code>duration</code> parameter is <code>null</code>.
556 */
557 public void add(/*@NotNull*/ Duration duration) {
558 try {
559 CalendarValue cv = toCalendarValue().add(((SaxonDuration) duration).getDurationValue());
560 setCalendarValue(cv);
561 } catch (XPathException err) {
562 throw new IllegalArgumentException(err.getMessage());
563 }
564 }
565
566 /**
567 * <p>Convert this <code>XMLGregorianCalendar</code> to a {@link java.util.GregorianCalendar}.</p>
568 * <p>When <code>this</code> instance has an undefined field, this
569 * conversion relies on the <code>java.util.GregorianCalendar</code> default
570 * for its corresponding field. A notable difference between
571 * XML Schema 1.0 date/time datatypes and <code>java.util.GregorianCalendar</code>
572 * is that Timezone value is optional for date/time datatypes and it is
573 * a required field for <code>java.util.GregorianCalendar</code>. See javadoc
574 * for <code>java.util.TimeZone.getDefault()</code> on how the default
575 * is determined. To explicitly specify the <code>TimeZone</code>
576 * instance, see
577 * {@link #toGregorianCalendar(java.util.TimeZone, Locale, javax.xml.datatype.XMLGregorianCalendar)}.</p>
578 *
579 * @see #toGregorianCalendar(java.util.TimeZone, java.util.Locale, javax.xml.datatype.XMLGregorianCalendar)
580 */
581 public GregorianCalendar toGregorianCalendar() {
582 return toCalendarValue().getCalendar();
583 }
584
585 /**
586 * <p>Convert this <code>XMLGregorianCalendar</code> along with provided parameters
587 * to a {@link java.util.GregorianCalendar} instance.</p>
588 * <p> Since XML Schema 1.0 date/time datetypes has no concept of
589 * timezone ids or daylight savings timezone ids, this conversion operation
590 * allows the user to explicitly specify one with
591 * <code>timezone</code> parameter.</p>
592 * <p>To compute the return value's <code>TimeZone</code> field:</p>
593 * <ul>
594 * <li>when parameter <code>timeZone</code> is non-null,
595 * it is the timezone field.</li>
596 * <li>else when <code>this.getTimezone() != FIELD_UNDEFINED</code>,
597 * create a <code>java.util.TimeZone</code> with a custom timezone id
598 * using the <code>this.getTimezone()</code>.</li>
599 * <li>else when <code>defaults.getTimezone() != FIELD_UNDEFINED</code>,
600 * create a <code>java.util.TimeZone</code> with a custom timezone id
601 * using <code>defaults.getTimezone()</code>.</li>
602 * <li>else use the <code>GregorianCalendar</code> default timezone value
603 * for the host is defined as specified by
604 * <code>java.util.TimeZone.getDefault()</code>.</li></ul>
605 * <p>To ensure consistency in conversion implementations, the new
606 * <code>GregorianCalendar</code> should be instantiated in the following
607 * manner:</p>
608 * <ul>
609 * <li>Create a new <code>java.util.GregorianCalendar(TimeZone,
610 * Locale)</code> with TimeZone set as specified above and the
611 * <code>Locale</code> parameter.</li>
612 * <li>Initialize all GregorianCalendar fields by calling {@link java.util.GregorianCalendar#clear()}</li>
613 * <li>Obtain a pure Gregorian Calendar by invoking
614 * <code>GregorianCalendar.setGregorianChange(
615 * new Date(Long.MIN_VALUE))</code>.</li>
616 * <li>Its fields ERA, YEAR, MONTH, DAY_OF_MONTH, HOUR_OF_DAY,
617 * MINUTE, SECOND and MILLISECOND are set using the method
618 * <code>Calendar.set(int,int)</code></li>
619 * </ul>
620 *
621 * @param timezone provide Timezone. <code>null</code> is a legal value.
622 * @param aLocale provide explicit Locale. Use default GregorianCalendar locale if
623 * value is <code>null</code>.
624 * @param defaults provide default field values to use when corresponding
625 * field for this instance is FIELD_UNDEFINED or null.
626 * If <code>defaults</code>is <code>null</code> or a field
627 * within the specified <code>defaults</code> is undefined,
628 * just use <code>java.util.GregorianCalendar</code> defaults.
629 * @return a java.util.GregorianCalendar conversion of this instance.
630 */
631 /*@NotNull*/
632 public GregorianCalendar toGregorianCalendar(TimeZone timezone, Locale aLocale, /*@NotNull*/ XMLGregorianCalendar defaults) {
633 GregorianCalendar gc = new GregorianCalendar(timezone, aLocale);
634 gc.setGregorianChange(new Date(Long.MIN_VALUE));
635 gc.set(Calendar.ERA, year == null ? defaults.getYear() > 0 ? +1 : -1 : year.signum());
636 gc.set(Calendar.YEAR, year == null ? defaults.getYear() : year.abs().intValue());
637 gc.set(Calendar.MONTH, month == DatatypeConstants.FIELD_UNDEFINED ? defaults.getMonth() : month);
638 gc.set(Calendar.DAY_OF_MONTH, day == DatatypeConstants.FIELD_UNDEFINED ? defaults.getDay() : day);
639 gc.set(Calendar.HOUR, hour == DatatypeConstants.FIELD_UNDEFINED ? defaults.getHour() : hour);
640 gc.set(Calendar.MINUTE, minute == DatatypeConstants.FIELD_UNDEFINED ? defaults.getMinute() : minute);
641 gc.set(Calendar.SECOND, second == DatatypeConstants.FIELD_UNDEFINED ? defaults.getSecond() : second);
642 gc.set(Calendar.MILLISECOND, microsecond == DatatypeConstants.FIELD_UNDEFINED
643 ? defaults.getMillisecond() : microsecond / 1000);
644 return gc;
645 }
646
647 /**
648 * <p>Returns a <code>java.util.TimeZone</code> for this class.</p>
649 * <p>If timezone field is defined for this instance,
650 * returns TimeZone initialized with custom timezone id
651 * of zoneoffset. If timezone field is undefined,
652 * try the defaultZoneoffset that was passed in.
653 * If defaultZoneoffset is FIELD_UNDEFINED, return
654 * default timezone for this host.
655 * (Same default as java.util.GregorianCalendar).</p>
656 *
657 * @param defaultZoneoffset default zoneoffset if this zoneoffset is
658 * {@link javax.xml.datatype.DatatypeConstants#FIELD_UNDEFINED}.
659 * @return TimeZone for this.
660 */
661 public TimeZone getTimeZone(int defaultZoneoffset) {
662 if (tzOffset == DatatypeConstants.FIELD_UNDEFINED) {
663 if (defaultZoneoffset == DatatypeConstants.FIELD_UNDEFINED) {
664 return new GregorianCalendar().getTimeZone();
665 } else {
666 return new SimpleTimeZone(defaultZoneoffset * 60000, "XXX");
667 }
668 } else {
669 return new SimpleTimeZone(tzOffset * 60000, "XXX");
670 }
671 }
672
673 /**
674 * <p>Creates and returns a copy of this object.</p>
675 *
676 * @return copy of this <code>Object</code>
677 */
678 /*@NotNull*/
679 @SuppressWarnings({"CloneDoesntCallSuperClone"})
680 public Object clone() {
681 SaxonXMLGregorianCalendar s = new SaxonXMLGregorianCalendar();
682 s.setYear(year);
683 s.setMonth(month);
684 s.setDay(day);
685 s.setHour(hour);
686 s.setMinute(minute);
687 s.setSecond(second);
688 s.setMillisecond(microsecond / 1000);
689 s.setTimezone(tzOffset);
690 return s;
691 }
692
693 /**
694 * Convert this SaxonXMLGregorianCalendar to a Saxon CalendarValue object
695 *
696 * @return the corresponding CalendarValue
697 */
698
699 /*@Nullable*/
700 public CalendarValue toCalendarValue() {
701 if (calendarValue != null) {
702 return calendarValue;
703 }
704 if (second == DatatypeConstants.FIELD_UNDEFINED) {
705 if (year == null) {
706 if (month == DatatypeConstants.FIELD_UNDEFINED) {
707 return new GDayValue((byte) day, tzOffset);
708 } else if (day == DatatypeConstants.FIELD_UNDEFINED) {
709 return new GMonthValue((byte) month, tzOffset);
710 } else {
711 return new GMonthDayValue((byte) month, (byte) day, tzOffset);
712 }
713 } else if (day == DatatypeConstants.FIELD_UNDEFINED) {
714 if (month == DatatypeConstants.FIELD_UNDEFINED) {
715 return new GYearValue(year.intValue(), tzOffset, true);
716 } else {
717 return new GYearMonthValue(year.intValue(), (byte) month, tzOffset, true);
718 }
719 }
720 return new DateValue(year.intValue(), (byte) month, (byte) day, tzOffset, true);
721 } else if (year == null) {
722 return new TimeValue((byte) hour, (byte) minute, (byte) second, getMicrosecond()*1000, tzOffset, "");
723 } else {
724 return new DateTimeValue(year.intValue(), (byte) month, (byte) day,
725 (byte) hour, (byte) minute, (byte) second, getMicrosecond(), tzOffset, true);
726 }
727 }
728 }