Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

117
LINES

< > BotCompany Repo | #592 // Stefan's Auto-Importer

JavaX translator [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (117L/2K/5K).

1  
// See #593 for an example!
2  
3  
import java.util.*;
4  
import java.io.*;
5  
import java.util.regex.*;
6  
import java.net.*;
7  
8  
public class main {
9  
  // My standard packages :)
10  
  static String[] standardClasses = {
11  
    "java.util.*",
12  
    "java.util.regex.*",
13  
    "javax.swing.*",
14  
    "java.io.*",
15  
    "java.net.*"
16  
  };
17  
  
18  
  public static void main(String[] args) throws IOException {
19  
    String s = loadMainJava();
20  
    List<String> imports = findImports(s);
21  
    for (String c : standardClasses)
22  
      if (!(imports.contains(c)))
23  
        s = "import " + c + ";\n" + s;
24  
    saveMainJava(s);
25  
  }
26  
  
27  
  static List<String> findImports(String src) {
28  
    List<String> imports = new ArrayList<String>();
29  
    for (String line : toLines(src)) {
30  
      Matcher matcher = Pattern.compile("^\\s*import\\s+(.*?)\\s*;").matcher(line);
31  
      if (matcher.find())
32  
        imports.add(matcher.group(1));
33  
    }
34  
    return imports;
35  
  }
36  
  
37  
 public static List<String> toLines(String s) {
38  
    List<String> lines = new ArrayList<String>();
39  
    int start = 0;
40  
    while (true) {
41  
      int i = toLines_nextLineBreak(s, start);
42  
      if (i < 0) {
43  
        if (s.length() > start) lines.add(s.substring(start));
44  
        break;
45  
      }
46  
47  
      lines.add(s.substring(start, i));
48  
      if (s.charAt(i) == '\r' && i+1 < s.length() && s.charAt(i+1) == '\n')
49  
        i += 2;
50  
      else
51  
        ++i;
52  
53  
      start = i;
54  
    }
55  
    return lines;
56  
  }
57  
58  
  private static int toLines_nextLineBreak(String s, int start) {
59  
    for (int i = start; i < s.length(); i++) {
60  
      char c = s.charAt(i);
61  
      if (c == '\r' || c == '\n')
62  
        return i;
63  
    }
64  
    return -1;
65  
  }
66  
  
67  
  static String loadMainJava() throws IOException {
68  
    return loadTextFile("input/main.java", "");
69  
  }
70  
  
71  
  static void saveMainJava(String s) throws IOException {
72  
    saveTextFile("output/main.java", s);
73  
  }
74  
  
75  
  static String charsetForTextFiles = "UTF-8";
76  
  
77  
  public static String loadTextFile(String fileName, String defaultContents) throws IOException {
78  
    if (!new File(fileName).exists())
79  
      return defaultContents;
80  
81  
    FileInputStream fileInputStream = new FileInputStream(fileName);
82  
    InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, charsetForTextFiles);
83  
    return loadTextFile(inputStreamReader);
84  
  }
85  
86  
  public static String loadTextFile(Reader reader) throws IOException {
87  
    StringBuilder builder = new StringBuilder();
88  
    try {
89  
      BufferedReader bufferedReader = new BufferedReader(reader);
90  
      String line;
91  
      while ((line = bufferedReader.readLine()) != null)
92  
        builder.append(line).append('\n');
93  
    } finally {
94  
      reader.close();
95  
    }
96  
    return builder.length() == 0 ? "" : builder.substring(0, builder.length()-1);
97  
  }
98  
  
99  
  /** writes safely (to temp file, then rename) */
100  
  public static void saveTextFile(String fileName, String contents) throws IOException {
101  
    File file = new File(fileName);
102  
    File parentFile = file.getParentFile();
103  
    if (parentFile != null)
104  
      parentFile.mkdirs();
105  
    String tempFileName = fileName + "_temp";
106  
    FileOutputStream fileOutputStream = new FileOutputStream(tempFileName);
107  
    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, charsetForTextFiles);
108  
    PrintWriter printWriter = new PrintWriter(outputStreamWriter);
109  
    printWriter.print(contents);
110  
    printWriter.close();
111  
    if (file.exists() && !file.delete())
112  
      throw new IOException("Can't delete " + fileName);
113  
114  
    if (!new File(tempFileName).renameTo(file))
115  
      throw new IOException("Can't rename " + tempFileName + " to " + fileName);
116  
  }  
117  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 23 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gkwdfizivqfm, gwrvuhgaqvyk, hxnwyiuffukg, ishqpsrjomds, jtubtzbbkimh, kajysqoxcvfe, kmhbujppghqa, lpdgvwnxivlt, mqqgnosmbjvj, nrtiiiyxqhmw, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, qbtsjoyahagl, teubizvjbppd, tslmcundralx, tvejysmllsmz, ugbnzuvxksoj, vouqrxazstgt

Comments [hide]

ID Author/Program Comment Date
750 #1000610 (pitcher) 2015-08-18 00:07:07
749 #1000604 (pitcher) 2015-08-18 00:07:22

add comment

Snippet ID: #592
Snippet name: Stefan's Auto-Importer
Eternal ID of this version: #592/1
Text MD5: d075b1c332cc0a39077e304abac30859
Transpilation MD5: d075b1c332cc0a39077e304abac30859
Author: stefan
Category: javax
Type: JavaX translator
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-06-20 21:24:58
Source code size: 3695 bytes / 117 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 1270 / 6900
Referenced in: [show references]