KickJava   Java API By Example, From Geeks To Geeks.

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


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 concatenate
30  * data from table with values placed into property file
31  * Date: 23.07.2004.
32  * @version 1.0
33  * @author Zeljko Kovacevic
34  *
35  */

36 public class ConcatenateData implements Transformer {
37     private String JavaDoc filePath = "";
38     private List JavaDoc retValue = new Vector JavaDoc();
39     private String JavaDoc prefix = "";
40     private String JavaDoc postfix = "";
41     private String JavaDoc insertBlank = "";
42     private Properties JavaDoc properties = new Properties JavaDoc();
43     /**
44      * This method reads path to property file which is used for replacing data
45      * Keys from property file are values from table, and values from property
46      * file are values which will be concatenated as prefix or postfix on original value
47      * @param filePath Path to property file
48      */

49     public void configure(String JavaDoc filePath) throws Exception JavaDoc {
50         try {
51
52             this.filePath = filePath;
53             File JavaDoc propertyFile = new File JavaDoc(this.filePath);
54             this.properties.load(new FileInputStream JavaDoc(propertyFile));
55             this.prefix = this.properties.getProperty("prefix");
56             this.postfix = this.properties.getProperty("postfix");
57                         this.insertBlank = this.properties.getProperty("insertBlank");
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 will concatenate values from property file to values from table
74      * as prefix or postfix
75      * @param valueToTransform List for transformation
76      * @return List with transformed values
77      */

78     public List JavaDoc transformValue(List JavaDoc valueToTransform) throws Exception JavaDoc {
79         retValue.clear();
80         String JavaDoc oldValue = valueToTransform.get(0).toString();
81         String JavaDoc newValue = "";
82         String JavaDoc finalValue = "";
83         String JavaDoc key = "";
84         boolean isSet = false;
85         try {
86             boolean containsKey = this.properties.containsKey(oldValue);
87             newValue = this.properties.getProperty(oldValue);
88
89             if (this.prefix.equalsIgnoreCase("true") && this.postfix.equalsIgnoreCase("true")) {
90                 if (containsKey == true) {
91                     isSet = true;
92                     if (this.insertBlank.equalsIgnoreCase("true")){
93                                                 finalValue = newValue +" "+ oldValue +" "+ newValue;
94                     }else{
95                             finalValue = newValue + oldValue + newValue;
96                     }
97                 }
98             } else if (this.prefix.equalsIgnoreCase("true")) {
99                 if (containsKey == true) {
100                     isSet = true;
101                     if (this.insertBlank.equalsIgnoreCase("true")){
102                                                 finalValue = newValue + " " + oldValue;
103                     }else{
104                             finalValue = newValue + oldValue;
105                     }
106                 }
107             } else if (this.postfix.equalsIgnoreCase("true")) {
108                 if (containsKey == true) {
109                     isSet = true;
110                     if (this.insertBlank.equalsIgnoreCase("true")){
111                                                 finalValue = oldValue + " " + newValue;
112                     }else{
113                             finalValue = oldValue + newValue;
114                     }
115                 }
116             }
117             if (isSet) {
118                 retValue.add(finalValue);
119             } else {
120                 retValue.add(oldValue);
121             }
122         } catch (Exception JavaDoc e) {
123             throw new Exception JavaDoc("Error while concatenate data using class ConcatenateData!",e);
124         }
125         return retValue;
126     }
127 }
Popular Tags