stevedlawrence commented on a change in pull request #61: Preliminary Review - 
Base64 layering runs first parsing unit test.
URL: https://github.com/apache/incubator-daffodil/pull/61#discussion_r181437467
 
 

 ##########
 File path: 
daffodil-runtime1/src/main/scala/org/apache/daffodil/layers/LayerTransformer.scala
 ##########
 @@ -0,0 +1,272 @@
+/*
+ * 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.layers
+
+import org.apache.daffodil.schema.annotation.props.gen.LayerLengthKind
+import org.apache.daffodil.schema.annotation.props.gen.LayerLengthUnits
+import org.apache.daffodil.processors.TermRuntimeData
+import org.apache.daffodil.processors.LayerLengthInBytesEv
+import org.apache.daffodil.processors.LayerTerminatorEv
+import org.apache.daffodil.processors.LayerCharsetEv
+import java.util.HashMap
+import org.apache.daffodil.processors.ParseOrUnparseState
+import org.apache.daffodil.util.NonAllocatingMap
+import org.apache.daffodil.io.DataInputStream
+import org.apache.daffodil.io.DataOutputStream
+import org.apache.daffodil.io.ByteBufferDataInputStream
+import org.apache.daffodil.io.DataInputStream.Mark
+import org.apache.daffodil.util.Misc
+import org.apache.daffodil.exceptions.Assert
+import org.apache.daffodil.util.Maybe
+import org.apache.daffodil.util.Maybe.One
+import org.apache.daffodil.util.Maybe.Nope
+import org.apache.daffodil.io.FormatInfo
+import org.apache.daffodil.schema.annotation.props.gen.BitOrder
+
+/**
+ * Factory for a layer transformer.
+ *
+ * Responsible for digesting the various args, erroring if the wrong ones
+ * are specified, and ultimately constructing the LayerTransformer
+ * of the correct type with the parameters it needs.
+ */
+abstract class LayerTransformerFactory(nom: String)
+  extends Serializable {
+
+  val name = nom.toUpperCase()
+
+  def newInstance(maybeLayerCharsetEv: Maybe[LayerCharsetEv],
+    maybeLayerLengthKind: Maybe[LayerLengthKind],
+    maybeLayerLengthInBytesEv: Maybe[LayerLengthInBytesEv],
+    maybeLayerLengthUnits: Maybe[LayerLengthUnits],
+    maybeLayerTerminatorEv: Maybe[LayerTerminatorEv],
+    trd: TermRuntimeData): LayerTransformer
+}
+
+/**
+ * Transformers have factories. This lets you find the transformer factory
+ * by the name obtained from dfdl:layerTransform.
+ */
+object LayerTransformerFactory {
+
+  private lazy val transformerMap = new NonAllocatingMap(new HashMap[String, 
LayerTransformerFactory])
+
+  def register(factory: LayerTransformerFactory): Unit = {
+    transformerMap.put(factory.name, factory)
+  }
+  /**
+   * Given name, finds the factory for the transformer. SDE otherwise.
+   *
+   * The state is passed in order to provide diagnostic context if not found.
+   */
+  def find(name: String, state: ParseOrUnparseState): LayerTransformerFactory 
= {
+    val maybeFactory = transformerMap.get(name)
+    if (maybeFactory.isEmpty) {
+      val choices = transformerMap.keySet.mkString(", ")
+      state.SDE("The dfdl:layerTransform '%s' was not found. Available choices 
are: %s", name, choices)
+    } else {
+      maybeFactory.get
+    }
+  }
+
+  /**
+   * All transformers must be registered so they are available by name.
+   *
+   * It is possible to package a transformer in a separate jar also, but then
+   * someone has to register it by calling the register method.
+   *
+   * Transformers built into the primary Daffodil jars/packages should be
+   * registered here.
+   */
 
 Review comment:
   We might want to use the java Services API for this kindof thing. Would 
prevent users from having to call the register function--just need to put a 
special file in the META-INF directory in the jar file.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to