mbeckerle commented on code in PR #1187:
URL: https://github.com/apache/daffodil/pull/1187#discussion_r1534185424


##########
daffodil-runtime1-unparser/src/main/scala/org/apache/daffodil/unparsers/runtime1/LayeredSequenceUnparser.scala:
##########
@@ -67,36 +68,52 @@ class LayeredSequenceUnparser(
     // create a new DOS where unparsers following this layer will unparse
     val layerFollowingDOS = layerUnderlyingDOS.addBuffered()
 
-    // New layerDOS is where the layer will unparse into. Ultimately anything 
written
-    // to layerDOS ends up, post transform, in layerUnderlyingDOS
-    val layerDOS = layerTransformer.addLayer(layerUnderlyingDOS, state, 
formatInfoPre)
-
-    // unparse the layer body into layerDOS
-    state.dataOutputStream = layerDOS
-    super.unparse(state)
-    layerTransformer.endLayerForUnparse(state)
-
-    // now we're done unparsing the layer, so finalize the last DOS in the
-    // chain. Note that there might be suspensions so some parts of the
-    // layerDOS chain may not be finished. When those suspensions are all
-    // finished, the layerDOS content will be written to the
-    // layerUnderlyingDOS, which will subsequently be finished. Like above, it
-    // is important to pass an immutable UState into the setFinished function
-    // because that state is stored in the DOS (as finishedFormatInfo) and its
-    // use may be delayed to after the actual UState has been changed. The
-    // UState has almost certainly changed since the last cloneForSuspension
-    // call, so we need a new clone to finish this DOS.
-    val layerDOSLast = layerDOS.lastInChain
-    val formatInfoPost = 
state.asInstanceOf[UStateMain].cloneForSuspension(layerDOSLast)
-    layerDOSLast.setFinished(formatInfoPost)
-
-    // clean up resources - note however, that due to suspensions, the whole
-    // layer stack is potentially still needed, so not clear what can be
-    // cleaned up at this point.
-    layerTransformer.removeLayer(layerDOS, state)
-
-    // reset the state so subsequent unparsers write to the following DOS
-    state.dataOutputStream = layerFollowingDOS
+    try {
+      val layerDriver = LayerDriver(formatInfoPre, ctxt.layerRuntimeData)
+
+      // New layerDOS is where the layer will unparse into. Ultimately 
anything written
+      // to layerDOS ends up, post transform, in layerUnderlyingDOS
+      val layerDOS = layerDriver.addOutputLayer(layerUnderlyingDOS)
+
+      // unparse the layer body into layerDOS
+      state.dataOutputStream = layerDOS
+      super.unparse(state)
+      // now we're done unparsing the layer recursively.
+      // While doing that unparsing, the data output stream may have been 
split, so the
+      // DOS in the state may no longer be the layerDOS.
+      //
+      // However, it is when whatever DOS is in the state at this point, that, 
when that
+      // DOS is consolidated and written out, that is when the layer is 
finished
+      // and the wrap-up of the layer (such as writing output variables) can 
occur.
+      //
+      val endOfLayerUnparseDOS = state.dataOutputStream
+      val formatInfoPost =
+        state.asInstanceOf[UStateMain].cloneForSuspension(endOfLayerUnparseDOS)
+
+      // setFinished on this end-of-layer-unparse data-output-stream  ensures
+      // that the layerDOS gets close() called on it.
+      endOfLayerUnparseDOS.setFinished(formatInfoPost)
+
+      // clean up resources - note however, that due to suspensions, the whole
+      // layer stack is potentially still needed, so
+      // nothing can be cleaned up at this point.
+    } catch {
+      case u: UnsuppressableException =>
+        throw u
+      case lre: LayerRuntimeException =>
+        throw lre
+      case re: RuntimeException =>
+        throw new LayerRuntimeException(re)
+      case pe: UnparseError =>
+        throw pe
+      case sde: RuntimeSchemaDefinitionError =>
+        throw sde
+      case e: Exception =>
+        state.toss(state.toProcessingError(new LayerUnexpectedException(e)))

Review Comment:
   Hmmm. Should it just be LayerException? 
   
   LayerException and LayerRuntimeException then have the same relationship as 
Exception and RuntimeException in terms of being routine vs. special. 



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

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to