KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > webdocwf > util > loader > transformation > DateTransform


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 JavaDoc;
21 import java.io.FileInputStream JavaDoc;
22 import java.io.FileNotFoundException JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.sql.Date JavaDoc;
25 import java.sql.Time JavaDoc;
26 import java.sql.Timestamp JavaDoc;
27 import java.text.SimpleDateFormat JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Properties JavaDoc;
30 import java.util.Vector JavaDoc;
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 JavaDoc retValue = new Vector JavaDoc();
43     private String JavaDoc dateFormat = "";
44         private String JavaDoc filePath = "";
45         private Properties JavaDoc properties = new Properties JavaDoc();
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 JavaDoc filePath) throws Exception JavaDoc {
51         
52         try {
53
54                    this.filePath = filePath;
55                    File JavaDoc propertyFile = new File JavaDoc(this.filePath);
56                    this.properties.load(new FileInputStream JavaDoc(propertyFile));
57                    this.dateFormat = this.properties.getProperty("dateFormat");
58
59                } catch (FileNotFoundException JavaDoc e) {
60                    throw new Exception JavaDoc("File not found!");
61                } catch (IOException JavaDoc e) {
62                    throw new Exception JavaDoc("Error while reading property file!");
63                }
64     }
65
66     /**
67      * This method release resources
68      */

69     public void release() throws Exception JavaDoc {
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 JavaDoc transformValue(List JavaDoc valueToTransform) throws Exception JavaDoc {
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 JavaDoc formatDate = new SimpleDateFormat JavaDoc(this.dateFormat);
85                 String JavaDoc strDate = valueToTransform.get(0).toString();
86                 Time JavaDoc tmpTime = null;
87                 Date JavaDoc tmpDate = null;
88                 Timestamp JavaDoc tmpTimestamp = null;
89                 String JavaDoc readValue = null;
90                 try {
91                     //if input data is Date type
92
tmpDate = Date.valueOf(strDate);
93                 } catch (Exception JavaDoc e) {
94                     try {
95                         //if input data is Time type
96
tmpTime = Time.valueOf(strDate);
97                     } catch (Exception JavaDoc e1) {
98                         try {
99                             //if input data is Timestamp type
100
tmpTimestamp = Timestamp.valueOf(strDate);
101                         } catch (Exception JavaDoc e2) {
102                             throw new Exception JavaDoc("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 JavaDoc e) {
117             throw new Exception JavaDoc("Error while transform format of date using class DateTransform!",e);
118         }
119         return retValue;
120     }
121
122 }
123
Popular Tags