KickJava   Java API By Example, From Geeks To Geeks.

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


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:gMonth data type
19  */

20
21 public class GMonthValue 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             // this tolerates the bogus format --MM-- which was wrongly permitted by the original schema spec
26

27     public GMonthValue(){};
28
29     public GMonthValue(CharSequence JavaDoc value) throws XPathException {
30         Matcher JavaDoc m = regex.matcher(value);
31         if (!m.matches()) {
32             throw new DynamicError("Cannot convert '" + value + "' to a gMonth");
33         }
34         String JavaDoc base = m.group(1);
35         String JavaDoc tz = m.group(3);
36         String JavaDoc date = "2000-" + base + "-01" + (tz==null ? "" : tz);
37         setLexicalValue(date);
38     }
39
40     public GMonthValue(GregorianCalendar JavaDoc calendar, boolean timezoneSpecified, int tzoffset) {
41         super(calendar, timezoneSpecified, tzoffset);
42     }
43
44     /**
45     * Determine the data type of the expression
46     * @return Type.G_MONTH_TYPE,
47     */

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

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