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/libreoffice.jar"),
49 new URL(officeURL
+ "/program/classes/java_uno.jar")
51 m_loader
= new CustomURLClassLoader(arURL
);
52 File fileProg
= new File(s
+ "/program");
53 m_loader
.addResourcePath(fileProg
.toURI().toURL());
55 } catch (MalformedURLException e
) {
63 Class
<?
> beanClass
= m_loader
.loadClass("com.sun.star.comp.beans.OOoBean");
64 m_objBean
= beanClass
.newInstance();
65 setLayout(new BorderLayout());
66 add((java
.awt
.Container
)m_objBean
, BorderLayout
.CENTER
);
68 //this does not work here. Why?
69 // Class arPropValClass = m_loader.loadClass("[Lcom.sun.star.beans.PropertyValue;");
70 Object arProp
= Array
.newInstance(
71 m_loader
.loadClass("com.sun.star.beans.PropertyValue"), 1);
73 Method methLoad
= beanClass
.getMethod(
74 "loadFromURL", new Class
[] {
75 String
.class, arProp
.getClass() });
77 methLoad
.invoke(m_objBean
, new Object
[] {"private:factory/swriter", null});
78 } catch (ClassNotFoundException e
) {
80 } catch (InstantiationException e
) {
82 } catch (IllegalAccessException e
) {
84 } catch (ClassCastException e
) {
86 } catch (java
.lang
.reflect
.InvocationTargetException e
) {
88 } catch (java
.lang
.NoSuchMethodException e
) {
89 e
.printStackTrace(); }
99 Method methStop
= m_objBean
.getClass().getMethod(
100 "stopOOoConnection", new Class
[0]);
101 methStop
.invoke(m_objBean
, (Object
[]) null);
102 } catch (java
.lang
.NoSuchMethodException e
) {
104 } catch (java
.lang
.IllegalAccessException e
) {
107 catch (java
.lang
.reflect
.InvocationTargetException e
) {
114 public void destroy() {
118 public void paint(Graphics g
) {
123 final class CustomURLClassLoader
extends URLClassLoader
{
125 private ArrayList
<URL
> resourcePaths
;
127 public CustomURLClassLoader( URL
[] urls
) {
132 protected Class
<?
> findClass( String name
) throws ClassNotFoundException
{
133 // This is only called via this.loadClass -> super.loadClass ->
134 // this.findClass, after this.loadClass has already called
135 // super.findClass, so no need to call super.findClass again:
136 throw new ClassNotFoundException( name
);
142 protected synchronized Class
<?
> loadClass( String name
, boolean resolve
)
143 throws ClassNotFoundException
145 Class c
= findLoadedClass( name
);
148 c
= super.findClass( name
);
149 } catch ( ClassNotFoundException e
) {
150 return super.loadClass( name
, resolve
);
151 } catch ( SecurityException e
) {
152 // A SecurityException "Prohibited package name: java.lang"
153 // may occur when the user added the JVM's rt.jar to the
155 return super.loadClass( name
, resolve
);
164 public void addResourcePath(URL rurl
) {
165 if (resourcePaths
== null) resourcePaths
= new ArrayList
<URL
>();
166 resourcePaths
.add(rurl
);
170 public URL
getResource(String name
) {
171 if (resourcePaths
== null) return null;
173 URL result
= super.getResource(name
);
174 if (result
!= null) {
178 for (URL u
: resourcePaths
) {
179 if (u
.getProtocol().startsWith("file")){
181 File f1
= new File(u
.getPath());
182 File f2
= new File(f1
, name
);
184 return new URL(f2
.toURI().toASCIIString());
186 } catch (MalformedURLException e1
) {
187 System
.err
.println("malformed url: "+e1
.getMessage());