@Test public void trimAll_A$String() throws Exception { TrimInsideOfBraceFilter target = new TrimInsideOfBraceFilter(); String src = "public class Hoge { { } static { System.out.println(\"aaa\"); } public static void main(String[] args) { String actual = target.trimAll(src); String expected = "public class Hoge { {} static {\"\"} public static void main(String[] args) {\"\"} }"; assertEquals(expected, actual); }
@Override public String trimAll(String src) { if (src == null) { return null; } src = src.replaceAll(RegExp.CRLF, StringValue.Space).replaceAll(RegExp.LF, StringValue.Space); int len = src.length(); boolean isInsideOfString = false; boolean isInsideOfChar = false; boolean isInsideOfTargetClass = false; boolean isInsideOfFirstBrace = false; boolean isInsideOfSecondBrace = false; Stack<Character> braceStack = new Stack<Character>(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < len; i++) { char current = src.charAt(i); if (i > 0) { if (current == '"') { int count = countPreviousContinuedBackslash(src, i, 0); if (count % 2 == 0) { isInsideOfString = !isInsideOfString; sb.append(current); continue; } } if (!isInsideOfString && current == '\'') { int count = countPreviousContinuedBackslash(src, i, 0); if (count % 2 == 0) { isInsideOfChar = !isInsideOfChar; sb.append(current); continue; } } } if (isInsideOfChar || isInsideOfString) { continue; } if (!isInsideOfTargetClass) { sb.append(current); if (i >= 6 && (src.charAt(i - 6) == ' ' || src.charAt(i - 6) == ';') && src.charAt(i - 5) == 'c' && src.charAt(i - 4) == 'l' && src.charAt(i - 3) == 'a' && src.charAt(i - 2) == 's' && src.charAt(i - 1) == 's' && current == ' ') { isInsideOfTargetClass = true; } else if (i >= 5 && (src.charAt(i - 5) == ' ' || src.charAt(i - 5) == ';') && src.charAt(i - 4) == 'e' && src.charAt(i - 3) == 'n' && src.charAt(i - 2) == 'u' && src.charAt(i - 1) == 'm' && current == ' ') { isInsideOfTargetClass = true; } continue; } if (!isInsideOfFirstBrace) { sb.append(current); if (current == '{') { isInsideOfFirstBrace = true; } continue; } if (!isInsideOfSecondBrace) { sb.append(current); } if (current == '{') { isInsideOfSecondBrace = true; braceStack.push(current); } if (!braceStack.empty() && current == '}') { braceStack.pop(); if (braceStack.empty()) { sb.append(current); } } if (braceStack.empty()) { isInsideOfSecondBrace = false; } } return sb.toString(); }
TrimInsideOfBraceFilter implements TrimFilter { @Override public String trimAll(String src) { if (src == null) { return null; } src = src.replaceAll(RegExp.CRLF, StringValue.Space).replaceAll(RegExp.LF, StringValue.Space); int len = src.length(); boolean isInsideOfString = false; boolean isInsideOfChar = false; boolean isInsideOfTargetClass = false; boolean isInsideOfFirstBrace = false; boolean isInsideOfSecondBrace = false; Stack<Character> braceStack = new Stack<Character>(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < len; i++) { char current = src.charAt(i); if (i > 0) { if (current == '"') { int count = countPreviousContinuedBackslash(src, i, 0); if (count % 2 == 0) { isInsideOfString = !isInsideOfString; sb.append(current); continue; } } if (!isInsideOfString && current == '\'') { int count = countPreviousContinuedBackslash(src, i, 0); if (count % 2 == 0) { isInsideOfChar = !isInsideOfChar; sb.append(current); continue; } } } if (isInsideOfChar || isInsideOfString) { continue; } if (!isInsideOfTargetClass) { sb.append(current); if (i >= 6 && (src.charAt(i - 6) == ' ' || src.charAt(i - 6) == ';') && src.charAt(i - 5) == 'c' && src.charAt(i - 4) == 'l' && src.charAt(i - 3) == 'a' && src.charAt(i - 2) == 's' && src.charAt(i - 1) == 's' && current == ' ') { isInsideOfTargetClass = true; } else if (i >= 5 && (src.charAt(i - 5) == ' ' || src.charAt(i - 5) == ';') && src.charAt(i - 4) == 'e' && src.charAt(i - 3) == 'n' && src.charAt(i - 2) == 'u' && src.charAt(i - 1) == 'm' && current == ' ') { isInsideOfTargetClass = true; } continue; } if (!isInsideOfFirstBrace) { sb.append(current); if (current == '{') { isInsideOfFirstBrace = true; } continue; } if (!isInsideOfSecondBrace) { sb.append(current); } if (current == '{') { isInsideOfSecondBrace = true; braceStack.push(current); } if (!braceStack.empty() && current == '}') { braceStack.pop(); if (braceStack.empty()) { sb.append(current); } } if (braceStack.empty()) { isInsideOfSecondBrace = false; } } return sb.toString(); } }
TrimInsideOfBraceFilter implements TrimFilter { @Override public String trimAll(String src) { if (src == null) { return null; } src = src.replaceAll(RegExp.CRLF, StringValue.Space).replaceAll(RegExp.LF, StringValue.Space); int len = src.length(); boolean isInsideOfString = false; boolean isInsideOfChar = false; boolean isInsideOfTargetClass = false; boolean isInsideOfFirstBrace = false; boolean isInsideOfSecondBrace = false; Stack<Character> braceStack = new Stack<Character>(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < len; i++) { char current = src.charAt(i); if (i > 0) { if (current == '"') { int count = countPreviousContinuedBackslash(src, i, 0); if (count % 2 == 0) { isInsideOfString = !isInsideOfString; sb.append(current); continue; } } if (!isInsideOfString && current == '\'') { int count = countPreviousContinuedBackslash(src, i, 0); if (count % 2 == 0) { isInsideOfChar = !isInsideOfChar; sb.append(current); continue; } } } if (isInsideOfChar || isInsideOfString) { continue; } if (!isInsideOfTargetClass) { sb.append(current); if (i >= 6 && (src.charAt(i - 6) == ' ' || src.charAt(i - 6) == ';') && src.charAt(i - 5) == 'c' && src.charAt(i - 4) == 'l' && src.charAt(i - 3) == 'a' && src.charAt(i - 2) == 's' && src.charAt(i - 1) == 's' && current == ' ') { isInsideOfTargetClass = true; } else if (i >= 5 && (src.charAt(i - 5) == ' ' || src.charAt(i - 5) == ';') && src.charAt(i - 4) == 'e' && src.charAt(i - 3) == 'n' && src.charAt(i - 2) == 'u' && src.charAt(i - 1) == 'm' && current == ' ') { isInsideOfTargetClass = true; } continue; } if (!isInsideOfFirstBrace) { sb.append(current); if (current == '{') { isInsideOfFirstBrace = true; } continue; } if (!isInsideOfSecondBrace) { sb.append(current); } if (current == '{') { isInsideOfSecondBrace = true; braceStack.push(current); } if (!braceStack.empty() && current == '}') { braceStack.pop(); if (braceStack.empty()) { sb.append(current); } } if (braceStack.empty()) { isInsideOfSecondBrace = false; } } return sb.toString(); }  }
TrimInsideOfBraceFilter implements TrimFilter { @Override public String trimAll(String src) { if (src == null) { return null; } src = src.replaceAll(RegExp.CRLF, StringValue.Space).replaceAll(RegExp.LF, StringValue.Space); int len = src.length(); boolean isInsideOfString = false; boolean isInsideOfChar = false; boolean isInsideOfTargetClass = false; boolean isInsideOfFirstBrace = false; boolean isInsideOfSecondBrace = false; Stack<Character> braceStack = new Stack<Character>(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < len; i++) { char current = src.charAt(i); if (i > 0) { if (current == '"') { int count = countPreviousContinuedBackslash(src, i, 0); if (count % 2 == 0) { isInsideOfString = !isInsideOfString; sb.append(current); continue; } } if (!isInsideOfString && current == '\'') { int count = countPreviousContinuedBackslash(src, i, 0); if (count % 2 == 0) { isInsideOfChar = !isInsideOfChar; sb.append(current); continue; } } } if (isInsideOfChar || isInsideOfString) { continue; } if (!isInsideOfTargetClass) { sb.append(current); if (i >= 6 && (src.charAt(i - 6) == ' ' || src.charAt(i - 6) == ';') && src.charAt(i - 5) == 'c' && src.charAt(i - 4) == 'l' && src.charAt(i - 3) == 'a' && src.charAt(i - 2) == 's' && src.charAt(i - 1) == 's' && current == ' ') { isInsideOfTargetClass = true; } else if (i >= 5 && (src.charAt(i - 5) == ' ' || src.charAt(i - 5) == ';') && src.charAt(i - 4) == 'e' && src.charAt(i - 3) == 'n' && src.charAt(i - 2) == 'u' && src.charAt(i - 1) == 'm' && current == ' ') { isInsideOfTargetClass = true; } continue; } if (!isInsideOfFirstBrace) { sb.append(current); if (current == '{') { isInsideOfFirstBrace = true; } continue; } if (!isInsideOfSecondBrace) { sb.append(current); } if (current == '{') { isInsideOfSecondBrace = true; braceStack.push(current); } if (!braceStack.empty() && current == '}') { braceStack.pop(); if (braceStack.empty()) { sb.append(current); } } if (braceStack.empty()) { isInsideOfSecondBrace = false; } } return sb.toString(); }  @Override String trimAll(String src); }
TrimInsideOfBraceFilter implements TrimFilter { @Override public String trimAll(String src) { if (src == null) { return null; } src = src.replaceAll(RegExp.CRLF, StringValue.Space).replaceAll(RegExp.LF, StringValue.Space); int len = src.length(); boolean isInsideOfString = false; boolean isInsideOfChar = false; boolean isInsideOfTargetClass = false; boolean isInsideOfFirstBrace = false; boolean isInsideOfSecondBrace = false; Stack<Character> braceStack = new Stack<Character>(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < len; i++) { char current = src.charAt(i); if (i > 0) { if (current == '"') { int count = countPreviousContinuedBackslash(src, i, 0); if (count % 2 == 0) { isInsideOfString = !isInsideOfString; sb.append(current); continue; } } if (!isInsideOfString && current == '\'') { int count = countPreviousContinuedBackslash(src, i, 0); if (count % 2 == 0) { isInsideOfChar = !isInsideOfChar; sb.append(current); continue; } } } if (isInsideOfChar || isInsideOfString) { continue; } if (!isInsideOfTargetClass) { sb.append(current); if (i >= 6 && (src.charAt(i - 6) == ' ' || src.charAt(i - 6) == ';') && src.charAt(i - 5) == 'c' && src.charAt(i - 4) == 'l' && src.charAt(i - 3) == 'a' && src.charAt(i - 2) == 's' && src.charAt(i - 1) == 's' && current == ' ') { isInsideOfTargetClass = true; } else if (i >= 5 && (src.charAt(i - 5) == ' ' || src.charAt(i - 5) == ';') && src.charAt(i - 4) == 'e' && src.charAt(i - 3) == 'n' && src.charAt(i - 2) == 'u' && src.charAt(i - 1) == 'm' && current == ' ') { isInsideOfTargetClass = true; } continue; } if (!isInsideOfFirstBrace) { sb.append(current); if (current == '{') { isInsideOfFirstBrace = true; } continue; } if (!isInsideOfSecondBrace) { sb.append(current); } if (current == '{') { isInsideOfSecondBrace = true; braceStack.push(current); } if (!braceStack.empty() && current == '}') { braceStack.pop(); if (braceStack.empty()) { sb.append(current); } } if (braceStack.empty()) { isInsideOfSecondBrace = false; } } return sb.toString(); }  @Override String trimAll(String src);  }