[Libreoffice-commits] core.git: scripting/java
Stephan Bergmann (via logerrit)
logerrit at kemper.freedesktop.org
Wed Mar 31 20:13:45 UTC 2021
scripting/java/com/sun/star/script/framework/container/XMLParserFactory.java | 101 ----------
1 file changed, 7 insertions(+), 94 deletions(-)
New commits:
commit 5707ec3303f8215af91aac7d7f7cc29bf67b6c99
Author: Stephan Bergmann <sbergman at redhat.com>
AuthorDate: Wed Mar 31 16:30:23 2021 +0200
Commit: Stephan Bergmann <sbergman at redhat.com>
CommitDate: Wed Mar 31 22:13:08 2021 +0200
Use javax.xml.transform instead of reflection to write a Document
At least on Fedora 34 when building with --with-jdk-home=/usr/lib/jvm/java-16
against java-latest-openjdk-headless-16.0.0.0.36-1.rolling.fc34.x86_64,
CustomTarget_odk/build-examples_java failed with
> ERROR: Exception occurred: An error occurred while enabling: SayHello .../desktop/source/deployment/registry/dp_backend.cxx:645
>
> ERROR: unopkg failed.
>
> make[2]: *** [Makefile:92: .../workdir/CustomTarget/odk/build-examples_java/out/sdk/LINUXexample.out/misc/ScriptingFramework/SayHello/devguide_scriptingframework_SayHello_register_scriptpkg.flag] Error 1
because of
> info:bridges:1209707:1209707:bridges/source/jni_uno/jni_uno2java.cxx:117: exception occurred uno->java: [com.sun.star.lang.WrappedTargetException] java.io.IOException
> java stack trace:
> com.sun.star.lang.WrappedTargetException: java.io.IOException
> at com.sun.star.script.framework.container.UnoPkgContainer.writeUnoPackageDB(UnoPkgContainer.java:279)
> at com.sun.star.script.framework.container.UnoPkgContainer.processUnoPackage(UnoPkgContainer.java:330)
> at com.sun.star.script.framework.provider.ScriptProvider.insertByName(ScriptProvider.java:563)
> Caused by: java.io.IOException
> at com.sun.star.script.framework.container.XMLParserFactory$DefaultParser.write(XMLParserFactory.java:190)
> at com.sun.star.script.framework.container.DeployedUnoPackagesDB.write(DeployedUnoPackagesDB.java:107)
> at com.sun.star.script.framework.container.UnoPkgContainer.writeUnoPackageDB(UnoPkgContainer.java:270)
> ... 2 more
> Caused by: java.lang.IllegalAccessException: class com.sun.star.script.framework.container.XMLParserFactory$DefaultParser cannot access class com.sun.org.apache.xml.internal.serialize.XMLSerializer (in module java.xml) because module java.xml does not export com.sun.org.apache.xml.internal.serialize to unnamed module @50860e85
> at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:385)
> at java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:687)
> at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:489)
> at java.base/java.lang.reflect.ReflectAccess.newInstance(ReflectAccess.java:128)
> at java.base/jdk.internal.reflect.ReflectionFactory.newInstance(ReflectionFactory.java:350)
> at java.base/java.lang.Class.newInstance(Class.java:642)
> at com.sun.star.script.framework.container.XMLParserFactory$DefaultParser.write(XMLParserFactory.java:145)
> ... 4 more
The javax.xml.transform functionality appears to be available since Java 1.4, so
it should not be a problem if we unconditionally use it.
Change-Id: Idc31f8f6fb092b6603c537414497d24aec886ce7
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/113421
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <sbergman at redhat.com>
diff --git a/scripting/java/com/sun/star/script/framework/container/XMLParserFactory.java b/scripting/java/com/sun/star/script/framework/container/XMLParserFactory.java
index c34908f8c773..02c9e6c34244 100644
--- a/scripting/java/com/sun/star/script/framework/container/XMLParserFactory.java
+++ b/scripting/java/com/sun/star/script/framework/container/XMLParserFactory.java
@@ -22,11 +22,13 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
-import java.lang.reflect.Method;
-
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
@@ -91,99 +93,10 @@ public class XMLParserFactory {
}
public void write(Document doc, OutputStream out) throws IOException {
-
- Class<?> clazz = doc.getClass();
- String name = clazz.getName();
-
- // depending on the class of the Document object use introspection
- // to invoke the appropriate methods for writing the XML
- // this code is based on the code used by the NetBeans
- // class XMLUtilImpl in the openide module
try {
- if (name.equals("com.sun.xml.tree.XmlDocument") ||
- name.equals("org.apache.crimson.tree.XmlDocument")) {
-
- // these DOM implementations are self writing
- Method write;
-
- write = clazz.getDeclaredMethod("write",
- new Class[] {OutputStream.class});
-
- write.invoke(doc, new Object[] {out});
- } else {
- // try xerces serialize package using introspection
- ClassLoader cl = this.getClass().getClassLoader();
-
- Class<?> serializerClass = null;
- Class<?> formatterClass = null;
-
- try {
-
- serializerClass =
- Class.forName("org.apache.xml.serialize.XMLSerializer",
- true, cl);
-
- formatterClass =
- Class.forName("org.apache.xml.serialize.OutputFormat",
- true, cl);
-
- } catch (ClassNotFoundException cnfe) {
- String prefix = "com.sun.org.apache.xml.internal.";
-
- serializerClass =
- Class.forName(prefix + "serialize.XMLSerializer",
- true, cl);
-
- formatterClass =
- Class.forName(prefix + "serialize.OutputFormat",
- true, cl);
- }
-
- Object serializerObject = serializerClass.newInstance();
- Object formatterObject = formatterClass.newInstance();
-
- // improve output readability using the OutputFormat class
- Method method =
- formatterClass.getMethod("setMethod",
- new Class[] {String.class});
-
- method.invoke(formatterObject, new Object[] {"xml"});
-
- method = formatterClass.getMethod("setIndenting",
- new Class[] {Boolean.TYPE});
-
- method.invoke(formatterObject, new Object[] {Boolean.TRUE});
-
- // now set up an instance of XMLSerializer with our
- // OutputStream and serialize our Document
- method = serializerClass.getMethod("setOutputByteStream",
- new Class[] {OutputStream.class});
-
- method.invoke(serializerObject, new Object[] {out});
-
- method = serializerClass.getMethod("setOutputFormat",
- new Class[] {formatterClass});
-
- method.invoke(serializerObject, new Object[] {formatterObject});
-
- method = serializerClass.getMethod("asDOMSerializer", new Class[0]);
-
- Object impl = method.invoke(serializerObject, new Object[0]);
-
- method = impl.getClass().getMethod("serialize",
- new Class[] {Document.class});
-
- method.invoke(impl, new Object[] {doc});
- }
- } catch (NoSuchMethodException ex1) {
- IOException ex2 = new IOException();
- ex2.initCause(ex1);
- throw ex2;
- } catch (ClassNotFoundException ex1) {
- IOException ex2 = new IOException();
- ex2.initCause(ex1);
- throw ex2;
- } catch (Exception ex1) {
+ TransformerFactory.newInstance().newTransformer().transform(
+ new DOMSource(doc), new StreamResult(out));
+ } catch (TransformerException ex1) {
IOException ex2 = new IOException();
ex2.initCause(ex1);
throw ex2;
More information about the Libreoffice-commits
mailing list