1 | import java.net.*; |
2 | import java.io.*; |
3 | import javax.swing.*; |
4 | import java.util.regex.*; |
5 | import java.util.*; |
6 | public class main { |
7 | public static void main(String[] args) throws Exception { |
8 | String prepost = loadSnippet("#1000288"); |
9 | saveMainJava(prepost.replace("[...]", loadMainJava()); |
10 | } |
11 | |
12 | static String loadMainJava() throws IOException { |
13 | return loadTextFile("input/main.java", ""); |
14 | } |
15 | |
16 | static void saveMainJava(String s) throws IOException { |
17 | saveTextFile("output/main.java", s); |
18 | } |
19 | |
20 | static boolean preferCached = false; |
21 | |
22 | public static String loadSnippet(String snippetID) throws IOException { |
23 | return loadSnippet(parseSnippetID(snippetID), preferCached); |
24 | } |
25 | |
26 | public static String loadSnippet(String snippetID, boolean preferCached) throws IOException { |
27 | return loadSnippet(parseSnippetID(snippetID), preferCached); |
28 | } |
29 | |
30 | public static long parseSnippetID(String snippetID) { |
31 | return Long.parseLong(shortenSnippetID(snippetID)); |
32 | } |
33 | |
34 | private static String shortenSnippetID(String snippetID) { |
35 | if (snippetID.startsWith("#")) |
36 | snippetID = snippetID.substring(1); |
37 | String httpBlaBla = "http://tinybrain.de/"; |
38 | if (snippetID.startsWith(httpBlaBla)) |
39 | snippetID = snippetID.substring(httpBlaBla.length()); |
40 | return snippetID; |
41 | } |
42 | |
43 | public static boolean isSnippetID(String snippetID) { |
44 | snippetID = shortenSnippetID(snippetID); |
45 | return isInteger(snippetID) && Long.parseLong(snippetID) != 0; |
46 | } |
47 | |
48 | public static boolean isInteger(String s) { |
49 | return Pattern.matches("\\-?\\d+", s); |
50 | } |
51 | |
52 | public static String loadSnippet(long snippetID, boolean preferCached) throws IOException { |
53 | if (preferCached) { |
54 | initSnippetCache(); |
55 | String text = DiskSnippetCache_get(snippetID); |
56 | if (text != null) |
57 | return text; |
58 | } |
59 | |
60 | String text; |
61 | try { |
62 | URL url = new URL("http://tinybrain.de:8080/getraw.php?id=" + snippetID); |
63 | text = loadPage(url); |
64 | } catch (FileNotFoundException e) { |
65 | throw new IOException("Snippet #" + snippetID + " not found or not public"); |
66 | } |
67 | |
68 | try { |
69 | initSnippetCache(); |
70 | DiskSnippetCache_put(snippetID, text); |
71 | } catch (IOException e) { |
72 | System.err.println("Minor warning: Couldn't save snippet to cache (" + DiskSnippetCache_getDir() + ")"); |
73 | } |
74 | |
75 | return text; |
76 | } |
77 | |
78 | private static String loadPage(URL url) throws IOException { |
79 | System.out.println("Loading: " + url.toExternalForm()); |
80 | URLConnection con = url.openConnection(); |
81 | return loadPage(con, url); |
82 | } |
83 | |
84 | public static String loadPage(URLConnection con, URL url) throws IOException { |
85 | String contentType = con.getContentType(); |
86 | if (contentType == null) |
87 | throw new IOException("Page could not be read: " + url); |
88 | //Log.info("Content-Type: " + contentType); |
89 | String charset = guessCharset(contentType); |
90 | Reader r = new InputStreamReader(con.getInputStream(), charset); |
91 | StringBuilder buf = new StringBuilder(); |
92 | while (true) { |
93 | int ch = r.read(); |
94 | if (ch < 0) |
95 | break; |
96 | //Log.info("Chars read: " + buf.length()); |
97 | buf.append((char) ch); |
98 | } |
99 | return buf.toString(); |
100 | } |
101 | |
102 | public static String guessCharset(String contentType) { |
103 | Pattern p = Pattern.compile("text/html;\\s+charset=([^\\s]+)\\s*"); |
104 | Matcher m = p.matcher(contentType); |
105 | /* If Content-Type doesn't match this pre-conception, choose default and hope for the best. */ |
106 | return m.matches() ? m.group(1) : "ISO-8859-1"; |
107 | } |
108 | |
109 | static File DiskSnippetCache_dir; |
110 | |
111 | public static void initDiskSnippetCache(File dir) { |
112 | DiskSnippetCache_dir = dir; |
113 | dir.mkdirs(); |
114 | } |
115 | |
116 | public static synchronized String DiskSnippetCache_get(long snippetID) throws IOException { |
117 | return loadTextFile(DiskSnippetCache_getFile(snippetID).getPath(), null); |
118 | } |
119 | |
120 | private static File DiskSnippetCache_getFile(long snippetID) { |
121 | return new File(DiskSnippetCache_dir, "" + snippetID); |
122 | } |
123 | |
124 | public static synchronized void DiskSnippetCache_put(long snippetID, String snippet) throws IOException { |
125 | saveTextFile(DiskSnippetCache_getFile(snippetID).getPath(), snippet); |
126 | } |
127 | |
128 | public static File DiskSnippetCache_getDir() { |
129 | return DiskSnippetCache_dir; |
130 | } |
131 | |
132 | public static void initSnippetCache() { |
133 | if (DiskSnippetCache_dir == null) |
134 | initDiskSnippetCache(new File(System.getProperty("user.home"), ".tinybrain/snippet-cache")); |
135 | } |
136 | |
137 | |
138 | /** writes safely (to temp file, then rename) */ |
139 | public static void saveTextFile(String fileName, String contents) throws IOException { |
140 | File file = new File(fileName); |
141 | File parentFile = file.getParentFile(); |
142 | if (parentFile != null) |
143 | parentFile.mkdirs(); |
144 | String tempFileName = fileName + "_temp"; |
145 | FileOutputStream fileOutputStream = new FileOutputStream(tempFileName); |
146 | OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8"); |
147 | PrintWriter printWriter = new PrintWriter(outputStreamWriter); |
148 | printWriter.print(contents); |
149 | printWriter.close(); |
150 | if (file.exists() && !file.delete()) |
151 | throw new IOException("Can't delete " + fileName); |
152 | |
153 | if (!new File(tempFileName).renameTo(file)) |
154 | throw new IOException("Can't rename " + tempFileName + " to " + fileName); |
155 | } |
156 | |
157 | public static String loadTextFile(String fileName, String defaultContents) throws IOException { |
158 | if (!new File(fileName).exists()) |
159 | return defaultContents; |
160 | |
161 | FileInputStream fileInputStream = new FileInputStream(fileName); |
162 | InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8"); |
163 | return loadTextFile(inputStreamReader); |
164 | } |
165 | |
166 | public static String loadTextFile(Reader reader) throws IOException { |
167 | StringBuilder builder = new StringBuilder(); |
168 | try { |
169 | BufferedReader bufferedReader = new BufferedReader(reader); |
170 | String line; |
171 | while ((line = bufferedReader.readLine()) != null) |
172 | builder.append(line).append('\n'); |
173 | } finally { |
174 | reader.close(); |
175 | } |
176 | return builder.length() == 0 ? "" : builder.substring(0, builder.length()-1); |
177 | } |
178 | } |