summaryrefslogtreecommitdiffstats
path: root/config-application-package
diff options
context:
space:
mode:
authorHarald Musum <musum@oath.com>2019-01-23 12:31:19 +0100
committerHarald Musum <musum@verizonmedia.com>2019-01-24 09:19:40 +0100
commit1e2c50fb8da139652262fa08b052242fb25e2264 (patch)
treef5e66c31d50a276229a64e7498531f7bc8510e54 /config-application-package
parent7f89340cdcb5cf49e88ab4dbd97a3cf49ad3eedf (diff)
Output context when schema validation fails
Diffstat (limited to 'config-application-package')
-rw-r--r--config-application-package/src/main/java/com/yahoo/config/model/application/provider/SchemaValidator.java39
1 files changed, 35 insertions, 4 deletions
diff --git a/config-application-package/src/main/java/com/yahoo/config/model/application/provider/SchemaValidator.java b/config-application-package/src/main/java/com/yahoo/config/model/application/provider/SchemaValidator.java
index 16469bb13ae..1f6822a770b 100644
--- a/config-application-package/src/main/java/com/yahoo/config/model/application/provider/SchemaValidator.java
+++ b/config-application-package/src/main/java/com/yahoo/config/model/application/provider/SchemaValidator.java
@@ -16,15 +16,20 @@ import org.xml.sax.SAXParseException;
import java.io.File;
import java.io.IOException;
+import java.io.LineNumberReader;
import java.io.Reader;
+import java.io.StringReader;
import java.util.logging.Level;
/**
* Validates xml files against a schema.
+ * Note: Tested by SchemaValidatorTest in config-model module, since
+ * needed schema files are in that module
*
* @author Tony Vaagenes
*/
public class SchemaValidator {
+ private static final int linesOfContextForErrors = 3;
private final CustomErrorHandler errorHandler = new CustomErrorHandler();
private final ValidationDriver driver;
@@ -59,7 +64,8 @@ public class SchemaValidator {
}
public void validate(InputSource inputSource, String fileName) throws IOException {
- errorHandler.fileName = (fileName == null ? " input" : fileName);
+ errorHandler.fileName = (fileName == null ? "input" : fileName);
+ errorHandler.reader = inputSource.getCharacterStream();
try {
if ( ! driver.validate(inputSource)) {
// Shouldn't happen, error handler should have thrown
@@ -68,8 +74,7 @@ public class SchemaValidator {
} catch (SAXException e) {
// This should never happen, as it is handled by the ErrorHandler
// installed for the driver.
- throw new IllegalArgumentException(
- "XML error in " + (fileName == null ? " input" : fileName) + ": " + Exceptions.toMessageString(e));
+ throw new IllegalArgumentException("XML error in " + errorHandler.fileName + ": " + Exceptions.toMessageString(e));
}
}
@@ -81,6 +86,7 @@ public class SchemaValidator {
private class CustomErrorHandler implements ErrorHandler {
volatile String fileName;
+ volatile Reader reader;
public void warning(SAXParseException e) {
deployLogger.log(Level.WARNING, message(e));
@@ -97,8 +103,33 @@ public class SchemaValidator {
private String message(SAXParseException e) {
return "XML error in " + fileName + ": " +
Exceptions.toMessageString(e)
- + " [" + e.getLineNumber() + ":" + e.getColumnNumber() + "]";
+ + " [" + e.getLineNumber() + ":" + e.getColumnNumber() + "]" +
+ ", input:\n" + getErrorContext(e.getLineNumber());
}
+
+ private String getErrorContext(int lineNumberWithError) {
+ if (!(reader instanceof StringReader)) return "";
+
+ int fromLine = Math.max(0, lineNumberWithError - linesOfContextForErrors);
+ int toLine = lineNumberWithError + linesOfContextForErrors;
+
+ LineNumberReader r = new LineNumberReader(reader);
+ StringBuilder sb = new StringBuilder();
+ String line;
+ try {
+ reader.reset();
+ while ((line = r.readLine()) != null) {
+ int lineNumber = r.getLineNumber();
+ if (lineNumber >= fromLine && lineNumber <= toLine)
+ sb.append(lineNumber).append(":").append(line).append("\n");
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+ return sb.toString();
+ }
+
}
}