From 86bed70a9bab67265a07b64bafe8d21fef4ca8a6 Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Thu, 10 May 2001 11:44:06 +0000 Subject: [PATCH] Improve error reporting in and tell the XML where the document is located. Removed tabs, this makes this patch look a lot bigger than it is. Submitted by: Jesse Glick git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@269019 13f79535-47bb-0310-9956-ffa450edef68 --- .../taskdefs/optional/XMLValidateTask.java | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/XMLValidateTask.java b/src/main/org/apache/tools/ant/taskdefs/optional/XMLValidateTask.java index cd0ae8463..cef41c1bd 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/XMLValidateTask.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/XMLValidateTask.java @@ -56,6 +56,8 @@ package org.apache.tools.ant.taskdefs.optional; 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,14 @@ public class XMLValidateTask extends Task { 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)); + String uri = "file:" + afile.getAbsolutePath().replace('\\', '/'); + for (int index = uri.indexOf('#'); index != -1; + index = uri.indexOf('#')) { + uri = uri.substring(0, index) + "%23" + uri.substring(index+1); + } + is.setSystemId(uri); + xmlReader.parse(is); } catch (SAXException ex) { if (failOnError) throw new BuildException("Could'nt validate document " + afile); @@ -411,8 +420,19 @@ public class XMLValidateTask extends Task { } 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(); } } }