Difference between revisions of "API/Samples/Java/Writer/TextReplace"
B michaelsen (Talk | contribs) m |
(→SearchDescriptor and ReplaceDescriptor Services) |
||
Line 32: | Line 32: | ||
− | The method ''createSearchDescriptor()'' of '''com.sun.star.util.XReplaceable''' creates a new SearchDescriptor that specify the properties of searching action, | + | 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 | + | 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 : | The hierarchy structure is : | ||
− | < | + | <syntaxhighlight lang="cpp"> |
XReplaceDescriptor | XReplaceDescriptor | ||
┗ XSearchDescriptor | ┗ XSearchDescriptor | ||
┗ ::com::sun::star::beans::XPropertySet | ┗ ::com::sun::star::beans::XPropertySet | ||
− | </ | + | </syntaxhighlight> |
Search and replace actions might be described as a logical of" | Search and replace actions might be described as a logical of" | ||
Line 54: | Line 54: | ||
Interfaces from XpropertySet are used to set properties of SearchDescriptor Service to defined the searching behaviors('''certain rules'''), such as: | Interfaces from XpropertySet are used to set properties of SearchDescriptor Service to defined the searching behaviors('''certain rules'''), such as: | ||
− | < | + | <syntaxhighlight lang="java"> |
// property SearchWords searches for whole cells! | // property SearchWords searches for whole cells! | ||
xReplaceDesc.setPropertyValue("SearchWords", new Boolean(false)); | xReplaceDesc.setPropertyValue("SearchWords", new Boolean(false)); | ||
− | </ | + | </syntaxhighlight> |
This means that “'''If true, only complete words will be found.'''” for instances: | This means that “'''If true, only complete words will be found.'''” for instances: | ||
Line 68: | Line 68: | ||
true, only cells containing the whole search text and nothing else is found. | 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. | If false, cells containing the search string as a substring is found. | ||
− | |||
== replaceall() method == | == replaceall() method == |
Revision as of 17:37, 30 January 2021
TextReplace
The example shows topics of search and replacement of text, introduces the interfaces/services should be known in the searching and replacing operations.
Contents
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 SearchDescriptor for detailed properties.
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