CheckFigureReferences extends Rule { @Override public List<Advice> evaluate(AnnotatedString s, AnnotatedString original) { List<Advice> out_list = new ArrayList<Advice>(); boolean in_figure = false; Map<String,Position> figure_defs = new HashMap<String,Position>(); List<String> lines = s.getLines(); boolean found_label = false; for (int line_cnt = 0; line_cnt < lines.size(); line_cnt++) { String line = lines.get(line_cnt); if (line.matches(".*\\\\begin\\s*\\{\\s*(figure|wrapfigure).*")) { in_figure = true; found_label = false; continue; } if (line.matches(".*\\\\end\\s*\\{\\s*(figure|wrapfigure).*")) { in_figure = false; if (!found_label) { Position start_pos = s.getSourcePosition(new Position(line_cnt, 0)); Position end_pos = start_pos.moveBy(1); Range r = new Range(start_pos, end_pos); out_list.add(new Advice(this, r, "This figure is missing a label", original.getResourceName(), original.getLine(start_pos.getLine()), original.getOffset(start_pos))); } continue; } if (in_figure) { Matcher mat = m_figurePattern.matcher(line); if (mat.find()) { String fig_name = mat.group(1).trim(); Position fig_pos = s.getSourcePosition(new Position(line_cnt, mat.start(1))); figure_defs.put(fig_name, fig_pos); found_label = true; } } } for (String fig_name : figure_defs.keySet()) { Pattern ref_pat = Pattern.compile("\\\\(C|c){0,1}ref\\s*\\{.*" + fig_name + ".*?\\}"); boolean found = false; for (String line : lines) { Matcher mat = ref_pat.matcher(line); if (mat.find()) { found = true; break; } } if (!found) { Position start_pos = figure_defs.get(fig_name); Position end_pos = s.getSourcePosition(new Position(start_pos.getLine(), start_pos.getColumn() + fig_name.length())); Range r = new Range(start_pos, end_pos); String original_line = original.getLine(start_pos.getLine()); out_list.add(new Advice(this, r, "Figure " + fig_name + " is never referenced in the text", original.getResourceName(), original_line, original.getOffset(start_pos))); } } return out_list; } CheckFigureReferences(); @Override List<Advice> evaluate(AnnotatedString s, AnnotatedString original); @Override String getDescription();  }
@Test public void test1() { AnnotatedString in_string = AnnotatedString.read(new Scanner(CheckFigureReferencesTest.class.getResourceAsStream("data/test4.tex"))); Rule r = new CheckFigureReferences(); List<Advice> ad_list = r.evaluate(in_string, in_string); assertTrue(ad_list.isEmpty()); }
@Test public void test2() { AnnotatedString in_string = AnnotatedString.read(new Scanner(CheckFigureReferencesTest.class.getResourceAsStream("data/test5.tex"))); Rule r = new CheckFigureReferences(); List<Advice> ad_list = r.evaluate(in_string, in_string); assertEquals(1, ad_list.size()); }
@Test public void test3() { AnnotatedString in_string = AnnotatedString.read(new Scanner(CheckSubsectionsTest.class.getResourceAsStream("data/test-subsec-3.tex"))); Rule r = new CheckSubsections(); List<Advice> ad_list = r.evaluate(in_string, in_string); assertTrue(containsAdviceWithLabel(ad_list, "sh:secorder")); }