mbeckerle commented on a change in pull request #224: Generate DaffodilTunables 
and WarnID from dafext.xsd
URL: https://github.com/apache/incubator-daffodil/pull/224#discussion_r289898544
 
 

 ##########
 File path: 
daffodil-propgen/src/main/scala/org/apache/daffodil/propGen/TunableGenerator.scala
 ##########
 @@ -0,0 +1,223 @@
+/*
+ * 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.propGen
+
+import scala.xml.XML
+
+class TunableGenerator(schema: scala.xml.Node) {
+
+  val top = """
+    |/*
+    | * 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
+    |
+    
|////////////////////////////////////////////////////////////////////////////////////////////
+    |//
+    |// Generated Code - Do not hand modify!
+    |//
+    |// This file is entirely generated code created from the
+    |// XML Schema files that describe Daffodil configuration files.
+    |//
+    |// Don't edit this. Go fix the generator to create what you need instead.
+    |//
+    
|////////////////////////////////////////////////////////////////////////////////////////////
+    |
+    |import org.apache.daffodil.exceptions.Assert
+    |import org.apache.daffodil.schema.annotation.props.EmptyElementParsePolicy
+    |import org.apache.daffodil.util.LogLevel
+    |import org.apache.daffodil.util.Logging
+    |import org.apache.daffodil.util.Misc
+    |import org.apache.daffodil.xml.DaffodilXMLLoader
+    |
+    |object DaffodilTunables {
+    |
+    |  def apply(tunables: Map[String, String]): DaffodilTunables = {
+    |    apply().setTunables(tunables)
+    |  }
+    |
+    |  def apply(tunable: String, value: String): DaffodilTunables = {
+    |    apply().setTunable(tunable, value)
+    |  }
+    |
+    |  def apply(): DaffodilTunables = {
+    |    // override tunables from the global configuration file on the class 
path, if it exists
+    |    val (configOpt, _) = Misc.getResourceOption("/daffodil-config.xml")
+    |    val configTunables: Map[String, String] =
+    |      if (configOpt.isDefined) {
+    |        val loader = new DaffodilXMLLoader()
+    |        val node = loader.load(URISchemaSource(configOpt.get))
+    |        val trimmed = scala.xml.Utility.trim(node)
+    |        val tunablesNode = (trimmed \ "tunables").headOption
+    |        val tunablesMap: Map[String, String] = tunablesNode match {
+    |          case None => Map.empty
+    |          case Some(tunableNode) => {
+    |            tunableNode.child.map { n => (n.label, n.text) }.toMap
+    |          }
+    |        }
+    |        tunablesMap
+    |      } else {
+    |        Map.empty
+    |      }
+    |
+    |    new DaffodilTunables().setTunables(configTunables)
+    |  }
+    |}
+    |
+    |case class DaffodilTunables private (
+    """.trim.stripMargin
+
+  val middle = """
+    |  extends Serializable
+    |  with Logging {
+    |
+    |  def setTunables(tunables: Map[String, String]): DaffodilTunables = {
+    |    tunables.foldLeft(this) { case (dafTuns, (tunable, value)) => 
dafTuns.setTunable(tunable, value) }
+    |  }
+    |
+    |  def setTunable(tunable: String, value: String): DaffodilTunables = {
+    |    tunable.toLowerCase match {
+    """.trim.stripMargin
+
+  val bottom = """
+    |      case _ => throw new IllegalArgumentException("Unknown tunable: " + 
tunable)
+    |    }
+    |  }
+    |
+    |  def notSuppressedWarning(warnID: WarnID) =
+    |    !suppressSchemaDefinitionWarnings.contains(warnID) &&
+    |      !suppressSchemaDefinitionWarnings.contains(WarnID.All)
+    |
+    |}
+    """.trim.stripMargin
+
+  val tunablesRoot = (schema \ "element").find(_ \@ "name" == "tunables").get
+  val tunableNodes = tunablesRoot \\ "all" \ "element"
+
+  def writeGeneratedCode(w: java.io.FileWriter) {
+    val tunables =
+      tunableNodes.map { tunableNode =>
+        val schemaName = tunableNode \@ "name"
+        val schemaType = tunableNode \@ "type"
+        val schemaDefault = tunableNode \@ "default"
+
+        schemaName match {
+          case "defaultEmptyElementParsePolicy" =>
 
 Review comment:
   I am uncertain as to why this case statement? This will require editing for 
every new property we add.  If this is unavoidable (or unavoidable without 
additional work you didn't plan to do currently), then a comment to the effect 
that this has to be edited as well as the dafext.xsd is in order. 

----------------------------------------------------------------
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]


With regards,
Apache Git Services

Reply via email to