| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 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 |
|
| 61 |
|
| 62 |
|
| 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 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public BuiltInAtomicType getPrimitiveType() { |
| 79 |
return BuiltInAtomicType.G_YEAR_MONTH; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 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); |
| 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 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 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 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 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 |
|