commit e4c9a6d4ba24c3d965dc3f0ebd4ce310fe673837
parent 4ff0f38bc4eab44cd09494a55921c84c83935ff9
Author: Tomas Hlavaty <tom@logand.com>
Date: Sun, 22 Jan 2012 23:12:13 +0100
2java pr_service implemented
Diffstat:
M | unoidl2java.c | | | 52 | +++++++++++++++++++++++++++++++++++++++++++--------- |
1 file changed, 43 insertions(+), 9 deletions(-)
diff --git a/unoidl2java.c b/unoidl2java.c
@@ -525,14 +525,48 @@ static void pr_arg(Any x) {
static void pr_service(Any x) {
Any name = cadr(x);
Any published = caddr(x);
- Any super = cadddr(x);
- Any body = caddddr(x);
- pr_package(name);
- pr("public class "); pp(last(name));
- if(!null(super)) {pr(" extends "); pp(super);}
- pl(" { // service");
- pp_list(body, "");
- pl("}");
+ if(NIL != published) {
+ Any super = cadddr(x); // interface service
+ Any body = caddddr(x); // accumulated service
+ pr_package(name);
+ pl("import com.sun.star.uno.XComponentContext;");
+ pl("import com.sun.star.uno.DeploymentException;");
+ pl("import com.sun.star.uno.TypeClass;");
+ pl("import com.sun.star.uno.Type;");
+ pl("import com.sun.star.uno.UnoRuntime;");
+ pl("import com.sun.star.lang.XMultiComponentFactory;");
+ pr("public final class "); pp(last(name));
+ pl(" { // service");
+ if(NIL != super && NIL == body) { // implicit constructor
+ Any s = super;
+ if(RELATIVE == kind(car(s)) && NIL == cddr(s))
+ s = cons(car(s), reverse(module, cdr(s)));
+ pr(" private "); pp(last(name)); pl("() {}");
+ pr(" public static "); pp(super); pl(" create(XComponentContext c) { // interface service");
+ pr(" "); pp(super); pl(" x = null;");
+ pl(" try {");
+ pl(" XMultiComponentFactory f = $getFactory(c);");
+ pr(" x = ("); pp(s); pr(") $castInstance(f.createInstanceWithContext(\""); pr_module(module, ".", 1); pp(name); pl("\", c), c);");
+ pl(" } catch(Exception e) {");
+ pr(" throw new DeploymentException(\"component context fails to supply "); pr_module(module, ".", 1); pp(name); pr(" of type "); pp(s); pl(": \".concat(e.toString()), c);");
+ pl(" }");
+ pl(" return x;");
+ pl(" }");
+ pl(" private static XMultiComponentFactory $getFactory(XComponentContext c) {");
+ pl(" XMultiComponentFactory f = c.getServiceManager();");
+ pl(" if(f == null)");
+ pl(" throw new DeploymentException(\"component context fails to supply service manager\", c);");
+ pl(" return f;");
+ pl(" }");
+ pl(" private static Object $castInstance(Object o, XComponentContext c) {");
+ pr(" java.lang.Object o2 = UnoRuntime.queryInterface(new Type(\""); pp(s); pl("\", TypeClass.INTERFACE), o);");
+ pl(" if(o2 == null)");
+ pr(" throw new DeploymentException(\"component context fails to supply service "); pr_module(module, ".", 1); pp(name); pr(" of type "); pp(s); pl("\", c);");
+ pl(" return o2;");
+ pl(" }");
+ }
+ pl("}");
+ }
}
static void pr_singleton(Any x) {
@@ -717,7 +751,7 @@ static void pp(Any x) {
case SEQUENCE: pp(cadr(x)); pr(seq0 ? "[0]" : "[]"); break;
case ATTRIBUTE: pr_attribute(x); break;
case EXCEPTION: pr_exception(x); break;
- //case SERVICE: pr_service(x); break;
+ case SERVICE: pr_service(x); break;
case PROPERTY: pr_property(x); break;
case SINGLETON: pr_singleton(x); break;
case TYPEDEF: pr_typedef(x); break;