View Javadoc
1 /* 2 * Scope: a generic MVC framework. 3 * Copyright (c) 2000-2002, The Scope team 4 * All rights reserved. 5 * 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 14 * Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * Neither the name "Scope" nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR 27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 * 35 * 36 * $Id: DateStringConvertor.java,v 1.8 2002/09/05 15:41:46 ludovicc Exp $ 37 */ 38 package org.scopemvc.util.convertor; 39 40 41 import java.text.DateFormat; 42 import java.text.ParseException; 43 import java.text.SimpleDateFormat; 44 import java.util.ArrayList; 45 import java.util.Date; 46 import java.util.Iterator; 47 import java.util.List; 48 import org.apache.commons.logging.Log; 49 import org.apache.commons.logging.LogFactory; 50 import org.scopemvc.util.ScopeConfig; 51 52 /*** 53 * String convertor for type <code>java.util.Date</code>. The Time part of 54 * <code>Date</code> instances are ignored. See {@link DateTimeStringConvertor 55 * DateTimeStringConvertor} and {@link TimeStringConvertor TimeStringConvertor}. 56 * <p> 57 * 58 * It uses one java.text.DateFormat for converting into <code>String</code> and 59 * set of <code>DateFormat</code>s for parsing. Parsing formats are successively 60 * used to try to parse until one is successful. </p> <p> 61 * 62 * Formats are picked up from config (see {@link 63 * org.scopemvc.util.DefaultScopeConfig} for details) or if none in config, the 64 * default formatter is: <pre> 65 * DateFormat.getDateInstance(DateFormat.MEDIUM) 66 * </pre> and default parsers are: <pre> 67 * DateFormat.getDateInstance(DateFormat.FULL); 68 * DateFormat.getDateInstance(DateFormat.LONG); 69 * DateFormat.getDateInstance(DateFormat.MEDIUM); 70 * DateFormat.getDateInstance(DateFormat.SHORT); 71 * </pre> <i>Note:</i> the default parsing set is initialized during class 72 * loading and based on default platform locale. If application uses other 73 * locales, there will be need to set parsing objects explicitly. 74 * 75 * @author <A HREF="mailto:danmi@users.sourceforge.net">Daniel Michalik</A> 76 * @author <A HREF="mailto:smeyfroi@users.sourceforge.net">Steve Meyfroidt</A> 77 * @created 05 September 2002 78 * @version $Revision: 1.8 $ $Date: 2002/09/05 15:41:46 $ 79 * @see DateTimeStringConvertor 80 * @see TimeStringConvertor 81 */ 82 public class DateStringConvertor extends NullStringConvertor { 83 84 private final static Log LOG = LogFactory.getLog(DateStringConvertor.class); 85 86 // ------------- Defaults if no config specified ---------------- 87 private final static DateFormat defaultParsers[] = new DateFormat[4]; 88 89 private DateFormat formatter; 90 private DateFormat parsers[]; 91 static { 92 defaultParsers[3] = DateFormat.getDateInstance(DateFormat.SHORT); 93 defaultParsers[2] = DateFormat.getDateInstance(DateFormat.MEDIUM); 94 defaultParsers[1] = DateFormat.getDateInstance(DateFormat.LONG); 95 defaultParsers[0] = DateFormat.getDateInstance(DateFormat.FULL); 96 } 97 98 // ------------------------------------------------------------------ 99 100 /*** 101 * Creates new DateStringConvertor. If formats and parsers are specified in 102 * config then use those else use current locale default format and platform 103 * locale default parsers. 104 */ 105 public DateStringConvertor() { 106 initDefaults(); 107 } 108 109 110 /*** 111 * Creates new DateStringConvertor with specified formatter and parsers. 112 * 113 * @param inFormatter TODO: Describe the Parameter 114 * @param inParsers TODO: Describe the Parameter 115 */ 116 public DateStringConvertor(DateFormat inFormatter, DateFormat inParsers[]) { 117 setFormatter(inFormatter); 118 setParsers(inParsers); 119 } 120 121 122 /*** 123 * Sets format used by this convertor for converting <code>Date</code>s into 124 * <code>String</code>s. 125 * 126 * @param inFormat The new formatter value 127 * @throws IllegalArgumentException if passed format is null or not a 128 * DateFormat or not a String. 129 */ 130 public final void setFormatter(Object inFormat) { 131 if (inFormat instanceof DateFormat) { 132 formatter = (DateFormat) inFormat; 133 } else if (inFormat instanceof String) { 134 formatter = new SimpleDateFormat((String) inFormat); 135 } else { 136 throw new IllegalArgumentException("Can't set formatter to: " + inFormat); 137 } 138 } 139 140 141 /*** 142 * Set parser array for this converter. 143 * 144 * @param inParsers The new parsers value 145 * @throws IllegalArgumentException if the array is <code>null</code> or 146 * contains a non-String or non-DateFormat object. 147 */ 148 public final void setParsers(Object[] inParsers) { 149 if (inParsers == null) { 150 throw new IllegalArgumentException("Passed array of formats cannot be null"); 151 } 152 153 parsers = new DateFormat[inParsers.length]; 154 for (int i = 0; i < inParsers.length; i++) { 155 if (LOG.isDebugEnabled()) { 156 LOG.debug("setParsers: " + inParsers[i]); 157 } 158 if (inParsers[i] instanceof String) { 159 SimpleDateFormat f = new SimpleDateFormat((String) inParsers[i]); 160 parsers[i] = f; 161 } else if (inParsers[i] instanceof DateFormat) { 162 parsers[i] = (DateFormat) inParsers[i]; 163 } else { 164 throw new IllegalArgumentException("Cannot set a parser to be: " + inParsers[i]); 165 } 166 parsers[i].setLenient(false); 167 } 168 } 169 170 171 /*** 172 * Returns instance of <code>java.util.Date</code>.Parsing formats are 173 * successively used to try to parse until one is successful or exception is 174 * thrown. <p> 175 * 176 * Empty, <code>null</code> and {@link #getNullAsString() getNullAsString()} 177 * strings are converted into <code>null</code>. </p> 178 * 179 * @param inString TODO: Describe the Parameter 180 * @return TODO: Describe the Return Value 181 * @throws IllegalArgumentException if can't convert from String using 182 * current {@link java.text.DateFormat DateFormat} 183 */ 184 public Object stringAsValue(String inString) throws IllegalArgumentException { 185 186 if (isNull(inString)) { 187 return null; 188 } 189 190 for (int i = parsers.length - 1; i >= 0; i--) { 191 if (LOG.isDebugEnabled()) { 192 LOG.debug("Parser: " + inString + ", " + parsers[i].format(new Date(0))); 193 } 194 try { 195 Date result = parsers[i].parse(inString); 196 if (result != null) { 197 return result; 198 } 199 } catch (ParseException ex) { 200 if (LOG.isDebugEnabled()) { 201 LOG.debug("stringAsValue", ex); 202 } 203 // ignore and try again 204 } 205 } 206 throw new IllegalArgumentException("Cannot convert to Date/Time: " + inString); 207 } 208 209 210 /*** 211 * @param inValue TODO: Describe the Parameter 212 * @return text representation of Date object. For null argument is called 213 * method {@link #getNullAsString() getNullAsString} 214 * @throws IllegalArgumentException it argument is not instance of {@link 215 * java.util.Date java.util.Date} 216 */ 217 public String valueAsString(Object inValue) throws IllegalArgumentException { 218 if (inValue == null) { 219 return getNullAsString(); 220 } 221 if (!(inValue instanceof Date)) { 222 throw new IllegalArgumentException("Passed object is not subclass " 223 + "of java.util.Date. Its class is " + inValue.getClass()); 224 } 225 return formatter.format(inValue); 226 } 227 228 229 /* 230 * for debug only 231 * public final void dumpConfig() { 232 * LOG.info(getClass().getName()); 233 * LOG.info(formatter.format(new Date(0))); 234 * LOG.info("Number of parsers: " + parsers.length); 235 * for (int i = 0; i < parsers.length; i++) { 236 * LOG.info(Integer.toString(i) + ": " + parsers[i].format(new Date(0))); 237 * } 238 * } 239 */ 240 /*** 241 * Gets the default parsers 242 * 243 * @return The defaultParsers value 244 */ 245 protected DateFormat[] getDefaultParsers() { 246 return DateStringConvertor.defaultParsers; 247 } 248 249 250 /*** 251 * TODO: document the method 252 */ 253 protected void initDefaults() { 254 Object configFormatter = ScopeConfig.getObject(getClass().getName() + ".formatter"); 255 setFormatter(configFormatter); 256 257 List parserList = new ArrayList(); 258 for (Iterator i = ScopeConfig.getKeysMatching(getClass().getName() + ".parser"); i.hasNext(); ) { 259 DateFormat df = (DateFormat) ScopeConfig.getObject((String) i.next()); 260 parserList.add(df); 261 } 262 if (parserList.size() < 1) { 263 setParsers(getDefaultParsers()); 264 } else { 265 setParsers(parserList.toArray()); 266 } 267 } 268 }

This page was automatically generated by Maven