1 | // See #593 for an example! |
2 | // Needs a translator (e.g. #595) |
3 | |
4 | import java.util.*; |
5 | import java.io.*; |
6 | import java.util.regex.*; |
7 | |
8 | public class main { |
9 | static String[] standardClasses = { |
10 | "java.util.*", |
11 | "javax.swing.*", |
12 | "java.io.*" |
13 | }; |
14 | |
15 | public static void main(String[] args) throws IOException { |
16 | String s = loadMainJava(); |
17 | List<String> imports = findImports(s); |
18 | for (String c : standardClasses) |
19 | if (!(imports.contains(c))) |
20 | s = "import " + c + ";\n" + s; |
21 | saveMainJava(s); |
22 | } |
23 | |
24 | static List<String> findImports(String src) { |
25 | List<String> imports = new ArrayList<String>(); |
26 | for (String line : toLines(src)) { |
27 | Matcher matcher = Pattern.compile("^\\s*import\\s+(.*?)\\s*;").matcher(line); |
28 | if (matcher.find()) |
29 | imports.add(matcher.group(1)); |
30 | } |
31 | return imports; |
32 | } |
33 | |
34 | static String loadMainJava() throws IOException { |
35 | return loadTextFile("input/main.java", ""); |
36 | } |
37 | |
38 | static void saveMainJava(String s) throws IOException { |
39 | saveTextFile("output/main.java", s); |
40 | } |
41 | |
42 | public static List<String> toLines(String s) { |
43 | List<String> lines = new ArrayList<String>(); |
44 | int start = 0; |
45 | while (true) { |
46 | int i = toLines_nextLineBreak(s, start); |
47 | if (i < 0) { |
48 | if (s.length() > start) lines.add(s.substring(start)); |
49 | break; |
50 | } |
51 | |
52 | lines.add(s.substring(start, i)); |
53 | if (s.charAt(i) == '\r' && i+1 < s.length() && s.charAt(i+1) == '\n') |
54 | i += 2; |
55 | else |
56 | ++i; |
57 | |
58 | start = i; |
59 | } |
60 | return lines; |
61 | } |
62 | |
63 | private static int toLines_nextLineBreak(String s, int start) { |
64 | for (int i = start; i < s.length(); i++) { |
65 | char c = s.charAt(i); |
66 | if (c == '\r' || c == '\n') |
67 | return i; |
68 | } |
69 | return -1; |
70 | } |
71 | |
72 | /** writes safely (to temp file, then rename) */ |
73 | public static void saveTextFile(String fileName, String contents) throws IOException { |
74 | File file = new File(fileName); |
75 | File parentFile = file.getParentFile(); |
76 | if (parentFile != null) |
77 | parentFile.mkdirs(); |
78 | String tempFileName = fileName + "_temp"; |
79 | FileOutputStream fileOutputStream = new FileOutputStream(tempFileName); |
80 | OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8"); |
81 | PrintWriter printWriter = new PrintWriter(outputStreamWriter); |
82 | printWriter.print(contents); |
83 | printWriter.close(); |
84 | if (file.exists() && !file.delete()) |
85 | throw new IOException("Can't delete " + fileName); |
86 | |
87 | if (!new File(tempFileName).renameTo(file)) |
88 | throw new IOException("Can't rename " + tempFileName + " to " + fileName); |
89 | } |
90 | |
91 | public static String loadTextFile(String fileName, String defaultContents) throws IOException { |
92 | if (!new File(fileName).exists()) |
93 | return defaultContents; |
94 | |
95 | FileInputStream fileInputStream = new FileInputStream(fileName); |
96 | InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8"); |
97 | return loadTextFile(inputStreamReader); |
98 | } |
99 | |
100 | public static String loadTextFile(Reader reader) throws IOException { |
101 | StringBuilder builder = new StringBuilder(); |
102 | try { |
103 | BufferedReader bufferedReader = new BufferedReader(reader); |
104 | String line; |
105 | while ((line = bufferedReader.readLine()) != null) |
106 | builder.append(line).append('\n'); |
107 | } finally { |
108 | reader.close(); |
109 | } |
110 | return builder.length() == 0 ? "" : builder.substring(0, builder.length()-1); |
111 | } |
112 | } |
download show line numbers debug dex old transpilations
Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1000263 |
Snippet name: | 596 with 595 (snapshot) |
Eternal ID of this version: | #1000263/1 |
Text MD5: | 15c9d686a707952b1bd5a1559728c680 |
Author: | stefan |
Category: | javax |
Type: | JavaX source code |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2015-04-29 01:49:05 |
Source code size: | 3547 bytes / 112 lines |
Pitched / IR pitched: | No / Yes |
Views / Downloads: | 686 / 553 |
Referenced in: | [show references] |