net.sf.saxon.value.GDayValue Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of saxon9 Show documentation
Show all versions of saxon9 Show documentation
Provides a basic XSLT 2.0 and XQuery 1.0 processor (W3C Recommendations,
January 2007). Command line interfaces and implementations of several
Java APIs (DOM, XPath, s9api) are also included.
The newest version!
package net.sf.saxon.value;
import net.sf.saxon.expr.XPathContext;
import net.sf.saxon.om.FastStringBuffer;
import net.sf.saxon.om.StandardNames;
import net.sf.saxon.trans.XPathException;
import net.sf.saxon.type.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Implementation of the xs:gDay data type
*/
public class GDayValue extends GDateValue {
private static Pattern regex =
Pattern.compile("---([0-9][0-9])(Z|[+-][0-9][0-9]:[0-9][0-9])?");
private GDayValue(){}
public static ConversionResult makeGDayValue(CharSequence value) {
Matcher m = regex.matcher(Whitespace.trimWhitespace(value));
if (!m.matches()) {
return new ValidationFailure("Cannot convert '" + value + "' to a gDay");
}
GDayValue g = new GDayValue();
String base = m.group(1);
String tz = m.group(2);
String date = "2000-01-" + base + (tz==null ? "" : tz);
g.typeLabel = BuiltInAtomicType.G_DAY;
return setLexicalValue(g, date);
}
public GDayValue(byte day, int tz) {
this(day, tz, BuiltInAtomicType.G_DAY);
}
public GDayValue(byte day, int tz, AtomicType type) {
this.year = 2000;
this.month = 1;
this.day = day;
setTimezoneInMinutes(tz);
this.typeLabel = type;
}
/**
* Make a copy of this date, time, or dateTime value
* @param typeLabel
*/
public AtomicValue copyAsSubType(AtomicType typeLabel) {
GDayValue v = new GDayValue(day, getTimezoneInMinutes());
v.typeLabel = typeLabel;
return v;
}
/**
* Determine the primitive type of the value. This delivers the same answer as
* getItemType().getPrimitiveItemType(). The primitive types are
* the 19 primitive types of XML Schema, plus xs:integer, xs:dayTimeDuration and xs:yearMonthDuration,
* and xs:untypedAtomic. For external objects, the result is AnyAtomicType.
*/
public BuiltInAtomicType getPrimitiveType() {
return BuiltInAtomicType.G_DAY;
}
/**
* Convert to target data type
* @param requiredType an integer identifying the required atomic type
* @param 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_G_DAY:
case StandardNames.XS_ANY_ATOMIC_TYPE:
return this;
case StandardNames.XS_STRING:
return new StringValue(getStringValueCS());
case StandardNames.XS_UNTYPED_ATOMIC:
return new UntypedAtomicValue(getStringValueCS());
default:
ValidationFailure err = new ValidationFailure("Cannot convert gDay to " +
requiredType.getDisplayName());
err.setErrorCode("XPTY0004");
return err;
}
}
public CharSequence getStringValueCS() {
FastStringBuffer sb = new FastStringBuffer(16);
sb.append("---");
appendTwoDigits(sb, day);
if (hasTimezone()) {
appendTimezone(sb);
}
return sb;
}
/**
* Add a duration to this date/time value
*
* @param duration the duration to be added (which might be negative)
* @return a new date/time value representing the result of adding the duration. The original
* object is not modified.
* @throws net.sf.saxon.trans.XPathException
*
*/
public CalendarValue add(DurationValue duration) throws XPathException {
XPathException err = new XPathException("Cannot add a duration to an xs:gDay");
err.setErrorCode("XPTY0004");
throw err;
}
/**
* Return a new date, time, or dateTime with the same normalized value, but
* in a different timezone
*
* @param tz the new timezone, in minutes
* @return the date/time in the new timezone
*/
public CalendarValue adjustTimezone(int tz) {
DateTimeValue dt = (DateTimeValue)toDateTime().adjustTimezone(tz);
return new GDayValue(dt.getDay(), dt.getTimezoneInMinutes());
}
}
//
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
// you may not use this file except in compliance with the License. You may obtain a copy of the
// License at http://www.mozilla.org/MPL/
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
// See the License for the specific language governing rights and limitations under the License.
//
// The Original Code is: all this file.
//
// The Initial Developer of the Original Code is Saxonica Limited
//
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
//
// Contributor(s): none
//
© 2015 - 2025 Weber Informatics LLC | Privacy Policy