KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > value > GDayValue


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 JavaDoc;
13 import java.util.GregorianCalendar JavaDoc;
14 import java.util.regex.Matcher JavaDoc;
15 import java.util.regex.Pattern JavaDoc;
16
17 /**
18  * Implementation of the xs:gDay data type
19  */

20
21 public class GDayValue extends DateValue {
22
23     private static Pattern JavaDoc regex =
24             Pattern.compile("---([0-9][0-9])(Z|[+-][0-9][0-9]:[0-9][0-9])?");
25
26     public GDayValue(){};
27
28     public GDayValue(CharSequence JavaDoc value) throws XPathException {
29         Matcher JavaDoc m = regex.matcher(value);
30         if (!m.matches()) {
31             throw new DynamicError("Cannot convert '" + value + "' to a gDay");
32         }
33         String JavaDoc base = m.group(1);
34         String JavaDoc tz = m.group(2);
35         String JavaDoc date = "2000-01-" + base + (tz==null ? "" : tz);
36         setLexicalValue(date);
37     }
38
39     public GDayValue(GregorianCalendar JavaDoc calendar, boolean timezoneSpecified, int tzoffset) {
40         super(calendar, timezoneSpecified, tzoffset);
41     }
42
43     /**
44     * Determine the data type of the expression
45     * @return Type.G_DAY_TYPE,
46     */

47
48     public ItemType getItemType() {
49         return Type.G_DAY_TYPE;
50     }
51
52     /**
53     * Convert to target data type
54     * @param requiredType an integer identifying the required atomic type
55     * @param conversion
56      * @return an AtomicValue, a value of the required type; or an ErrorValue
57     */

58
59     public AtomicValue convertPrimitive(BuiltInAtomicType requiredType, boolean validate, ConversionContext conversion) {
60         switch(requiredType.getPrimitiveType()) {
61         case Type.G_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 gDay to " +
72                     requiredType.getDisplayName());
73             err.setErrorCode("FORG0001");
74             return new ValidationErrorValue(err);
75         }
76     }
77
78     public String JavaDoc getStringValue() {
79
80         FastStringBuffer sb = new FastStringBuffer(16);
81
82         sb.append("---");
83         DateTimeValue.appendString(sb, calendar.get(Calendar.DATE), 2);
84
85         if (zoneSpecified) {
86             DateTimeValue.appendTimezone(tzOffset, sb);
87         }
88
89         return sb.toString();
90
91     }
92 }
93
94 //
95
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
96
// you may not use this file except in compliance with the License. You may obtain a copy of the
97
// License at http://www.mozilla.org/MPL/
98
//
99
// Software distributed under the License is distributed on an "AS IS" basis,
100
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
101
// See the License for the specific language governing rights and limitations under the License.
102
//
103
// The Original Code is: all this file.
104
//
105
// The Initial Developer of the Original Code is Saxonica Limited
106
//
107
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
108
//
109
// Contributor(s): none
110
//
Popular Tags