This patch fixes two (IMHO major) problems with this very handy task.

1. The task was not telling the parser where the source file came from. This
means that entity includes using relative URLs did not work at all:

<!ENTITY foo SYSTEM "foo.xml">
...
&foo;

2. The error reporting did not take advantage of all the information given by
the parser, especially the file name, so when checking syntax on a wrapper
document with many includes, nearly all messages would be misleading and
useless.

-Jesse

-- 
Jesse Glick   <mailto:[EMAIL PROTECTED]>
NetBeans, Open APIs  <http://www.netbeans.org/>
tel (+4202) 3300-9161 Sun Micro x49161 Praha CR
Index: src/main/org/apache/tools/ant/taskdefs/optional/XMLValidateTask.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/XMLValidateTask.java,v
retrieving revision 1.1
diff -u -t -r1.1 XMLValidateTask.java
--- src/main/org/apache/tools/ant/taskdefs/optional/XMLValidateTask.java        
2001/04/10 04:47:00     1.1
+++ src/main/org/apache/tools/ant/taskdefs/optional/XMLValidateTask.java        
2001/05/03 19:59:54
@@ -56,6 +56,8 @@
 import java.io.File;
 import java.io.FileReader;
 import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
 import java.util.Enumeration;
 import java.util.Hashtable;
 import java.util.Vector;
@@ -346,7 +348,9 @@
         try {
             log("Validating " + afile.getName() + "... ", Project.MSG_VERBOSE);
             errorHandler.init(afile);
-            xmlReader.parse(new InputSource(new FileReader(afile)));
+            InputSource is = new InputSource(new FileReader(afile));
+            is.setSystemId(afile.toURL().toExternalForm());
+            xmlReader.parse(is);
         } catch (SAXException ex) {
             if (failOnError)
                 throw new BuildException("Could'nt validate document " + 
afile);
@@ -411,8 +415,19 @@
         }
 
         private String getMessage(SAXParseException e) {
-
-            return currentFile + ":" + e.getLineNumber() + ": " + 
e.getMessage();
+            String sysID = e.getSystemId();
+            if (sysID != null) {
+                try {
+                    int line = e.getLineNumber();
+                    int col = e.getColumnNumber();
+                    return new URL(sysID).getFile() +
+                        (line == -1 ? "" : (":" + line +
+                                            (col == -1 ? "" : (":" + col)))) +
+                        ": " + e.getMessage();
+                } catch (MalformedURLException mfue) {
+                }
+            }
+            return e.getMessage();
         }
     }
 }

Reply via email to