1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir package basicrunner; 28*cdf0e10cSrcweir 29*cdf0e10cSrcweir 30*cdf0e10cSrcweir import com.sun.star.beans.PropertyValue; 31*cdf0e10cSrcweir import com.sun.star.beans.XPropertySet; 32*cdf0e10cSrcweir import com.sun.star.connection.ConnectionSetupException; 33*cdf0e10cSrcweir import com.sun.star.container.ContainerEvent; 34*cdf0e10cSrcweir import com.sun.star.container.XContainer; 35*cdf0e10cSrcweir import com.sun.star.container.XContainerListener; 36*cdf0e10cSrcweir import com.sun.star.container.XNameContainer; 37*cdf0e10cSrcweir import com.sun.star.frame.XComponentLoader; 38*cdf0e10cSrcweir import com.sun.star.frame.XDesktop; 39*cdf0e10cSrcweir import com.sun.star.lang.WrappedTargetException; 40*cdf0e10cSrcweir import com.sun.star.lang.XComponent; 41*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory; 42*cdf0e10cSrcweir import com.sun.star.lang.XServiceInfo; 43*cdf0e10cSrcweir import com.sun.star.lang.XSingleServiceFactory; 44*cdf0e10cSrcweir import com.sun.star.lang.XTypeProvider; 45*cdf0e10cSrcweir import com.sun.star.uno.Type; 46*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 47*cdf0e10cSrcweir import com.sun.star.util.XChangesBatch; 48*cdf0e10cSrcweir import java.util.Hashtable; 49*cdf0e10cSrcweir import lib.TestParameters; 50*cdf0e10cSrcweir 51*cdf0e10cSrcweir import share.LogWriter; 52*cdf0e10cSrcweir 53*cdf0e10cSrcweir 54*cdf0e10cSrcweir /** 55*cdf0e10cSrcweir * This class is a java-part of BASIC-java interaction "driver" 56*cdf0e10cSrcweir * It is used to call Star-Basic's function from java using 57*cdf0e10cSrcweir * basic's part of "driver" where listeners are implemented. 58*cdf0e10cSrcweir * The instance of the BasicHandler should be added to the MSF that will be 59*cdf0e10cSrcweir * used for loading BASIC's part of "driver".<br> 60*cdf0e10cSrcweir * After opening basic's document it creates an instance of the 61*cdf0e10cSrcweir * HandlerContainer using BasicHandler. HandlerContainer is a UNO 62*cdf0e10cSrcweir * XContainer and XNameContainer. 63*cdf0e10cSrcweir * Only one instance of BasicHandler can be used at the moment. 64*cdf0e10cSrcweir * @see com.sun.star.lang.XServiceInfo 65*cdf0e10cSrcweir * @see com.sun.star.lang.XSingleServiceFactory 66*cdf0e10cSrcweir */ 67*cdf0e10cSrcweir public class BasicHandler implements XServiceInfo, XSingleServiceFactory { 68*cdf0e10cSrcweir /** 69*cdf0e10cSrcweir * serviceName is the name of service that can be created in BASIC. 70*cdf0e10cSrcweir */ 71*cdf0e10cSrcweir static final String serviceName = 72*cdf0e10cSrcweir "com.sun.star.jsuite.basicrunner.BasicHandler"; 73*cdf0e10cSrcweir 74*cdf0e10cSrcweir /** 75*cdf0e10cSrcweir * <code>container</code> is a SHARED variable (between BASIC and Java). 76*cdf0e10cSrcweir * It is used for interacting. 77*cdf0e10cSrcweir */ 78*cdf0e10cSrcweir static private HandlerContainer container = null; 79*cdf0e10cSrcweir 80*cdf0e10cSrcweir /** 81*cdf0e10cSrcweir * Contains a writer to log an information about the interface testing, to 82*cdf0e10cSrcweir * allows for tests to access it. 83*cdf0e10cSrcweir */ 84*cdf0e10cSrcweir static private LogWriter log; 85*cdf0e10cSrcweir 86*cdf0e10cSrcweir /** 87*cdf0e10cSrcweir * <code>oHandlerDoc</code> is a referrence to BASIC's document. 88*cdf0e10cSrcweir */ 89*cdf0e10cSrcweir static private XComponent oHandlerDoc = null; 90*cdf0e10cSrcweir 91*cdf0e10cSrcweir /** 92*cdf0e10cSrcweir * <code>xMSF</code> is a MultiServiceFactory currently used by 93*cdf0e10cSrcweir * BasicHandler. 94*cdf0e10cSrcweir */ 95*cdf0e10cSrcweir static private XMultiServiceFactory xMSF = null; 96*cdf0e10cSrcweir 97*cdf0e10cSrcweir /** 98*cdf0e10cSrcweir * Interface being tested now. 99*cdf0e10cSrcweir */ 100*cdf0e10cSrcweir static private BasicIfcTest TestedInterface = null; 101*cdf0e10cSrcweir 102*cdf0e10cSrcweir /** 103*cdf0e10cSrcweir * Ab enhanced scheme of timeouts can be used with BASIC tests. 104*cdf0e10cSrcweir * A small timeout can be used zo wait for changes in the test status. 105*cdf0e10cSrcweir * <code>respFlag</code> is set to <code>true</code> when a BASIC test 106*cdf0e10cSrcweir * writes any log information. 107*cdf0e10cSrcweir */ 108*cdf0e10cSrcweir static private boolean respFlag = false; 109*cdf0e10cSrcweir 110*cdf0e10cSrcweir /** 111*cdf0e10cSrcweir * <code>iBasicTimeout</code> is the amount of milliseconds that 112*cdf0e10cSrcweir * the BasicHandler will wait for a response from tests 113*cdf0e10cSrcweir * (finish to execute a method or add log information) 114*cdf0e10cSrcweir * before it decides that SOffice is dead. 115*cdf0e10cSrcweir */ 116*cdf0e10cSrcweir static private int iBasicTimeout = 10000; 117*cdf0e10cSrcweir 118*cdf0e10cSrcweir 119*cdf0e10cSrcweir 120*cdf0e10cSrcweir /** 121*cdf0e10cSrcweir * Creates an instance of a HandlerContainer. This instance is used from 122*cdf0e10cSrcweir * BASIC. 123*cdf0e10cSrcweir * @param tParam The test parameters. 124*cdf0e10cSrcweir */ 125*cdf0e10cSrcweir public BasicHandler(TestParameters tParam) { 126*cdf0e10cSrcweir if (tParam.get("soapi.test.basic.debugFile") != null) { 127*cdf0e10cSrcweir iBasicTimeout = 0; // Debug mode. 128*cdf0e10cSrcweir } 129*cdf0e10cSrcweir container = new HandlerContainer(this); 130*cdf0e10cSrcweir } 131*cdf0e10cSrcweir 132*cdf0e10cSrcweir /** 133*cdf0e10cSrcweir * Set the tested interface and a log writer. 134*cdf0e10cSrcweir * @param ifc The test of an interface 135*cdf0e10cSrcweir * @param log A log writer. 136*cdf0e10cSrcweir */ 137*cdf0e10cSrcweir public void setTestedInterface(BasicIfcTest ifc, LogWriter log) { 138*cdf0e10cSrcweir this.log = log; 139*cdf0e10cSrcweir TestedInterface = ifc; 140*cdf0e10cSrcweir } 141*cdf0e10cSrcweir 142*cdf0e10cSrcweir /** 143*cdf0e10cSrcweir * Is called when BASIC signals that it has performed the test of a method. 144*cdf0e10cSrcweir * @param methodName The name of the method. 145*cdf0e10cSrcweir * @bResult The result of the test. 146*cdf0e10cSrcweir */ 147*cdf0e10cSrcweir synchronized void methodTested(String methodName, boolean bResult) { 148*cdf0e10cSrcweir respFlag = true; 149*cdf0e10cSrcweir TestedInterface.methodTested(methodName, bResult); 150*cdf0e10cSrcweir notify() ; 151*cdf0e10cSrcweir } 152*cdf0e10cSrcweir 153*cdf0e10cSrcweir /** 154*cdf0e10cSrcweir * Is called when BASIC sends a signal to write some log information. 155*cdf0e10cSrcweir * @param info The string to write. 156*cdf0e10cSrcweir */ 157*cdf0e10cSrcweir synchronized public void Log(String info) { 158*cdf0e10cSrcweir respFlag = true; 159*cdf0e10cSrcweir log.println(info); 160*cdf0e10cSrcweir notify() ; 161*cdf0e10cSrcweir } 162*cdf0e10cSrcweir 163*cdf0e10cSrcweir /** 164*cdf0e10cSrcweir * Is called by BasicIfcTest to find out if this BasicHandler uses the 165*cdf0e10cSrcweir * correct MultiServiceFactory. 166*cdf0e10cSrcweir * @param xMSF The MultiServiceFactory 167*cdf0e10cSrcweir * @see com.sun.star.lang.XMultiServiceFactory 168*cdf0e10cSrcweir * @return True, if xMSF is equal to the MultiServiceFactory of this class. 169*cdf0e10cSrcweir */ 170*cdf0e10cSrcweir public boolean isUptodate(XMultiServiceFactory xMSF) { 171*cdf0e10cSrcweir return xMSF.equals(this.xMSF); 172*cdf0e10cSrcweir } 173*cdf0e10cSrcweir 174*cdf0e10cSrcweir 175*cdf0e10cSrcweir /** 176*cdf0e10cSrcweir * Establishes a connection between BASIC and Java. 177*cdf0e10cSrcweir * If required, hte BASIC part of the "driver" is loaded. 178*cdf0e10cSrcweir * @param sBasicBridgeURL The URL of the basic bridge document 179*cdf0e10cSrcweir * (BasicBridge.sxw) 180*cdf0e10cSrcweir * @param tParam The test parameters. 181*cdf0e10cSrcweir * @param xMSF The MultiServiceFactory 182*cdf0e10cSrcweir * @param log The log writer. 183*cdf0e10cSrcweir * @see com.sun.star.lang.XMultiServiceFactory 184*cdf0e10cSrcweir * @throws ConnectionSetupException Exception is thrown, if no connection could be made. 185*cdf0e10cSrcweir */ 186*cdf0e10cSrcweir public synchronized void Connect(String sBasicBridgeURL, 187*cdf0e10cSrcweir TestParameters tParam, XMultiServiceFactory xMSF, 188*cdf0e10cSrcweir LogWriter log) throws ConnectionSetupException { 189*cdf0e10cSrcweir this.log = log; 190*cdf0e10cSrcweir try { 191*cdf0e10cSrcweir this.xMSF = xMSF; 192*cdf0e10cSrcweir Object oInterface = xMSF.createInstance( 193*cdf0e10cSrcweir "com.sun.star.frame.Desktop"); 194*cdf0e10cSrcweir XDesktop oDesktop = (XDesktop) UnoRuntime.queryInterface( 195*cdf0e10cSrcweir XDesktop.class, oInterface); 196*cdf0e10cSrcweir XComponentLoader oCLoader = (XComponentLoader) 197*cdf0e10cSrcweir UnoRuntime.queryInterface( 198*cdf0e10cSrcweir XComponentLoader.class, oDesktop); 199*cdf0e10cSrcweir 200*cdf0e10cSrcweir // load BasicBridge with MarcoEceutionMode = Always-no warn 201*cdf0e10cSrcweir //PropertyValue[] DocArgs = null; 202*cdf0e10cSrcweir PropertyValue[] DocArgs = new PropertyValue[1]; 203*cdf0e10cSrcweir PropertyValue DocArg = new PropertyValue(); 204*cdf0e10cSrcweir DocArg.Name = "MacroExecutionMode"; 205*cdf0e10cSrcweir DocArg.Value = new Short( 206*cdf0e10cSrcweir com.sun.star.document.MacroExecMode.ALWAYS_EXECUTE_NO_WARN); 207*cdf0e10cSrcweir DocArgs[0] = DocArg; 208*cdf0e10cSrcweir 209*cdf0e10cSrcweir // configure Office to allow to execute macos 210*cdf0e10cSrcweir PropertyValue [] ProvArgs = new PropertyValue [1]; 211*cdf0e10cSrcweir PropertyValue Arg = new PropertyValue(); 212*cdf0e10cSrcweir Arg.Name = "nodepath"; 213*cdf0e10cSrcweir Arg.Value = "/org.openoffice.Office.Common/Security"; 214*cdf0e10cSrcweir ProvArgs[0] = Arg; 215*cdf0e10cSrcweir 216*cdf0e10cSrcweir Object oProvider = xMSF.createInstance( 217*cdf0e10cSrcweir "com.sun.star.configuration.ConfigurationProvider"); 218*cdf0e10cSrcweir 219*cdf0e10cSrcweir XMultiServiceFactory oProviderMSF = (XMultiServiceFactory) 220*cdf0e10cSrcweir UnoRuntime.queryInterface( 221*cdf0e10cSrcweir XMultiServiceFactory.class, oProvider); 222*cdf0e10cSrcweir 223*cdf0e10cSrcweir Object oSecure = oProviderMSF.createInstanceWithArguments( 224*cdf0e10cSrcweir "com.sun.star.configuration.ConfigurationUpdateAccess", 225*cdf0e10cSrcweir ProvArgs); 226*cdf0e10cSrcweir 227*cdf0e10cSrcweir XPropertySet oSecureProps = (XPropertySet) 228*cdf0e10cSrcweir UnoRuntime.queryInterface(XPropertySet.class, oSecure); 229*cdf0e10cSrcweir 230*cdf0e10cSrcweir Object oScripting = oSecureProps.getPropertyValue("Scripting"); 231*cdf0e10cSrcweir XPropertySet oScriptingSettings = (XPropertySet) 232*cdf0e10cSrcweir UnoRuntime.queryInterface(XPropertySet.class, oScripting); 233*cdf0e10cSrcweir 234*cdf0e10cSrcweir oScriptingSettings.setPropertyValue("Warning", Boolean.FALSE); 235*cdf0e10cSrcweir oScriptingSettings.setPropertyValue("OfficeBasic", new Integer(2)); 236*cdf0e10cSrcweir 237*cdf0e10cSrcweir XChangesBatch oSecureChange = (XChangesBatch) 238*cdf0e10cSrcweir UnoRuntime.queryInterface(XChangesBatch.class, oSecure); 239*cdf0e10cSrcweir oSecureChange.commitChanges(); 240*cdf0e10cSrcweir 241*cdf0e10cSrcweir // As we want to have some information about a debugFile 242*cdf0e10cSrcweir // BEFORE connection is established 243*cdf0e10cSrcweir // we pass the information about it in frame name. 244*cdf0e10cSrcweir String sFrameName = (String)tParam.get( 245*cdf0e10cSrcweir "soapi.test.basic.debugFile"); 246*cdf0e10cSrcweir if (sFrameName == null) sFrameName = "BasicRunner"; 247*cdf0e10cSrcweir 248*cdf0e10cSrcweir oHandlerDoc = oCLoader.loadComponentFromURL(sBasicBridgeURL, 249*cdf0e10cSrcweir sFrameName, 40, DocArgs); 250*cdf0e10cSrcweir 251*cdf0e10cSrcweir do { 252*cdf0e10cSrcweir respFlag = false ; 253*cdf0e10cSrcweir wait(10000); // waiting for basic response for 10 seconds. 254*cdf0e10cSrcweir } while (respFlag && !container.hasByName("BASIC_Done")) ; 255*cdf0e10cSrcweir 256*cdf0e10cSrcweir if (!container.hasByName("BASIC_Done")) { 257*cdf0e10cSrcweir throw new ConnectionSetupException("Connection timed out."); 258*cdf0e10cSrcweir } 259*cdf0e10cSrcweir } catch (Exception e) { 260*cdf0e10cSrcweir System.out.println("Exception: " + e.toString()); 261*cdf0e10cSrcweir throw new ConnectionSetupException(); 262*cdf0e10cSrcweir } 263*cdf0e10cSrcweir 264*cdf0e10cSrcweir log.println("Java-BASIC connection established!"); 265*cdf0e10cSrcweir } 266*cdf0e10cSrcweir 267*cdf0e10cSrcweir /** 268*cdf0e10cSrcweir * Overloads perform(Strin fName, Object params) for convenience. 269*cdf0e10cSrcweir * @return A proprty value as result. 270*cdf0e10cSrcweir * 271*cdf0e10cSrcweir public synchronized PropertyValue perform(String fName) 272*cdf0e10cSrcweir throws BasicException { 273*cdf0e10cSrcweir return perform(fName, ""); 274*cdf0e10cSrcweir } 275*cdf0e10cSrcweir */ 276*cdf0e10cSrcweir /** 277*cdf0e10cSrcweir * Perform a test of a method. 278*cdf0e10cSrcweir * @param fName The name of the method to test. 279*cdf0e10cSrcweir * @param params The test parameters. 280*cdf0e10cSrcweir * @return A proprty value as result of the test. 281*cdf0e10cSrcweir * @throws BasicException The method could not be executed. 282*cdf0e10cSrcweir */ 283*cdf0e10cSrcweir public synchronized PropertyValue perform(String fName, Object params) 284*cdf0e10cSrcweir throws BasicException { 285*cdf0e10cSrcweir try { 286*cdf0e10cSrcweir container.callBasicFunction(fName, params); 287*cdf0e10cSrcweir 288*cdf0e10cSrcweir do { 289*cdf0e10cSrcweir respFlag = false; 290*cdf0e10cSrcweir // waiting for basic response for iBasicTimeout milliseconds. 291*cdf0e10cSrcweir wait(iBasicTimeout); 292*cdf0e10cSrcweir } while(respFlag && !container.hasByName("BASIC_Done")); 293*cdf0e10cSrcweir 294*cdf0e10cSrcweir } catch (InterruptedException e) { 295*cdf0e10cSrcweir System.out.println("The operation " + fName + " was interrupted."); 296*cdf0e10cSrcweir } catch (com.sun.star.lang.DisposedException de) { 297*cdf0e10cSrcweir System.out.println("## Office is disposed"); 298*cdf0e10cSrcweir } 299*cdf0e10cSrcweir 300*cdf0e10cSrcweir if (!container.hasByName("BASIC_Done")) { 301*cdf0e10cSrcweir System.out.println("Operation timed out."); 302*cdf0e10cSrcweir throw new BasicException( 303*cdf0e10cSrcweir "Operation timed out."); 304*cdf0e10cSrcweir } 305*cdf0e10cSrcweir 306*cdf0e10cSrcweir Object res = container.getByName("BASIC_Done") ; 307*cdf0e10cSrcweir container.removeByName("BASIC_Done"); 308*cdf0e10cSrcweir 309*cdf0e10cSrcweir if (!(res instanceof PropertyValue)) { 310*cdf0e10cSrcweir if (res == null) { 311*cdf0e10cSrcweir System.out.println( 312*cdf0e10cSrcweir "BasicBridge returns null"); 313*cdf0e10cSrcweir throw new BasicException( 314*cdf0e10cSrcweir "BasicBridge returns null"); 315*cdf0e10cSrcweir } else { 316*cdf0e10cSrcweir System.out.println( 317*cdf0e10cSrcweir "BasicBridge returns wrong type: " + res.getClass()); 318*cdf0e10cSrcweir throw new BasicException( 319*cdf0e10cSrcweir "BasicBridge returns wrong type: " + res.getClass()); 320*cdf0e10cSrcweir } 321*cdf0e10cSrcweir } 322*cdf0e10cSrcweir 323*cdf0e10cSrcweir PropertyValue result = (PropertyValue) res ; 324*cdf0e10cSrcweir 325*cdf0e10cSrcweir if ((result.Value instanceof String) && (((String)result.Value)).startsWith("Exception")) { 326*cdf0e10cSrcweir throw new BasicException((String)result.Value); 327*cdf0e10cSrcweir } 328*cdf0e10cSrcweir 329*cdf0e10cSrcweir return result; 330*cdf0e10cSrcweir } 331*cdf0e10cSrcweir 332*cdf0e10cSrcweir /** 333*cdf0e10cSrcweir * Returns true, if name is a supported service of this class. 334*cdf0e10cSrcweir * @param name The service name. 335*cdf0e10cSrcweir * @return True, if the service is supported. 336*cdf0e10cSrcweir */ 337*cdf0e10cSrcweir public boolean supportsService(String name) { 338*cdf0e10cSrcweir return serviceName.equals(name); 339*cdf0e10cSrcweir } 340*cdf0e10cSrcweir 341*cdf0e10cSrcweir /** 342*cdf0e10cSrcweir * Return all supported service names. 343*cdf0e10cSrcweir * @return All supported services. 344*cdf0e10cSrcweir */ 345*cdf0e10cSrcweir public String[] getSupportedServiceNames() { 346*cdf0e10cSrcweir return new String[] {serviceName}; 347*cdf0e10cSrcweir } 348*cdf0e10cSrcweir 349*cdf0e10cSrcweir /** 350*cdf0e10cSrcweir * Get the implementation name. 351*cdf0e10cSrcweir * @return Implementation name. 352*cdf0e10cSrcweir */ 353*cdf0e10cSrcweir public String getImplementationName() { 354*cdf0e10cSrcweir return getClass().getName(); 355*cdf0e10cSrcweir } 356*cdf0e10cSrcweir 357*cdf0e10cSrcweir /** 358*cdf0e10cSrcweir * Create an instance of HandlerContainer. 359*cdf0e10cSrcweir * Arguments are not supported here, so they will be ignored. 360*cdf0e10cSrcweir * @param args The arguments. 361*cdf0e10cSrcweir * @return The instance. 362*cdf0e10cSrcweir */ 363*cdf0e10cSrcweir public Object createInstanceWithArguments(Object[] args) { 364*cdf0e10cSrcweir return container; 365*cdf0e10cSrcweir } 366*cdf0e10cSrcweir 367*cdf0e10cSrcweir /** 368*cdf0e10cSrcweir * Create an instance of HandlerContainer. 369*cdf0e10cSrcweir * @return The instance. 370*cdf0e10cSrcweir */ 371*cdf0e10cSrcweir public Object createInstance() { 372*cdf0e10cSrcweir return createInstanceWithArguments(null); 373*cdf0e10cSrcweir } 374*cdf0e10cSrcweir 375*cdf0e10cSrcweir /** 376*cdf0e10cSrcweir * Dispose the BASIC document. 377*cdf0e10cSrcweir */ 378*cdf0e10cSrcweir public synchronized void dispose() { 379*cdf0e10cSrcweir try { 380*cdf0e10cSrcweir if (oHandlerDoc != null) { 381*cdf0e10cSrcweir //oHandlerDoc.dispose(); 382*cdf0e10cSrcweir util.DesktopTools.closeDoc(oHandlerDoc); 383*cdf0e10cSrcweir wait(1000); 384*cdf0e10cSrcweir } 385*cdf0e10cSrcweir } catch (Exception e) { 386*cdf0e10cSrcweir System.out.println("Exception: " + e.toString()); 387*cdf0e10cSrcweir } 388*cdf0e10cSrcweir } 389*cdf0e10cSrcweir } 390*cdf0e10cSrcweir 391*cdf0e10cSrcweir 392*cdf0e10cSrcweir /** 393*cdf0e10cSrcweir * This class handles the communication between Java and BASIC. 394*cdf0e10cSrcweir * @see com.sun.star.container.XContainer 395*cdf0e10cSrcweir * @see com.sun.star.container.XNameContainer 396*cdf0e10cSrcweir * @see com.sun.star.lang.XTypeProvider 397*cdf0e10cSrcweir */ 398*cdf0e10cSrcweir class HandlerContainer implements XContainer, XNameContainer, XTypeProvider{ 399*cdf0e10cSrcweir 400*cdf0e10cSrcweir /** Container for parameters. 401*cdf0e10cSrcweir **/ 402*cdf0e10cSrcweir Hashtable container = new Hashtable(20); 403*cdf0e10cSrcweir /** 404*cdf0e10cSrcweir * An array of listeners for container events. 405*cdf0e10cSrcweir * @see com.sun.star.container.XContainerListener 406*cdf0e10cSrcweir */ 407*cdf0e10cSrcweir static XContainerListener[] listener = null; 408*cdf0e10cSrcweir 409*cdf0e10cSrcweir /** The BasicHandler belonging to this handler. **/ 410*cdf0e10cSrcweir BasicHandler parent = null; 411*cdf0e10cSrcweir 412*cdf0e10cSrcweir /** 413*cdf0e10cSrcweir * Constructor with the parent BasicHandler. 414*cdf0e10cSrcweir * @param par The BasicHandler. 415*cdf0e10cSrcweir */ 416*cdf0e10cSrcweir public HandlerContainer(BasicHandler par) { 417*cdf0e10cSrcweir parent = par; 418*cdf0e10cSrcweir } 419*cdf0e10cSrcweir 420*cdf0e10cSrcweir /** 421*cdf0e10cSrcweir * Call a BASIC function, meaning a test method. 422*cdf0e10cSrcweir * @param fName The method name. 423*cdf0e10cSrcweir * @param args Arguments for the method. 424*cdf0e10cSrcweir */ 425*cdf0e10cSrcweir public void callBasicFunction(String fName, Object args) { 426*cdf0e10cSrcweir // BASIC's listener should be called ONLY in this case. 427*cdf0e10cSrcweir if (container.containsKey(fName)) { 428*cdf0e10cSrcweir container.remove(fName); 429*cdf0e10cSrcweir } 430*cdf0e10cSrcweir container.put(fName, args); 431*cdf0e10cSrcweir if (listener != null) { 432*cdf0e10cSrcweir ContainerEvent event = new ContainerEvent(); 433*cdf0e10cSrcweir event.Element = fName; 434*cdf0e10cSrcweir for (int i=0; i<listener.length; i++){ 435*cdf0e10cSrcweir if (listener[i] != null) { 436*cdf0e10cSrcweir listener[i].elementInserted(event); 437*cdf0e10cSrcweir } 438*cdf0e10cSrcweir } 439*cdf0e10cSrcweir } 440*cdf0e10cSrcweir } 441*cdf0e10cSrcweir 442*cdf0e10cSrcweir /** 443*cdf0e10cSrcweir * Insert an object into the container. 444*cdf0e10cSrcweir * @param name The key for the object. 445*cdf0e10cSrcweir * @param object The object to insert. 446*cdf0e10cSrcweir * @throws IllegalArgumentException Throws this exception when trying to insert null. 447*cdf0e10cSrcweir */ 448*cdf0e10cSrcweir public void insertByName(String name, Object object) throws com.sun.star.lang.IllegalArgumentException, com.sun.star.container.ElementExistException, com.sun.star.lang.WrappedTargetException { 449*cdf0e10cSrcweir 450*cdf0e10cSrcweir // BASIC and Java can insert into the container. 451*cdf0e10cSrcweir if (container.containsKey(name)) { 452*cdf0e10cSrcweir container.remove(name); 453*cdf0e10cSrcweir } 454*cdf0e10cSrcweir container.put(name, object); 455*cdf0e10cSrcweir 456*cdf0e10cSrcweir PropertyValue result = null ; 457*cdf0e10cSrcweir 458*cdf0e10cSrcweir if (object instanceof PropertyValue) { 459*cdf0e10cSrcweir result = (PropertyValue)object; 460*cdf0e10cSrcweir if (name.equals("BASIC_Done")) { 461*cdf0e10cSrcweir synchronized (parent) { 462*cdf0e10cSrcweir parent.notify(); 463*cdf0e10cSrcweir } 464*cdf0e10cSrcweir } else if (name.equals("BASIC_MethodTested")) { 465*cdf0e10cSrcweir parent.methodTested(result.Name, 466*cdf0e10cSrcweir ((Boolean)result.Value).booleanValue()); 467*cdf0e10cSrcweir } 468*cdf0e10cSrcweir } else if (name.equals("BASIC_Log")) { 469*cdf0e10cSrcweir parent.Log(object.toString()); 470*cdf0e10cSrcweir } 471*cdf0e10cSrcweir } 472*cdf0e10cSrcweir 473*cdf0e10cSrcweir /** 474*cdf0e10cSrcweir * Remove the object with this name from the container. 475*cdf0e10cSrcweir * @param name The key. 476*cdf0e10cSrcweir */ 477*cdf0e10cSrcweir public void removeByName(String name) { 478*cdf0e10cSrcweir container.remove(name) ; 479*cdf0e10cSrcweir } 480*cdf0e10cSrcweir 481*cdf0e10cSrcweir /** 482*cdf0e10cSrcweir * Unsupported method. 483*cdf0e10cSrcweir * @param name The name of the key. 484*cdf0e10cSrcweir * @param value The value. 485*cdf0e10cSrcweir * @throws WrappedTargetException Throws this exception when called falsely. 486*cdf0e10cSrcweir */ 487*cdf0e10cSrcweir public void replaceByName(String name, Object value) 488*cdf0e10cSrcweir throws WrappedTargetException { 489*cdf0e10cSrcweir throw new WrappedTargetException("Unsupported"); 490*cdf0e10cSrcweir } 491*cdf0e10cSrcweir 492*cdf0e10cSrcweir /** 493*cdf0e10cSrcweir * Has a value for this key. 494*cdf0e10cSrcweir * @param name The name of a key. 495*cdf0e10cSrcweir * @return True, if name exists as key in the container. 496*cdf0e10cSrcweir */ 497*cdf0e10cSrcweir public boolean hasByName(String name) { 498*cdf0e10cSrcweir return container.containsKey(name); 499*cdf0e10cSrcweir } 500*cdf0e10cSrcweir 501*cdf0e10cSrcweir /** 502*cdf0e10cSrcweir * Get an object by its key. 503*cdf0e10cSrcweir * @param name The name of the key. 504*cdf0e10cSrcweir * @return The object of this key. 505*cdf0e10cSrcweir */ 506*cdf0e10cSrcweir public Object getByName(String name) { 507*cdf0e10cSrcweir return container.get(name); 508*cdf0e10cSrcweir } 509*cdf0e10cSrcweir 510*cdf0e10cSrcweir /** 511*cdf0e10cSrcweir * Get all key names. 512*cdf0e10cSrcweir * @return All names of keys. 513*cdf0e10cSrcweir */ 514*cdf0e10cSrcweir public String[] getElementNames() { 515*cdf0e10cSrcweir String[] res = new String[container.size()]; 516*cdf0e10cSrcweir return (String[])container.keySet().toArray(res); 517*cdf0e10cSrcweir } 518*cdf0e10cSrcweir 519*cdf0e10cSrcweir /** 520*cdf0e10cSrcweir * Is the xcontainer empty? 521*cdf0e10cSrcweir * @return True, if the container has elements. 522*cdf0e10cSrcweir */ 523*cdf0e10cSrcweir public boolean hasElements() { 524*cdf0e10cSrcweir return !container.isEmpty(); 525*cdf0e10cSrcweir } 526*cdf0e10cSrcweir 527*cdf0e10cSrcweir /** 528*cdf0e10cSrcweir * Get the type of this class. 529*cdf0e10cSrcweir * @return The type of this class. 530*cdf0e10cSrcweir */ 531*cdf0e10cSrcweir public Type getElementType() { 532*cdf0e10cSrcweir try { 533*cdf0e10cSrcweir return new Type(String.class); 534*cdf0e10cSrcweir } catch (Exception e) { 535*cdf0e10cSrcweir return null; 536*cdf0e10cSrcweir } 537*cdf0e10cSrcweir } 538*cdf0e10cSrcweir 539*cdf0e10cSrcweir /** 540*cdf0e10cSrcweir * Get the implementation id of this class. 541*cdf0e10cSrcweir * @return A unique id for this class 542*cdf0e10cSrcweir * @see com.sun.star.lang.XTypeProvider 543*cdf0e10cSrcweir */ 544*cdf0e10cSrcweir public byte[] getImplementationId() { 545*cdf0e10cSrcweir return toString().getBytes(); 546*cdf0e10cSrcweir } 547*cdf0e10cSrcweir 548*cdf0e10cSrcweir /** 549*cdf0e10cSrcweir * Get all types of this class. 550*cdf0e10cSrcweir * @return All implemented UNO types. 551*cdf0e10cSrcweir */ 552*cdf0e10cSrcweir public Type[] getTypes() { 553*cdf0e10cSrcweir Class interfaces[] = getClass().getInterfaces(); 554*cdf0e10cSrcweir Type types[] = new Type[interfaces.length]; 555*cdf0e10cSrcweir for(int i = 0; i < interfaces.length; ++ i) { 556*cdf0e10cSrcweir types[i] = new Type(interfaces[i]); 557*cdf0e10cSrcweir } 558*cdf0e10cSrcweir return types; 559*cdf0e10cSrcweir } 560*cdf0e10cSrcweir 561*cdf0e10cSrcweir /** 562*cdf0e10cSrcweir * Add a listener 563*cdf0e10cSrcweir * @param xListener The listener. 564*cdf0e10cSrcweir */ 565*cdf0e10cSrcweir public void addContainerListener(XContainerListener xListener){ 566*cdf0e10cSrcweir int length = 0; 567*cdf0e10cSrcweir if (listener != null) 568*cdf0e10cSrcweir length = listener.length; 569*cdf0e10cSrcweir 570*cdf0e10cSrcweir XContainerListener[] mListener = 571*cdf0e10cSrcweir new XContainerListener[length+1]; 572*cdf0e10cSrcweir for (int i=0; i<length-1; i++) { 573*cdf0e10cSrcweir mListener[i] = listener[i]; 574*cdf0e10cSrcweir // listener already added 575*cdf0e10cSrcweir if (((Object)xListener).equals(listener[i])) 576*cdf0e10cSrcweir return; 577*cdf0e10cSrcweir } 578*cdf0e10cSrcweir mListener[length] = xListener; 579*cdf0e10cSrcweir listener = mListener; 580*cdf0e10cSrcweir } 581*cdf0e10cSrcweir 582*cdf0e10cSrcweir /** 583*cdf0e10cSrcweir * Remove a listener 584*cdf0e10cSrcweir * @param xListener The listener. 585*cdf0e10cSrcweir */ 586*cdf0e10cSrcweir public void removeContainerListener(XContainerListener xListener){ 587*cdf0e10cSrcweir if (listener != null && listener.length != 0) { 588*cdf0e10cSrcweir int length = listener.length; 589*cdf0e10cSrcweir XContainerListener[] mListener = 590*cdf0e10cSrcweir new XContainerListener[length-1]; 591*cdf0e10cSrcweir boolean found = false; 592*cdf0e10cSrcweir int j=0; 593*cdf0e10cSrcweir for (int i=0; i<length-1; i++) { 594*cdf0e10cSrcweir if (!((Object)xListener).equals(listener[j])) { 595*cdf0e10cSrcweir mListener[i] = listener[j]; 596*cdf0e10cSrcweir } 597*cdf0e10cSrcweir else { 598*cdf0e10cSrcweir j++; 599*cdf0e10cSrcweir found = true; 600*cdf0e10cSrcweir } 601*cdf0e10cSrcweir j++; 602*cdf0e10cSrcweir } 603*cdf0e10cSrcweir if (!found) { 604*cdf0e10cSrcweir if (((Object)xListener).equals(listener[length-1])) 605*cdf0e10cSrcweir listener = mListener; 606*cdf0e10cSrcweir } 607*cdf0e10cSrcweir else 608*cdf0e10cSrcweir listener = mListener; 609*cdf0e10cSrcweir 610*cdf0e10cSrcweir } 611*cdf0e10cSrcweir } 612*cdf0e10cSrcweir } 613