1 package org.crosswire.jsword.view.web;
2
3 import java.io.File;
4 import java.io.FileFilter;
5 import java.io.IOException;
6 import java.text.DateFormat;
7 import java.text.NumberFormat;
8 import java.text.ParseException;
9 import java.text.SimpleDateFormat;
10 import java.util.Date;
11 import java.util.SortedSet;
12 import java.util.TreeSet;
13
14 import org.crosswire.common.util.Logger;
15
16
23 public class DownloadSet implements Comparable<DownloadSet> {
24 public static final String BIN_ZIP = "-bin.zip";
25 public static final String BIN_TGZ = "-bin.tar.gz";
26 public static final String SRC_ZIP = "-src.zip";
27 public static final String SRC_TGZ = "-src.tar.gz";
28 public static final String DOC_ZIP = "-doc.zip";
29 public static final String DOC_TGZ = "-doc.tar.gz";
30
31
34 public static DownloadSet[] getDownloadSets(String localprefix, String webprefix, boolean datesort) throws IOException {
35 File dir = new File(localprefix);
36 if (!dir.isDirectory()) {
37 throw new IOException(Msg.lookupText("{0} is not a directory", localprefix));
38 }
39
40 log.debug("dig " + localprefix);
41 File[] files = dir.listFiles(new FileFilter() {
42 public boolean accept(File file) {
43 String name = file.getName();
44 log.debug("found " + name);
45 return file.canRead() && name.startsWith(TEST_PREFIX) && name.endsWith(TEST_SUFFIX);
46 }
47 });
48
49 SortedSet<DownloadSet> reply = new TreeSet<DownloadSet>();
50 for (int i = 0; i < files.length; i++) {
51 String name = files[i].getName();
52 log.debug("adding " + name);
53 String sets = name.substring(TEST_PREFIX.length(), name.length() - TEST_SUFFIX.length());
54 reply.add(new DownloadSet(localprefix, webprefix, sets, datesort));
55 }
56
57 return reply.toArray(new DownloadSet[reply.size()]);
58 }
59
60
63 private DownloadSet(String localprefix, String webprefix, String setname, boolean datesort) {
64 this.localprefix = localprefix;
65 this.webprefix = webprefix;
66 this.setname = setname;
67 this.datesort = datesort;
68
69 log.debug("ctor " + webprefix);
70 }
71
72
77 public int compareTo(DownloadSet that) {
78 if (datesort) {
79 try {
80 String thisSetdate = this.setname.substring(this.setname.length() - VERSION_DATE.length());
83 Date thisdate = DF_DISK.parse(thisSetdate);
84 String thatSetdate = that.setname.substring(that.setname.length() - VERSION_DATE.length());
85 Date thatdate = DF_DISK.parse(thatSetdate);
86
87 return thisdate.compareTo(thatdate);
88 } catch (ParseException ex) {
89 log.error("Failed to parse dates", ex);
90 return 0;
91 }
92 }
93 return that.setname.compareTo(this.setname);
94 }
95
96
99 public String getDateString() throws ParseException {
100 String setdate = setname.substring(setname.length() - VERSION_DATE.length());
102 Date date = DF_DISK.parse(setdate);
103 return DF_USER.format(date);
104 }
105
106
109 public String getVersionString() {
110 return Msg.lookupText("Version {0}", setname);
111 }
112
113
120 public String getLinkString(String extension) {
121 File file = new File(localprefix, TEST_PREFIX + setname + extension);
122 String size = NF.format(file.length() / (1024.0F * 1024.0F));
123 String reply = "<a href='" + webprefix + '/' + TEST_PREFIX + setname + extension + "'>" + size + " Mb</a>";
124
125 log.debug("link=" + reply);
126
127 return reply;
128 }
129
130 private boolean datesort;
131 private String webprefix;
132 private String localprefix;
133 private String setname;
134
135 private static final String TEST_PREFIX = "jsword-";
136 private static final String TEST_SUFFIX = BIN_ZIP;
137
138 private static final NumberFormat NF = NumberFormat.getNumberInstance();
139 private static final String VERSION_DATE = "yyyyMMdd";
140 private static final DateFormat DF_DISK = new SimpleDateFormat(VERSION_DATE);
141 private static final DateFormat DF_USER = new SimpleDateFormat("dd MMM yyyy");
142 static {
143 NF.setMaximumFractionDigits(2);
144 }
145
146
149 protected static final Logger log = Logger.getLogger(DownloadSet.class);
150 }
151