Bugzilla – Attachment 49745 Details for
Bug 39694
JAVA UNO API: XTextRange.getString returns correct String only after some delay
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
Program to reproduce text field placeholder problem
OO_LO_Placeholder_Test.java (text/x-java), 9.92 KB, created by
Bodo Meissner
on 2011-07-30 09:33:04 UTC
(
hide
)
Description:
Program to reproduce text field placeholder problem
Filename:
MIME Type:
Creator:
Bodo Meissner
Created:
2011-07-30 09:33:04 UTC
Size:
9.92 KB
patch
obsolete
>package ooTests; > >import java.io.IOException; >import java.util.ArrayList; > >import com.sun.org.apache.bcel.internal.generic.GETSTATIC; >import com.sun.star.beans.PropertyValue; >import com.sun.star.beans.XPropertySet; >import com.sun.star.bridge.XBridge; >import com.sun.star.bridge.XBridgeFactory; >import com.sun.star.comp.helper.Bootstrap; >import com.sun.star.connection.XConnection; >import com.sun.star.connection.XConnector; >import com.sun.star.container.XEnumeration; >import com.sun.star.container.XEnumerationAccess; >import com.sun.star.frame.XComponentLoader; >import com.sun.star.lang.XComponent; >import com.sun.star.lang.XMultiComponentFactory; >import com.sun.star.lang.XServiceInfo; >import com.sun.star.text.XTextDocument; >import com.sun.star.text.XTextField; >import com.sun.star.text.XTextFieldsSupplier; >import com.sun.star.text.XTextRange; >import com.sun.star.uno.UnoRuntime; >import com.sun.star.uno.XComponentContext; >import com.sun.star.util.XCloseable; > >public class OO_LO_Placeholder_Test >{ > private XBridge bridge = null ; > private XComponentContext xRemoteContext = null; > > private com.sun.star.frame.XComponentLoader officeComponentLoader; > private XMultiComponentFactory xRemoteServiceManager = null; > > private XTextDocument xTextDocument = null; > > > // adapt to your testing environment > private String templateFile = "file:///C:/Test.ott"; > // adapt to your testing environment > private String OFFICE_APP_NAME ="C:/Programme/LibreOffice 3.4/program/soffice"; > //private String OFFICE_APP_NAME ="C:/Programme/OpenOffice.org 3/program/soffice.exe"; > > private String con; > private String host = "localhost"; > private String port = (OFFICE_APP_NAME.contains("Libre") ? "8105" : "8100"); > > private Process process = null; > private Boolean actHidden; > > /** > * main > */ > public static void main(String[] args) > throws Exception > { > > OO_LO_Placeholder_Test ooloTests = > new OO_LO_Placeholder_Test() ; > > /* > * Do the same operation once with actHidden=true > * and once with actHidden=false. > * With actHidden = true not all or none of the > * placeholders will be found. > */ > ooloTests.actHidden = Boolean.valueOf(true); > // ooloTests.actHidden = Boolean.valueOf(false); > > ooloTests.startOO_LO_Instance(); > > ooloTests.useConnection() ; > > /* When I uncomment this delay, the loop below takes longer > * until a non-empty string is returned. > */ > // Thread.sleep(1000); > > ooloTests.loadDocument(); > > /* With one of these delays the loop ends after the first call to > * com.sun.star.text.XTextRange.getString() > */ > // Thread.sleep(1000); > > ArrayList<XTextRange> placeholders = ooloTests.getPlaceholders(); > > /* With one of these delays the loop ends after the first call to > * com.sun.star.text.XTextRange.getString() > */ > // Thread.sleep(1000); > > int i; > boolean loop; > String placeholderString = ""; > XTextRange textRange = placeholders.get(0); > > /* With one of these delays the loop ends after the first call to > * com.sun.star.text.XTextRange.getString() > */ > // Thread.sleep(1000); > > for(i = 0, loop = true; loop && i < 100000; i++) > { > placeholderString = textRange.getString(); > if(placeholderString.length() > 0) loop = false; > } > System.out.println("loop " + i + " placeholder[0] = " + placeholderString); > > ooloTests.replacePlaceholders(placeholders); > > ooloTests.release(); > > System.exit(0); > > } > > public static void listPlaceholders(ArrayList<XTextRange> placeholders) > { > for(int i = 0; i < placeholders.size();i++ ) > { > XTextRange placeholder = placeholders.get(i); > String placeholderString = placeholder.getString(); > System.out.println( > "placeholde no. " + (i+1) + " = " + > (placeholderString.equals("") ? > "empty placeholder name !!!!!!" : > placeholderString)); > } > } > > public OO_LO_Placeholder_Test() > { > con = "socket,host="+host+",port="+port; > } > > private void startOO_LO_Instance(){ > > String[] cmdArray = new String[6]; > String prefix = (OFFICE_APP_NAME.contains("Libre") ? "--" : "-"); > cmdArray[0] = OFFICE_APP_NAME; > cmdArray[1] = prefix+"nologo"; > cmdArray[2] = prefix+"nofirststartwizard"; > cmdArray[3] = prefix+"norestore"; > cmdArray[4] = prefix+"headless"; > cmdArray[5] = prefix+"accept=socket,port=" + port + ";urp"; > try { > process = Runtime.getRuntime().exec(cmdArray); > } catch (IOException e) { > e.printStackTrace(); > } > if (process == null) > throw new RuntimeException("soffice start failed: " + cmdArray); > > try { > Thread.sleep(500); > } catch (InterruptedException e) { > e.printStackTrace(); > } > > if(process==null){ > System.out.println("Cannot start "+OFFICE_APP_NAME+" instance"); > System.exit(1); > } > } > /**********************************************************/ > private void loadDocument(){ > try{ > PropertyValue[] props = new PropertyValue[2]; > props[0] = new PropertyValue(); > props[0].Name = "AsTemplate"; > props[0].Value = Boolean.valueOf(true); > props[1] = new PropertyValue(); > props[1].Name = "Hidden"; > props[1].Value = actHidden; > > XComponent xcomp = officeComponentLoader.loadComponentFromURL(templateFile, "_blank", 0, props); > System.out.println("Document loaded as XComponent"); > > XServiceInfo xServiceInfo = (XServiceInfo) UnoRuntime.queryInterface(XServiceInfo.class,xcomp); > if (xServiceInfo.supportsService("com.sun.star.text.TextDocument")) { > xTextDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class,xcomp); > System.out.println("Document now available as XTextDocument"); > } > > }catch(Exception ex){ > System.out.println("Exception in loadDocument"); > xTextDocument = null; > ex.printStackTrace(); > } > } > /**********************************************************/ > > private ArrayList<XTextRange> getPlaceholders(){ > if(xTextDocument==null){return null;} > > ArrayList<XTextRange> arrayList = new ArrayList<XTextRange>(); > > try { > XTextFieldsSupplier xTextFieldsSupplier = (XTextFieldsSupplier)UnoRuntime.queryInterface(XTextFieldsSupplier.class, xTextDocument); > XEnumerationAccess xEnumerationAccess = xTextFieldsSupplier.getTextFields(); > XEnumeration xEnumeration = xEnumerationAccess.createEnumeration(); > while(xEnumeration.hasMoreElements()) { > Object object = xEnumeration.nextElement(); > XTextField xTextField = (XTextField)UnoRuntime.queryInterface(XTextField.class, object); > XServiceInfo xInfo = (XServiceInfo)UnoRuntime.queryInterface(XServiceInfo.class, xTextField); > XTextRange range = xTextField.getAnchor(); > // placeholders only > if(xInfo.supportsService("com.sun.star.text.TextField.JumpEdit") > /* || xInfo.supportsService("com.sun.star.text.TextField.User") */ > ) > { > arrayList.add(range); > } > } > }catch(Exception exception) { > System.out.println("exception in getPlaceholders"); > exception.printStackTrace(); > } > return arrayList; > } > > private void replacePlaceholders(ArrayList<XTextRange> arrayList) > { > if(arrayList==null){return;} > try{ > for(int i = 0; i < arrayList.size();i++ ) > { > XTextRange placeholder = arrayList.get(i); > String placeholderString = placeholder.getString(); > System.out.println( > "Replace placeholder[" + i + "] = " + > (placeholderString.equals("") ? > "empty placeholder name !!!!!!" : > placeholderString) > + " with dummy content: Dummy" + (i+1)); > placeholder.setString("Dummy" + (i+1)); > } > }catch(Exception ex){ > System.out.println("exception on replacing placeholders"); > ex.printStackTrace(); > } > } > > /**********************************************************/ > public void useConnection() throws java.lang.Exception { > try > { > XComponentContext _ctx = > Bootstrap.createInitialComponentContext( null ); > > xRemoteContext = _ctx ; > > Object obj = xRemoteContext.getServiceManager().createInstanceWithContext( > "com.sun.star.connection.Connector", xRemoteContext ); > > XConnector xConnector = (XConnector ) > UnoRuntime.queryInterface(XConnector.class, obj); > XConnection connection = xConnector.connect( con); > if (connection == null) > System.out.println("Connection is null"); > obj = xRemoteContext.getServiceManager().createInstanceWithContext( > "com.sun.star.bridge.BridgeFactory", xRemoteContext); > XBridgeFactory xBridgeFactory = (XBridgeFactory) UnoRuntime.queryInterface( > XBridgeFactory.class , obj ); > if (xBridgeFactory== null) > System.out.println("bridge factory is null"); > > bridge = xBridgeFactory.createBridge( "" , "urp", connection , null ); > > obj = bridge.getInstance( "StarOffice.ServiceManager"); > > xRemoteServiceManager = ( XMultiComponentFactory ) > UnoRuntime.queryInterface( XMultiComponentFactory.class, obj ); > if(xRemoteServiceManager == null) > System.out.println("remote service manager is null"); > > XPropertySet xProperySet = ( XPropertySet ) > UnoRuntime.queryInterface( XPropertySet.class, xRemoteServiceManager ); > > Object oDefaultContext = > xProperySet.getPropertyValue( "DefaultContext" ); > > XComponentContext xOfficeComponentContext = > ( XComponentContext ) UnoRuntime.queryInterface( > XComponentContext.class, oDefaultContext ); > > Object oDesktop = xRemoteServiceManager.createInstanceWithContext( > "com.sun.star.frame.Desktop", xOfficeComponentContext ); > > officeComponentLoader = ( XComponentLoader ) > UnoRuntime.queryInterface( XComponentLoader.class, oDesktop ); > > String available = (null !=officeComponentLoader ? "installed" : "not installed"); > System.out.println( "remote ServiceManager = " + available ); > } > catch( com.sun.star.lang.DisposedException e ) { > xRemoteContext = null; > throw e; > } > } > > /**********************************************************/ > > public void release() throws Exception > { > XCloseable xcloseable = (XCloseable) UnoRuntime.queryInterface(XCloseable.class, xTextDocument); > xcloseable.close(false); > System.out.println( "XTextDocument closed"); > XComponent xcomponent = ( XComponent ) UnoRuntime.queryInterface( XComponent.class,bridge ); > xcomponent.dispose(); > System.out.println( "UNO access bridge closed"); > } >}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Raw
Actions:
View
Attachments on
bug 39694
: 49745 |
49746