summaryrefslogtreecommitdiffstats
path: root/config-application-package
diff options
context:
space:
mode:
authorHarald Musum <musum@verizonmedia.com>2019-08-14 08:21:18 +0200
committerHarald Musum <musum@verizonmedia.com>2019-08-14 08:21:18 +0200
commitaf8ac01b3883882c792cc872b03aa77ef81a179a (patch)
tree79e761fd933bcd1b90fa37da720d597fb0c72137 /config-application-package
parent23566b7d1b574cf21bfdfc587706f2bddebc29c9 (diff)
Cleanup of config definition name and namespace
Config definition name and namespace are mandatory, so no need to fallback to default namespace anymore
Diffstat (limited to 'config-application-package')
-rw-r--r--config-application-package/SchemaValidator.java121
-rw-r--r--config-application-package/src/main/java/com/yahoo/config/model/application/provider/StaticConfigDefinitionRepo.java4
2 files changed, 121 insertions, 4 deletions
diff --git a/config-application-package/SchemaValidator.java b/config-application-package/SchemaValidator.java
new file mode 100644
index 00000000000..35fdc57eeb1
--- /dev/null
+++ b/config-application-package/SchemaValidator.java
@@ -0,0 +1,121 @@
+// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.config.model.application.provider;
+
+import com.thaiopensource.util.PropertyMap;
+import com.thaiopensource.util.PropertyMapBuilder;
+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.reader.NamedReader;
+import com.yahoo.yolean.Exceptions;
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.Reader;
+import java.io.StringReader;
+import java.util.logging.Level;
+
+/**
+ * Validates xml files against a schema.
+ *
+ * @author Tony Vaagenes
+ */
+public class SchemaValidator {
+ private static final int linesOfContextForErrors = 3;
+
+ private final CustomErrorHandler errorHandler = new CustomErrorHandler();
+ private final ValidationDriver driver;
+ private final DeployLogger deployLogger;
+
+ /**
+ * Initializes the validator by using the given file as schema file
+ *
+ * @param schemaFile schema file
+ * @throws IOException if it is not possible to read schema files
+ */
+ SchemaValidator(File schemaFile, DeployLogger deployLogger) throws IOException, SAXException {
+ this.deployLogger = deployLogger;
+ this.driver = new ValidationDriver(PropertyMap.EMPTY, instanceProperties(), CompactSchemaReader.getInstance());
+ driver.loadSchema(ValidationDriver.fileInputSource(schemaFile));
+ }
+
+ public void validate(File file) throws IOException {
+ validate(file, file.getName());
+ }
+
+ public void validate(File file, String fileName) throws IOException {
+ validate(ValidationDriver.fileInputSource(file), fileName);
+ }
+
+ public void validate(Reader reader) throws IOException {
+ validate(new InputSource(reader), null);
+ }
+
+ public void validate(NamedReader reader) throws IOException {
+ validate(new InputSource(reader), reader.getName());
+ }
+
+ public void validate(InputSource inputSource, String fileName) throws IOException {
+ errorHandler.fileName = (fileName == null ? "input" : fileName);
+ errorHandler.reader = inputSource.getCharacterStream();
+ try {
+ if ( ! driver.validate(inputSource)) {
+ // Shouldn't happen, error handler should have thrown
+ 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.
+ throw new IllegalArgumentException("XML error in " + errorHandler.fileName + ": " + Exceptions.toMessageString(e));
+ }
+ }
+
+ private PropertyMap instanceProperties() {
+ PropertyMapBuilder builder = new PropertyMapBuilder();
+ builder.put(ValidateProperty.ERROR_HANDLER, errorHandler);
+ return builder.toPropertyMap();
+ }
+
+ private class CustomErrorHandler implements ErrorHandler {
+ volatile String fileName;
+ volatile Reader reader;
+
+ public void warning(SAXParseException e) {
+ deployLogger.log(Level.WARNING, message(e));
+ }
+
+ public void error(SAXParseException e) {
+ throw new IllegalArgumentException(message(e));
+ }
+
+ public void fatalError(SAXParseException e) {
+ throw new IllegalArgumentException(message(e));
+ }
+
+ private String message(SAXParseException e) {
+ return "XML error in " + fileName + ": " +
+ Exceptions.toMessageString(e)
+ + " [" + e.getLineNumber() + ":" + e.getColumnNumber() + "]" +
+ ", input\n" + getErrorContext(e.getLineNumber());
+ }
+
+ private String getErrorContext(int lineNumberWithError) {
+ if (!(reader instanceof StringReader)) return "";
+
+ return "";
+
+
+
+
+
+ }
+
+
+ }
+
+}
diff --git a/config-application-package/src/main/java/com/yahoo/config/model/application/provider/StaticConfigDefinitionRepo.java b/config-application-package/src/main/java/com/yahoo/config/model/application/provider/StaticConfigDefinitionRepo.java
index 58318958dfb..d9253d6105b 100644
--- a/config-application-package/src/main/java/com/yahoo/config/model/application/provider/StaticConfigDefinitionRepo.java
+++ b/config-application-package/src/main/java/com/yahoo/config/model/application/provider/StaticConfigDefinitionRepo.java
@@ -1,7 +1,6 @@
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.config.model.application.provider;
-import com.yahoo.config.codegen.CNode;
import com.yahoo.config.model.api.ConfigDefinitionRepo;
import com.yahoo.io.IOUtils;
import com.yahoo.log.LogLevel;
@@ -16,7 +15,6 @@ import java.io.IOException;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
-import java.util.stream.Collectors;
/**
* A global pool of all config definitions that this server knows about. These objects can be shared
@@ -48,8 +46,6 @@ public class StaticConfigDefinitionRepo implements ConfigDefinitionRepo {
private void addConfigDefinition(File def) {
try {
ConfigDefinitionKey key = ConfigUtils.createConfigDefinitionKeyFromDefFile(def);
- if (key.getNamespace().isEmpty())
- key = new ConfigDefinitionKey(key.getName(), CNode.DEFAULT_NAMESPACE);
addConfigDefinition(key, def);
} catch (IOException e) {
log.log(LogLevel.WARNING, "Exception adding config definition " + def, e);