private static ValidationFailure badDate(String msg, CharSequence value) { ValidationFailure err = new ValidationFailure( "Invalid dateTime value " + Err.wrap(value, Err.VALUE) + " (" + msg + ")"); err.setErrorCode("FORG0001"); return err; }
/** * Convert the value to a built-in subtype of xs:dateTime * * @param subtype the target subtype * @return null if the conversion succeeds; a ValidationFailure describing the failure if it * fails. */ public ValidationFailure convertToSubType(BuiltInAtomicType subtype) { if (subtype.getFingerprint() == StandardNames.XS_DATE_TIME_STAMP) { if (hasTimezone()) { setTypeLabel(subtype); return null; } else { ValidationFailure err = new ValidationFailure( "The value " + Err.depict(this) + " is not a valid xs:dateTimeStamp: it has no timezone"); err.setErrorCode("FORG0001"); return err; } } else { throw new IllegalArgumentException("Unknown subtype of xs:dateTime"); } }
/** * Convert to target data type * * @param requiredType an integer identifying the required atomic type * @param context the XPath dynamic context * @return an AtomicValue, a value of the required type; or an ErrorValue */ public ConversionResult convertPrimitive( BuiltInAtomicType requiredType, boolean validate, XPathContext context) { switch (requiredType.getPrimitiveType()) { case StandardNames.XS_DATE_TIME: case StandardNames.XS_ANY_ATOMIC_TYPE: return this; case StandardNames.XS_DATE: return new DateValue(year, month, day, getTimezoneInMinutes()); case StandardNames.XS_TIME: return new TimeValue(hour, minute, second, microsecond, getTimezoneInMinutes()); case StandardNames.XS_G_YEAR: return new GYearValue(year, getTimezoneInMinutes()); case StandardNames.XS_G_YEAR_MONTH: return new GYearMonthValue(year, month, getTimezoneInMinutes()); case StandardNames.XS_G_MONTH: return new GMonthValue(month, getTimezoneInMinutes()); case StandardNames.XS_G_MONTH_DAY: return new GMonthDayValue(month, day, getTimezoneInMinutes()); case StandardNames.XS_G_DAY: return new GDayValue(day, getTimezoneInMinutes()); case StandardNames.XS_STRING: return new StringValue(getStringValueCS()); case StandardNames.XS_UNTYPED_ATOMIC: return new UntypedAtomicValue(getStringValueCS()); default: ValidationFailure err = new ValidationFailure("Cannot convert dateTime to " + requiredType.getDisplayName()); err.setErrorCode("XPTY0004"); return err; } }