stevedlawrence closed pull request #85: Fix bugs associated with 
truncateSpecifiedLengthString="yes"
URL: https://github.com/apache/incubator-daffodil/pull/85
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git 
a/daffodil-core/src/main/scala/org/apache/daffodil/grammar/primitives/Padded.scala
 
b/daffodil-core/src/main/scala/org/apache/daffodil/grammar/primitives/Padded.scala
index af961572d..5b48a2a3b 100644
--- 
a/daffodil-core/src/main/scala/org/apache/daffodil/grammar/primitives/Padded.scala
+++ 
b/daffodil-core/src/main/scala/org/apache/daffodil/grammar/primitives/Padded.scala
@@ -70,9 +70,7 @@ trait PaddingInfoMixin {
       else eBase.textStringJustification match {
         case TextStringJustification.Left => TextTruncationType.Left
         case TextStringJustification.Right => TextTruncationType.Right
-        case TextStringJustification.Center => {
-          eBase.SDE("Properties dfdl:truncateSpecifiedLengthString 'yes' and 
dfdl:textStringJustification 'center' are incompatible.")
-        }
+        case TextStringJustification.Center => TextTruncationType.ErrorIfNeeded
       }
     res
   }
diff --git 
a/daffodil-runtime1-unparser/src/main/scala/org/apache/daffodil/processors/unparsers/StringLengthUnparsers.scala
 
b/daffodil-runtime1-unparser/src/main/scala/org/apache/daffodil/processors/unparsers/StringLengthUnparsers.scala
index 9ae35e7f9..14fa253fe 100644
--- 
a/daffodil-runtime1-unparser/src/main/scala/org/apache/daffodil/processors/unparsers/StringLengthUnparsers.scala
+++ 
b/daffodil-runtime1-unparser/src/main/scala/org/apache/daffodil/processors/unparsers/StringLengthUnparsers.scala
@@ -84,8 +84,14 @@ sealed abstract class 
StringSpecifiedLengthUnparserTruncateBase(
       case TextTruncationType.Left => {
         str.substring(0, str.length - nCharsToTrim)
       }
-      case _ =>
+      case TextTruncationType.ErrorIfNeeded => {
+        // justification type was "center", which cannot be truncated, so
+        // should be an error
+        UE(ustate, "Truncation required but disallowed when 
dfdl:truncateSpecifiedLengthString=\"yes\" and 
dfdl:textStringJustification=\"center\"")
+      }
+      case TextTruncationType.None => {
         Assert.invariantFailed("cannot be TextTruncationType.None")
+      }
     }
     result
   }
@@ -193,16 +199,21 @@ class StringMaybeTruncateBitsUnparser(
       Assert.invariant(maybeTargetLengthInBits.isDefined)
       val targetLengthInBits = maybeTargetLengthInBits.get
       val (nBits, nChars) = getLengthInBits(valueString, state)
-      val targetLengthDiff = nBits - targetLengthInBits // positive if we need 
to truncate
-      val nBitsToTrim = targetLengthDiff
-      val dcs = erd.encInfo.getDFDLCharset(state)
-      val minBitsPerChar = 
erd.encodingInfo.encodingMinimumCodePointWidthInBits(dcs)
-      // padChar must be a minimum-width char
-      val nCharsToTrim = nBitsToTrim / minBitsPerChar // positive if we need 
to truncate.
-      Assert.invariant(nCharsToTrim <= nChars)
-      val truncatedValue = truncateByJustification(state, valueString, nChars 
- nCharsToTrim.toInt)
-      Assert.invariant(truncatedValue.length <= valueString.length)
-      truncatedValue
+      val targetLengthDiff = nBits - targetLengthInBits
+      if (targetLengthDiff <= 0) {
+        // truncation is not needed, just write the original string
+        valueString
+      } else {
+        val nBitsToTrim = targetLengthDiff
+        val dcs = erd.encInfo.getDFDLCharset(state)
+        val minBitsPerChar = 
erd.encodingInfo.encodingMinimumCodePointWidthInBits(dcs)
+        // padChar must be a minimum-width char
+        val nCharsToTrim = nBitsToTrim / minBitsPerChar // positive if we need 
to truncate.
+        Assert.invariant(nCharsToTrim <= nChars)
+        val truncatedValue = truncateByJustification(state, valueString, 
nChars - nCharsToTrim.toInt)
+        Assert.invariant(truncatedValue.length <= valueString.length)
+        truncatedValue
+      }
     }
 
     val nCharsWritten = dos.putString(valueToWrite, state)
diff --git 
a/daffodil-runtime1/src/main/scala/org/apache/daffodil/processors/TextJustification.scala
 
b/daffodil-runtime1/src/main/scala/org/apache/daffodil/processors/TextJustification.scala
index b406f3012..f48c846b6 100644
--- 
a/daffodil-runtime1/src/main/scala/org/apache/daffodil/processors/TextJustification.scala
+++ 
b/daffodil-runtime1/src/main/scala/org/apache/daffodil/processors/TextJustification.scala
@@ -31,5 +31,6 @@ object TextTruncationType extends Enum {
   sealed abstract trait Type extends EnumValueType
   case object Left extends Type
   case object Right extends Type
+  case object ErrorIfNeeded extends Type
   case object None extends Type
 }
