Hi Rainer,
Rainer Hahnekamp wrote:
Hi Jürgen,
thanks for your answer. According to my version of the developer's guide
there is no need for a function named _getComponentFactory. Maybe I
misunderstood something here...
no you didn't misunderstood the guide. _getComponentFactory is the newer
suggested way to go because it uses a component context. For creating a
new instance of a service you should use XMultiCompopnentFactory instead
of XMultiServiceFactory. But the old __getServiceFactory should work
also as well as XMultiServiceFactory. This is documented in the latest
version of the DevGuide which of course is not online until now. But i
will update it after my vacation.
But you have two errors in your code, see below ...
For a better understanding I'm posting my 3 classes. As you can see
there is no "real" language implemented. That's because I just want to
try if I can get that thing working.
---------------------------
import com.sun.star.uno.Type;
import com.sun.star.uno.Any;
import com.sun.star.lang.IllegalArgumentException;
import com.sun.star.lang.WrappedTargetException;
import com.sun.star.reflection.InvocationTargetException;
import com.sun.star.script.CannotConvertException;
import com.sun.star.script.provider.XScriptContext;
import com.sun.star.script.provider.XScript;
import com.sun.star.script.provider.ScriptFrameworkErrorException;
import com.sun.star.script.provider.ScriptFrameworkErrorType;
import com.sun.star.script.framework.provider.ClassLoaderFactory;
import com.sun.star.script.framework.container.ScriptMetaData;
public class JavaScript implements XScript {
private XScriptContext xScriptContext;
private ScriptMetaData scriptMetaData;
public JavaScript(XScriptContext xsc, ScriptMetaData smd) {
this.xScriptContext = xsc;
this.scriptMetaData = smd;
}
public Object invoke(Object[] aParams, short[][] aOutParamIndex,
Object[][] aOutParam) throws
com.sun.star.script.provider.ScriptFrameworkErrorException,
com.sun.star.reflection.InvocationTargetException {
aOutParamIndex[0] = new short[0];
aOutParam[0] = new Object[0];
ClassLoader cl = null;
try {
cl =
ClassLoaderFactory.getURLClassLoader(scriptMetaData);
}
catch (java.lang.Exception e) {
throw new ScriptFrameworkErrorException(e.getMessage(),
null, scriptMetaData.getLanguageName(), scriptMetaData.getLanguage(),
ScriptFrameworkErrorType.UNKNOWN);
}
scriptMetaData.loadSource();
String source = scriptMetaData.getSource();
Any result = null;
if (result == null) {
return new Any(new Type(), null);
}
else {
return result;
}
}
}
---------------------------
import com.sun.star.script.framework.provider.ScriptEditor;
import com.sun.star.script.provider.XScriptContext;
import com.sun.star.script.framework.container.ScriptMetaData;
import javax.swing.*;
public class ScriptEditorForJava implements ScriptEditor {
public Object execute() throws Exception {
return null;
}
public void indicateErrorLine(int lineNum) {
return;
}
public void edit(XScriptContext context, ScriptMetaData entry) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JTextArea ts = new JTextArea();
entry.loadSource();
ts.setText(entry.getSource());
frame.getContentPane().add(ts);
frame.setSize(400, 400);
frame.show();
}
public String getTemplate() {
return "the code for Java script";
}
public String getExtension() {
return "y1";
}
}
---------------------------
import com.sun.star.uno.XComponentContext;
import com.sun.star.lang.XMultiServiceFactory;
import com.sun.star.lang.XSingleServiceFactory;
import com.sun.star.registry.XRegistryKey;
import com.sun.star.comp.loader.FactoryHelper;
import com.sun.star.lang.XServiceInfo;
import com.sun.star.lang.XInitialization;
import com.sun.star.script.provider.ScriptFrameworkErrorException;
import com.sun.star.script.provider.XScriptContext;
import com.sun.star.script.provider.XScript;
import com.sun.star.script.framework.provider.ScriptProvider;
import com.sun.star.script.framework.provider.ScriptEditor;
import com.sun.star.script.framework.container.ScriptMetaData;
public class ScriptProviderForJava {
public static class _ScriptProviderForJava extends ScriptProvider {
public _ScriptProviderForJava(XComponentContext ctx) {
super(ctx, "Java");
}
public XScript getScript(String scriptURI) throws
com.sun.star.uno.RuntimeException, ScriptFrameworkErrorException {
JavaScript script = null;
try {
ScriptMetaData scriptMetaData =
getScriptData(scriptURI);
XScriptContext xScriptContext = getScriptingContext();
script = new JavaScript(xScriptContext, scriptMetaData);
}
catch (com.sun.star.uno.Exception e) {
System.err.println("Failed to get script: " +
scriptURI);
}
return script;
}
public boolean hasScriptEditor() {
return true;
}
public ScriptEditor getScriptEditor() {
return new ScriptEditorForJava();
}
}
public static XSingleServiceFactory _getServiceFactory(String implName,
XMultiServiceFactory multiFactory, XRegistryKey regKey) {
it should be __getServiceFactory with two underscores
XSingleServiceFactory xSingleServiceFactory = null;
if(implName.equals(ScriptProviderForJava._ScriptProviderForJava.class.getName()))
{
xSingleServiceFactory =
FactoryHelper.getServiceFactory(ScriptProviderForJava._ScriptProviderForJava.class,
"com.sun.star.script.provider.ScriptProviderForJava", multiFactory, regKey);
}
return xSingleServiceFactory;
}
public static boolean __writeRegistryServiceInfo(XRegistryKey regKey) {
String impl = "ScriptProviderForJava$_ScriptProvderForJava";
a type in the string, better you use
String impl = _ScriptProvderForJava.class.geTName();
That has the advantage that it works for Java 1.5 as well because Java
has changed the class format in Java 1.5 and inner classes have changed.
Correct this twi erros and it shoudl work. I've tried to register at
least the new built jar file and it worked fine.
Juergen
String service1 = "com.sun.star.script.provider.ScriptProvider";
String service2 = "com.sun.star.script.provider.LanguageScriptProvider";
String service3= "com.sun.star.script.provider.JavaScriptProvider";
FactoryHelper.writeRegistryServiceInfo(impl, service1, regKey);
FactoryHelper.writeRegistryServiceInfo(impl, service2, regKey);
FactoryHelper.writeRegistryServiceInfo(impl, service3, regKey);
return true;
}
}
Greeting,-
Rainer Hahnekamp
Jürgen Schmidt:
Hi Rainer,
Rainer Hahnekamp wrote:
Hello,
I've tried to write my own ScriptProvider component in Java according to
the Developer's Guide instructions in chapter 18.5. I managed to compile
all four classes and packed them into a jar file called
ScriptProviderForJava.jar. Further more I appended the subdirectory
META-INF which has just one file called MANIFEST.MF with following two
rows:
Built-By: Rainer Hahnekamp
RegistrationClassName: ScriptProviderForJava
Then I packed this jar-file into a zip file named
ScriptProviderForJava.uno.pkg including a the subfolder META-INF and the
file manifest.xml with following content:
<manifest:file-entry manifest:media-type="application/vnd.sun.star.uno-component;type=Java"
manifest:full-path="ScriptProviderForJava.jar"/>
Finally I added the package via
unopkg add ScriptProviderForJava.uno.pkg
Unfortunately it did not succeed. Still in OpenOffice.org under
Tools->Package Manager... it is listed as disabled. When I want to
enable it I get the error message:
Enable: Cannot determine registration class.
If the registration class is the entry in MANIFEST.MF OpenOffice should
be able to find it, since there exists a class called
ScriptProviderForJava.
Are you sure that ScriptProviderForJava is the correct name of your
implemention class and that this class does implement the necessary
component helper funtions (__getComponentFactory,
__writeRegistryServiceInfo)? If your file is defined in a package you
have to use the full qualified class name. All dependencies to external
Java files have to be resolved. Either by extending the office class
path manually or or you put all dependecies in the package as well. A
further solution is to provide an own classlaoder for external classes
and load them explicitly from your package.
Juergen
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]