Skip to content

Commit

Permalink
small cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
danielnaber committed Apr 14, 2015
1 parent 2e0a1ae commit 69c4eea
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 22 deletions.
Expand Up @@ -352,20 +352,15 @@ private void printTimingInformation(final boolean listUnknownWords, final List<R
private int handleLine(final XmlPrintMode mode, final int lineOffset, private int handleLine(final XmlPrintMode mode, final int lineOffset,
final StringBuilder sb) throws IOException { final StringBuilder sb) throws IOException {
int matches = 0; int matches = 0;
String string = sb.toString(); String s = filterXML(sb.toString());
string = filterXML(string);

if (applySuggestions) { if (applySuggestions) {
System.out.print(Tools.correctText(string, System.out.print(Tools.correctText(s, lt));
lt));
} else if (profileRules) { } else if (profileRules) {
matches += Tools.profileRulesOnLine(string, matches += Tools.profileRulesOnLine(s, lt, currentRule);
lt, currentRule);
} else if (!taggerOnly) { } else if (!taggerOnly) {
matches += CommandLineTools.checkText(string, lt, matches += CommandLineTools.checkText(s, lt, apiFormat, -1, lineOffset, matches, mode);
apiFormat, -1, lineOffset, matches, mode);
} else { } else {
CommandLineTools.tagText(string, lt); CommandLineTools.tagText(s, lt);
} }
return matches; return matches;
} }
Expand Down
Expand Up @@ -149,6 +149,7 @@ public String ruleMatchesToXml(List<RuleMatch> ruleMatches, String text, int con


/** /**
* Get an XML representation of the given rule matches. * Get an XML representation of the given rule matches.
*
* @param text the original text that was checked, used to get the context of the matches * @param text the original text that was checked, used to get the context of the matches
* @param contextSize the desired context size in characters * @param contextSize the desired context size in characters
* @since 3.0 * @since 3.0
Expand Down
Expand Up @@ -49,14 +49,14 @@
public class RuleAsXmlSerializerTest { public class RuleAsXmlSerializerTest {


private static final RuleAsXmlSerializer SERIALIZER = new RuleAsXmlSerializer(); private static final RuleAsXmlSerializer SERIALIZER = new RuleAsXmlSerializer();
private static final Language language = TestTools.getDemoLanguage(); private static final Language LANG = TestTools.getDemoLanguage();


@Test @Test
public void testLanguageAttributes() throws IOException { public void testLanguageAttributes() throws IOException {
final String xml1 = SERIALIZER.ruleMatchesToXml(Collections.<RuleMatch>emptyList(), "Fake", 5, NORMAL_XML, language); final String xml1 = SERIALIZER.ruleMatchesToXml(Collections.<RuleMatch>emptyList(), "Fake", 5, NORMAL_XML, LANG);
assertTrue(xml1.contains("shortname=\"xx-XX\"")); assertTrue(xml1.contains("shortname=\"xx-XX\""));
assertTrue(xml1.contains("name=\"Testlanguage\"")); assertTrue(xml1.contains("name=\"Testlanguage\""));
final String xml2 = SERIALIZER.ruleMatchesToXml(Collections.<RuleMatch>emptyList(), "Fake", 5, language, new FakeLanguage()); final String xml2 = SERIALIZER.ruleMatchesToXml(Collections.<RuleMatch>emptyList(), "Fake", 5, LANG, new FakeLanguage());
assertTrue(xml2.contains("shortname=\"xx-XX\"")); assertTrue(xml2.contains("shortname=\"xx-XX\""));
assertTrue(xml2.contains("name=\"Testlanguage\"")); assertTrue(xml2.contains("name=\"Testlanguage\""));
assertTrue(xml2.contains("shortname=\"yy\"")); assertTrue(xml2.contains("shortname=\"yy\""));
Expand All @@ -67,16 +67,16 @@ public void testLanguageAttributes() throws IOException {


@Test @Test
public void testApiModes() throws IOException { public void testApiModes() throws IOException {
String xmlStart = SERIALIZER.ruleMatchesToXml(Collections.<RuleMatch>emptyList(), "Fake", 5, START_XML, language); String xmlStart = SERIALIZER.ruleMatchesToXml(Collections.<RuleMatch>emptyList(), "Fake", 5, START_XML, LANG);
assertThat(StringUtils.countMatches(xmlStart, "<matches"), is(1)); assertThat(StringUtils.countMatches(xmlStart, "<matches"), is(1));
assertThat(StringUtils.countMatches(xmlStart, "</matches>"), is(0)); assertThat(StringUtils.countMatches(xmlStart, "</matches>"), is(0));
String xmlMiddle = SERIALIZER.ruleMatchesToXml(Collections.<RuleMatch>emptyList(), "Fake", 5, CONTINUE_XML, language); String xmlMiddle = SERIALIZER.ruleMatchesToXml(Collections.<RuleMatch>emptyList(), "Fake", 5, CONTINUE_XML, LANG);
assertThat(StringUtils.countMatches(xmlMiddle, "<matches"), is(0)); assertThat(StringUtils.countMatches(xmlMiddle, "<matches"), is(0));
assertThat(StringUtils.countMatches(xmlMiddle, "</matches>"), is(0)); assertThat(StringUtils.countMatches(xmlMiddle, "</matches>"), is(0));
String xmlEnd = SERIALIZER.ruleMatchesToXml(Collections.<RuleMatch>emptyList(), "Fake", 5, END_XML, language); String xmlEnd = SERIALIZER.ruleMatchesToXml(Collections.<RuleMatch>emptyList(), "Fake", 5, END_XML, LANG);
assertThat(StringUtils.countMatches(xmlEnd, "<matches"), is(0)); assertThat(StringUtils.countMatches(xmlEnd, "<matches"), is(0));
assertThat(StringUtils.countMatches(xmlEnd, "</matches>"), is(1)); assertThat(StringUtils.countMatches(xmlEnd, "</matches>"), is(1));
String xml = SERIALIZER.ruleMatchesToXml(Collections.<RuleMatch>emptyList(), "Fake", 5, NORMAL_XML, language); String xml = SERIALIZER.ruleMatchesToXml(Collections.<RuleMatch>emptyList(), "Fake", 5, NORMAL_XML, LANG);
assertThat(StringUtils.countMatches(xml, "<matches"), is(1)); assertThat(StringUtils.countMatches(xml, "<matches"), is(1));
assertThat(StringUtils.countMatches(xml, "</matches>"), is(1)); assertThat(StringUtils.countMatches(xml, "</matches>"), is(1));
} }
Expand All @@ -92,7 +92,7 @@ public void testRuleMatchesToXML() throws IOException {
match.setLine(44); match.setLine(44);
match.setEndLine(45); match.setEndLine(45);
matches.add(match); matches.add(match);
final String xml = SERIALIZER.ruleMatchesToXml(matches, text, 5, NORMAL_XML, language); final String xml = SERIALIZER.ruleMatchesToXml(matches, text, 5, NORMAL_XML, LANG);
assertTrue(xml.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")); assertTrue(xml.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"));
final Pattern matchesPattern = final Pattern matchesPattern =
Pattern.compile(".*<matches software=\"LanguageTool\" version=\"" + JLanguageTool.VERSION + "\" buildDate=\".*?\">.*", Pattern.DOTALL); Pattern.compile(".*<matches software=\"LanguageTool\" version=\"" + JLanguageTool.VERSION + "\" buildDate=\".*?\">.*", Pattern.DOTALL);
Expand All @@ -110,15 +110,15 @@ public void testRuleMatchesToXMLWithCategory() throws IOException {
final List<RuleMatch> matches = new ArrayList<>(); final List<RuleMatch> matches = new ArrayList<>();
final String text = "This is a test sentence."; final String text = "This is a test sentence.";
final List<PatternToken> patternTokens = Collections.emptyList(); final List<PatternToken> patternTokens = Collections.emptyList();
final Rule patternRule = new PatternRule("MY_ID", language, patternTokens, "my description", "my message", "short message"); final Rule patternRule = new PatternRule("MY_ID", LANG, patternTokens, "my description", "my message", "short message");
patternRule.setCategory(new Category("MyCategory")); patternRule.setCategory(new Category("MyCategory"));
final RuleMatch match = new RuleMatch(patternRule, 8, 10, "myMessage"); final RuleMatch match = new RuleMatch(patternRule, 8, 10, "myMessage");
match.setColumn(99); match.setColumn(99);
match.setEndColumn(100); match.setEndColumn(100);
match.setLine(44); match.setLine(44);
match.setEndLine(45); match.setEndLine(45);
matches.add(match); matches.add(match);
final String xml = SERIALIZER.ruleMatchesToXml(matches, text, 5, language, language); final String xml = SERIALIZER.ruleMatchesToXml(matches, text, 5, LANG, LANG);
assertTrue(xml.contains(">\n" + assertTrue(xml.contains(">\n" +
"<error fromy=\"44\" fromx=\"98\" toy=\"45\" tox=\"99\" ruleId=\"MY_ID\" msg=\"myMessage\" " + "<error fromy=\"44\" fromx=\"98\" toy=\"45\" tox=\"99\" ruleId=\"MY_ID\" msg=\"myMessage\" " +
"replacements=\"\" context=\"...s is a test ...\" contextoffset=\"8\" offset=\"8\" errorlength=\"2\" category=\"MyCategory\" " + "replacements=\"\" context=\"...s is a test ...\" contextoffset=\"8\" offset=\"8\" errorlength=\"2\" category=\"MyCategory\" " +
Expand All @@ -145,7 +145,7 @@ public URL getUrl() {
match.setLine(44); match.setLine(44);
match.setEndLine(45); match.setEndLine(45);
matches.add(match); matches.add(match);
final String xml = SERIALIZER.ruleMatchesToXml(matches, text, 5, NORMAL_XML, language); final String xml = SERIALIZER.ruleMatchesToXml(matches, text, 5, NORMAL_XML, LANG);
assertTrue(xml.contains(">\n" + assertTrue(xml.contains(">\n" +
"<error fromy=\"44\" fromx=\"98\" toy=\"45\" tox=\"99\" ruleId=\"FAKE_ID\" msg=\"myMessage\" " + "<error fromy=\"44\" fromx=\"98\" toy=\"45\" tox=\"99\" ruleId=\"FAKE_ID\" msg=\"myMessage\" " +
"replacements=\"\" context=\"...s is an test...\" contextoffset=\"8\" offset=\"8\" errorlength=\"2\" url=\"http://server.org?id=1&amp;foo=bar\" " + "replacements=\"\" context=\"...s is an test...\" contextoffset=\"8\" offset=\"8\" errorlength=\"2\" url=\"http://server.org?id=1&amp;foo=bar\" " +
Expand All @@ -163,7 +163,7 @@ public void testRuleMatchesToXMLEscapeBug() throws IOException {
match.setLine(44); match.setLine(44);
match.setEndLine(45); match.setEndLine(45);
matches.add(match); matches.add(match);
final String xml = SERIALIZER.ruleMatchesToXml(matches, text, 5, NORMAL_XML, language); final String xml = SERIALIZER.ruleMatchesToXml(matches, text, 5, NORMAL_XML, LANG);
assertTrue(xml.contains(">\n" + assertTrue(xml.contains(">\n" +
"<error fromy=\"44\" fromx=\"98\" toy=\"45\" tox=\"99\" ruleId=\"FAKE_ID\" msg=\"myMessage\" " + "<error fromy=\"44\" fromx=\"98\" toy=\"45\" tox=\"99\" ruleId=\"FAKE_ID\" msg=\"myMessage\" " +
"replacements=\"\" context=\"... is &quot;an test...\" contextoffset=\"8\" offset=\"9\" errorlength=\"2\" " + "replacements=\"\" context=\"... is &quot;an test...\" contextoffset=\"8\" offset=\"9\" errorlength=\"2\" " +
Expand Down

0 comments on commit 69c4eea

Please sign in to comment.