View Javadoc

1   /**
2      This file is part of GoldenGate Project (named also GoldenGate or GG).
3   
4      Copyright 2009, Frederic Bregier, and individual contributors by the @author
5      tags. See the COPYRIGHT.txt in the distribution for a full listing of
6      individual contributors.
7   
8      All GoldenGate Project is free software: you can redistribute it and/or 
9      modify it under the terms of the GNU General Public License as published 
10     by the Free Software Foundation, either version 3 of the License, or
11     (at your option) any later version.
12  
13     GoldenGate is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17  
18     You should have received a copy of the GNU General Public License
19     along with GoldenGate .  If not, see <http://www.gnu.org/licenses/>.
20   */
21  package goldengate.common.utility;
22  
23  import java.io.File;
24  import java.io.FileNotFoundException;
25  import java.io.FileReader;
26  import java.io.IOException;
27  import java.nio.charset.Charset;
28  import java.sql.Timestamp;
29  import java.text.ParseException;
30  import java.text.SimpleDateFormat;
31  import java.util.Arrays;
32  import java.util.Date;
33  
34  import org.dom4j.Node;
35  
36  import goldengate.common.exception.FileTransferException;
37  import goldengate.common.exception.InvalidArgumentException;
38  import goldengate.common.logging.GgInternalLogger;
39  import goldengate.common.logging.GgInternalLoggerFactory;
40  
41  /**
42   * Various utilities for reading files, transforming dates, ...
43   * 
44   * @author Frederic Bregier
45   *
46   */
47  public class GgStringUtils {
48      /**
49       * Internal Logger
50       */
51      private static final GgInternalLogger logger = GgInternalLoggerFactory
52              .getLogger(GgStringUtils.class);
53      
54      /**
55       * Format used for Files
56       */
57      public static final Charset UTF8 = Charset.forName("UTF-8");
58      
59      /**
60       * Read a file and return its content in String format
61       * @param filename
62       * @return the content of the File in String format
63       * @throws InvalidArgumentException for File not found
64       * @throws FileTransferException for reading exception
65       */
66      public static String readFileException(String filename) throws InvalidArgumentException, FileTransferException {
67          File file = new File(filename);
68          char [] chars = new char[(int) file.length()];
69          FileReader fileReader;
70          try {
71              fileReader = new FileReader(file);
72          } catch (FileNotFoundException e) {
73              logger.error("File not found while trying to access: "+filename, e);
74              throw new InvalidArgumentException("File not found while trying to access",e);
75              //return null;
76          }
77          try {
78              fileReader.read(chars);
79          } catch (IOException e) {
80              logger.error("Error on File while trying to read: "+filename, e);
81              throw new FileTransferException("Error on File while trying to read",e);
82              //return null;
83          }
84          return new String(chars);
85      }
86      /**
87       * Read file and return "" if an error occurs
88       * @param filename
89       * @return the string associated with the file, or "" if an error occurs
90       */
91      public static String readFile(String filename) {
92          try {
93              return readFileException(filename);
94          } catch (InvalidArgumentException e) {
95              logger.error("Error while trying to open: "+filename,e);
96              return "";
97          } catch (FileTransferException e) {
98              logger.error("Error while trying to read: "+filename,e);
99              return "";
100         }
101     }
102     /**
103      * Simple Date format
104      */
105     private static final SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
106     /**
107      * Get a date in String and return the corresponding Timestamp
108      * @param date
109      * @return the corresponding Timestamp
110      */
111     public static Timestamp fixDate(String date) {
112         Timestamp tdate = null;
113         String ndate = date.replaceAll("/|:|\\.| |-", "");
114         if (ndate.length() > 0) {
115             if (ndate.length() < 15) {
116                 int len = ndate.length();
117                 ndate += "000000000000000".substring(len);
118             }
119             try {
120                 Date ddate = format.parse(ndate);
121                 tdate = new Timestamp(ddate.getTime());
122             } catch (ParseException e) {
123                 logger.debug("start",e);
124             }
125         }
126         return tdate;
127     }
128     /**
129      * From a date in String format and a Timestamp, return the Timestamp as :<br>
130      * if before = null as date<br>
131      * if before != null and before < date, as date<br>
132      * if before != null and before >= date, as before end of day (23:59:59:9999)
133      * @param date
134      * @param before
135      * @return the end date
136      */
137     public static Timestamp fixDate(String date, Timestamp before) {
138         Timestamp tdate = null;
139         String ndate = date.replaceAll("/|:|\\.| |-", "");
140         if (ndate.length() > 0) {
141             if (ndate.length() < 15) {
142                 int len = ndate.length();
143                 ndate += "000000000000000".substring(len);
144             }
145             try {
146                 Date ddate = format.parse(ndate);
147                 if (before != null) {
148                     Date bef = new Date(before.getTime());
149                     if (bef.compareTo(ddate) >= 0) {
150                         ddate = new Date(bef.getTime()+1000*3600*24-1);
151                     }
152                 }
153                 tdate = new Timestamp(ddate.getTime());
154             } catch (ParseException e) {
155                 logger.debug("start",e);
156             }
157         }
158         return tdate;
159     }
160     
161     /**
162      * Read a boolean value (0,1,true,false) from a node
163      * @param node
164      * @return the corresponding value
165      */
166     public static boolean getBoolean(Node node) {
167         String val = node.getText();
168         boolean bval;
169         try {
170             int ival = Integer.parseInt(val);
171             bval = (ival == 1) ? true : false;
172         } catch (NumberFormatException e) {
173             bval = Boolean.parseBoolean(val);
174         }
175         return bval;
176     }
177     /**
178      * Read an integer value from a node
179      * @param node
180      * @return the corresponding value
181      * @throws InvalidArgumentException 
182      */
183     public static int getInteger(Node node) throws InvalidArgumentException {
184         if (node == null)
185             throw new InvalidArgumentException("Node empty");
186         String val = node.getText();
187         int ival;
188         try {
189             ival = Integer.parseInt(val);
190         } catch (NumberFormatException e) {
191             throw new InvalidArgumentException("Incorrect value");
192         }
193         return ival;
194     }
195     /**
196      * Make a replacement of first "find" string by "replace" string into the StringBuilder
197      * @param builder
198      * @param find
199      * @param replace
200      */
201     public static boolean replace(StringBuilder builder, String find, String replace) {
202         int start = builder.indexOf(find);
203         if (start == -1) {
204             return false;
205         }
206         int end = start+find.length();
207         builder.replace(start, end, replace);
208         return true;
209     }
210     /**
211      * Make replacement of all "find" string by "replace" string into the StringBuilder
212      * @param builder
213      * @param find
214      * @param replace
215      */
216     public static void replaceAll(StringBuilder builder, String find, String replace) {
217         while (replace(builder, find, replace)) {
218         }
219     }
220     /**
221      * Build a String with count chars using fillChar
222      * @param fillChar
223      * @param count
224      * @return the String of length count filled with fillChar
225      */
226     public static String fillString(char fillChar, int count) {
227         char []chars = new char[count];
228         Arrays.fill(chars, fillChar);
229         return new String(chars);
230     }
231 }