Debian Codegraph @

net/sf/saxon/value/GYearValue.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:gYear data type
23 */
24
25 public class GYearValue extends GDateValue {
26
27 private static Pattern regex =
28 Pattern.compile("(-?[0-9]+)(Z|[+-][0-9][0-9]:[0-9][0-9])?");
29
30 private GYearValue() {
31 }
32
33 public static ConversionResult makeGYearValue(CharSequence value, ConversionRules rules) {
34 GYearValue g = new GYearValue();
35 Matcher m = regex.matcher(Whitespace.trimWhitespace(value));
36 if (!m.matches()) {
37 return new ValidationFailure("Cannot convert '" + value + "' to a gYear");
38 }
39 String base = m.group(1);
40 String tz = m.group(2);
41 String date = base + "-01-01" + (tz == null ? "" : tz);
42 g.typeLabel = BuiltInAtomicType.G_YEAR;
43 return setLexicalValue(g, date, rules.isAllowYearZero());
44 }
45
46 public GYearValue(int year, int tz, boolean xsd10) {
47 this(year, tz, BuiltInAtomicType.G_YEAR);
48 this.hasNoYearZero = xsd10;
49 }
50
51 public GYearValue(int year, int tz, AtomicType type) {
52 this.year = year;
53 this.month = 1;
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 GYearValue v = new GYearValue(year, getTimezoneInMinutes(), true);
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;
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 if (hasTimezone()) {
96 appendTimezone(sb);
97 }
98
99 return sb;
100
101 }
102
103 /**
104 * Add a duration to this date/time value
105 *
106 * @param duration the duration to be added (which might be negative)
107 * @return a new date/time value representing the result of adding the duration. The original
108 * object is not modified.
109 * @throws net.sf.saxon.trans.XPathException
110 *
111 */
112
113 public CalendarValue add(DurationValue duration) throws XPathException {
114 XPathException err = new XPathException("Cannot add a duration to an xs:gYear");
115 err.setErrorCode("XPTY0004");
116 throw err;
117 }
118
119 /**
120 * Return a new date, time, or dateTime with the same normalized value, but
121 * in a different timezone
122 *
123 * @param tz the new timezone, in minutes
124 * @return the date/time in the new timezone
125 */
126
127 public CalendarValue adjustTimezone(int tz) {
128 DateTimeValue dt = (DateTimeValue) toDateTime().adjustTimezone(tz);
129 return new GYearValue(dt.getYear(), dt.getTimezoneInMinutes(), hasNoYearZero);
130 }
131 }
132