git.net

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GitHub] mbeckerle commented on a change in pull request #61: Preliminary Review - Base64 layering runs first parsing unit test.


mbeckerle 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_r180913897
 
 

 ##########
 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.
+   */
+  register(Base64MIMETransformerFactory)
+}
+
+/**
+ * Shared functionality of all LayerTransformers.
+ */
+abstract class LayerTransformer()
+  extends Serializable {
+
+  protected def wrapLayerDecoder(jis: java.io.InputStream): java.io.InputStream
+
+  def wrapJavaInputStream(s: DataInputStream, fInfo: FormatInfo): java.io.InputStream = {
+    new JavaIOInputStream(s, fInfo)
+  }
+
+  /**
+   *  Override these if we ever have transformers that don't have these
+   *  requirements.
+   */
+  val mandatoryLayerAlignmentInBits: Int = 8
+  val mandatoryLengthUnit: LayerLengthUnits = LayerLengthUnits.Bytes
+
+  def addLayer(s: DataInputStream, fInfo: FormatInfo): DataInputStream = {
+    val jis = wrapJavaInputStream(s, fInfo)
+    val decodedInputStream = wrapLayerDecoder(jis)
+    val newDIS = ByteBufferDataInputStream(decodedInputStream, 0L)
+    newDIS.cst.setPriorBitOrder(BitOrder.MostSignificantBitFirst) // must initialize priorBitOrder
+    newDIS
+  }
+
+  def removeLayer(s: DataInputStream): Unit = {
+    // nothing for now
+  }
+
+  def addLayer(s: DataOutputStream, fInfo: FormatInfo): DataOutputStream = ???
+
+  def removeLayer(s: DataOutputStream): Unit = ???
+
+}
+
+class JavaIOInputStream(s: DataInputStream, finfo: FormatInfo)
+  extends java.io.InputStream {
+
+  private lazy val id = Misc.getNameFromClass(this)
+
+  private val readBuf = new Array[Byte](1)
+
+  override def read(): Int = {
+    val n = read(readBuf, 0, 1)
+    if (n != -1)
+      readBuf(0)
+    else
+      n
+  }
+
+  override def read(b: Array[Byte]) =
+    read(b, 0, b.length)
+
+  /**
 
 Review comment:
   Revise this javadoc. This was copied here for reference when writing the code, but really this is scala code and this needs to be scaladoc, or just omitted since this is a well-documented interface already.

----------------------------------------------------------------
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:
users@xxxxxxxxxxxxxxxx


With regards,
Apache Git Services