stevedlawrence commented on a change in pull request #143: Support
dfdl:length="prefixed"
URL: https://github.com/apache/incubator-daffodil/pull/143#discussion_r239894089
##########
File path:
daffodil-runtime1/src/main/scala/org/apache/daffodil/processors/parsers/BinaryNumberTraits.scala
##########
@@ -50,3 +56,63 @@ trait HasRuntimeExplicitLength {
nBytes * toBits
}
}
+
+
+trait PrefixedLengthParserMixin {
+
+ def prefixedLengthParser: Parser
+ def prefixedLengthERD: ElementRuntimeData
+ def lengthUnits: LengthUnits
+ def prefixedLengthAdjustmentInUnits: Long
+
+ /**
+ * Runs the prefixedLengthParser to determine the prefix length value, making
+ * adjustments as necessary. If the parse fails, this returns 0. The caller
+ * is expected to check to see if state.processorStatus is error upon
+ * returning, indicating that the returned value is meaningless.
+ */
+ def getPrefixedLengthInUnits(state: PState): Long = {
+ val savedInfoset = state.infoset
+
+ // create a "detached" element that the prefix length will be parsed to.
+ // This temporarily removes to infoset.
+ val plElement = Infoset.newElement(prefixedLengthERD,
state.tunable).asInstanceOf[DISimple]
+ state.infoset = plElement
+
+ val parsedLen: JLong = try {
+ prefixedLengthParser.parse1(state)
+ // Return zero if there was an error parsing, the caller of this
+ // evaluatable should check the processorStatus to see if anything
+ // failed and ignore this zero. If there was no error, return the value
+ // as a long.
+ if (state.processorStatus ne Success) 0 else
Numbers.asLong(plElement.dataValue)
+ } finally {
+ // reset back to the original infoset and throw away the detatched
+ // element
+ state.infoset = savedInfoset
+ }
+ if (parsedLen < 0) {
+ state.SDE("Prefixed length result must be non-negative, but was: %d",
parsedLen)
+ }
+ val adjustedLen = parsedLen - prefixedLengthAdjustmentInUnits
+ if (adjustedLen < 0) {
+ state.SDE("Prefixed length result must be non-negative after
dfdl:prefixIncludesPrefixLength adjustment , but was: %d", adjustedLen)
+ }
+ adjustedLen
+ }
+
+ /**
+ * Get the prefixe length in bits. If the parse fails, this returns 0. The
Review comment:
fixed
----------------------------------------------------------------
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