jw3 commented on a change in pull request #431: URL: https://github.com/apache/incubator-daffodil/pull/431#discussion_r505075462
########## 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: I think so. Ill rework the impl and see how it shakes out. The most visible change will be that SPI will provide factory instances that in turn provide validator instances. ---------------------------------------------------------------- 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]
