Revision: 10482
http://sourceforge.net/p/languagetool/code/10482
Author: dnaber
Date: 2013-07-10 07:43:39 +0000 (Wed, 10 Jul 2013)
Log Message:
-----------
small code cleanup (better method names)
Modified Paths:
--------------
trunk/languagetool/languagetool-core/src/test/java/org/languagetool/ValidateFalseFriendsXmlTest.java
trunk/languagetool/languagetool-core/src/test/java/org/languagetool/XMLValidator.java
trunk/languagetool/languagetool-core/src/test/java/org/languagetool/rules/patterns/PatternRuleTest.java
trunk/languagetool/languagetool-core/src/test/java/org/languagetool/tagging/disambiguation/rules/DisambiguationRuleTest.java
Modified:
trunk/languagetool/languagetool-core/src/test/java/org/languagetool/ValidateFalseFriendsXmlTest.java
===================================================================
---
trunk/languagetool/languagetool-core/src/test/java/org/languagetool/ValidateFalseFriendsXmlTest.java
2013-07-09 22:00:29 UTC (rev 10481)
+++
trunk/languagetool/languagetool-core/src/test/java/org/languagetool/ValidateFalseFriendsXmlTest.java
2013-07-10 07:43:39 UTC (rev 10482)
@@ -27,7 +27,7 @@
public void testFalseFriendsXML() throws IOException {
System.out.println("Validating false-friends.xml...");
final XMLValidator validator = new XMLValidator();
- validator.validate(JLanguageTool.getDataBroker().getRulesDir() +
"/false-friends.xml",
+ validator.validateWithDtd(JLanguageTool.getDataBroker().getRulesDir() +
"/false-friends.xml",
JLanguageTool.getDataBroker().getRulesDir() +
"/false-friends.dtd", "rules");
System.out.println("Validation successfully finished.");
}
Modified:
trunk/languagetool/languagetool-core/src/test/java/org/languagetool/XMLValidator.java
===================================================================
---
trunk/languagetool/languagetool-core/src/test/java/org/languagetool/XMLValidator.java
2013-07-09 22:00:29 UTC (rev 10481)
+++
trunk/languagetool/languagetool-core/src/test/java/org/languagetool/XMLValidator.java
2013-07-10 07:43:39 UTC (rev 10482)
@@ -78,16 +78,16 @@
}
/**
- * Validate XML file with the given DTD. Throws exception on error.
+ * Validate XML file in classpath with the given DTD. Throws exception on
error.
*/
- public final void validate(String filename, String dtdFile, String docType)
throws IOException {
+ public void validateWithDtd(String filename, String dtdPath, String docType)
throws IOException {
InputStream xmlStream = this.getClass().getResourceAsStream(filename);
if (xmlStream == null) {
throw new IOException("Not found in classpath: " + filename);
}
try {
final String xml = StringTools.readFile(xmlStream, "utf-8");
- validateInternal(xml, dtdFile, docType);
+ validateInternal(xml, dtdPath, docType);
} catch (Exception e) {
throw new IOException("Cannot load or parse '" + filename + "'", e);
} finally {
@@ -96,23 +96,22 @@
}
/**
- * Validate XML file using the given XSD. Throws an exception on error
- * @param filename File in classpath to validate.
- * @param xmlSchema Schema to use.
- * @throws IOException Thrown on error.
+ * Validate XML file using the given XSD. Throws an exception on error.
+ * @param filename File in classpath to validate
+ * @param xmlSchemaPath XML schema file in classpath
*/
- public final void validate(String filename, String xmlSchema) throws
IOException {
+ public void validateWithXmlSchema(String filename, String xmlSchemaPath)
throws IOException {
try {
final InputStream xmlStream =
this.getClass().getResourceAsStream(filename);
if (xmlStream == null) {
- throw new IOException("Not found in classpath: " + filename);
+ throw new IOException("File not found in classpath: " + filename);
}
try {
- final URL schemaStream = this.getClass().getResource(xmlSchema);
- if (schemaStream == null) {
- throw new IOException("Not found in classpath: " + xmlSchema);
+ final URL schemaUrl = this.getClass().getResource(xmlSchemaPath);
+ if (schemaUrl == null) {
+ throw new IOException("XML schema not found in classpath: " +
xmlSchemaPath);
}
- validateInternal(xmlStream, schemaStream);
+ validateInternal(xmlStream, schemaUrl);
} finally {
xmlStream.close();
}
@@ -121,21 +120,25 @@
}
}
- private void validateInternal(String xml, String dtdFile, String docType)
throws SAXException, IOException, ParserConfigurationException {
+ private void validateInternal(String xml, String dtdPath, String docType)
throws SAXException, IOException, ParserConfigurationException {
final SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(true);
final SAXParser saxParser = factory.newSAXParser();
//used for removing existing DOCTYPE from grammar.xml files
- xml = xml.replaceAll("<!DOCTYPE.+>", "");
+ final String cleanXml = xml.replaceAll("<!DOCTYPE.+>", "");
final String decl = "<?xml version=\"1.0\"";
final String endDecl = "?>";
- final String dtd = "<!DOCTYPE " + docType + " PUBLIC \"-//W3C//DTD Rules
0.1//EN\" \"" +this.getClass().getResource(dtdFile)+ "\">";
- final int pos = xml.indexOf(decl);
- final int endPos = xml.indexOf(endDecl);
+ final URL dtdUrl = this.getClass().getResource(dtdPath);
+ if (dtdUrl == null) {
+ throw new RuntimeException("DTD not found in classpath: " + dtdPath);
+ }
+ final String dtd = "<!DOCTYPE " + docType + " PUBLIC \"-//W3C//DTD Rules
0.1//EN\" \"" + dtdUrl + "\">";
+ final int pos = cleanXml.indexOf(decl);
+ final int endPos = cleanXml.indexOf(endDecl);
if (pos == -1) {
- throw new IOException("No XML declaration found in '" + xml.substring(0,
Math.min(100, xml.length())) + "...'");
+ throw new IOException("No XML declaration found in '" +
cleanXml.substring(0, Math.min(100, cleanXml.length())) + "...'");
}
- final String newXML = xml.substring(0, endPos+endDecl.length()) + "\r\n" +
dtd + xml.substring(endPos+endDecl.length());
+ final String newXML = cleanXml.substring(0, endPos+endDecl.length()) +
"\r\n" + dtd + cleanXml.substring(endPos+endDecl.length());
final InputSource is = new InputSource(new StringReader(newXML));
saxParser.parse(is, new ErrorHandler());
}
@@ -148,27 +151,27 @@
validator.validate(new StreamSource(xml));
}
-}
+ /**
+ * XML handler that throws exception on error and warning, does nothing
otherwise.
+ */
+ static class ErrorHandler extends DefaultHandler {
-/**
- * XML handler that throws exception on error and warning, does nothing
otherwise.
- */
-class ErrorHandler extends DefaultHandler {
+ @Override
+ public void warning (SAXParseException e) throws SAXException {
+ System.err.println(e.getMessage()
+ + " Problem found at line " + e.getLineNumber()
+ + ", column " + e.getColumnNumber() + ".");
+ throw e;
+ }
- @Override
- public void warning (SAXParseException e) throws SAXException {
- System.err.println(e.getMessage()
- + " Problem found at line " + e.getLineNumber()
- + ", column " + e.getColumnNumber() + ".");
- throw e;
- }
+ @Override
+ public void error (SAXParseException e) throws SAXException {
+ System.err.println(e.getMessage()
+ + " Problem found at line " + e.getLineNumber()
+ + ", column " + e.getColumnNumber() + ".");
+ throw e;
+ }
- @Override
- public void error (SAXParseException e) throws SAXException {
- System.err.println(e.getMessage()
- + " Problem found at line " + e.getLineNumber()
- + ", column " + e.getColumnNumber() + ".");
- throw e;
}
}
Modified:
trunk/languagetool/languagetool-core/src/test/java/org/languagetool/rules/patterns/PatternRuleTest.java
===================================================================
---
trunk/languagetool/languagetool-core/src/test/java/org/languagetool/rules/patterns/PatternRuleTest.java
2013-07-09 22:00:29 UTC (rev 10481)
+++
trunk/languagetool/languagetool-core/src/test/java/org/languagetool/rules/patterns/PatternRuleTest.java
2013-07-10 07:43:39 UTC (rev 10482)
@@ -137,7 +137,7 @@
return;
}
try {
- validator.validate(ruleFilePath, rulesDir + "/rules.xsd");
+ validator.validateWithXmlSchema(ruleFilePath, rulesDir + "/rules.xsd");
} finally {
xmlStream.close();
}
Modified:
trunk/languagetool/languagetool-core/src/test/java/org/languagetool/tagging/disambiguation/rules/DisambiguationRuleTest.java
===================================================================
---
trunk/languagetool/languagetool-core/src/test/java/org/languagetool/tagging/disambiguation/rules/DisambiguationRuleTest.java
2013-07-09 22:00:29 UTC (rev 10481)
+++
trunk/languagetool/languagetool-core/src/test/java/org/languagetool/tagging/disambiguation/rules/DisambiguationRuleTest.java
2013-07-10 07:43:39 UTC (rev 10482)
@@ -30,7 +30,6 @@
import junit.framework.TestCase;
import org.languagetool.*;
-import org.languagetool.rules.patterns.PatternRule;
import org.languagetool.rules.patterns.PatternTestTools;
import org.languagetool.tagging.disambiguation.xx.DemoDisambiguator;
import org.languagetool.tagging.disambiguation.xx.TrimDisambiguator;
@@ -86,7 +85,7 @@
final InputStream stream = this.getClass().getResourceAsStream(filePath);
try {
if (stream != null) {
- validator.validate(filePath,
JLanguageTool.getDataBroker().getResourceDir() + "/disambiguation.xsd");
+ validator.validateWithXmlSchema(filePath,
JLanguageTool.getDataBroker().getResourceDir() + "/disambiguation.xsd");
}
} finally {
if (stream != null) { stream.close(); }
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
------------------------------------------------------------------------------
See everything from the browser to the database with AppDynamics
Get end-to-end visibility with application monitoring from AppDynamics
Isolate bottlenecks and diagnose root cause in seconds.
Start your free trial of AppDynamics Pro today!
http://pubads.g.doubleclick.net/gampad/clk?id=48808831&iu=/4140/ostg.clktrk
_______________________________________________
Languagetool-commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/languagetool-commits