Xalan 2.7.2 on Android Studio

Here should go questions about transforming XML with XSLT and FOP.
SergioC
Posts: 2

Xalan 2.7.2 on Android Studio

Mon Jan 29, 2018 1:12 pm

I'm trying Xalan 2.7.2 to use javascript function on XSLT,
I've already added the jar files and I have tried several approaches without success , but I'm not getting the expected result.

On windows I get the result of XSLT, but in android not.

I get the following error message in the studio on

transformer.transform(new StreamSource(new StringReader(xml)), new StreamResult(result));


01/29 10:05:44: Launching app
$ adb install-multiple -r -t -p com.example.scalixto.xalanjavaxslt D:\SCalixto\AndroidStudioProjects\XalanJavaXSLT\app\build\intermediates\split-apk\debug\slices\slice_1.apk D:\SCalixto\AndroidStudioProjects\XalanJavaXSLT\app\build\outputs\apk\debug\app-debug.apk
Split APKs installed
$ adb shell am start -n "com.example.scalixto.xalanjavaxslt/com.example.scalixto.xalanjavaxslt.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Client not ready yet..Waiting for process to come online
Connected to process 14404 on device zebra_technologies-tc75x-161945225D0299
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
I/art: Rejecting re-init on previously-failed class java.lang.Class<com.android.webview.chromium.WebViewContentsClientAdapter$6>
I/art: Rejecting re-init on previously-failed class java.lang.Class<com.android.webview.chromium.WebViewContentsClientAdapter$6>
V/Log: xsl ==> <?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xalan="http://xml.apache.org/xalan"
xmlns:counter="MyCounter"
extension-element-prefixes="counter"
version="1.0">

<xalan:component prefix="counter"
elements="init incr" functions="read">
<xalan:script lang="javascript">
<![CDATA[
var counters = new Array();

function init (xslproc, elem) {
name = elem.getAttribute ("name");
value = parseInt(elem.getAttribute ("value"));
counters[name] = value;
return null;
}

function read (name) {
// Return a string.
return "" + (counters[name]);
}

function incr (xslproc, elem)
{
name = elem.getAttribute ("name");
counters[name]++;
return null;
}
]]>
</xalan:script>
</xalan:component>

<xsl:template match="/">
<HTML>
<H1>Names in alphatebical order</H1>
<counter:init name="index" value="1"/>
<xsl:for-each select="doc/name">
<xsl:sort select="@last"/>
<xsl:sort select="@first"/>
<p>
<xsl:text>[</xsl:text>
<xsl:value-of select="counter:read('index')"/>
<xsl:text>]. </xsl:text>
<xsl:value-of select="@last"/>
<xsl:text>, </xsl:text>
<xsl:value-of select="@first"/>
</p>
<counter:incr name="index"/>
</xsl:for-each>
</HTML>
</xsl:template>

</xsl:stylesheet>
V/Log: xsl ==> <?xml version="1.0"?>
<doc>
<name first="David" last="Marston"/>
<name first="David" last="Bertoni"/>
<name first="Donald" last="Leslie"/>
<name first="Emily" last="Farmer"/>
<name first="Jack" last="Donohue"/>
<name first="Myriam" last="Midy"/>
<name first="Paul" last="Dick"/>
<name first="Robert" last="Weir"/>
<name first="Scott" last="Boag"/>
<name first="Shane" last="Curcuru"/>
</doc>
W/art: Failed to open zip archive '/system/framework/WfdCommon.jar': I/O Error

[ 01-29 10:05:49.406 14404:14404 W/ ]
Unable to open '/system/framework/com.qti.location.sdk.jar': No such file or directory
W/art: Failed to open zip archive '/system/framework/com.qti.location.sdk.jar': I/O Error

[ 01-29 10:05:49.429 14404:14404 W/ ]
Unable to open '/system/framework/WfdCommon.jar': No such file or directory
W/art: Failed to open zip archive '/system/framework/WfdCommon.jar': I/O Error

[ 01-29 10:05:49.429 14404:14404 W/ ]
Unable to open '/system/framework/com.qti.location.sdk.jar': No such file or directory
W/art: Failed to open zip archive '/system/framework/com.qti.location.sdk.jar': I/O Error

