KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > generator > util > DateFilter


1 /*
2  * Copyright 2003, 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15
16  */

17 package org.apache.ws.jaxme.generator.util;
18
19 import java.io.BufferedReader JavaDoc;
20 import java.io.BufferedWriter JavaDoc;
21 import java.io.File JavaDoc;
22 import java.io.FileOutputStream JavaDoc;
23 import java.io.FileReader JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.OutputStreamWriter JavaDoc;
26 import java.io.PrintStream JavaDoc;
27 import java.io.Writer JavaDoc;
28 import java.text.DateFormat JavaDoc;
29 import java.text.ParseException JavaDoc;
30 import java.text.SimpleDateFormat JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.Arrays JavaDoc;
33 import java.util.Calendar JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.List JavaDoc;
36 import java.util.ListIterator JavaDoc;
37
38
39 /** <p>Helper class, that filters a file by replacing certain
40  * patterns. Used by the test suite.</p>
41  *
42  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
43  * @version $Id: DateFilter.java,v 1.2 2004/02/16 23:39:58 jochen Exp $
44  */

45 public class DateFilter {
46   private File JavaDoc fromFile, toFile;
47   private boolean force;
48
49   public void setFromFile(File JavaDoc pFile) { fromFile = pFile; }
50   public File JavaDoc getFromFile() { return fromFile; }
51   public void setToFile(File JavaDoc pFile) { toFile = pFile; }
52   public File JavaDoc getToFile() { return toFile; }
53   public void setForce(boolean pForce) { force = pForce; }
54   public boolean getForce() { return force; }
55
56   public String JavaDoc validate() {
57     StringBuffer JavaDoc result = new StringBuffer JavaDoc();
58     if (fromFile == null) {
59       result.append("Input file (option fromFile) is not set.\n");
60     } else if (!fromFile.exists()) {
61       result.append("Input file " + fromFile + " doesn't exist.");
62     }
63     if (toFile == null) {
64       result.append("toFile is not set.\n");
65     }
66
67     if (result.length() > 0) {
68       return result.toString();
69     } else {
70       return null;
71     }
72   }
73
74   public boolean isRunning() {
75     if (getForce()) {
76       return true;
77     }
78
79     long l1 = fromFile.lastModified();
80     if (l1 == -1) {
81       return true;
82     }
83     long l2 = toFile.lastModified();
84     if (l2 == -1) {
85       return true;
86     }
87     return l2 <= l1;
88   }
89
90   public List JavaDoc getLines() throws IOException JavaDoc {
91     List JavaDoc result = new ArrayList JavaDoc();
92     BufferedReader JavaDoc br = new BufferedReader JavaDoc(new FileReader JavaDoc(getFromFile()));
93     for (;;) {
94       String JavaDoc s = br.readLine();
95       if (s == null) {
96         break;
97       }
98       result.add(s);
99     }
100     return result;
101   }
102
103   public Calendar JavaDoc getCalendar(String JavaDoc pDateTime, String JavaDoc pPattern)
104       throws ParseException JavaDoc {
105     DateFormat JavaDoc simpleFormat = new SimpleDateFormat JavaDoc(pPattern);
106     Calendar JavaDoc cal = Calendar.getInstance();
107     cal.setTime(simpleFormat.parse(pDateTime));
108     return cal;
109   }
110
111   public String JavaDoc replacePattern(String JavaDoc pPattern, String JavaDoc pSourceFormat,
112                                 DateFormat JavaDoc pTargetFormat) throws ParseException JavaDoc {
113     return pTargetFormat.format(getCalendar(pPattern, pSourceFormat).getTime());
114   }
115
116   public String JavaDoc getDateTime(String JavaDoc pDateTime) throws ParseException JavaDoc {
117     return replacePattern(pDateTime, "yyyy-MM-dd HH:mm:ss",
118                            DateFormat.getDateTimeInstance());
119   }
120
121   public String JavaDoc getDate(String JavaDoc pDateTime) throws ParseException JavaDoc {
122     return replacePattern(pDateTime, "yyyy-MM-dd",
123                            DateFormat.getDateInstance());
124   }
125
126   public String JavaDoc getTime(String JavaDoc pDateTime) throws ParseException JavaDoc {
127     return replacePattern(pDateTime, "HH:mm:ss",
128                            DateFormat.getTimeInstance());
129   }
130
131   public void replace(List JavaDoc pLines, List JavaDoc fromStrings, List JavaDoc toStrings) {
132     for (ListIterator JavaDoc iter = pLines.listIterator(); iter.hasNext(); ) {
133       String JavaDoc s = (String JavaDoc) iter.next();
134       StringBuffer JavaDoc result = new StringBuffer JavaDoc();
135
136       while (s.length() > 0) {
137         boolean done = false;
138         for (int i = 0; i < fromStrings.size(); i++) {
139           String JavaDoc from = (String JavaDoc) fromStrings.get(i);
140           if (s.startsWith(from)) {
141             result.append((String JavaDoc) toStrings.get(i));
142             s = s.substring(from.length());
143             done = true;
144             break;
145           }
146         }
147         if (!done) {
148           result.append(s.charAt(0));
149           s = s.substring(1);
150         }
151       }
152
153       iter.set(result.toString());
154     }
155   }
156
157   public void putLines(List JavaDoc pLines) throws IOException JavaDoc {
158     Writer JavaDoc fw = new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(new FileOutputStream JavaDoc(getToFile()), "UTF-8"));
159     for (Iterator JavaDoc iter = pLines.iterator(); iter.hasNext(); ) {
160       String JavaDoc s = (String JavaDoc) iter.next();
161       fw.write(s + "\n");
162     }
163     fw.close();
164   }
165
166   public void execute() throws IOException JavaDoc, ParseException JavaDoc {
167     if (!isRunning()) {
168       return;
169     }
170     List JavaDoc lines = getLines();
171
172     List JavaDoc fromStrings = new ArrayList JavaDoc();
173     List JavaDoc toStrings = new ArrayList JavaDoc();
174
175     fromStrings.add("2002-12-17 12:23:11");
176     toStrings.add(getDateTime("2002-12-17 12:23:11"));
177
178     fromStrings.add("2002-12-16 12:00:11");
179     toStrings.add(getDateTime("2002-12-16 12:00:11"));
180
181     fromStrings.add("2002-12-17");
182     toStrings.add(getDate("2002-12-17"));
183
184     fromStrings.add("2002-12-16");
185     toStrings.add(getDate("2002-12-16"));
186
187     fromStrings.add("12:23:11");
188     toStrings.add(getTime("12:23:11"));
189
190     fromStrings.add("12:00:11");
191     toStrings.add(getTime("12:00:11"));
192
193     replace(lines, fromStrings, toStrings);
194     putLines(lines);
195   }
196
197   public static void Usage(String JavaDoc pMsg) {
198     PrintStream JavaDoc ps = System.err;
199     if (pMsg != null) {
200       ps.println(pMsg);
201       ps.println();
202     }
203     ps.println("Usage: java " + DateFilter.class + " <options>");
204     ps.println();
205     ps.println("Possible options are:");
206     ps.println(" --fromFile <file> Sets the input file");
207     ps.println(" --toFile <file> Sets the output file");
208     ps.println(" --force Forces overwriting of the output file");
209     System.exit(1);
210   }
211
212   public static void main(String JavaDoc[] args) throws Exception JavaDoc {
213     DateFilter filter = new DateFilter();
214     List JavaDoc argList = new ArrayList JavaDoc(Arrays.asList(args));
215     while (!argList.isEmpty()) {
216       String JavaDoc arg = (String JavaDoc) argList.remove(0);
217       String JavaDoc opt = null;
218       if (arg.startsWith("--")) {
219         opt = arg.substring(2);
220       } else if (arg.startsWith("-")) {
221         opt = arg.substring(1);
222       } else {
223         Usage("Unknown argument: " + arg);
224       }
225
226       if ("fromFile".equals(opt)) {
227         if (argList.isEmpty()) {
228           Usage("Option " + arg + " requires an argument: Input file");
229         }
230         filter.setFromFile(new File JavaDoc((String JavaDoc) argList.remove(0)));
231       } else if ("toFile".equals(opt)) {
232         if (argList.isEmpty()) {
233           Usage("Option " + arg + " requires an argument: Output file");
234         }
235         filter.setToFile(new File JavaDoc((String JavaDoc) argList.remove(0)));
236       } else if ("force".equals(opt)) {
237         filter.setForce(true);
238       } else {
239         Usage("Unknown option: " + opt);
240       }
241     }
242
243     String JavaDoc msg = filter.validate();
244     if (msg != null) {
245       Usage(msg);
246     } else {
247       filter.execute();
248     }
249   }
250 }
251
Popular Tags