diff options
author | Harald Musum <musum@verizonmedia.com> | 2021-04-23 08:38:39 +0200 |
---|---|---|
committer | Harald Musum <musum@verizonmedia.com> | 2021-04-23 08:38:39 +0200 |
commit | aee7ff2c99074b5ba90000b91ed73d9465c7b2eb (patch) | |
tree | e064df628607a2791682b8d42eb65f30141abc85 /config-application-package | |
parent | f40237d5bdfe32a7b33df13e426b67b88fd0288a (diff) |
Make sure we are able to output context for validation errors
Diffstat (limited to 'config-application-package')
-rw-r--r-- | config-application-package/src/main/java/com/yahoo/config/model/application/provider/SchemaValidator.java | 30 |
1 files changed, 24 insertions, 6 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 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)); } } |