2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 import java
.lang
.reflect
.Method
;
21 import java
.lang
.reflect
.Array
;
25 import java
.applet
.Applet
;
26 import java
.awt
.Graphics
;
29 public class OOoViewer
extends Applet
{
31 private static CustomURLClassLoader m_loader
;
38 if (m_loader
== null) {
39 String s
= getParameter("office");
40 System
.out
.println("sun.awt.noxembed: " + System
.getProperty("sun.awt.noxembed"));
41 System
.setProperty("sun.awt.xembedserver", "true");
44 URL url
= f
.toURI().toURL();
45 String officeURL
= url
.toString();
46 URL
[] arURL
= new URL
[] {
47 new URL(officeURL
+ "/program/classes/officebean.jar"),
48 new URL(officeURL
+ "/program/classes/jurt.jar"),
49 new URL(officeURL
+ "/program/classes/ridl.jar"),
50 new URL(officeURL
+ "/program/classes/unoil.jar"),
51 new URL(officeURL
+ "/program/classes/java_uno.jar"),
52 new URL(officeURL
+ "/program/classes/juh.jar")
54 m_loader
= new CustomURLClassLoader(arURL
);
55 File fileProg
= new File(s
+ "/program");
56 m_loader
.addResourcePath(fileProg
.toURI().toURL());
58 } catch (MalformedURLException e
) {
66 Class
<?
> beanClass
= m_loader
.loadClass("com.sun.star.comp.beans.OOoBean");
67 m_objBean
= beanClass
.newInstance();
68 setLayout(new BorderLayout());
69 add((java
.awt
.Container
)m_objBean
, BorderLayout
.CENTER
);
71 //this does not work here. Why?
72 // Class arPropValClass = m_loader.loadClass("[Lcom.sun.star.beans.PropertyValue;");
73 Object arProp
= Array
.newInstance(
74 m_loader
.loadClass("com.sun.star.beans.PropertyValue"), 1);
76 Method methLoad
= beanClass
.getMethod(
77 "loadFromURL", new Class
[] {
78 String
.class, arProp
.getClass() });
80 methLoad
.invoke(m_objBean
, new Object
[] {"private:factory/swriter", null});
81 } catch (ClassNotFoundException e
) {
83 } catch (InstantiationException e
) {
85 } catch (IllegalAccessException e
) {
87 } catch (ClassCastException e
) {
89 } catch (java
.lang
.reflect
.InvocationTargetException e
) {
91 } catch (java
.lang
.NoSuchMethodException e
) {
92 e
.printStackTrace(); }
102 Method methStop
= m_objBean
.getClass().getMethod(
103 "stopOOoConnection", new Class
[0]);
104 methStop
.invoke(m_objBean
, (Object
[]) null);
105 } catch (java
.lang
.NoSuchMethodException e
) {
107 } catch (java
.lang
.IllegalAccessException e
) {
110 catch (java
.lang
.reflect
.InvocationTargetException e
) {
117 public void destroy() {
121 public void paint(Graphics g
) {
126 final class CustomURLClassLoader
extends URLClassLoader
{
128 private ArrayList
<URL
> resourcePaths
;
130 public CustomURLClassLoader( URL
[] urls
) {
135 protected Class
<?
> findClass( String name
) throws ClassNotFoundException
{
136 // This is only called via this.loadClass -> super.loadClass ->
137 // this.findClass, after this.loadClass has already called
138 // super.findClass, so no need to call super.findClass again:
139 throw new ClassNotFoundException( name
);
145 protected synchronized Class
<?
> loadClass( String name
, boolean resolve
)
146 throws ClassNotFoundException
148 Class c
= findLoadedClass( name
);
151 c
= super.findClass( name
);
152 } catch ( ClassNotFoundException e
) {
153 return super.loadClass( name
, resolve
);
154 } catch ( SecurityException e
) {
155 // A SecurityException "Prohibited package name: java.lang"
156 // may occur when the user added the JVM's rt.jar to the
158 return super.loadClass( name
, resolve
);
167 public void addResourcePath(URL rurl
) {
168 if (resourcePaths
== null) resourcePaths
= new ArrayList
<URL
>();
169 resourcePaths
.add(rurl
);
173 public URL
getResource(String name
) {
174 if (resourcePaths
== null) return null;
176 URL result
= super.getResource(name
);
177 if (result
!= null) {
181 for (URL u
: resourcePaths
) {
182 if (u
.getProtocol().startsWith("file")){
184 File f1
= new File(u
.getPath());
185 File f2
= new File(f1
, name
);
187 return new URL(f2
.toURI().toASCIIString());
189 } catch (MalformedURLException e1
) {
190 System
.err
.println("malformed url: "+e1
.getMessage());