Debian Codegraph @

net/sf/saxon/value/GMonthValue.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:gMonth data type
22 */
23
24 public class GMonthValue 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 Pattern.compile("--([0-9][0-9])(Z|[+-][0-9][0-9]:[0-9][0-9])?");
29 // The commented-out pattern tolerates the bogus format --MM-- which was wrongly permitted by the original schema spec
30
31 private GMonthValue() {
32 }
33
34 public static ConversionResult makeGMonthValue(CharSequence value) {
35 GMonthValue g = new GMonthValue();
36 Matcher m = regex.matcher(Whitespace.trimWhitespace(value));
37 if (!m.matches()) {
38 return new ValidationFailure("Cannot convert '" + value + "' to a gMonth");
39 }
40 String base = m.group(1);
41 String tz = m.group(2);
42 String date = "2000-" + base + "-01" + (tz == null ? "" : tz);
43 g.typeLabel = BuiltInAtomicType.G_MONTH;
44 return setLexicalValue(g, date, true);
45 }
46
47 public GMonthValue(byte month, int tz) {
48 this(month, tz, BuiltInAtomicType.G_MONTH);
49 }
50
51 public GMonthValue(byte month, int tz, AtomicType type) {
52 this.year = 2000;
53 this.month = month;
54 this.day = 1;
55 setTimezoneInMinutes(tz);
56 this.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 GMonthValue v = new GMonthValue(month, getTimezoneInMinutes());
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_MONTH;
80 }
81
82 /*@NotNull*/
83 public CharSequence getPrimitiveStringValue() {
84
85 FastStringBuffer sb = new FastStringBuffer(FastStringBuffer.C16);
86
87 sb.append("--");
88 appendTwoDigits(sb, month);
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:gMonth");
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 GMonthValue(dt.getMonth(), dt.getTimezoneInMinutes());
125 }
126 }
127