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

112
LINES

< > BotCompany Repo | #1000263 // 596 with 595 (snapshot)

JavaX source code - run with: x30.jar

// See #593 for an example!
// Needs a translator (e.g. #595)

import java.util.*;
import java.io.*;
import java.util.regex.*;

public class main {
  static String[] standardClasses = {
    "java.util.*",
    "javax.swing.*",
    "java.io.*"
  };
  
  public static void main(String[] args) throws IOException {
    String s = loadMainJava();
    List<String> imports = findImports(s);
    for (String c : standardClasses)
      if (!(imports.contains(c)))
        s = "import " + c + ";\n" + s;
    saveMainJava(s);
  }
  
  static List<String> findImports(String src) {
    List<String> imports = new ArrayList<String>();
    for (String line : toLines(src)) {
      Matcher matcher = Pattern.compile("^\\s*import\\s+(.*?)\\s*;").matcher(line);
      if (matcher.find())
        imports.add(matcher.group(1));
    }
    return imports;
  }

  static String loadMainJava() throws IOException {
    return loadTextFile("input/main.java", "");
  }

  static void saveMainJava(String s) throws IOException {
    saveTextFile("output/main.java", s);
  }

 public static List<String> toLines(String s) {
    List<String> lines = new ArrayList<String>();
    int start = 0;
    while (true) {
      int i = toLines_nextLineBreak(s, start);
      if (i < 0) {
        if (s.length() > start) lines.add(s.substring(start));
        break;
      }

      lines.add(s.substring(start, i));
      if (s.charAt(i) == '\r' && i+1 < s.length() && s.charAt(i+1) == '\n')
        i += 2;
      else
        ++i;

      start = i;
    }
    return lines;
  }

  private static int toLines_nextLineBreak(String s, int start) {
    for (int i = start; i < s.length(); i++) {
      char c = s.charAt(i);
      if (c == '\r' || c == '\n')
        return i;
    }
    return -1;
  }

  /** writes safely (to temp file, then rename) */
  public static void saveTextFile(String fileName, String contents) throws IOException {
    File file = new File(fileName);
    File parentFile = file.getParentFile();
    if (parentFile != null)
      parentFile.mkdirs();
    String tempFileName = fileName + "_temp";
    FileOutputStream fileOutputStream = new FileOutputStream(tempFileName);
    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8");
    PrintWriter printWriter = new PrintWriter(outputStreamWriter);
    printWriter.print(contents);
    printWriter.close();
    if (file.exists() && !file.delete())
      throw new IOException("Can't delete " + fileName);

    if (!new File(tempFileName).renameTo(file))
      throw new IOException("Can't rename " + tempFileName + " to " + fileName);
  }

  public static String loadTextFile(String fileName, String defaultContents) throws IOException {
    if (!new File(fileName).exists())
      return defaultContents;

    FileInputStream fileInputStream = new FileInputStream(fileName);
    InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
    return loadTextFile(inputStreamReader);
  }

  public static String loadTextFile(Reader reader) throws IOException {
    StringBuilder builder = new StringBuilder();
    try {
      BufferedReader bufferedReader = new BufferedReader(reader);
      String line;
      while ((line = bufferedReader.readLine()) != null)
        builder.append(line).append('\n');
    } finally {
      reader.close();
    }
    return builder.length() == 0 ? "" : builder.substring(0, builder.length()-1);
  }
}

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: 677 / 543
Referenced in: [show references]