View Javadoc
1 /* 2 Copyright (C) 2003 Together 3 4 This library is free software; you can redistribute it and/or 5 modify it under the terms of the GNU Lesser General Public 6 License as published by the Free Software Foundation; either 7 version 2.1 of the License, or (at your option) any later version. 8 9 This library is distributed in the hope that it will be useful, 10 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 Lesser General Public License for more details. 13 14 You should have received a copy of the GNU Lesser General Public 15 License along with this library; if not, write to the Free Software 16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 */ 18 package org.webdocwf.util.loader.transformation; 19 20 import java.io.File; 21 import java.io.FileInputStream; 22 import java.io.FileNotFoundException; 23 import java.io.IOException; 24 import java.sql.Date; 25 import java.sql.Time; 26 import java.sql.Timestamp; 27 import java.text.SimpleDateFormat; 28 import java.util.List; 29 import java.util.Properties; 30 import java.util.Vector; 31 32 /*** 33 * This class implements Transformer interface. It can be used to transform date from one 34 * format to another 35 * Date: 22.07.2004. 36 * @version 1.0 37 * @author Zeljko Kovacevic 38 * 39 */ 40 public class DateTransform implements Transformer { 41 42 private List retValue = new Vector(); 43 private String dateFormat = ""; 44 private String filePath = ""; 45 private Properties properties = new Properties(); 46 /*** This method uses to set configuration string 47 * which contains pattern for new date format 48 * @param filePath to property file 49 */ 50 public void configure(String filePath) throws Exception { 51 52 try { 53 54 this.filePath = filePath; 55 File propertyFile = new File(this.filePath); 56 this.properties.load(new FileInputStream(propertyFile)); 57 this.dateFormat = this.properties.getProperty("dateFormat"); 58 59 } catch (FileNotFoundException e) { 60 throw new Exception("File not found!"); 61 } catch (IOException e) { 62 throw new Exception("Error while reading property file!"); 63 } 64 } 65 66 /*** 67 * This method release resources 68 */ 69 public void release() throws Exception { 70 71 } 72 73 /*** This method transform date format 74 * @param valueToTransform List for transformation 75 * @return List with transformed values 76 */ 77 public List transformValue(List valueToTransform) throws Exception { 78 retValue.clear(); 79 try { 80 if (this.dateFormat == "") { 81 //if not set new date format DO NOTHING 82 this.retValue = valueToTransform; 83 } else { 84 SimpleDateFormat formatDate = new SimpleDateFormat(this.dateFormat); 85 String strDate = null; 86 if(valueToTransform.get(0) != null) 87 strDate = valueToTransform.get(0).toString(); 88 Time tmpTime = null; 89 Date tmpDate = null; 90 Timestamp tmpTimestamp = null; 91 String readValue = null; 92 try { 93 //if input data is Date type 94 if(strDate != null) 95 tmpDate = Date.valueOf(strDate); 96 } catch (Exception e) { 97 try { 98 //if input data is Time type 99 tmpTime = Time.valueOf(strDate); 100 } catch (Exception e1) { 101 try { 102 //if input data is Timestamp type 103 tmpTimestamp = Timestamp.valueOf(strDate); 104 } catch (Exception e2) { 105 throw new Exception("Error. Input date format is not supported!"); 106 } 107 } 108 } 109 if (tmpTime != null) { 110 readValue = formatDate.format(tmpTime); 111 } else if (tmpDate != null) { 112 readValue = formatDate.format(tmpDate); 113 } else if (tmpTimestamp != null) { 114 readValue = formatDate.format(tmpTimestamp); 115 } 116 117 retValue.add(readValue); 118 } 119 } catch (Exception e) { 120 throw new Exception("Error while transform format of date using class DateTransform!",e); 121 } 122 return retValue; 123 } 124 125 }

This page was automatically generated by Maven