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