mbeckerle commented on a change in pull request #88: Daffodil 1919 separators
URL: https://github.com/apache/incubator-daffodil/pull/88#discussion_r206571525
 
 

 ##########
 File path: 
daffodil-runtime1-unparser/src/main/scala/org/apache/daffodil/processors/unparsers/SeparatedSequenceUnparsers.scala
 ##########
 @@ -0,0 +1,423 @@
+/*
+ * 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.processors.unparsers
+
+import org.apache.daffodil.equality.TypeEqual
+import org.apache.daffodil.exceptions.Assert
+import org.apache.daffodil.processors.{ ElementRuntimeData, 
SequenceRuntimeData, TermRuntimeData }
+import org.apache.daffodil.schema.annotation.props.SeparatorSuppressionPolicy
+import org.apache.daffodil.schema.annotation.props.gen.{ SeparatorPosition }
+import org.apache.daffodil.infoset.DIElement
+
+/**
+ * DFDL Spec. section 14.2.3 specifies only a few different behaviors
+ * for separator suppression. Each has an algorithm.
+ */
+sealed trait SeparatorSuppressionAlgorithm extends Serializable {
+
+  /**
+   * Determines if we should suppress a zero length element and
+   * separator.
+   *
+   * Checks if the length is zero if necessary to decide.
+   *
+   * Returns Suppress or IfTrailing when the element is, in fact, zero length,
+   * and the algorithm wants those to be suppressed.
+   * Returns DoNotSuppress otherwise.
+   */
+  def shouldSuppressIfZeroLength(state: UState, infosetElement: DIElement): 
SeparatorSuppressionAction
+
+  def shouldDoExtraSeparators = false
+
+  def checkArrayPos(state: UState, maxRepeats: Long): Boolean = true
+
+}
+
+object SeparatorSuppressionAlgorithm {
+  type Type = SeparatorSuppressionAlgorithm
+  import SeparatorSuppressionAction._
+
+  object None extends Type {
+    def shouldSuppressIfZeroLength(state: UState, infosetElement: DIElement) = 
Assert.usageError("not to be called.")
+  }
+  object FixedOrExpression extends Type {
+    def shouldSuppressIfZeroLength(state: UState, infosetElement: DIElement) = 
DoNotSuppress
+  }
+
+  class SuppressAnyEmpty(zeroLengthDetector: ZeroLengthDetector) extends Type {
+    def shouldSuppressIfZeroLength(state: UState, infosetElement: DIElement) = 
{
+      val isZL = zeroLengthDetector.isZeroLength(infosetElement)
+      if (isZL) Suppress
+      else DoNotSuppress
+    }
+  }
+
+  trait CheckArrayPosMixin { self: SeparatorSuppressionAlgorithm =>
+    override def checkArrayPos(state: UState, maxRepeats: Long): Boolean = {
+      state.arrayPos <= maxRepeats
+    }
+  }
+
+  class ImplicitSuppressAnyEmpty(zeroLengthDetector: ZeroLengthDetector)
+    extends SuppressAnyEmpty(zeroLengthDetector)
+    with CheckArrayPosMixin
+
+  object ImplicitNeverOrNotPotentiallyTrailing extends Type
+    with CheckArrayPosMixin {
+    def shouldSuppressIfZeroLength(state: UState, infosetElement: DIElement) = 
DoNotSuppress
+
+    override def shouldDoExtraSeparators = true
+
+  }
+
+  class ImplicitPotentiallyTrailing(zeroLengthDetector: ZeroLengthDetector) 
extends Type
+    with CheckArrayPosMixin {
+    def shouldSuppressIfZeroLength(state: UState, infosetElement: DIElement) = 
{
+      val isZL = zeroLengthDetector.isZeroLength(infosetElement)
+      if (isZL) IfTrailing
+      else DoNotSuppress
+    }
+  }
+}
+
+sealed trait SeparatorSuppressionAction extends Serializable
+object SeparatorSuppressionAction {
+  type Type = SeparatorSuppressionAction
+  case object Suppress extends Type
+  case object DoNotSuppress extends Type
+  case object IfTrailing extends Type
+}
+
+trait Separated { self: SequenceChildUnparser =>
+
+  def sep: Unparser
+  def spos: SeparatorPosition
+  def ssp: SeparatorSuppressionPolicy
+  def ssAlgorithm: SeparatorSuppressionAlgorithm
+
+  val childProcessors = Seq(childUnparser, sep)
+}
+
+class ScalarOrderedSeparatedSequenceChildUnparser(
+  childUnparser: Unparser,
+  srd: SequenceRuntimeData,
+  trd: TermRuntimeData,
+  val sep: Unparser,
+  val spos: SeparatorPosition,
+  val ssp: SeparatorSuppressionPolicy,
+  val ssAlgorithm: SeparatorSuppressionAlgorithm)
+  extends SequenceChildUnparser(childUnparser, srd, trd)
+  with Separated {
+
+  override def unparse(state: UState) = childUnparser.unparse1(state)
+}
+
+class RepOrderedSeparatedSequenceChildUnparser(
+  childUnparser: Unparser,
+  srd: SequenceRuntimeData,
+  erd: ElementRuntimeData,
+  val sep: Unparser,
+  val spos: SeparatorPosition,
+  val ssp: SeparatorSuppressionPolicy, // need for diagnostics perhaps
+  val ssAlgorithm: SeparatorSuppressionAlgorithm)
+  extends RepeatingChildUnparser(childUnparser, srd, erd)
+  with Separated {
+
+  override def checkArrayPos(state: UState) = ssAlgorithm.checkArrayPos(state, 
maxRepeats(state))
+}
+
+class OrderedSeparatedSequenceUnparser(
+  rd: SequenceRuntimeData,
+  ssp: SeparatorSuppressionPolicy,
+  spos: SeparatorPosition,
+  sep: Unparser,
+  childUnparsersArg: Seq[SequenceChildUnparser])
+  extends OrderedSequenceUnparserBase(rd, childUnparsersArg) {
+
+  private val childUnparsers = 
childUnparsersArg.asInstanceOf[Seq[SequenceChildUnparser with Separated]]
+
+  /**
+   * True if requires special treatment in the unparse processing loop, as 
occurrences
+   * of later sequence children can influence whether possible trailing 
separators
+   * from earlier sequence children are actually trailing or not.
+   */
+  private type IPT = SeparatorSuppressionAlgorithm.ImplicitPotentiallyTrailing
+
+  private val hasTrailingSeparatorSuppression = {
+    childUnparsers.last.ssAlgorithm.isInstanceOf[IPT]
+  }
+
+  /**
+   * Unparses one occurrence.
+   */
+  protected def unparseOne(
+    unparser: SequenceChildUnparser,
+    trd: TermRuntimeData,
+    state: UState): Unit = {
+
+    if (trd.isRepresented) {
+      if ((spos eq SeparatorPosition.Prefix)) {
+        sep.unparse1(state)
+      } else if ((spos eq SeparatorPosition.Infix) && state.groupPos > 1) {
+        sep.unparse1(state)
+      }
+    }
+
+    unparser.unparse1(state)
+
+    if ((spos eq SeparatorPosition.Postfix) && trd.isRepresented) {
+      sep.unparse1(state)
+    }
+  }
+
+  /**
+   * Unparses a zero-length occurrence, without the separator. This is so that
+   * any statements or other side-effects (discriminators, setVariable, etc.)
+   * will occur.
+   */
+  private def unparseZeroLengthWithoutSeparatorForSideEffect(
+    unparser: SequenceChildUnparser,
+    trd: TermRuntimeData,
+    state: UState): Unit = {
+    //
+    // Unfortunately there's no way to confirm that this produced zero length
+    // because of the possible buffering going on in the unparser.
+    // We'd have to depend on intricate details of the unparser behavior to do 
this,
+    // and that's unwise from separation-of-concerns perspective.
+    //
+    unparser.unparse1(state)
+  }
+
+  /**
+   * Unparses the separator only.
+   *
+   * Does not deals with infix boundary condition, because
+   * the counting of the potential trailing separators takes
+   * this into account.
+   */
+  private def unparseJustSeparator(state: UState): Unit = {
+    sep.unparse1(state)
+  }
+
+  /**
+   * Returns 1, or 0 if infix separator, and this is the first thing
+   * in the sequence meaning there is no separator for it.
+   *
+   * However, if we're not doing trailing separator suppression, always
+   * returns 0.
+   */
+  private def suppressedTrailingSeparatorIncrement(unparser: 
SequenceChildUnparser with Separated, state: UState): Int = {
+    Assert.usage(unparser.trd.isRepresented)
+    val notIPT = !unparser.ssAlgorithm.isInstanceOf[IPT]
 
 Review comment:
   Note that all this could be pre-computed at compile time except the groupPos 
check.

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

Reply via email to