Revision: 10484
          http://sourceforge.net/p/languagetool/code/10484
Author:   dnaber
Date:     2013-07-10 08:20:19 +0000 (Wed, 10 Jul 2013)
Log Message:
-----------
rule editor: XML gets validated now in advanced mode

Modified Paths:
--------------
    
trunk/languagetool/languagetool-core/src/test/java/org/languagetool/XMLValidator.java
    
trunk/ltcommunity/grails-app/controllers/org/languagetool/RuleEditorController.groovy
    trunk/ltcommunity/grails-app/views/ruleEditor/_checkXmlProblem.gsp

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-10 07:50:07 UTC (rev 10483)
+++ 
trunk/languagetool/languagetool-core/src/test/java/org/languagetool/XMLValidator.java
       2013-07-10 08:20:19 UTC (rev 10484)
@@ -18,6 +18,7 @@
  */
 package org.languagetool;
 
+import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.StringReader;
@@ -120,6 +121,25 @@
     }
   }
 
+  /**
+   * Validate XML file using the given XSD. Throws an exception on error.
+   * @param xml the XML string to be validated
+   * @param xmlSchemaPath XML schema file in classpath
+   * @since 2.3
+   */
+  public void validateStringWithXmlSchema(String xml, String xmlSchemaPath) 
throws IOException {
+    try {
+      final URL schemaUrl = this.getClass().getResource(xmlSchemaPath);
+      if (schemaUrl == null) {
+        throw new IOException("XML schema not found in classpath: " + 
xmlSchemaPath);
+      }
+      final ByteArrayInputStream stream = new 
ByteArrayInputStream(xml.getBytes("utf-8"));
+      validateInternal(stream, schemaUrl);
+    } catch (SAXException e) {
+      throw new RuntimeException(e);
+    }
+  }
+
   private void validateInternal(String xml, String dtdPath, String docType) 
throws SAXException, IOException, ParserConfigurationException {
     final SAXParserFactory factory = SAXParserFactory.newInstance();
     factory.setValidating(true);

Modified: 
trunk/ltcommunity/grails-app/controllers/org/languagetool/RuleEditorController.groovy
===================================================================
--- 
trunk/ltcommunity/grails-app/controllers/org/languagetool/RuleEditorController.groovy
       2013-07-10 07:50:07 UTC (rev 10483)
+++ 
trunk/ltcommunity/grails-app/controllers/org/languagetool/RuleEditorController.groovy
       2013-07-10 08:20:19 UTC (rev 10484)
@@ -103,10 +103,18 @@
         PatternRuleLoader loader = new PatternRuleLoader()
         loader.setRelaxedMode(true)
         String xml = "<rules lang=\"" + language.getShortName() + 
"\"><category name=\"fakeCategory\">" + params.xml + "</category></rules>"
-        if (xml.trim().isEmpty()) {
+        if (params.xml.trim().isEmpty()) {
             render(template: 'checkXmlProblem', model: [error: "No XML found"])
             return
         }
+        XMLValidator validator = new XMLValidator()
+        String xsd = JLanguageTool.getDataBroker().getRulesDir() + "/rules.xsd"
+        try {
+            validator.validateStringWithXmlSchema(xml, xsd)
+        } catch (Exception e) {
+            render(template: 'checkXmlProblem', model: [error: "XML validation 
failed: " + e.getMessage()])
+            return
+        }
         xml = xml.replaceAll("&([a-zA-Z]+);", "&amp;\$1;")  // entities 
otherwise lead to an error
         InputStream input = new ByteArrayInputStream(xml.getBytes())
         def rules = loader.getRules(input, "<form>")

Modified: trunk/ltcommunity/grails-app/views/ruleEditor/_checkXmlProblem.gsp
===================================================================
--- trunk/ltcommunity/grails-app/views/ruleEditor/_checkXmlProblem.gsp  
2013-07-10 07:50:07 UTC (rev 10483)
+++ trunk/ltcommunity/grails-app/views/ruleEditor/_checkXmlProblem.gsp  
2013-07-10 08:20:19 UTC (rev 10484)
@@ -1,2 +1,7 @@
 <br/>
-Error: ${error.encodeAsHTML()}
\ No newline at end of file
+<table>
+    <tr>
+        <td><img style="margin:5px" src="${resource(dir:'images', 
file:'exclamation.png')}" /></td>
+        <td>Error: ${error.encodeAsHTML()}</td>
+    </tr>
+</table>

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

Reply via email to