mbeckerle commented on a change in pull request #431:
URL: https://github.com/apache/incubator-daffodil/pull/431#discussion_r512780676
##########
File path: daffodil-cli/src/main/scala/org/apache/daffodil/Main.scala
##########
@@ -230,11 +234,18 @@ class CLIConf(arguments: Array[String]) extends
scallop.ScallopConf(arguments)
}
implicit def validateConverter = singleArgConverter[ValidationMode.Type]((s:
String) => {
Review comment:
Do you really want to use an implicit def here? I always suggest
avoiding them unless you are defining some domain-specific language, a language
feature (e.g., we have a typed-equality library that uses them), etc.
Is this implicit def somehow required by the idiom this code is working in?
If so comments to that effect would help.
##########
File path:
daffodil-lib/src/main/scala/org/apache/daffodil/util/DefaultValidator.scala
##########
@@ -0,0 +1,117 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.daffodil.util
+
+import java.net.URI
+
+import com.typesafe.config.Config
+import javax.xml.XMLConstants
+import javax.xml.transform.stream.StreamSource
+import org.apache.daffodil.api.SaxValidationError
+import org.apache.daffodil.api.SaxValidationWarning
+import org.apache.daffodil.api.ValidationFailure
+import org.apache.daffodil.api.ValidationResult
+import org.apache.daffodil.api.ValidationWarning
+import org.apache.daffodil.api.Validator
+import org.apache.daffodil.api.ValidatorFactory
+import org.apache.daffodil.util.DefaultValidator.XercesValidator
+import org.apache.daffodil.xml.DFDLCatalogResolver
+import org.xml.sax.ErrorHandler
+import org.xml.sax.SAXException
+import org.xml.sax.SAXParseException
+
+import scala.collection.JavaConverters._
+
+/**
+ * Use this for extra validation passes in the TDML Runner
+ * to do a validation pass on the TDML expected Infoset w.r.t. the model and to
+ * do a validation pass on the actual result w.r.t. the model as an XML
document.
+ */
+class DefaultValidator(schemaFileNames: Seq[String])
+ extends Validator
+ with ErrorHandler {
+
+ private var errors = List.empty[ValidationFailure]
+ private var warnings = List.empty[ValidationWarning]
+
+ private val schemaSources: Seq[javax.xml.transform.Source] =
schemaFileNames.map { fn =>
+ val uri = new URI(fn)
+ val is = uri.toURL.openStream()
+ val stream = new StreamSource(is)
+ stream.setSystemId(uri.toString) // must set this so that relative URIs
will be created for import/include files.
+ stream
+ }
+
+ private val factory = new
org.apache.xerces.jaxp.validation.XMLSchemaFactory()
+ private val resolver = DFDLCatalogResolver.get
+ factory.setResourceResolver(resolver)
+
+ private val schema = factory.newSchema(schemaSources.toArray)
+
+ private val validator = new ThreadLocal[XercesValidator] {
+ override def initialValue(): XercesValidator =
+ initializeValidator(schema.newValidator(), DefaultValidator.this,
resolver)
+ }
+
+ def validateXML(document: java.io.InputStream): ValidationResult = {
+ val documentSource = new StreamSource(document)
+
+ try validator.get().validate(documentSource)
+ catch {
+ // Some SAX Parse errors are thrown even if you specify an error handler
to the
+ // validator. So we also need this catch
+ case e: SAXException =>
+ errors :+= SaxValidationError(e)
+ ValidationResult(warnings, errors)
+ }
+ ValidationResult(warnings, errors)
+ }
+
+ def initializeValidator(validator: javax.xml.validation.Validator,
errHandler: ErrorHandler, resolver: DFDLCatalogResolver):
+ javax.xml.validation.Validator = {
+ validator.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true)
+ //
+ validator.setFeature("http://xml.org/sax/features/validation", true)
+
+ // If you enable the feature below, it seems to do no validation at all.
Just passes.
+ //
validator.setFeature("http://apache.org/xml/features/validation/dynamic", true)
+
+ validator.setFeature("http://apache.org/xml/features/validation/schema",
true)
+
validator.setFeature("http://apache.org/xml/features/validation/schema-full-checking",
true)
+ validator.setErrorHandler(errHandler)
+ validator.setResourceResolver(resolver)
+ validator
+ }
+
+ override def warning(spe: SAXParseException): Unit = warnings :+=
SaxValidationWarning(spe)
+ override def error(spe: SAXParseException): Unit = errors :+=
SaxValidationError(spe)
+ override def fatalError(spe: SAXParseException): Unit = errors :+=
SaxValidationError(spe)
+}
+
+object DefaultValidator {
+ type XercesValidator = javax.xml.validation.Validator
Review comment:
Not sure you need this type any more if the DefaultValidator class name
is changed to XercesValidator.
##########
File path:
daffodil-lib/src/main/scala/org/apache/daffodil/util/DefaultValidator.scala
##########
@@ -0,0 +1,117 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.daffodil.util
+
+import java.net.URI
+
+import com.typesafe.config.Config
+import javax.xml.XMLConstants
+import javax.xml.transform.stream.StreamSource
+import org.apache.daffodil.api.SaxValidationError
+import org.apache.daffodil.api.SaxValidationWarning
+import org.apache.daffodil.api.ValidationFailure
+import org.apache.daffodil.api.ValidationResult
+import org.apache.daffodil.api.ValidationWarning
+import org.apache.daffodil.api.Validator
+import org.apache.daffodil.api.ValidatorFactory
+import org.apache.daffodil.util.DefaultValidator.XercesValidator
+import org.apache.daffodil.xml.DFDLCatalogResolver
+import org.xml.sax.ErrorHandler
+import org.xml.sax.SAXException
+import org.xml.sax.SAXParseException
+
+import scala.collection.JavaConverters._
+
+/**
+ * Use this for extra validation passes in the TDML Runner
+ * to do a validation pass on the TDML expected Infoset w.r.t. the model and to
+ * do a validation pass on the actual result w.r.t. the model as an XML
document.
+ */
+class DefaultValidator(schemaFileNames: Seq[String])
Review comment:
I think this class should be XercesValidator. Scaladoc shouldn't mention
TDML runner as that is orthogonal.
##########
File path:
daffodil-lib/src/main/scala/org/apache/daffodil/util/DefaultValidator.scala
##########
@@ -0,0 +1,117 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.daffodil.util
+
+import java.net.URI
+
+import com.typesafe.config.Config
+import javax.xml.XMLConstants
+import javax.xml.transform.stream.StreamSource
+import org.apache.daffodil.api.SaxValidationError
+import org.apache.daffodil.api.SaxValidationWarning
+import org.apache.daffodil.api.ValidationFailure
+import org.apache.daffodil.api.ValidationResult
+import org.apache.daffodil.api.ValidationWarning
+import org.apache.daffodil.api.Validator
+import org.apache.daffodil.api.ValidatorFactory
+import org.apache.daffodil.util.DefaultValidator.XercesValidator
+import org.apache.daffodil.xml.DFDLCatalogResolver
+import org.xml.sax.ErrorHandler
+import org.xml.sax.SAXException
+import org.xml.sax.SAXParseException
+
+import scala.collection.JavaConverters._
+
+/**
+ * Use this for extra validation passes in the TDML Runner
+ * to do a validation pass on the TDML expected Infoset w.r.t. the model and to
+ * do a validation pass on the actual result w.r.t. the model as an XML
document.
+ */
+class DefaultValidator(schemaFileNames: Seq[String])
+ extends Validator
+ with ErrorHandler {
+
+ private var errors = List.empty[ValidationFailure]
+ private var warnings = List.empty[ValidationWarning]
+
+ private val schemaSources: Seq[javax.xml.transform.Source] =
schemaFileNames.map { fn =>
+ val uri = new URI(fn)
+ val is = uri.toURL.openStream()
+ val stream = new StreamSource(is)
+ stream.setSystemId(uri.toString) // must set this so that relative URIs
will be created for import/include files.
+ stream
+ }
+
+ private val factory = new
org.apache.xerces.jaxp.validation.XMLSchemaFactory()
+ private val resolver = DFDLCatalogResolver.get
+ factory.setResourceResolver(resolver)
+
+ private val schema = factory.newSchema(schemaSources.toArray)
+
+ private val validator = new ThreadLocal[XercesValidator] {
+ override def initialValue(): XercesValidator =
+ initializeValidator(schema.newValidator(), DefaultValidator.this,
resolver)
+ }
+
+ def validateXML(document: java.io.InputStream): ValidationResult = {
+ val documentSource = new StreamSource(document)
+
+ try validator.get().validate(documentSource)
+ catch {
+ // Some SAX Parse errors are thrown even if you specify an error handler
to the
+ // validator. So we also need this catch
+ case e: SAXException =>
+ errors :+= SaxValidationError(e)
+ ValidationResult(warnings, errors)
+ }
+ ValidationResult(warnings, errors)
+ }
+
+ def initializeValidator(validator: javax.xml.validation.Validator,
errHandler: ErrorHandler, resolver: DFDLCatalogResolver):
+ javax.xml.validation.Validator = {
+ validator.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true)
+ //
+ validator.setFeature("http://xml.org/sax/features/validation", true)
+
+ // If you enable the feature below, it seems to do no validation at all.
Just passes.
+ //
validator.setFeature("http://apache.org/xml/features/validation/dynamic", true)
+
+ validator.setFeature("http://apache.org/xml/features/validation/schema",
true)
+
validator.setFeature("http://apache.org/xml/features/validation/schema-full-checking",
true)
+ validator.setErrorHandler(errHandler)
+ validator.setResourceResolver(resolver)
+ validator
+ }
+
+ override def warning(spe: SAXParseException): Unit = warnings :+=
SaxValidationWarning(spe)
+ override def error(spe: SAXParseException): Unit = errors :+=
SaxValidationError(spe)
+ override def fatalError(spe: SAXParseException): Unit = errors :+=
SaxValidationError(spe)
+}
+
+object DefaultValidator {
+ type XercesValidator = javax.xml.validation.Validator
+}
+
+class DefaultValidatorFactory extends ValidatorFactory {
Review comment:
This, could stay DefaultValidatorFactory that happens to create a
XercesValidator.
Or it could change to be XercesValidatorFactory.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]