1 package net.sf.saxon.value; 2 3 import net.sf.saxon.om.FastStringBuffer; 4 import net.sf.saxon.trans.DynamicError; 5 import net.sf.saxon.trans.XPathException; 6 import net.sf.saxon.type.BuiltInAtomicType; 7 import net.sf.saxon.type.ItemType; 8 import net.sf.saxon.type.Type; 9 import net.sf.saxon.type.ValidationException; 10 import net.sf.saxon.ConversionContext; 11 12 import java.util.Calendar ; 13 import java.util.GregorianCalendar ; 14 import java.util.regex.Matcher ; 15 import java.util.regex.Pattern ; 16 17 20 21 public class GMonthDayValue extends DateValue { 22 23 private static Pattern regex = 24 Pattern.compile("--([0-9][0-9]-[0-9][0-9])(Z|[+-][0-9][0-9]:[0-9][0-9])?"); 25 26 public GMonthDayValue(){}; 27 28 public GMonthDayValue(CharSequence value) throws XPathException { 29 Matcher m = regex.matcher(value); 30 if (!m.matches()) { 31 throw new DynamicError("Cannot convert '" + value + "' to a gMonthDay"); 32 } 33 String base = m.group(1); 34 String tz = m.group(2); 35 String date = "2000-" + base + (tz==null ? "" : tz); 36 setLexicalValue(date); 37 } 38 39 public GMonthDayValue(GregorianCalendar calendar, boolean timezoneSpecified, int tzoffset) { 40 super(calendar, timezoneSpecified, tzoffset); 41 } 42 43 47 48 public ItemType getItemType() { 49 return Type.G_MONTH_DAY_TYPE; 50 } 51 52 58 59 public AtomicValue convertPrimitive(BuiltInAtomicType requiredType, boolean validate, ConversionContext conversion) { 60 switch(requiredType.getPrimitiveType()) { 61 case Type.G_MONTH_DAY: 62 case Type.ATOMIC: 63 case Type.ITEM: 64 return this; 65 66 case Type.STRING: 67 return new StringValue(getStringValueCS()); 68 case Type.UNTYPED_ATOMIC: 69 return new UntypedAtomicValue(getStringValueCS()); 70 default: 71 ValidationException err = new ValidationException("Cannot convert gMonthDay to " + 72 requiredType.getDisplayName()); 73 err.setErrorCode("FORG0001"); 74 return new ValidationErrorValue(err); 75 } 76 } 77 78 public String getStringValue() { 79 80 FastStringBuffer sb = new FastStringBuffer(16); 81 82 sb.append("--"); 83 DateTimeValue.appendString(sb, calendar.get(Calendar.MONTH)+1, 2); 84 sb.append('-'); 85 DateTimeValue.appendString(sb, calendar.get(Calendar.DATE), 2); 86 87 if (zoneSpecified) { 88 DateTimeValue.appendTimezone(tzOffset, sb); 89 } 90 91 return sb.toString(); 92 93 } 94 } 95 96 | Popular Tags |