Debian Codegraph @

net/sf/saxon/value/GMonthDayValue.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:gYear data type
22 */
23
24 public class GMonthDayValue extends GDateValue {
25
26 private static Pattern regex =
27 Pattern.compile("--([0-9][0-9]-[0-9][0-9])(Z|[+-][0-9][0-9]:[0-9][0-9])?");
28
29 private GMonthDayValue() {
30 }
31
32 public static ConversionResult makeGMonthDayValue(CharSequence value) {
33 Matcher m = regex.matcher(Whitespace.trimWhitespace(value));
34 if (!m.matches()) {
35 return new ValidationFailure("Cannot convert '" + value + "' to a gMonthDay");
36 }
37 GMonthDayValue g = new GMonthDayValue();
38 String base = m.group(1);
39 String tz = m.group(2);
40 String date = "2000-" + base + (tz == null ? "" : tz);
41 g.typeLabel = BuiltInAtomicType.G_MONTH_DAY;
42 return setLexicalValue(g, date, true);
43 }
44
45 public GMonthDayValue(byte month, byte day, int tz) {
46 this(month, day, tz, BuiltInAtomicType.G_MONTH_DAY);
47 }
48
49 public GMonthDayValue(byte month, byte day, int tz, AtomicType type) {
50 this.year = 2000;
51 this.month = month;
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 GMonthDayValue v = new GMonthDayValue(month, 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_MONTH_DAY;
78 }
79
80 /*@NotNull*/
81 public CharSequence getPrimitiveStringValue() {
82
83 FastStringBuffer sb = new FastStringBuffer(FastStringBuffer.C16);
84
85 sb.append("--");
86 appendTwoDigits(sb, month);
87 sb.append('-');
88 appendTwoDigits(sb, day);
89
90 if (hasTimezone()) {
91 appendTimezone(sb);
92 }
93
94 return sb;
95
96 }
97
98 /**
99 * Add a duration to this date/time value
100 *
101 * @param duration the duration to be added (which might be negative)
102 * @return a new date/time value representing the result of adding the duration. The original
103 * object is not modified.
104 * @throws net.sf.saxon.trans.XPathException
105 *
106 */
107
108 public CalendarValue add(DurationValue duration) throws XPathException {
109 XPathException err = new XPathException("Cannot add a duration to an xs:gMonthDay");
110 err.setErrorCode("XPTY0004");
111 throw err;
112 }
113
114 /**
115 * Return a new date, time, or dateTime with the same normalized value, but
116 * in a different timezone
117 *
118 * @param tz the new timezone, in minutes
119 * @return the date/time in the new timezone
120 */
121
122 public CalendarValue adjustTimezone(int tz) {
123 DateTimeValue dt = (DateTimeValue) toDateTime().adjustTimezone(tz);
124 return new GMonthDayValue(dt.getMonth(), dt.getDay(), dt.getTimezoneInMinutes());
125 }
126 }
127