Debian Codegraph @

net/sf/saxon/value/GDayValue.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.trans.XPathException;
11 import net.sf.saxon.tree.util.FastStringBuffer;
12 import net.sf.saxon.type.AtomicType;
13 import net.sf.saxon.type.BuiltInAtomicType;
14 import net.sf.saxon.type.ConversionResult;
15 import net.sf.saxon.type.ValidationFailure;
16
17 import java.util.regex.Matcher;
18 import java.util.regex.Pattern;
19
20 /**
21 * Implementation of the xs:gDay data type
22 */
23
24 public class GDayValue extends GDateValue {
25
26 private static Pattern regex =
27 Pattern.compile("---([0-9][0-9])(Z|[+-][0-9][0-9]:[0-9][0-9])?");
28
29 private GDayValue() {
30 }
31
32 public static ConversionResult makeGDayValue(CharSequence value) {
33 Matcher m = regex.matcher(Whitespace.trimWhitespace(value));
34 if (!m.matches()) {
35 return new ValidationFailure("Cannot convert '" + value + "' to a gDay");
36 }
37 GDayValue g = new GDayValue();
38 String base = m.group(1);
39 String tz = m.group(2);
40 String date = "2000-01-" + base + (tz == null ? "" : tz);
41 g.typeLabel = BuiltInAtomicType.G_DAY;
42 return setLexicalValue(g, date, true);
43 }
44
45 public GDayValue(byte day, int tz) {
46 this(day, tz, BuiltInAtomicType.G_DAY);
47 }
48
49 public GDayValue(byte day, int tz, AtomicType type) {
50 this.year = 2000;
51 this.month = 1;
52 this.day = day;
53 setTimezoneInMinutes(tz);
54 this.typeLabel = type;
55 }
56
57 /**
58 * Make a copy of this date, time, or dateTime value
59 *
60 * @param typeLabel
61 */
62
63 public AtomicValue copyAsSubType(AtomicType typeLabel) {
64 GDayValue v = new GDayValue(day, getTimezoneInMinutes());
65 v.typeLabel = typeLabel;
66 return v;
67 }
68
69 /**
70 * Determine the primitive type of the value. This delivers the same answer as
71 * getItemType().getPrimitiveItemType(). The primitive types are
72 * the 19 primitive types of XML Schema, plus xs:integer, xs:dayTimeDuration and xs:yearMonthDuration,
73 * and xs:untypedAtomic. For external objects, the result is AnyAtomicType.
74 */
75
76 public BuiltInAtomicType getPrimitiveType() {
77 return BuiltInAtomicType.G_DAY;
78 }
79
80 /*@NotNull*/
81 public CharSequence getPrimitiveStringValue() {
82
83 FastStringBuffer sb = new FastStringBuffer(FastStringBuffer.C16);
84
85 sb.append("---");
86 appendTwoDigits(sb, day);
87
88 if (hasTimezone()) {
89 appendTimezone(sb);
90 }
91
92 return sb;
93
94 }
95
96 /**
97 * Add a duration to this date/time value
98 *
99 * @param duration the duration to be added (which might be negative)
100 * @return a new date/time value representing the result of adding the duration. The original
101 * object is not modified.
102 * @throws net.sf.saxon.trans.XPathException
103 *
104 */
105
106 public CalendarValue add(DurationValue duration) throws XPathException {
107 XPathException err = new XPathException("Cannot add a duration to an xs:gDay");
108 err.setErrorCode("XPTY0004");
109 throw err;
110 }
111
112 /**
113 * Return a new date, time, or dateTime with the same normalized value, but
114 * in a different timezone
115 *
116 * @param tz the new timezone, in minutes
117 * @return the date/time in the new timezone
118 */
119
120 public CalendarValue adjustTimezone(int tz) {
121 DateTimeValue dt = (DateTimeValue) toDateTime().adjustTimezone(tz);
122 return new GDayValue(dt.getDay(), dt.getTimezoneInMinutes());
123 }
124 }
125