@Test public void parseFails() { String raw = Resources.asString("FileContentUtils/SectionsTestFail.java.txt"); thrown.expect(IllegalArgumentException.class); thrown.expectMessage("unmatching section"); FileContentUtils.parseContent(raw); }
public static Map<String, FileContent.Section> parseContent(String raw) { Map<String, FileContent.Section> sections = Maps.newHashMap(); int currentLineNum = 1; List<String> lines = Lists.newArrayList(); boolean inSection = false; String sectionName = null; int startLineNum = 0; int endLineNum = 0; try (Scanner sc = new Scanner(raw)) { while (sc.hasNextLine()) { String currentLine = sc.nextLine(); if (!inSection) { Matcher matcher = startPattern.matcher(currentLine); if (matcher.find()) { startLineNum = currentLineNum + 1; sectionName = matcher.group(1); inSection = true; lines.clear(); } } else { if (currentLine.contains("{{end:" + sectionName + "}}")) { inSection = false; endLineNum = currentLineNum - 1; if (lines.size() > 0) { Matcher matcher = spacePattern.matcher(lines.get(0)); if (matcher.find()) { int indentSpaces = matcher.group(1).length(); StringBuilder sb = new StringBuilder(); lines.stream().forEach(line -> { line.replaceAll("\t", " "); sb.append(line.substring(Math.min(line.length(), indentSpaces)) + "\n"); }); sections.put(sectionName, new FileContent.Section(startLineNum, endLineNum, sb.toString())); } } } else { lines.add(currentLine); } } currentLineNum++; } } if (inSection == true) { throw new IllegalArgumentException("unmatching section for " + sectionName); } return sections; }
FileContentUtils { public static Map<String, FileContent.Section> parseContent(String raw) { Map<String, FileContent.Section> sections = Maps.newHashMap(); int currentLineNum = 1; List<String> lines = Lists.newArrayList(); boolean inSection = false; String sectionName = null; int startLineNum = 0; int endLineNum = 0; try (Scanner sc = new Scanner(raw)) { while (sc.hasNextLine()) { String currentLine = sc.nextLine(); if (!inSection) { Matcher matcher = startPattern.matcher(currentLine); if (matcher.find()) { startLineNum = currentLineNum + 1; sectionName = matcher.group(1); inSection = true; lines.clear(); } } else { if (currentLine.contains("{{end:" + sectionName + "}}")) { inSection = false; endLineNum = currentLineNum - 1; if (lines.size() > 0) { Matcher matcher = spacePattern.matcher(lines.get(0)); if (matcher.find()) { int indentSpaces = matcher.group(1).length(); StringBuilder sb = new StringBuilder(); lines.stream().forEach(line -> { line.replaceAll("\t", " "); sb.append(line.substring(Math.min(line.length(), indentSpaces)) + "\n"); }); sections.put(sectionName, new FileContent.Section(startLineNum, endLineNum, sb.toString())); } } } else { lines.add(currentLine); } } currentLineNum++; } } if (inSection == true) { throw new IllegalArgumentException("unmatching section for " + sectionName); } return sections; } }
FileContentUtils { public static Map<String, FileContent.Section> parseContent(String raw) { Map<String, FileContent.Section> sections = Maps.newHashMap(); int currentLineNum = 1; List<String> lines = Lists.newArrayList(); boolean inSection = false; String sectionName = null; int startLineNum = 0; int endLineNum = 0; try (Scanner sc = new Scanner(raw)) { while (sc.hasNextLine()) { String currentLine = sc.nextLine(); if (!inSection) { Matcher matcher = startPattern.matcher(currentLine); if (matcher.find()) { startLineNum = currentLineNum + 1; sectionName = matcher.group(1); inSection = true; lines.clear(); } } else { if (currentLine.contains("{{end:" + sectionName + "}}")) { inSection = false; endLineNum = currentLineNum - 1; if (lines.size() > 0) { Matcher matcher = spacePattern.matcher(lines.get(0)); if (matcher.find()) { int indentSpaces = matcher.group(1).length(); StringBuilder sb = new StringBuilder(); lines.stream().forEach(line -> { line.replaceAll("\t", " "); sb.append(line.substring(Math.min(line.length(), indentSpaces)) + "\n"); }); sections.put(sectionName, new FileContent.Section(startLineNum, endLineNum, sb.toString())); } } } else { lines.add(currentLine); } } currentLineNum++; } } if (inSection == true) { throw new IllegalArgumentException("unmatching section for " + sectionName); } return sections; }  }
FileContentUtils { public static Map<String, FileContent.Section> parseContent(String raw) { Map<String, FileContent.Section> sections = Maps.newHashMap(); int currentLineNum = 1; List<String> lines = Lists.newArrayList(); boolean inSection = false; String sectionName = null; int startLineNum = 0; int endLineNum = 0; try (Scanner sc = new Scanner(raw)) { while (sc.hasNextLine()) { String currentLine = sc.nextLine(); if (!inSection) { Matcher matcher = startPattern.matcher(currentLine); if (matcher.find()) { startLineNum = currentLineNum + 1; sectionName = matcher.group(1); inSection = true; lines.clear(); } } else { if (currentLine.contains("{{end:" + sectionName + "}}")) { inSection = false; endLineNum = currentLineNum - 1; if (lines.size() > 0) { Matcher matcher = spacePattern.matcher(lines.get(0)); if (matcher.find()) { int indentSpaces = matcher.group(1).length(); StringBuilder sb = new StringBuilder(); lines.stream().forEach(line -> { line.replaceAll("\t", " "); sb.append(line.substring(Math.min(line.length(), indentSpaces)) + "\n"); }); sections.put(sectionName, new FileContent.Section(startLineNum, endLineNum, sb.toString())); } } } else { lines.add(currentLine); } } currentLineNum++; } } if (inSection == true) { throw new IllegalArgumentException("unmatching section for " + sectionName); } return sections; }  static Map<String, FileContent.Section> parseContent(String raw); }
FileContentUtils { public static Map<String, FileContent.Section> parseContent(String raw) { Map<String, FileContent.Section> sections = Maps.newHashMap(); int currentLineNum = 1; List<String> lines = Lists.newArrayList(); boolean inSection = false; String sectionName = null; int startLineNum = 0; int endLineNum = 0; try (Scanner sc = new Scanner(raw)) { while (sc.hasNextLine()) { String currentLine = sc.nextLine(); if (!inSection) { Matcher matcher = startPattern.matcher(currentLine); if (matcher.find()) { startLineNum = currentLineNum + 1; sectionName = matcher.group(1); inSection = true; lines.clear(); } } else { if (currentLine.contains("{{end:" + sectionName + "}}")) { inSection = false; endLineNum = currentLineNum - 1; if (lines.size() > 0) { Matcher matcher = spacePattern.matcher(lines.get(0)); if (matcher.find()) { int indentSpaces = matcher.group(1).length(); StringBuilder sb = new StringBuilder(); lines.stream().forEach(line -> { line.replaceAll("\t", " "); sb.append(line.substring(Math.min(line.length(), indentSpaces)) + "\n"); }); sections.put(sectionName, new FileContent.Section(startLineNum, endLineNum, sb.toString())); } } } else { lines.add(currentLine); } } currentLineNum++; } } if (inSection == true) { throw new IllegalArgumentException("unmatching section for " + sectionName); } return sections; }  static Map<String, FileContent.Section> parseContent(String raw);  }