From aee7ff2c99074b5ba90000b91ed73d9465c7b2eb Mon Sep 17 00:00:00 2001 From: Harald Musum Date: Fri, 23 Apr 2021 08:38:39 +0200 Subject: Make sure we are able to output context for validation errors --- .../application/provider/SchemaValidator.java | 30 +++++++++++++++++----- 1 file changed, 24 insertions(+), 6 deletions(-) (limited to 'config-application-package') 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 b0b1209aa90..f3da3f1aafc 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 @@ -7,6 +7,7 @@ import com.thaiopensource.validate.ValidateProperty; import com.thaiopensource.validate.ValidationDriver; import com.thaiopensource.validate.rng.CompactSchemaReader; import com.yahoo.config.application.api.DeployLogger; +import com.yahoo.io.IOUtils; import com.yahoo.io.reader.NamedReader; import com.yahoo.yolean.Exceptions; import org.xml.sax.ErrorHandler; @@ -52,18 +53,20 @@ public class SchemaValidator { } public void validate(File file, String fileName) throws IOException { - validate(ValidationDriver.fileInputSource(file), fileName); + validate(IOUtils.createReader(file.getAbsolutePath()), fileName); } public void validate(Reader reader) throws IOException { - validate(new InputSource(reader), null); + validate(reader, null); } public void validate(NamedReader reader) throws IOException { - validate(new InputSource(reader), reader.getName()); + validate(reader, reader.getName()); } - public void validate(InputSource inputSource, String fileName) throws IOException { + @Deprecated + /* @deprecated Will not give proper context from errors, use another validate method instead */ + public void validate(InputSource inputSource, String fileName) throws IOException { errorHandler.fileName = (fileName == null ? "input" : fileName); errorHandler.reader = inputSource.getCharacterStream(); try { @@ -72,8 +75,23 @@ public class SchemaValidator { throw new RuntimeException("Aborting due to earlier XML errors."); } } catch (SAXException e) { - // This should never happen, as it is handled by the ErrorHandler - // installed for the driver. + // Shouldn't happen, error handler should have thrown + throw new IllegalArgumentException("XML error in " + errorHandler.fileName + ": " + Exceptions.toMessageString(e)); + } + } + + private void validate(Reader reader, String fileName) throws IOException { + errorHandler.fileName = (fileName == null ? "input" : fileName); + // We need to read from a reader in error handler, so need to read all content into a new one + Reader newReader = new StringReader(IOUtils.readAll(reader)); + errorHandler.reader = newReader; + try { + if ( ! driver.validate(new InputSource(newReader))) { + // Shouldn't happen, error handler should have thrown + throw new RuntimeException("Aborting due to earlier XML errors."); + } + } catch (SAXException e) { + // Shouldn't happen, error handler should have thrown throw new IllegalArgumentException("XML error in " + errorHandler.fileName + ": " + Exceptions.toMessageString(e)); } } -- cgit v1.2.3