1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
43
44
45
46
47 public class GgStringUtils {
48
49
50
51 private static final GgInternalLogger logger = GgInternalLoggerFactory
52 .getLogger(GgStringUtils.class);
53
54
55
56
57 public static final Charset UTF8 = Charset.forName("UTF-8");
58
59
60
61
62
63
64
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
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
83 }
84 return new String(chars);
85 }
86
87
88
89
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
104
105 private static final SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
106
107
108
109
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
130
131
132
133
134
135
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
163
164
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
179
180
181
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
197
198
199
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
212
213
214
215
216 public static void replaceAll(StringBuilder builder, String find, String replace) {
217 while (replace(builder, find, replace)) {
218 }
219 }
220
221
222
223
224
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 }