1 /* 2 * Copyright 2006 Edward Kuns 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 * 14 * $Id: $ 15 */ 16 package org.exolab.castor.types; 17 18 import org.exolab.castor.mapping.FieldDescriptor; 19 import org.exolab.castor.mapping.ValidityException; 20 import org.exolab.castor.xml.NodeType; 21 import org.exolab.castor.xml.XMLFieldDescriptor; 22 import org.exolab.castor.xml.XMLFieldHandler; 23 import org.exolab.castor.xml.util.XMLFieldDescriptorImpl; 24 25 /** 26 * The DateTime Descriptor. Note: Under normal circumstances, this descriptor will never be used. 27 * Castor treats XSD DateTime values as java.util.Date and parses these values into java.util.Date. 28 * 29 * @author <a href="edward.kuns@aspect.com">Edward Kuns</a> 30 * @version $Revision: 0000 $ $Date: $ 31 */ 32 public class DateTimeDescriptor extends BaseDescriptor { 33 34 /** The name of the XML element. */ 35 private static final String XML_NAME = "dateTime"; 36 /** Our field descriptor. */ 37 private static final XMLFieldDescriptorImpl CONTENT_DESCRIPTOR; 38 /** Our field descriptor array. Lists the fields we describe. */ 39 private static final FieldDescriptor[] FIELDS; 40 41 static { 42 CONTENT_DESCRIPTOR = 43 new XMLFieldDescriptorImpl(String.class, "content", "content", NodeType.Text); 44 CONTENT_DESCRIPTOR.setHandler((new DateTimeDescriptor()).new DateTimeFieldHandler()); 45 FIELDS = new FieldDescriptor[1]; 46 FIELDS[0] = CONTENT_DESCRIPTOR; 47 } 48 49 // ----------------/ 50 // - Constructors -/ 51 // ----------------/ 52 53 public DateTimeDescriptor() { 54 super(XML_NAME, DateTime.class); 55 } // -- DateDescriptor 56 57 // ------------------/ 58 // - Public Methods -/ 59 // ------------------/ 60 61 /** 62 * Returns the XMLFieldDescriptor for the member that should be marshalled as text content. 63 * 64 * @return the XMLFieldDescriptor for the member that should be marshalled as text content. 65 */ 66 public XMLFieldDescriptor getContentDescriptor() { 67 return CONTENT_DESCRIPTOR; 68 } // getContentDescriptor 69 70 /** 71 * Returns a list of fields represented by this descriptor. 72 * 73 * @return A list of fields 74 */ 75 public FieldDescriptor[] getFields() { 76 return FIELDS; 77 } // -- getFields 78 79 /** 80 * A specialized FieldHandler for the XML Schema DateTime related types. 81 * 82 * @author <a href="edward.kuns@aspect.com">Edward Kuns</a> 83 * @version $Revision: 0000 $ $Date: $ 84 */ 85 class DateTimeFieldHandler extends XMLFieldHandler { 86 87 // ----------------/ 88 // - Constructors -/ 89 // ----------------/ 90 91 /** 92 * Creates a new TimeFieldHandler. 93 */ 94 public DateTimeFieldHandler() { 95 super(); 96 } // -- TimeFieldHandler 97 98 // ------------------/ 99 // - Public Methods -/ 100 // ------------------/ 101 102 /** 103 * Returns the value of the field associated with this descriptor from the given target object. 104 * 105 * @param target the object to get the value from 106 * @return the value of the field associated with this descriptor from the given target object. 107 * @throws IllegalStateException if the target value is an inappropriate class, is null, or 108 * returns a String() on toString() that fails validation. 109 */ 110 public Object getValue(Object target) throws java.lang.IllegalStateException { 111 // -- check for DateTime class -- add later 112 DateTime date = (DateTime) target; 113 return date.toString(); 114 } // -- getValue 115 116 /** 117 * Sets the value of the field associated with this descriptor. 118 * 119 * @param target the object in which to set the value. 120 * @param value the value of the field. 121 * @throws IllegalStateException if the target value is an inappropriate class, is null, or 122 * returns a String() on toString() that fails validation. 123 */ 124 public void setValue(Object target, Object value) throws java.lang.IllegalStateException { 125 if (!(target instanceof DateTime)) { 126 String err = "DateTimeDescriptor#setValue: expected DateTime, received instead: " 127 + target.getClass(); 128 throw new IllegalStateException(err); 129 } 130 131 DateTime dateTarget = (DateTime) target; 132 133 if (value == null) { 134 String err = "DateTimeDescriptor#setValue: null value."; 135 throw new IllegalStateException(err); 136 } 137 138 // -- update current instance of time with new time 139 try { 140 DateTime temp = DateTime.parseDateTime(value.toString()); 141 dateTarget.setCentury(temp.getCentury()); 142 dateTarget.setYear(temp.getYear()); 143 dateTarget.setMonth(temp.getMonth()); 144 dateTarget.setDay(temp.getDay()); 145 dateTarget.setHour(temp.getHour()); 146 dateTarget.setMinute(temp.getMinute()); 147 dateTarget.setSecond(temp.getSeconds(), temp.getMilli()); 148 if (temp.isUTC()) { 149 dateTarget.setUTC(); 150 dateTarget.setZone(temp.getZoneHour(), temp.getZoneMinute()); 151 dateTarget.setZoneNegative(temp.isZoneNegative()); 152 } 153 } catch (java.text.ParseException ex) { 154 String err = "DateDescriptor#setValue: wrong value\n" + ex.getMessage(); 155 throw new IllegalStateException(err); 156 } 157 } // -- setValue 158 159 public void resetValue(Object target) throws java.lang.IllegalStateException { 160 // nothing to do? 161 } 162 163 /** 164 * Checks the field validity. Returns successfully if the field can be stored, is valid, etc, 165 * throws an exception otherwise. 166 * 167 * @param object The object 168 * @throws ValidityException The field is invalid, is required and null, or any other validity 169 * violation 170 * @throws IllegalStateException The Java object has changed and is no longer supported by this 171 * handler, or the handler is not compatiable with the Java object 172 */ 173 public void checkValidity(Object object) throws ValidityException, IllegalStateException { 174 // nothing to do? 175 } // -- checkValidity 176 177 /** 178 * Creates a new instance of the object described by this field. 179 * 180 * @param parent The object for which the field is created 181 * @return A new instance of the field's value 182 * @throws IllegalStateException never 183 */ 184 public Object newInstance(Object parent) throws IllegalStateException { 185 return new DateTime(); 186 } // -- newInstance 187 188 } // -- DateFieldHandler 189 190 } // -- DateDescriptor