git.net

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GitHub] stevedlawrence commented on a change in pull request #70: Adding property binaryCalendarRep with values "binarySeconds" and "bi…


stevedlawrence commented on a change in pull request #70: Adding property binaryCalendarRep with values "binarySeconds" and "bi…
URL: https://github.com/apache/incubator-daffodil/pull/70#discussion_r189956783
 
 

 ##########
 File path: daffodil-runtime1/src/main/scala/org/apache/daffodil/processors/parsers/PrimitivesDateTime1.scala
 ##########
 @@ -188,3 +189,83 @@ object TextCalendarConstants {
     }
   }
 }
+
+case class ConvertBinaryCalendarParser(erd: ElementRuntimeData,
+  xsdType: String,
+  prettyType: String,
+  pattern: String,
+  hasTZ: Boolean,
+  localeEv: CalendarLanguageEv,
+  calendarEv: CalendarEv,
+  infosetPattern: String,
+  firstDay: Int,
+  calendarDaysInFirstWeek: Int,
+  calendarCheckPolicy: Boolean,
+  calendarTz: Option[TimeZone],
+  tz: TimeZone,
+  binCalRep: BinaryCalendarRep,
+  binCalEpoch: String,
+  lengthInBits: Int)
+  extends ConvertTextCalendarProcessorBase(erd,
+    xsdType, prettyType, pattern, hasTZ, localeEv, calendarEv, infosetPattern, firstDay, calendarDaysInFirstWeek,
+    calendarCheckPolicy, calendarTz, tz)
+  with TextPrimParser {
+
+  override lazy val runtimeDependencies = List(localeEv, calendarEv)
+
+  def parse(start: PState): Unit = {
+
+    val dis = start.dataInputStream
+    val int: BigInt = dis.getSignedBigInt(lengthInBits, start)
+    val pos = new ParsePosition(0)
+    val locale: ULocale = localeEv.evaluate(start)
+    val calendar: Calendar = calendarEv.evaluate(start)
+
+    // This initialization is needed because the calendar object may have
+    // been persisted, and that computes/completes fields that are not yet completed,
+    // such as the Julian day, which freezes the year to 1970.
+    // We want a fresh start on all the fields that are filled in from a parse.
+
+    calendar.clear()
+
+    val df = tlDataFormatter(locale, calendar)
+    val cal = df.getCalendar.clone.asInstanceOf[Calendar]
+
+    df.parse(binCalEpoch, cal, pos)
+
+    // Use pos to verify all characters consumed & check for errors
+    if (pos.getIndex != binCalEpoch.length || pos.getErrorIndex >= 0) {
+      val errIndex = if (pos.getErrorIndex >= 0) pos.getErrorIndex else pos.getIndex
+      PE(start, "Parsing the binaryCalendarEpoch from String '%s' -  Failed to parse at index: %d.", binCalEpoch, errIndex)
+      return
+    }
+
+    if (binCalRep.eq(BinaryCalendarRep.BinarySeconds)) {
+      cal.add(Calendar.SECOND, int.intValue())
+    } else if (binCalRep.eq(BinaryCalendarRep.BinaryMilliseconds)) {
+      cal.add(Calendar.MILLISECOND, int.intValue())
+    }
+
+    // Unfortunately, there is no publicly available method for validating
+    // Calendar values are correct with respect to leniency. So instead, just
+    // try to calculate the time, which forces validation. This causes an
+    // exception to be thrown if a Calendar is not valid.
+    try {
+      cal.getTime
 
 Review comment:
   Not sure this is needed here. It's not possible for time since an epoch to be invalid.

----------------------------------------------------------------
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:
users@xxxxxxxxxxxxxxxx


With regards,
Apache Git Services