Libraryless. Click here for Pure Java version (121L/2K/5K).
1 | import javax.imageio.*; |
2 | import java.awt.image.*; |
3 | import java.awt.*; |
4 | import java.security.NoSuchAlgorithmException; |
5 | import java.security.MessageDigest; |
6 | import java.lang.reflect.*; |
7 | import java.net.*; |
8 | import java.io.*; |
9 | import javax.swing.*; |
10 | import java.util.regex.*; |
11 | import java.util.List; |
12 | import java.util.zip.*; |
13 | import java.util.*; |
14 | |
15 | public class main { |
16 | public static void main(String[] args) throws Exception { |
17 | saveMainJava(moveImportsUp(loadMainJava())); |
18 | } |
19 | |
20 | static String loadMainJava() throws IOException { |
21 | return loadTextFile("input/main.java", ""); |
22 | } |
23 | |
24 | static void saveMainJava(String s) throws IOException { |
25 | saveTextFile("output/main.java", s); |
26 | } |
27 | |
28 | static String moveImportsUp(String s) { |
29 | List<String> l = toLines(s); |
30 | List<String> x = new ArrayList<String>(); |
31 | Pattern p = Pattern.compile("^\\s*import\\s"); |
32 | for (ListIterator<String> i = l.listIterator(); i.hasNext(); ) { |
33 | String line = i.next(); |
34 | if (p.matcher(line).find()) { |
35 | x.add(line); |
36 | i.remove(); |
37 | } |
38 | } |
39 | x.addAll(l); |
40 | return fromLines(x); |
41 | } |
42 | |
43 | /** writes safely (to temp file, then rename) */ |
44 | public static void saveTextFile(String fileName, String contents) throws IOException { |
45 | File file = new File(fileName); |
46 | File parentFile = file.getParentFile(); |
47 | if (parentFile != null) |
48 | parentFile.mkdirs(); |
49 | String tempFileName = fileName + "_temp"; |
50 | FileOutputStream fileOutputStream = new FileOutputStream(tempFileName); |
51 | OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8"); |
52 | PrintWriter printWriter = new PrintWriter(outputStreamWriter); |
53 | printWriter.print(contents); |
54 | printWriter.close(); |
55 | if (file.exists() && !file.delete()) |
56 | throw new IOException("Can't delete " + fileName); |
57 | |
58 | if (!new File(tempFileName).renameTo(file)) |
59 | throw new IOException("Can't rename " + tempFileName + " to " + fileName); |
60 | } |
61 | |
62 | public static String loadTextFile(String fileName, String defaultContents) throws IOException { |
63 | if (!new File(fileName).exists()) |
64 | return defaultContents; |
65 | |
66 | FileInputStream fileInputStream = new FileInputStream(fileName); |
67 | InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8"); |
68 | return loadTextFile(inputStreamReader); |
69 | } |
70 | |
71 | public static String loadTextFile(Reader reader) throws IOException { |
72 | StringBuilder builder = new StringBuilder(); |
73 | try { |
74 | BufferedReader bufferedReader = new BufferedReader(reader); |
75 | String line; |
76 | while ((line = bufferedReader.readLine()) != null) |
77 | builder.append(line).append('\n'); |
78 | } finally { |
79 | reader.close(); |
80 | } |
81 | return builder.length() == 0 ? "" : builder.substring(0, builder.length()-1); |
82 | } |
83 | |
84 | public static List<String> toLines(String s) { |
85 | List<String> lines = new ArrayList<String>(); |
86 | int start = 0; |
87 | while (true) { |
88 | int i = toLines_nextLineBreak(s, start); |
89 | if (i < 0) { |
90 | if (s.length() > start) lines.add(s.substring(start)); |
91 | break; |
92 | } |
93 | |
94 | lines.add(s.substring(start, i)); |
95 | if (s.charAt(i) == '\r' && i+1 < s.length() && s.charAt(i+1) == '\n') |
96 | i += 2; |
97 | else |
98 | ++i; |
99 | |
100 | start = i; |
101 | } |
102 | return lines; |
103 | } |
104 | |
105 | private static int toLines_nextLineBreak(String s, int start) { |
106 | for (int i = start; i < s.length(); i++) { |
107 | char c = s.charAt(i); |
108 | if (c == '\r' || c == '\n') |
109 | return i; |
110 | } |
111 | return -1; |
112 | } |
113 | |
114 | public static String fromLines(List<String> lines) { |
115 | StringBuilder buf = new StringBuilder(); |
116 | for (String line : lines) { |
117 | buf.append(line).append('\n'); |
118 | } |
119 | return buf.toString(); |
120 | } |
121 | } |
download show line numbers debug dex old transpilations
Travelled to 24 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, dhtvkmknsjym, gwrvuhgaqvyk, hxnwyiuffukg, ishqpsrjomds, jlatgrcjtklg, jtubtzbbkimh, kmhbujppghqa, liwcxgsjrgqn, lpdgvwnxivlt, mqqgnosmbjvj, omzkqqnokwlv, onfqjnomoxuw, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, teubizvjbppd, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1000691 |
Snippet name: | moveImportsUp (translator) |
Eternal ID of this version: | #1000691/1 |
Text MD5: | 6d71435eb8363c610072404d756bb29a |
Transpilation MD5: | 6d71435eb8363c610072404d756bb29a |
Author: | stefan |
Category: | |
Type: | JavaX translator |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2015-08-22 19:10:50 |
Source code size: | 3767 bytes / 121 lines |
Pitched / IR pitched: | No / Yes |
Views / Downloads: | 712 / 16759 |
Referenced in: | [show references] |