diff --git 
a/daffodil-test/src/test/resources/org/apache/daffodil/section13/text_number_props/TextNumberPropsUnparse.tdml
 
b/daffodil-test/src/test/resources/org/apache/daffodil/section13/text_number_props/TextNumberPropsUnparse.tdml
index 7e4a1e903..629fb861f 100644
--- 
a/daffodil-test/src/test/resources/org/apache/daffodil/section13/text_number_props/TextNumberPropsUnparse.tdml
+++ 
b/daffodil-test/src/test/resources/org/apache/daffodil/section13/text_number_props/TextNumberPropsUnparse.tdml
@@ -258,7 +258,7 @@
       </tdml:dfdlInfoset>
     </tdml:infoset>
     <tdml:errors>
-      <tdml:error>Schema Definition Error</tdml:error>
+      <tdml:error>Unparse Error</tdml:error>
       <tdml:error>textStringJustification</tdml:error>
       <tdml:error>center</tdml:error>
       <tdml:error>truncateSpecifiedLengthString</tdml:error>
@@ -297,6 +297,53 @@
     <tdml:document>6789100000</tdml:document>
   </tdml:unparserTestCase>
 
+<!--
+      Test Name: unparsePaddedStringTruncate04
+      Schema: delimitedStringsPadding
+      Purpose: This test demonstrates unparsing a string with padding, where 
truncateSpecifiedLengthString is set to "yes"
+               In this case, the string does not need to be truncated, and 
should be output with centered justification.
+-->
+  <tdml:unparserTestCase name="unparsePaddedStringTruncate04" 
model="delimitedStringsPadding" root="e6">
+    <tdml:infoset>
+      <tdml:dfdlInfoset>
+        <ex:e6 xmlns:ex="http://example.com";>123456</ex:e6>
+      </tdml:dfdlInfoset>
+    </tdml:infoset>
+    <tdml:document>__123456__</tdml:document>
+  </tdml:unparserTestCase>
+
+<!--
+      Test Name: unparsePaddedStringTruncate05
+      Schema: delimitedStringsPadding
+      Purpose: This test demonstrates unparsing a string with padding, where 
truncateSpecifiedLengthString is set to "yes"
+               In this case, the string does not need to be truncated and the 
justification is "left", so the data is output justified to the left.
+-->
+  <tdml:unparserTestCase name="unparsePaddedStringTruncate05" 
model="delimitedStringsPadding" root="e7">
+    <tdml:infoset>
+      <tdml:dfdlInfoset>
+        <ex:e7 xmlns:ex="http://example.com";>123456</ex:e7>
+      </tdml:dfdlInfoset>
+    </tdml:infoset>
+    <tdml:document>123456____</tdml:document>
+  </tdml:unparserTestCase>
+
+
+<!--
+      Test Name: unparsePaddedStringTruncate06
+      Schema: delimitedStringsPadding
+      Purpose: This test demonstrates unparsing a string with padding, where 
truncateSpecifiedLengthString is set to "yes"
+               In this case, the string does not need to be truncated and the 
justification is "right", is output right justified.
+-->
+  <tdml:unparserTestCase name="unparsePaddedStringTruncate06" 
model="delimitedStringsPadding" root="e8">
+    <tdml:infoset>
+      <tdml:dfdlInfoset>
+        <ex:e8 xmlns:ex="http://example.com";>123456</ex:e8>
+      </tdml:dfdlInfoset>
+    </tdml:infoset>
+    <tdml:document>____123456</tdml:document>
+  </tdml:unparserTestCase>
+
+
 <!--
       Test Name: unparseDelimitedPaddedString06
       Schema: delimitedStringsPadding
diff --git 
a/daffodil-test/src/test/scala/org/apache/daffodil/section13/text_number_props/TestTextNumberPropsUnparse.scala
 
b/daffodil-test/src/test/scala/org/apache/daffodil/section13/text_number_props/TestTextNumberPropsUnparse.scala
index 1ad37275c..1a414069e 100644
--- 
a/daffodil-test/src/test/scala/org/apache/daffodil/section13/text_number_props/TestTextNumberPropsUnparse.scala
+++ 
b/daffodil-test/src/test/scala/org/apache/daffodil/section13/text_number_props/TestTextNumberPropsUnparse.scala
@@ -53,6 +53,9 @@ class TestTextNumberPropsUnparse {
   @Test def test_unparsePaddedStringTruncate01() { 
runner.runOneTest("unparsePaddedStringTruncate01") }
   @Test def test_unparsePaddedStringTruncate02() { 
runner.runOneTest("unparsePaddedStringTruncate02") }
   @Test def test_unparsePaddedStringTruncate03() { 
runner.runOneTest("unparsePaddedStringTruncate03") }
+  @Test def test_unparsePaddedStringTruncate04() { 
runner.runOneTest("unparsePaddedStringTruncate04") }
+  @Test def test_unparsePaddedStringTruncate05() { 
runner.runOneTest("unparsePaddedStringTruncate05") }
+  @Test def test_unparsePaddedStringTruncate06() { 
runner.runOneTest("unparsePaddedStringTruncate06") }
 
   @Test def test_parseDelimitedPaddedString01() { 
runner.runOneTest("parseDelimitedPaddedString01") }
 


 

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

Reply via email to