[ 01-29 10:05:49.430 14404:14404 W/ ]
Unable to open '/system/framework/WfdCommon.jar': No such file or directory
W/art: Failed to open zip archive '/system/framework/WfdCommon.jar': I/O Error

[ 01-29 10:05:49.430 14404:14404 W/ ]
Unable to open '/system/framework/com.qti.location.sdk.jar': No such file or directory
W/art: Failed to open zip archive '/system/framework/com.qti.location.sdk.jar': I/O Error

[ 01-29 10:05:49.438 14404:14404 W/ ]
Unable to open '/system/framework/WfdCommon.jar': No such file or directory
W/art: Failed to open zip archive '/system/framework/WfdCommon.jar': I/O Error

[ 01-29 10:05:49.438 14404:14404 W/ ]
Unable to open '/system/framework/com.qti.location.sdk.jar': No such file or directory
W/art: Failed to open zip archive '/system/framework/com.qti.location.sdk.jar': I/O Error

[ 01-29 10:05:49.445 14404:14404 W/ ]
Unable to open '/system/framework/WfdCommon.jar': No such file or directory
W/art: Failed to open zip archive '/system/framework/WfdCommon.jar': I/O Error

[ 01-29 10:05:49.445 14404:14404 W/ ]
Unable to open '/system/framework/com.qti.location.sdk.jar': No such file or directory
W/art: Failed to open zip archive '/system/framework/com.qti.location.sdk.jar': I/O Error
D/AndroidRuntime: Shutting down VM


--------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.scalixto.xalanjavaxslt, PID: 14404
java.lang.NoSuchMethodError: No static method lookUpFactoryClassName(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String; in class Lorg/apache/xalan/extensions/ObjectFactory; or its super classes (declaration of 'org.apache.xalan.extensions.ObjectFactory' appears in /system/framework/apache-xml.jar)
at org.apache.xalan.extensions.ExtensionHandlerGeneral.<clinit>(ExtensionHandlerGeneral.java:95)
at java.lang.reflect.Constructor.newInstance(Native Method)
at org.apache.xalan.extensions.ExtensionNamespaceSupport.launch(ExtensionNamespaceSupport.java:95)
at org.apache.xalan.extensions.ExtensionsTable.<init>(ExtensionsTable.java:66)
at org.apache.xalan.transformer.TransformerImpl.setExtensionsTable(TransformerImpl.java:387)
at org.apache.xalan.transformer.TransformerImpl.transformNode(TransformerImpl.java:1186)
at org.apache.xalan.transformer.TransformerImpl.transform(TransformerImpl.java:616)
at org.apache.xalan.transformer.TransformerImpl.transform(TransformerImpl.java:1147)
at org.apache.xalan.transformer.TransformerImpl.transform(TransformerImpl.java:1125)
at com.example.scalixto.xalanjavaxslt.MainActivity.transform(MainActivity.java:197)
at com.example.scalixto.xalanjavaxslt.MainActivity.onCreate(MainActivity.java:80)
at android.app.Activity.performCreate(Activity.java:6308)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.access$900(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)


On Android Studio

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
implementation files('libs/xalan.jar')
implementation files('libs/bsf.jar')
implementation files('libs/commons-logging-1.1.2.jar')
implementation files('libs/serializer.jar')
implementation files('libs/js-1.2.jar')
}

