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

121
LINES

< > BotCompany Repo | #1000691 // moveImportsUp (translator)

JavaX translator [tags: use-pretranspiled]

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

import javax.imageio.*;
import java.awt.image.*;
import java.awt.*;
import java.security.NoSuchAlgorithmException;
import java.security.MessageDigest;
import java.lang.reflect.*;
import java.net.*;
import java.io.*;
import javax.swing.*;
import java.util.regex.*;
import java.util.List;
import java.util.zip.*;
import java.util.*;

public class main {
  public static void main(String[] args) throws Exception {
    saveMainJava(moveImportsUp(loadMainJava()));
  }

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

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

static String moveImportsUp(String s) {
  List<String> l = toLines(s);
  List<String> x = new ArrayList<String>();
  Pattern p = Pattern.compile("^\\s*import\\s");
  for (ListIterator<String> i = l.listIterator(); i.hasNext(); ) {
    String line = i.next();
    if (p.matcher(line).find()) {
      x.add(line);
      i.remove();
    }
  }
  x.addAll(l);
  return fromLines(x);
}

  /** 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);
  }

 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;
  }

  public static String fromLines(List<String> lines) {
    StringBuilder buf = new StringBuilder();
    for (String line : lines) {
      buf.append(line).append('\n');
    }
    return buf.toString();
  }
}

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: 711 / 16758
Referenced in: #636 - Resolve symbolic translator invocations
#3000382 - Answer for ferdie (>> t = 1, f = 0)
#3000383 - Answer for funkoverflow (>> t=1, f=0 okay)
#3000390 - Translation of #1002298 with #1000691