jferard / fastods

A very fast and lightweight (no dependency) library for creating ODS (Open Document Spreadsheet, mainly for Calc) files in Java. It's a Martin Schulz's SimpleODS fork
GNU General Public License v3.0
35 stars 6 forks source link

java.lang.IllegalArgumentException #221

Closed ueen closed 3 years ago

ueen commented 3 years ago

Android 8.1 (Redmi S2) Just received this crash, does this ring any bells?

java.lang.RuntimeException: 
  at android.app.ActivityThread.deliverResults (ActivityThread.java:4332)
  at android.app.ActivityThread.handleSendResult (ActivityThread.java:4376)
  at android.app.ActivityThread.-wrap19 (Unknown Source)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1670)
  at android.os.Handler.dispatchMessage (Handler.java:106)
  at android.os.Looper.loop (Looper.java:173)
  at android.app.ActivityThread.main (ActivityThread.java:6634)
  at java.lang.reflect.Method.invoke (Method.java)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:547)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:822)
Caused by: java.lang.IllegalArgumentException: 
  at com.github.jferard.fastods.ref.TableNameUtil.checkTableName (TableNameUtil.java:5)
  at com.github.jferard.fastods.ref.PositionUtil.checkTableName (PositionUtil.java:5)
  at com.github.jferard.fastods.Table.create (Table.java:5)
  at com.github.jferard.fastods.odselement.ContentElement.createTable (ContentElement.java:5)
  at com.github.jferard.fastods.odselement.OdsElements.createTable (OdsElements.java:5)
  at com.github.jferard.fastods.CommonOdsDocument.addTable (CommonOdsDocument.java:5)
  at com.github.jferard.fastods.CommonOdsDocument.addTable (CommonOdsDocument.java:5)
  at com.github.jferard.fastods.AnonymousOdsDocument.addTable (AnonymousOdsDocument.java:29)
  at de.ueen.filmklappepro.ShotlogsFragment.sendShotLog (ShotlogsFragment.java:29)
  at de.ueen.filmklappepro.ShotlogsFragment.access$sendShotLog (ShotlogsFragment.java:29)
  at de.ueen.filmklappepro.ShotlogsFragment$permissionlistener$1.onPermissionGranted (ShotlogsFragment.java:29)
  at com.gun0912.tedpermission.TedPermissionActivity.permissionResult (TedPermissionActivity.java:45)
  at com.gun0912.tedpermission.TedPermissionActivity.onRequestPermissionsResult (TedPermissionActivity.java:2)
  at android.app.Activity.dispatchRequestPermissionsResult (Activity.java:7494)
  at android.app.Activity.dispatchActivityResult (Activity.java:7345)
  at android.app.ActivityThread.deliverResults (ActivityThread.java:4328)
  at android.app.ActivityThread.handleSendResult (ActivityThread.java:4376)
  at android.app.ActivityThread.-wrap19 (Unknown Source)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1670)
  at android.os.Handler.dispatchMessage (Handler.java:106)
  at android.os.Looper.loop (Looper.java:173)
  at android.app.ActivityThread.main (ActivityThread.java:6634)
  at java.lang.reflect.Method.invoke (Method.java)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:547)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:822)

thats my code works on Android 10/11 in my testing on an Smsung A40

val writer = OdsFactory.create().createWriter()
val document = writer.document()
val table = document.addTable(args.filmtitle)
data.forEachIndexed { i, e ->
    e.forEachIndexed { iE, eE ->
        val cell = table.getRow(i).getOrCreateCell(iE)
        try { //try to parse as int, otherwise string
            val parse = eE.toInt()
            cell.setFloatValue(parse)
        } catch (e: NumberFormatException) {
            cell.setStringValue(eE)
        }

    }
}
writer.saveAs(file)

If you have no idea, please close, somtimes there are just random crahes, due to the specific device and its software implementations.

jferard commented 3 years ago

I believe this is not a random crash. I updated TableNameUtil.checkTableName javadoc to state that it throws an IllegalArgumentException when the table name is not valid (according to LO limitations):

A valid table name should not start with a single quote (') or contain of of the following characters: left square bracket ([), right square bracket (]), star (*), question mark (?), colon (:), slash (/) or backslash (\).

Maybe your args.filmtitle contains one of these characters (colons or question marks are frequent in film titles).

I will think of providing a TableNameUtil.sanitizeTableName method to simply replace those chars with a space.

ueen commented 3 years ago

Thats a bright idea! Can those characters be escaped somehow?

jferard commented 3 years ago

I just added the method to TableNameUtil. If you want to sanitize the table name (e.g. when it's not a literal but comes from a foreign source, like a user input, a database...), you can write:

 val name = TableNameUtil().sanitizeTableName(args.filmtitle)
 val table = document.addTable(name)
ueen commented 3 years ago

Awesome! Can you foster a release so I can get it via jitpack?

jferard commented 3 years ago

I pushed the new version this evening. Will be available on maven central soon.