Debian Codegraph @

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