stevedlawrence commented on a change in pull request #83: Adding value of 'bcd'
to binaryCalendarRep
URL: https://github.com/apache/incubator-daffodil/pull/83#discussion_r203374405
##########
File path:
daffodil-runtime1/src/main/scala/org/apache/daffodil/processors/parsers/PrimitivesDateTime1.scala
##########
@@ -207,3 +230,94 @@ case class ConvertBinaryCalendarSecMilliParser(
start.simpleElement.overwriteDataValue(newCal)
}
}
+
+abstract class BinaryCalendarBCDParser(
+ override val context: ElementRuntimeData,
+ hasTZ: Boolean,
+ pattern: String,
+ localeEv: CalendarLanguageEv,
+ calendarEv: CalendarEv,
+ xsdType: String,
+ prettyType: String)
+ extends CalendarParser {
+
+ def toBigDecimal(num: Array[Byte], scale: Int): JBigDecimal =
DecimalUtils.bcdToBigDecimal(num, scale)
+
+ def getBitLength(start: ParseOrUnparseState): Int
+
+ def parse(start: PState): Unit = {
+
+ val dis = start.dataInputStream
+ val nBits = getBitLength(start)
+ if (nBits == 0) return // zero length is used for outputValueCalc often.
+
+ if (!dis.isDefinedForLength(nBits)) {
+ PE(start, "Insufficient bits in data. Needed %d bit(s) but found only %d
available.", nBits, dis.remainingBits.get)
+ return
+ }
+
+ try {
+ // Use '0' for the binaryDecimalVirtualPoint because its location will
be implied by the pattern
+ val bigDec: BigDecimal = toBigDecimal(dis.getByteArray(nBits, start), 0)
+ writeResult(start, bigDec.toString())
+ } catch {
+ case n: NumberFormatException => PE(start, "Error in packed data: \n%s",
n.getMessage())
+ }
+ }
+}
+
+case class BinaryCalendarBCDKnownLengthParser(
+ override val context: ElementRuntimeData,
+ hasTZ: Boolean,
+ lengthInBits: Int,
+ pattern: String,
+ localeEv: CalendarLanguageEv,
+ calendarEv: CalendarEv,
+ xsdType: String,
+ prettyType: String)
+ extends BinaryCalendarBCDParser(context, hasTZ, pattern, localeEv,
calendarEv, xsdType, prettyType)
+ with PrimParser
+ with HasKnownLengthInBits {
+
+ override lazy val runtimeDependencies = List(localeEv, calendarEv)
+}
+
+case class BinaryCalendarBCDRuntimeLengthParser(
+ override val e: ElementRuntimeData,
+ hasTZ: Boolean,
+ pattern: String,
+ localeEv: CalendarLanguageEv,
+ calendarEv: CalendarEv,
+ xsdType: String,
+ prettyType: String,
+ val lengthEv: Evaluatable[JLong],
+ val lUnits: LengthUnits)
+ extends BinaryCalendarBCDParser(e, hasTZ, pattern, localeEv, calendarEv,
xsdType, prettyType)
+ with PrimParser
+ with HasRuntimeExplicitLength {
+
+ override lazy val runtimeDependencies = List(localeEv, calendarEv, lengthEv)
+}
+
+case class BinaryCalendarBCDDelimitedLengthParser(
+ e: ElementRuntimeData,
+ hasTZ: Boolean,
+ pattern: String,
+ localeEv: CalendarLanguageEv,
+ calendarEv: CalendarEv,
+ xsdType: String,
+ prettyType: String,
+ textParser: TextDelimitedParserBase,
+ fieldDFAEv: FieldDFAParseEv,
+ isDelimRequired: Boolean)
+ extends PackedBinaryDecimalDelimitedBaseParser(e, textParser, fieldDFAEv,
isDelimRequired, 0 )
+ with CalendarParser {
+
+ override def toBigInteger(num: Array[Byte]): JBigInteger =
DecimalUtils.bcdToBigInteger(num)
+ def toBigDecimal(num: Array[Byte], scale: Int): JBigDecimal =
DecimalUtils.bcdToBigDecimal(num, scale)
+
+ override def writeResult(state: PState, num: BigDecimal) {
+ super[CalendarParser].writeResult(state, num.toString())
+ }
Review comment:
This is an interesting way to do this (and I know see that you got
writeResult from existing code), and makes wonder if the other parsers can be
done the same way. We already have parsers that handle parsing packed binary
integers of known, runtime, and delimited lengths. So you could modify
PackedBinaryIntegerBaseParser to call writeResult instead of setting the
infoset element, and set the default writeResult to just set the infoset. But
then your PackedCalendar parsers could convert that BigInteger to a String,
parse it using the DateFormatter, and then set the infoset. So basically all
the length and packed stuff is handled for you already, you just need to
convert that int to calendar.
Alternatively, that actually gets me thinking that we already have
ConvertTextCalendarParsers. Would it be possible to just reuse those? So we
could change the grammar to be something like BinaryIntegerBCDParser ~
ConvertTextCalendarParser? Seems like we might not actually need all these new
parsers and we just need to modify our grammar to chain existing parsers
together?
----------------------------------------------------------------
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