]> WPIA git - gigi.git/blob - util/org/cacert/gigi/util/HighFinancialValueFetcher.java
fix: ResultSet.getDate is often wrong as it fetches day-precision times
[gigi.git] / util / org / cacert / gigi / util / HighFinancialValueFetcher.java
1 package org.cacert.gigi.util;
2
3 import java.io.BufferedReader;
4 import java.io.File;
5 import java.io.IOException;
6 import java.io.InputStreamReader;
7 import java.io.PrintWriter;
8 import java.net.URL;
9 import java.util.zip.ZipEntry;
10 import java.util.zip.ZipInputStream;
11
12 public abstract class HighFinancialValueFetcher {
13
14     public final int max;
15
16     private File f;
17
18     private String base;
19
20     public HighFinancialValueFetcher(File f, int max, String base) {
21         this.f = f;
22         this.max = max;
23         this.base = base;
24     }
25
26     public static void main(String[] args) throws IOException {
27         int max = 1000;
28         if (args.length > 1) {
29             max = Integer.parseInt(args[1]);
30         }
31         HighFinancialValueFetcher fetcher;
32         if (args.length > 2 && "--alexa".equals(args[2])) {
33             fetcher = new HighFinancialValueFetcherAlexa(new File(args[0]), max);
34         } else {
35             fetcher = new HighFinancialValueFetcherUmbrella(new File(args[0]), max);
36         }
37         fetcher.fetch();
38     }
39
40     public final void fetch() throws IOException {
41         try (PrintWriter fos = new PrintWriter(f, "UTF-8"); ZipInputStream zis = new ZipInputStream(new URL(base).openStream())) {
42             ZipEntry ze;
43             outer:
44             while ((ze = zis.getNextEntry()) != null) {
45                 System.out.println(ze.getName());
46                 BufferedReader br = new BufferedReader(new InputStreamReader(zis, "UTF-8"));
47                 String line;
48                 while ((line = br.readLine()) != null) {
49                     handle(line, fos);
50                     if (entries == -1) {
51                         break outer;
52                     }
53                 }
54             }
55         }
56     }
57
58     private int entries;
59
60     public void emit(PrintWriter fos, String value) {
61         fos.println(value);
62         if (entries == -1 || entries++ > max) {
63             entries = -1;
64         }
65     }
66
67     public abstract void handle(String line, PrintWriter fos);
68 }