Skip to content

fix: handle XmlValueNotSupportedException for decimal twips in XWPFUt…#732

Open
shakeel-mushtaq wants to merge 1 commit into
opensagres:masterfrom
shakeel-mushtaq:fix/xmlbeans-decimal-twips-float-parsing
Open

fix: handle XmlValueNotSupportedException for decimal twips in XWPFUt…#732
shakeel-mushtaq wants to merge 1 commit into
opensagres:masterfrom
shakeel-mushtaq:fix/xmlbeans-decimal-twips-float-parsing

Conversation

@shakeel-mushtaq
Copy link
Copy Markdown

…ils.floatValue()

OOXML documents generated by certain tools (e.g. LibreOffice, some Word automation libraries) may store table grid column widths as non-integer decimal values (e.g. w:w="3457.7537212449256"). The XML Schema type STTwipsMeasure / STSignedTwipsMeasure / STMeasurementOrPercent are defined as xs:union types, so XMLBeans represents such values as XmlUnionImpl. Calling getFloatValue() on that union throws:

XmlValueNotSupportedException: Could not get a Java numeric type
from a Schema string type

This cascades up through XWPFTableUtil.getGridColList() and crashes the entire XHTML converter with an XWPFConverterException, making thumbnail and preview generation impossible for affected documents.

Fix:

  • Wrap getFloatValue() in the three floatValue() overloads with a try/catch that falls back to parseSafeFloat().
  • parseSafeFloat() retrieves the raw string value and handles:
    • Decimal twips strings (e.g. "3457.75") → parsed via Float.parseFloat()
    • Percentage strings (e.g. "50%") → divided by 100
    • The literal "auto" → 0.0f
    • Any remaining unparseable value → 0.0f (safe fallback)
  • Added null-guards on all three overloads.

Reproducer: a .docx whose word/document.xml contains
<w:gridCol w:w="3457.7537212449256"/>
will now convert successfully instead of throwing.

…ils.floatValue()

OOXML documents generated by certain tools (e.g. LibreOffice, some Word
automation libraries) may store table grid column widths as non-integer
decimal values (e.g. w:w="3457.7537212449256"). The XML Schema type
STTwipsMeasure / STSignedTwipsMeasure / STMeasurementOrPercent are
defined as xs:union types, so XMLBeans represents such values as
XmlUnionImpl. Calling getFloatValue() on that union throws:

  XmlValueNotSupportedException: Could not get a Java numeric type
  from a Schema string type

This cascades up through XWPFTableUtil.getGridColList() and crashes
the entire XHTML converter with an XWPFConverterException, making
thumbnail and preview generation impossible for affected documents.

Fix:
  - Wrap getFloatValue() in the three floatValue() overloads with a
    try/catch that falls back to parseSafeFloat().
  - parseSafeFloat() retrieves the raw string value and handles:
      * Decimal twips strings (e.g. "3457.75")  → parsed via Float.parseFloat()
      * Percentage strings   (e.g. "50%")        → divided by 100
      * The literal "auto"                        → 0.0f
      * Any remaining unparseable value           → 0.0f (safe fallback)
  - Added null-guards on all three overloads.

Reproducer: a .docx whose word/document.xml contains
  <w:gridCol w:w="3457.7537212449256"/>
will now convert successfully instead of throwing.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant