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

 ##########
 File path: 
daffodil-runtime1-unparser/src/main/scala/org/apache/daffodil/processors/unparsers/SequenceChildUnparsers.scala
 ##########
 @@ -0,0 +1,276 @@
+/*
+ * 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.processors.Success
+import org.apache.daffodil.processors.SequenceRuntimeData
+import org.apache.daffodil.processors.ElementRuntimeData
+import org.apache.daffodil.processors.TermRuntimeData
+import org.apache.daffodil.api.ValidationMode
+import org.apache.daffodil.exceptions.Assert
+import org.apache.daffodil.infoset.DIArray
+import org.apache.daffodil.equality._
+import org.apache.daffodil.processors.parsers.MinMaxRepeatsMixin
+import org.apache.daffodil.schema.annotation.props.gen.OccursCountKind
+
+/**
+ * base for unparsers for the children of sequences.
+ *
+ * There is one sequence child unparser for each child (declared) of the 
sequence.
+ *
+ * These do not iterate over multiple recurring instances. That iteration 
happens
+ * in the caller. These unparse only a single occurrence when the child 
unparser
+ * is for an array/optional element.
+ */
+abstract class SequenceChildUnparser(
+  val childUnparser: Unparser,
+  val srd: SequenceRuntimeData,
+  val trd: TermRuntimeData)
+  extends CombinatorUnparser(srd) {
+
+  override def runtimeDependencies = Nil
+}
+
+/**
+ * Base for unparsers of array/optional elements.
+ *
+ * The unparse() method unparses exactly one occurrance, does NOT iterate over
+ * all the occurrences.
+ */
+abstract class RepeatingChildUnparser(
+  override val childUnparser: Unparser,
+  override val srd: SequenceRuntimeData,
+  val erd: ElementRuntimeData)
+  extends SequenceChildUnparser(childUnparser, srd, erd)
+  with MinMaxRepeatsMixin {
+
+  /**
+   * Unparse exactly one occurrence of an array/optional element.
+   *
+   * Iterating for arrays/optionals is done in the caller.
+   */
+  override protected def unparse(state: UState): Unit = {
+    childUnparser.unparse1(state)
+  }
+
+  override lazy val runtimeDependencies = Nil
+
+  override def toString = "RepUnparser(" + childUnparser.toString + ")"
+
+  override def toBriefXML(depthLimit: Int = -1): String = {
+    if (depthLimit == 0) "..." else
+      "<RepUnparser name='" + erd.name + "'>" + 
childUnparser.toBriefXML(depthLimit - 1) +
+        "</RepUnparser>"
+  }
+
+  /**
+   * Sets up for the start of an array. Pulls an event, which must be a 
start-array
+   * event.
+   */
+  def startArray(state: UState): Unit = {
+    //
+    // The Infoset events don't create an array start/end event for a
+    // truly optional (0..1) element.
+    //
+    // But an expression might still refer to dfdl:occursIndex() and we want 
that
+    // to be 1, not wherever an enclosing parent array is.
+    //
+    state.arrayIndexStack.push(1L) // one-based indexing
+
+    val ev = state.inspectAccessor
+    if (ev.erd.isArray) {
+      // only pull start array event for a true array, not an optional.
+      val event = state.advanceOrError
+      Assert.invariant(event.isStart && event.node.isInstanceOf[DIArray])
+      // pull the next event, so that nothing else sees or deals with array 
start events
+      state.inspect
+    }
+  }
+
+  /**
+   * Ends an array. Pulls an event which must be an end-array event.
+   * Validates array dimensions if validation has been requested.
+   */
+  def endArray(currentArrayERD: ElementRuntimeData, state: UState): Unit = {
+    if (currentArrayERD.isArray) {
+      // only pull end array event for a true array, not an optional
+      val event = state.advanceOrError
+      if (!(event.isEnd && event.node.isInstanceOf[DIArray])) {
+        UE(state, "Expected array end event for %s, but received %s.", 
erd.namedQName, event)
+      }
+    }
+
+    // However, we pop the arrayIndexStack for either true arrays or optionals.
+    val actualOccurs = state.arrayIndexStack.pop()
+
+    if (state.processorStatus ne Success) return
+
+    val shouldValidate =
+      state.dataProc.isDefined && state.dataProc.value.getValidationMode != 
ValidationMode.Off
+
+    if (shouldValidate) {
+      val minO = erd.minOccurs
+      val maxO = erd.maxOccurs
+      val isUnbounded = maxO == -1
+      val occurrence = actualOccurs - 1
+
+      if (isUnbounded && occurrence < minO)
+        state.validationError("%s occurred '%s' times when it was expected to 
be a " +
+          "minimum of '%s' and a maximum of 'UNBOUNDED' times.", 
erd.diagnosticDebugName,
+          occurrence, minO)
+      else if (!isUnbounded && (occurrence < minO || occurrence > maxO))
+        state.validationError("%s occurred '%s' times when it was expected to 
be a " +
+          "minimum of '%s' and a maximum of '%s' times.", 
erd.diagnosticDebugName,
+          occurrence, minO, maxO)
+      else {
+        //ok
+      }
+    }
+  }
+
+  /**
+   * Determines if the incoming event matches the current term, so we
+   * should run its unparser.
+   *
+   * If the term is an element and the event is a start element,
+   * then true if the incoming element event namedQName matches the expected 
element namedQName.
+   * Always true if the term is a model-group and the event is a start element.
+   * If the event is not a start, it must be an endArray for the enclosing 
complex element, and
+   * the answer is false.
+   */
+  def shouldDoUnparser(unparser: RepeatingChildUnparser, state: UState): 
Boolean = {
+    val childRD = unparser.trd
+    val res =
+      childRD match {
+        case erd: ElementRuntimeData => {
+          // check to see if we have a matching
+          // incoming infoset event
+          if (state.inspect) {
+            val ev = state.inspectAccessor
+            if (ev.isStart) {
+              val eventNQN = ev.node.namedQName
+              if (eventNQN =:= erd.namedQName) {
+                true
+              } else {
+                // System.err.println("Stopping occurrences(1) of %s due to 
event %s".format(erd.namedQName, ev))
+                false // event not a start for this element
+              }
+            } else if (ev.isEnd && ev.isComplex) {
+              val c = ev.asComplex
+              //ok. We've peeked ahead and found the end of the complex element
+              //that this sequence is the model group of.
+              val optParentRD = srd.immediateEnclosingElementRuntimeData
+              // FIXME: there's no reason to walk backpointer to 
immediateEnclosingElementRuntimeData
+              // as we just want that element's name, and we could precompute 
that and
+              // include it on the SequenceChildUnparser for this
+              optParentRD match {
+                case Some(e: ElementRuntimeData) => {
+                  Assert.invariant(c.runtimeData.namedQName =:= e.namedQName)
+                  // System.err.println("Stopping occurrences(2) of %s due to 
event %s".format(erd.namedQName, ev))
+                  false
+                }
+                case _ =>
+                  Assert.invariantFailed("Not end element for this sequence's 
containing element. Event %s, optParentRD %s.".format(
+                    ev, optParentRD))
+              }
+            } else {
+              // end of simple element. We shouldUnparse on the start event.
+              // System.err.println("Stopping occurrences(3) of %s due to 
event %s".format(erd.namedQName, ev))
+              false
+            }
+          } else {
+            // was no event, so no unparse
+            // System.err.println("Stopping occurrences of(4) %s due to No 
infoset event".format(erd.namedQName))
+            false
+          }
+        }
+        case _ => {
+          // since only elements can be optional, anything else is non-optional
+          true
+        }
+      }
+    val chk: Boolean =
+      if (res) {
+        val check = unparser.checkArrayPos(state)
+        if (!check) {
+          // System.err.println("Stopping occurrences(5) of %s due to array 
pos %s".format(erd.namedQName, state.arrayPos))
+        }
+        check
+      } else
+        false
+    chk
+  }
+
+  /**
+   * True if arrayPos is less than maxOccurs, but only if we care about
+   * maxOccurs. Always true if occursCountKind is not one where we
+   * bound occurrences with maxOccurs.
+   *
+   */
+  def checkArrayPos(state: UState) = true
 
 Review comment:
   Done

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