mbeckerle commented on a change in pull request #431: URL: https://github.com/apache/incubator-daffodil/pull/431#discussion_r504958131
########## File path: daffodil-lib/src/main/scala/org/apache/daffodil/api/Validator.scala ########## @@ -0,0 +1,106 @@ +/* + * 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.api + +import org.xml.sax.ErrorHandler + +/** + * User defined validation logic. + * Can be added programmatically or registered via SPI. + */ +trait Validator { + def name(): String = getClass.getTypeName.toLowerCase + def validateXML(document: java.io.InputStream, errHandler: ErrorHandler, args: Validator.Arguments): Unit +} + +object Validator { + /** + * Unstructured argument list to support inputs from non-programmatic sources (eg. cli) + */ + type ArgumentList = Seq[Validator.Argument] + + /** + * Named arguments to be passed to the validator + * @param x + */ + case class Arguments(private val x: Map[String, AnyRef]) { + // todo;; add all getAs and as methods + + def as[T](k: String): T = { + x.get(k).asInstanceOf[T] + } + + def getAs[T](k: String): Option[T] = { + x.get(k).map(_.asInstanceOf[T]) + } + } + object Arguments { + def apply(): Arguments = Empty + val Empty = Arguments(Map.empty) + } + + /** + * Named input to a validator in string form. + * Can be validated and converted during the compilation process. + * @param key arg name + * @param value arg value + */ + case class Argument(key: String, value: String) + object Argument { + val DefaultKey = "default" + def apply(value: String): Argument = Argument(DefaultKey, value) + } + + /** + * Opt-in compilation hints + */ + object CompilerOps { + + /** + * Compiles an unstructured argument list in to a structured list + */ + trait CheckArgs { + def checkArgs(args: ArgumentList): Validator.Arguments + } + } +} + +/** + * Thrown when the requested validator was not found in the SPI registered list + * @param name + */ +case class ValidatorNotFoundException(name: String) extends Exception(s"$name validator was not found") + +/** + * Thrown when validator compilation fails + * @param message + */ +case class ValidatorCompilationException(message: String) extends Exception(message) + +/** + * Provides validation logic packaged with arguments. This compiled object has had validation and + * resource intensive initialization operations performed and is in an operation state + * @param v Validator instance + * @param args argument list + */ +class CompiledValidator(val v: Validator, args: Validator.Arguments) extends Serializable { Review comment: Hmmm. I'm expecting CompiledValidator to be a scala trait with just one method, def validateXML(document, errorHandler): Unit with no implementation provided. Then specific validator service providers, including the default one, would have a factory method - perhaps named compile(args) that returns an instance of a specific implementation class, e.g., called CompiledXSDXercesValidator (for the default validator implementation) or something like that. The service provider's compile method would take the args and return an instance of this class which implements the CompiledValidator trait. All the initialization and other one-time stuff would happen in the compile method for that service provider, if... that is, the resulting CompiledValidator and it's guts member values are all serializable. If they're not serializable, then probably that work has to be done lazily on first use, with the serializable members being the raw args. In addition, if the CompiledValidator's guts runtime structures are not thread-safe, and in Xerces they are not, then the on-first use on-the-fly compilation has to be done in the getInstance method of a thread local so that all threads get their own. Does that make sense? ---------------------------------------------------------------- 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]