public String transform(String xml, String xsl) throws TransformerConfigurationException {
// Set the TransformerFactory system property.
// Note: For more flexibility, load properties from a properties file.

String key;
String value;

key = "javax.xml.transform.TransformerFactory";
//value = "org.apache.xalan.xsltc.trax.TransformerImpl";
value = "org.apache.xalan.xsltc.trax.SmartTransformerFactoryImpl";

Properties props = System.getProperties();
props.put(key, value);
//region 20170125
key = "org.apache.xalan.extensions.bsf.BSFManager";
value = "org.apache.bsf.BSFManager";
props.put(key, value);
//endregion

System.setProperties(props);
String ver = org.apache.xml.serializer.Version.getVersion();


//org.apache.xalan.extensions.bsf.BSFManager
//System.setProperty("org.apache.xalan.extensions.bsf.BSFManager","org.apache.bsf.BSFManager");
// force usage of Xalan trax implementation

TransformerFactory transformerFactory = null;

/*
try {
//org.apache.xalan.xsltc.trax.SmartTransformerFactoryImpl
transformerFactory = (TransformerFactory) Class.forName(value).newInstance();
//transformerFactory.setAttribute("enable-inlining", Boolean.TRUE);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
*/
transformerFactory = TransformerFactory.newInstance();


Transformer transformer = null;
try {
transformer = transformerFactory.newTransformer(new StreamSource(new StringReader(xsl)));
} catch (TransformerConfigurationException e) {
e.printStackTrace();
}
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");

//teste(transformer);

/*
//java.lang.ClassCastException: org.apache.xalan.transformer.TransformerImpl cannot be cast to org.apache.xalan.xsltc.trax.TransformerImpl
// transformer created by factory.newTransformer(...)
TransformerImpl xalanTransformer = (TransformerImpl)transformer;
StylesheetRoot sroot = new StylesheetRoot((ErrorListener)null);
// creates an empty Vector to be returned from getExtensions()
sroot.getExtensionNamespacesManager();
// make sure the workaround works in Marsmallow too
sroot.setSecureProcessing(false);
// sets up an empty ExtensionTable, so functionAvailable works without NPE
//xalanTransformer.setExtensionsTable(sroot); // default visibility
*/



StringWriter result = new StringWriter();
try {
transformer.transform(new StreamSource(new StringReader(xml)), new StreamResult(result));

//xalanTransformer.transform(new StreamSource(new StringReader(xml)), new StreamResult(result));
} catch (TransformerException e) {
e.printStackTrace();
}

return result.toString();
}


public String XalanTransform(String xml, String xsl){
//String htmlOutURI = "";
StringWriter result = new StringWriter();

// Instantiate the TransformerFactory, and use it along with a StreamSource
// XSL stylesheet to create a Transformer.
TransformerFactory tFactory = TransformerFactory.newInstance();
tFactory.setErrorListener(new ErrorListener() {
String ex;
@Override
public void warning(TransformerException e) throws TransformerException {
ex = e.getMessageAndLocation();
}

@Override
public void error(TransformerException e) throws TransformerException {
ex = e.getMessageAndLocation();
}

@Override
public void fatalError(TransformerException e) throws TransformerException {
ex = e.getMessageAndLocation();
}
});
Transformer transformer = null;
try {
//transformer = tFactory.newTransformer(new StreamSource(xsl));
transformer = tFactory.newTransformer(new StreamSource(new StringReader(xsl)));
transformer.setErrorListener(new ErrorListener() {
String ex;
@Override
public void warning(TransformerException e) throws TransformerException {
ex = e.getMessageAndLocation();
}

public void error(TransformerException e) throws TransformerException {
ex = e.getMessageAndLocation();
}

@Override
public void fatalError(TransformerException e) throws TransformerException {
ex = e.getMessageAndLocation();
}
});
} catch (TransformerConfigurationException e) {
e.printStackTrace();
}
// Perform the transformation from a StreamSource to a StreamResult;
try {
//transformer.transform(new StreamSource(xml), new StreamResult(result));
transformer.transform(new StreamSource(new StringReader(xml)), new StreamResult(result));
} catch (TransformerException e) {
e.printStackTrace();
}
return result.toString();
}
adrian
Posts: 2453

Re: Xalan 2.7.2 on Android Studio

Tue Jan 30, 2018 2:49 pm

Hi,

Not familiar with Android studio development, but the NoSuchMethodError indicates the class org/apache/xalan/extensions/ObjectFactory is loaded from /system/framework/apache-xml.jar, not from 'libs/xalan.jar' like you are probably expecting.

Code: Select all

java.lang.NoSuchMethodError: No static method lookUpFactoryClassName(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String; in class Lorg/apache/xalan/extensions/ObjectFactory; or its super classes (declaration of 'org.apache.xalan.extensions.ObjectFactory' appears in /system/framework/apache-xml.jar)

Not sure how you can change the order of libraries/jars for Android Studio, but you should try moving libs/xalan.jar at the top (first in order).

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
SergioC
Posts: 2

Re: Xalan 2.7.2 on Android Studio

Thu Feb 01, 2018 8:17 pm

Adrian,

I change the order of libraries/jars. but it still gives me the same error.

Thanks for the suggestion.

Return to “XSLT and FOP”

Who is online

Users browsing this forum: No registered users and 0 guests