API/Samples/Java/Writer/TextReplace
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, see service <idls>com.sun.star.util.SearchDescriptor</idls> for detailed properties.
The method createReplaceDescriptor() creates a new ReplaceDescriptor that contains all data for the replace action. It returns the interface <idl>com.sun.star.util.XReplaceDescriptor</idl> 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 define 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 replacedescriptor.
For completed information of search and replace, here is the good place to look at:
Search and Replace in Text Document