1 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 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 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 69 public void release() throws Exception { 70 71 } 72 73 77 public List transformValue(List valueToTransform) throws Exception { 78 retValue.clear(); 79 try { 80 if (this.dateFormat == "") { 81 this.retValue = valueToTransform; 83 } else { 84 SimpleDateFormat formatDate = new SimpleDateFormat (this.dateFormat); 85 String strDate = valueToTransform.get(0).toString(); 86 Time tmpTime = null; 87 Date tmpDate = null; 88 Timestamp tmpTimestamp = null; 89 String readValue = null; 90 try { 91 tmpDate = Date.valueOf(strDate); 93 } catch (Exception e) { 94 try { 95 tmpTime = Time.valueOf(strDate); 97 } catch (Exception e1) { 98 try { 99 tmpTimestamp = Timestamp.valueOf(strDate); 101 } catch (Exception e2) { 102 throw new Exception ("Error. Input date format is not supported!"); 103 } 104 } 105 } 106 if (tmpTime != null) { 107 readValue = formatDate.format(tmpTime); 108 } else if (tmpDate != null) { 109 readValue = formatDate.format(tmpDate); 110 } else if (tmpTimestamp != null) { 111 readValue = formatDate.format(tmpTimestamp); 112 } 113 114 retValue.add(readValue); 115 } 116 } catch (Exception e) { 117 throw new Exception ("Error while transform format of date using class DateTransform!",e); 118 } 119 return retValue; 120 } 121 122 } 123 | Popular Tags |