TransWikia.com

How to use citeproc-java on Android

Stack Overflow Asked by Hector on February 1, 2021

Im investigating de.undercouch:citeproc-java:2.0.0 within my current Android application.

I cannot identify a ScriptEngine that works on Android with citeproc-java.

my Gradle resembles this:-

ext {
    compileSdkVersion = 30
    minSdkVersion = 26
    targetSdkVersion = 30
}

dependencies {

    implementation 'io.apisense:rhino-android:1.1.1'

    implementation 'de.undercouch:citeproc-java:2.0.0'
    implementation 'org.citationstyles:styles:20.11'
    implementation 'org.citationstyles:locales:20.11'

    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.2.1'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.4.10"
}

Citeproc requires javax.script.ScriptEngineManager which is not available on the Android platform therefore I have substituted io.apisense:rhino-android:1.1.1

I am now facing this exception…

2020-11-19 10:02:22.325 4426-4426/com.google.android.gms.location.sample.basiclocationsample E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.google.android.gms.location.sample.basiclocationsample, PID: 4426
    java.lang.RuntimeException: Unable to start activity ComponentInfo{----}: java.lang.IllegalArgumentException: Could not make bibliography
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     Caused by: java.lang.IllegalArgumentException: Could not make bibliography
        at de.undercouch.citeproc.CSL.makeBibliography(CSL.java:797)
        at de.undercouch.citeproc.CSL.makeBibliography(CSL.java:764)
        at de.undercouch.citeproc.CSL.makeBibliography(CSL.java:750)
        at com.google.android.gms.location.sample.basiclocationsample.MainActivity.what(MainActivity.kt:86)
        at com.google.android.gms.location.sample.basiclocationsample.MainActivity.onCreate(MainActivity.kt:66)
        at android.app.Activity.performCreate(Activity.java:7802)
        at android.app.Activity.performCreate(Activity.java:7791)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1306)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7356) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 
     Caused by: de.undercouch.citeproc.script.ScriptRunnerException: Could not call method
        at de.undercouch.citeproc.script.JREScriptRunner.callMethod(JREScriptRunner.java:87)
        at de.undercouch.citeproc.CSL.makeBibliography(CSL.java:784)
        at de.undercouch.citeproc.CSL.makeBibliography(CSL.java:764) 
        at de.undercouch.citeproc.CSL.makeBibliography(CSL.java:750) 
        at com.google.android.gms.location.sample.basiclocationsample.MainActivity.what(MainActivity.kt:86) 
        at com.google.android.gms.location.sample.basiclocationsample.MainActivity.onCreate(MainActivity.kt:66) 
        at android.app.Activity.performCreate(Activity.java:7802) 
        at android.app.Activity.performCreate(Activity.java:7791) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1306) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7356) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 
     Caused by: javax.script.ScriptException: org.mozilla.javascript.EcmaError: TypeError: Cannot read property "strings" from undefined (<Unknown source>#14312) in <Unknown source> at line number 14312
        at com.sun.script.javascript.RhinoScriptEngine.invoke(RhinoScriptEngine.java:330)
        at com.sun.script.javascript.RhinoScriptEngine.invokeMethod(RhinoScriptEngine.java:296)
        at de.undercouch.citeproc.script.JREScriptRunner.callMethod(JREScriptRunner.java:84)
        at de.undercouch.citeproc.CSL.makeBibliography(CSL.java:784) 
        at de.undercouch.citeproc.CSL.makeBibliography(CSL.java:764) 
        at de.undercouch.citeproc.CSL.makeBibliography(CSL.java:750) 
        at com.google.android.gms.location.sample.basiclocationsample.MainActivity.what(MainActivity.kt:86) 
        at com.google.android.gms.location.sample.basiclocationsample.MainActivity.onCreate(MainActivity.kt:66) 
        at android.app.Activity.performCreate(Activity.java:7802) 
        at android.app.Activity.performCreate(Activity.java:7791) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1306) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7356) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 
     Caused by: org.mozilla.javascript.EcmaError: TypeError: Cannot read property "strings" from undefined (<Unknown source>#14312)
        at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:4198)
        at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:4176)
        at org.mozilla.javascript.ScriptRuntime.typeError(ScriptRuntime.java:4209)
        at org.mozilla.javascript.ScriptRuntime.typeError2(ScriptRuntime.java:4228)
        at org.mozilla.javascript.ScriptRuntime.undefReadError(ScriptRuntime.java:4240)
        at org.mozilla.javascript.ScriptRuntime.getObjectProp(ScriptRuntime.java:1570)
        at org.mozilla.javascript.Interpreter.interpretLoop(Interpreter.java:1336)
        at org.mozilla.javascript.Interpreter.interpret(Interpreter.java:911)
        at org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:109)
        at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:405)
        at com.sun.script.javascript.RhinoScriptEngine$1.superDoTopCall(RhinoScriptEngine.java:146)
        at com.sun.script.javascript.RhinoScriptEngine$1.doTopCall(RhinoScriptEngine.java:139)
        at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3508)
        at org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:107)
2020-11-19 10:02:22.325 4426-4426/com.google.android.gms.location.sample.basiclocationsample E/AndroidRuntime:     at com.sun.script.javascript.RhinoScriptEngine.invoke(RhinoScriptEngine.java:324)
            ... 22 more

The citeproc code that causes this exception is taken straight from their examples…

private fun what() {

    val citeproc = CSL(MyItemProvider(), "ieee")
    citeproc.setOutputFormat("text")
    citeproc.registerCitationItems("ID-1", "ID-2", "ID-3")

    val s1 = citeproc.makeCitation("ID-1")
    println(s1[0].text)
    //=> [1] (for the "ieee" style)

    //=> [1] (for the "ieee" style)
    val s2 = citeproc.makeCitation("ID-2")
    println(s2[0].text)
    //=> [2]

    //=> [2]
    val bibl = citeproc.makeBibliography()  //EXCEPTION RAISED HERE!!!!!!
    for (entry in bibl.entries) {
        println(entry)
    }

}

I’ve created an AndroidJUnit to investigate this issue.

While stepping through the code I have found the issue is an arrayOutOfBoundsException accessing the strings[]

The array has only 12 entries however the index is 39

as shown in the AS image

enter image description here

One Answer

As I saw values shown in debugger its shows 12 items in iCode[] array . And from.pc has value 39 that is the cause of array index out of exception.

As par your code from.pc value increase by pre increments by one condition and in other value increase by post increments adding 2 each time.

And you are using continue with out checking size of array that is always creates exception.

You should check your conditions because it look like array does initialize properly if expect value at 39th position or increments in index are improper.

Answered by Chetan Joshi on February 1, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP