| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 22 |
|
| 23 |
|
| 24 |
public class GMonthValue extends GDateValue { |
| 25 |
|
| 26 |
private static Pattern regex = |
| 27 |
|
| 28 |
Pattern.compile("--([0-9][0-9])(Z|[+-][0-9][0-9]:[0-9][0-9])?"); |
| 29 |
|
| 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 |
|
| 61 |
|
| 62 |
|
| 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 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public BuiltInAtomicType getPrimitiveType() { |
| 79 |
return BuiltInAtomicType.G_MONTH; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 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 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 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 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 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 |
|