KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.List JavaDoc;
25 import java.util.Properties JavaDoc;
26 import java.util.Vector JavaDoc;
27
28 /**
29  * This class implements Transformer interface. It can be used to replace
30  * data from database table with values placed in property file
31  * format to another
32  * Date: 23.07.2004.
33  * @version 1.0
34  * @author Zeljko Kovacevic
35  *
36  */

37 public class ReplaceData implements Transformer {
38
39     private String JavaDoc filePath = "";
40     private List JavaDoc retValue = new Vector JavaDoc();
41     private String JavaDoc valueToReplace = "";
42     private String JavaDoc valueReplaceWith = "";
43     private String JavaDoc replaceAll = "";
44     private String JavaDoc newPropValue = "";
45     private Properties JavaDoc properties = new Properties JavaDoc();
46     /**
47      * This method reads path to property file which is used for replacing data
48      * Keys from property file are values from table, and values from property
49      * file are new values which will be placed instead of old ones.
50      * @param filePath Path to property file
51      */

52     public void configure(String JavaDoc filePath) throws Exception JavaDoc {
53         try {
54
55             this.filePath = filePath;
56             File JavaDoc propertyFile = new File JavaDoc(this.filePath);
57             this.properties.load(new FileInputStream JavaDoc(propertyFile));
58             this.replaceAll = this.properties.getProperty("replaceAll");
59             this.newPropValue = this.properties.getProperty("newPropValue");
60
61         } catch (FileNotFoundException JavaDoc e) {
62             throw new Exception JavaDoc("File not found!");
63         } catch (IOException JavaDoc e) {
64             throw new Exception JavaDoc("Error while reading property file!");
65         }
66     }
67
68         /**
69      * This method release resources
70      */

71     public void release() throws Exception JavaDoc {
72
73     }
74
75         /** This method will change old values from table with new ones from
76          * property file
77          * @param valueToTransform List for transformation
78          * @return List with transformed values
79          */

80     public List JavaDoc transformValue(List JavaDoc valueToTransform) throws Exception JavaDoc {
81         retValue.clear();
82         String JavaDoc oldValue = valueToTransform.get(0).toString();
83         String JavaDoc key = "";
84        try {
85                 if (this.replaceAll.equalsIgnoreCase("true")){
86                             retValue.add(this.newPropValue);
87                 }else{
88                 boolean containsKey = this.properties.containsKey(oldValue);
89                 if (containsKey == true) {
90                     String JavaDoc newValue = this.properties.getProperty(oldValue);
91                     retValue.add(newValue);
92                 } else {
93                     retValue.add(oldValue);
94                 }
95                 }
96         } catch (Exception JavaDoc e) {
97            throw new Exception JavaDoc("Error while replacing data using class ReplaceData!",e);
98         }
99
100         return retValue;
101     }
102 }
Popular Tags