API/Samples/Java/Writer/TextReplace

From Apache OpenOffice Wiki
< API‎ | Samples‎ | Java
Jump to: navigation, search


TextReplace

The example shows topics of search and replacement of text, introduces the interfaces/services should be known in the searching and replacing operations.


Key Interface: com.sun.star.util.XReplaceable

Interface com.sun.star.util.XReplaceable is derived from com.sun.star.util.XSearchable providing search and replacement of text. Interface com.sun.star.util.XReplaceable contains the following methods:

  com::sun::star::util::util.XSearchDescriptor createSearchDescriptor(); 
 
 com::sun::star::util::XReplaceDescriptor createReplaceDescriptor(); 
 
 com::sun::star::uno::XInterface findFirst( 
                         [in] com::sun::star::util::XSearchDescriptor xDesc); 
 
 com::sun::star::uno::XInterface findNext( 
                         [in] com::sun::star::uno::XInterface xStartAt,
                         [in] com::sun::star::util::XSearchDescriptor xDesc);
 
 com::sun::star::container::XIndexAccess findAll( 
                         [in]  com::sun::star::util::XSearchDescriptor xDesc); 
 
 long replaceAll(        [in] com::sun::star::util::XSearchDescriptor xDesc);

SearchDescriptor and ReplaceDescriptor Services

The method createSearchDescriptor() of com.sun.star.util.XReplaceable creates a new SearchDescriptor that specify the properties of searching action, for detailed properties of service com.sun.star.util.SearchDescriptor, please visit Here.


The method createReplaceDescriptor() creates a new ReplaceDescriptor that contains all data for the replace action. It returns the interface com.sun.star.util.XReplaceDescriptor of this descriptor.


The hierarchy structure is :

 XReplaceDescriptor
  ┗ XSearchDescriptor
    ┗ ::com::sun::star::beans::XPropertySet

Search and replace actions might be described as a logical of"

Find Something in certain rules and replace it with other thing

XSearchDescriptor interface provides the method to set search string(Something) and XRelaceDescriptor interface provides the method to set replace string(other thing). However you don't have to create another SearchDescriptor when you got a ReplaceDescriptor in your program because the ReplaceDescriptor is extended from SearchDescriptor.


Interfaces from XpropertySet are used to set properties of SearchDescriptor Service to defined the searching behaviors(certain rules), such as:

    // property SearchWords searches for whole cells!
    xReplaceDesc.setPropertyValue("SearchWords", new Boolean(false));

This means that “If true, only complete words will be found.” for instances:

The search string was set as “book”, the replacement string was set as “magazine”. if the property of “SearchWords” was set to “true”, Only the word “book” will be replaced with “magazine”, the words like “books” and “booking” were not to be considered as found.

Notice:

The property SearchWords has a different meaning in spreadsheets: If 
true, only cells containing the whole search text and nothing else is found. 
If false, cells containing the search string as a substring is found.


replaceall() method

The method replaceAll() of com.sun.star.util.XReplaceable performs a replacement according to the passed replace descriptor.


For completed information of search and replace, here is the good place to look at:

Search and Replace in Text Document

Search and Replace in Cells


Example project download

File:TextReplace.zip

Personal tools