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_r189947594
 
 

 ##########
 File path: daffodil-runtime1-unparser/src/main/scala/org/apache/daffodil/processors/unparsers/ConvertBinaryCalendarUnparser.scala
 ##########
 @@ -0,0 +1,129 @@
+/*
+ * 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 java.text.ParsePosition
+
+import scala.math.BigInt.int2bigInt
+
+import org.apache.daffodil.calendar.DFDLCalendar
+import org.apache.daffodil.exceptions.Assert
+import org.apache.daffodil.io.DataOutputStream
+import org.apache.daffodil.io.FormatInfo
+import org.apache.daffodil.processors.CalendarEv
+import org.apache.daffodil.processors.CalendarLanguageEv
+import org.apache.daffodil.processors.ElementRuntimeData
+import org.apache.daffodil.processors.parsers.ConvertTextCalendarProcessorBase
+import org.apache.daffodil.schema.annotation.props.gen.BinaryCalendarRep
+import org.apache.daffodil.util.Maybe.One
+import org.apache.daffodil.util.Misc
+
+import com.ibm.icu.util.Calendar
+import com.ibm.icu.util.TimeZone
+import com.ibm.icu.util.ULocale
+
+case class ConvertBinaryCalendarUnparser(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 TextPrimUnparser {
+
+  /**
+   * Primitive unparsers must override runtimeDependencies
+   */
+  override lazy val runtimeDependencies = Seq(localeEv, calendarEv)
+
+  protected def putNumber(dos: DataOutputStream, value: Int, nBits: Int, finfo: FormatInfo): Boolean = {
+    dos.putBigInt(value, nBits, true, finfo)
+  }
+
+  def unparse(state: UState): Unit = {
+    val pos = new ParsePosition(0)
+    val locale: ULocale = localeEv.evaluate(state)
+    val calendar: Calendar = calendarEv.evaluate(state)
+
+    // 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 epochCal = df.getCalendar.clone.asInstanceOf[Calendar]
+
+    df.parse(binCalEpoch, epochCal, 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
+      UE(state, "Parsing the binaryCalendarEpoch from String '%s' -  Failed to parse at index: %d.", binCalEpoch, errIndex)
+      return
+    }
+
+    // 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 {
+      epochCal.getTime
+    } catch {
+      case e: IllegalArgumentException => {
+        UE(state, "Convert to %s (for xs:%s): Failed to parse: %s.", prettyType, xsdType, e.getMessage())
+        return
+      }
+    }
 
 Review comment:
   Can all this epoch stuff be calculated in the primitive and passed in as a paramater to this unparser? Seems like it shouldn't be necessary to recalculate this every time unparse is called since it should never change. Removing that would probably speed things up, and also might mean you don't need to pass in so many parameters to the constructor. In fact, it seems like all we really need is the return value of getTimeInMillis.

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