import java.io.*; import java.util.regex.Pattern; public class SimpleTagEditor { private final String tag1 = "", tag2 = ""; private final Pattern p; public SimpleTagEditor() { // "simple" code: may contain letters, digits, spaces, // or arithmetical operation signs p = Pattern.compile("[ a-zA-Z0-9_.,:;!()\\[\\]\\*\\+\\-/\\^|\"\'=]+"); } private boolean isSimpleCode(String code) { return p.matcher(code).matches(); } private void editFile(String path) { System.out.println("processing " + path + "..."); BufferedReader br = null; BufferedWriter bw = null; String pathTmp = path + ".replace"; try { br = new BufferedReader(new FileReader(path)); bw = new BufferedWriter(new FileWriter(pathTmp)); String s; while((s = br.readLine()) != null) { String tmp = s.trim(); if (tmp.startsWith("/*") || tmp.startsWith("*")) { // is comment s = replaceSimpleCode(s); } bw.write(s + "\n"); } } catch (Exception e) { return; } finally { try { if (br != null) { br.close(); } } catch (IOException ioe) {} try { if (bw != null) { bw.close(); } } catch (IOException ioe) {} } File fOld = new File(path); fOld.delete(); (new File(pathTmp)).renameTo(fOld); } private void run(String path) { File[] files = new File(path).listFiles(); if (files == null) { return; } for (File f : files) { if (f.isDirectory()) { run(f.getAbsolutePath()); } else { if (f.getName().endsWith(".java")) { editFile(f.getAbsolutePath()); } } } } private String rightTrim(String s) { int i = s.length() - 1; while (i >= 0 && Character.isWhitespace(s.charAt(i))) { --i; } return s.substring(0, i + 1); } private String replaceSimpleCode(String ln) { String s = ln.replaceAll("(?i)", "").replaceAll("(?i)", ""); int from = 0; while (true) { int i1 = s.indexOf(tag1, from), i2; if (i1 > -1) { from = i1 + tag1.length(); i2 = s.indexOf(tag2, from); if (i2 > -1) { from = i2 + tag2.length(); } else { break; } } else { break; } String code = s.substring(i1 + tag1.length(), i2); if (isSimpleCode(code)) { String sNew = "{@code " + code.trim() + "}"; String sOld = "" + code + ""; s = s.replace(sOld, sNew); System.out.println(sOld + " -> " + sNew); from = i1 + sNew.length(); } } return rightTrim(s); // making jcheck happy } public static void main(String[] args) { if (args.length != 1) { System.out.println("usage: java SimpleTagEditor "); } else { (new SimpleTagEditor()).run(args[0]); } } }