1 | import java.util.*; |
2 | import java.io.*; |
3 | import java.util.regex.*; |
4 | import java.net.*; |
5 | |
6 | public class main { |
7 | static String[] standardClasses = {}; |
8 | |
9 | public static void main(String[] args) throws IOException { |
10 | String s = loadMainJava(); |
11 | List<String> defd = findClasses(s); |
12 | for (int i = 0; i < 2; i++) |
13 | for (String x : standardClasses) { |
14 | String[] f = x.split("/"); |
15 | if (hasInvocation(s, f[1]) && !defd.contains(f[1])) { |
16 | System.out.println("Adding class: " + f[1] + " (" + f[0] + ")"); |
17 | s = addClass(s, f[0]); |
18 | defd.add(f[1]); |
19 | } |
20 | } |
21 | saveMainJava(s); |
22 | } |
23 | |
24 | static boolean hasInvocation(String src, String f) { |
25 | return src.indexOf(f) >= 0; // generous matching :) |
26 | } |
27 | |
28 | static List<String> findClasses(String src) { |
29 | System.out.println("Scanning for classes"); |
30 | List<String> classes = new ArrayList<String>(); |
31 | Matcher matcher = Pattern.compile("class\\s+(\\w+)[^\\n]*\\{").matcher(src); |
32 | while (matcher.find()) { |
33 | String c = matcher.group(1); |
34 | classes.add(c); |
35 | //System.out.println("Class found: " + c); |
36 | } |
37 | return classes; |
38 | } |
39 | |
40 | public static String addClass(String s, String cID) throws IOException { |
41 | String def = loadSnippet(cID); |
42 | return s + "\n" + def; |
43 | } |
44 | |
45 | static String loadMainJava() throws IOException { |
46 | return loadTextFile("input/main.java", ""); |
47 | } |
48 | |
49 | static void saveMainJava(String s) throws IOException { |
50 | saveTextFile("output/main.java", s); |
51 | } |
52 | |
53 | static String charsetForTextFiles = "UTF-8"; |
54 | |
55 | public static String loadTextFile(String fileName, String defaultContents) throws IOException { |
56 | if (!new File(fileName).exists()) |
57 | return defaultContents; |
58 | |
59 | FileInputStream fileInputStream = new FileInputStream(fileName); |
60 | InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, charsetForTextFiles); |
61 | return loadTextFile(inputStreamReader); |
62 | } |
63 | |
64 | public static String loadTextFile(Reader reader) throws IOException { |
65 | StringBuilder builder = new StringBuilder(); |
66 | try { |
67 | BufferedReader bufferedReader = new BufferedReader(reader); |
68 | String line; |
69 | while ((line = bufferedReader.readLine()) != null) |
70 | builder.append(line).append('\n'); |
71 | } finally { |
72 | reader.close(); |
73 | } |
74 | return builder.length() == 0 ? "" : builder.substring(0, builder.length()-1); |
75 | } |
76 | |
77 | /** writes safely (to temp file, then rename) */ |
78 | public static void saveTextFile(String fileName, String contents) throws IOException { |
79 | File file = new File(fileName); |
80 | File parentFile = file.getParentFile(); |
81 | if (parentFile != null) |
82 | parentFile.mkdirs(); |
83 | String tempFileName = fileName + "_temp"; |
84 | FileOutputStream fileOutputStream = new FileOutputStream(tempFileName); |
85 | OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, charsetForTextFiles); |
86 | PrintWriter printWriter = new PrintWriter(outputStreamWriter); |
87 | printWriter.print(contents); |
88 | printWriter.close(); |
89 | if (file.exists() && !file.delete()) |
90 | throw new IOException("Can't delete " + fileName); |
91 | |
92 | if (!new File(tempFileName).renameTo(file)) |
93 | throw new IOException("Can't rename " + tempFileName + " to " + fileName); |
94 | } |
95 | |
96 | public static String loadSnippet(String snippetID) throws IOException { |
97 | return loadSnippet(snippetID, preferCached); |
98 | } |
99 | |
100 | public static String loadSnippet(String snippetID, boolean preferCached) throws IOException { |
101 | return loadSnippet(parseSnippetID(snippetID), preferCached); |
102 | } |
103 | |
104 | public static long parseSnippetID(String snippetID) { |
105 | return Long.parseLong(shortenSnippetID(snippetID)); |
106 | } |
107 | |
108 | private static String shortenSnippetID(String snippetID) { |
109 | if (snippetID.startsWith("#")) |
110 | snippetID = snippetID.substring(1); |
111 | String httpBlaBla = "http://tinybrain.de/"; |
112 | if (snippetID.startsWith(httpBlaBla)) |
113 | snippetID = snippetID.substring(httpBlaBla.length()); |
114 | return snippetID; |
115 | } |
116 | |
117 | public static boolean isSnippetID(String snippetID) { |
118 | snippetID = shortenSnippetID(snippetID); |
119 | return isInteger(snippetID) && Long.parseLong(snippetID) != 0; |
120 | } |
121 | |
122 | public static boolean isInteger(String s) { |
123 | return Pattern.matches("\\-?\\d+", s); |
124 | } |
125 | |
126 | static boolean preferCached = false; |
127 | |
128 | public static String loadSnippet(long snippetID) throws IOException { |
129 | return loadSnippet(snippetID, preferCached); |
130 | } |
131 | |
132 | public static String loadSnippet(long snippetID, boolean preferCached) throws IOException { |
133 | if (preferCached) { |
134 | initSnippetCache(); |
135 | String text = DiskSnippetCache_get(snippetID); |
136 | if (text != null) |
137 | return text; |
138 | } |
139 | |
140 | String text; |
141 | try { |
142 | URL url = new URL("http://tinybrain.de:8080/getraw.php?id=" + snippetID); |
143 | text = loadPage(url); |
144 | } catch (FileNotFoundException e) { |
145 | throw new IOException("Snippet #" + snippetID + " not found or not public"); |
146 | } |
147 | |
148 | try { |
149 | initSnippetCache(); |
150 | DiskSnippetCache_put(snippetID, text); |
151 | } catch (IOException e) { |
152 | System.err.println("Minor warning: Couldn't save snippet to cache (" + DiskSnippetCache_getDir() + ")"); |
153 | } |
154 | |
155 | return text; |
156 | } |
157 | |
158 | private static String loadPage(URL url) throws IOException { |
159 | System.out.println("Loading: " + url.toExternalForm()); |
160 | URLConnection con = url.openConnection(); |
161 | return loadPage(con, url); |
162 | } |
163 | |
164 | public static String loadPage(URLConnection con, URL url) throws IOException { |
165 | String contentType = con.getContentType(); |
166 | if (contentType == null) |
167 | throw new IOException("Page could not be read: " + url); |
168 | //Log.info("Content-Type: " + contentType); |
169 | String charset = guessCharset(contentType); |
170 | Reader r = new InputStreamReader(con.getInputStream(), charset); |
171 | StringBuilder buf = new StringBuilder(); |
172 | while (true) { |
173 | int ch = r.read(); |
174 | if (ch < 0) |
175 | break; |
176 | //Log.info("Chars read: " + buf.length()); |
177 | buf.append((char) ch); |
178 | } |
179 | return buf.toString(); |
180 | } |
181 | |
182 | public static String guessCharset(String contentType) { |
183 | Pattern p = Pattern.compile("text/html;\\s+charset=([^\\s]+)\\s*"); |
184 | Matcher m = p.matcher(contentType); |
185 | /* If Content-Type doesn't match this pre-conception, choose default and hope for the best. */ |
186 | return m.matches() ? m.group(1) : "ISO-8859-1"; |
187 | } |
188 | |
189 | static File DiskSnippetCache_dir; |
190 | |
191 | public static void initDiskSnippetCache(File dir) { |
192 | DiskSnippetCache_dir = dir; |
193 | dir.mkdirs(); |
194 | } |
195 | |
196 | public static synchronized String DiskSnippetCache_get(long snippetID) throws IOException { |
197 | return loadTextFile(DiskSnippetCache_getFile(snippetID).getPath(), null); |
198 | } |
199 | |
200 | private static File DiskSnippetCache_getFile(long snippetID) { |
201 | return new File(DiskSnippetCache_dir, "" + snippetID); |
202 | } |
203 | |
204 | public static synchronized void DiskSnippetCache_put(long snippetID, String snippet) throws IOException { |
205 | saveTextFile(DiskSnippetCache_getFile(snippetID).getPath(), snippet); |
206 | } |
207 | |
208 | public static File DiskSnippetCache_getDir() { |
209 | return DiskSnippetCache_dir; |
210 | } |
211 | |
212 | public static void initSnippetCache() { |
213 | if (DiskSnippetCache_dir == null) |
214 | initDiskSnippetCache(new File(System.getProperty("user.home"), ".tinybrain/snippet-cache")); |
215 | } |
216 | } |
Began life as a copy of #2000472
Snippet is not live.
Travelled to 12 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
2 comment(s) hidden. show
Snippet ID: | #2000509 |
Snippet name: | Standard Classes Adder Template, silenced |
Eternal ID of this version: | #2000509/1 |
Text MD5: | 0d34b2f243def7b91f1f15b8a3e04724 |
Author: | stefan |
Category: | javax |
Type: | New Tinybrain snippet |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2015-08-09 22:43:40 |
Source code size: | 7466 bytes / 216 lines |
Pitched / IR pitched: | No / Yes |
Views / Downloads: | 902 / 453 |
Referenced in: | [show references] |