Difference between revisions of "API/Samples/Java/Writer/TextReplace"
m (→replaceall() method) |
|||
(3 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
− | <noinclude>[[Category:API]] [[Category:Samples]] [[Category:Java]] [[Category:Writer]]</noinclude> | + | <noinclude>[[Category:API]] [[Category:Samples]] [[Category:Java]] [[Category:Writer/API]]</noinclude> |
'''TextReplace''' | '''TextReplace''' | ||
Line 32: | Line 32: | ||
− | The method ''createSearchDescriptor()'' of | + | 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 49: | Line 49: | ||
Find '''Something''' in '''certain rules''' and replace it with '''other thing''' | 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. | + | 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 | + | Interfaces from XpropertySet are used to set properties of SearchDescriptor Service to define 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.'''” | + | 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”. | + | 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:''' | '''Notice:''' | ||
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 == | ||
− | The method ''replaceAll()'' of '''com.sun.star.util.XReplaceable''' performs a replacement according to the passed | + | The method ''replaceAll()'' of '''com.sun.star.util.XReplaceable''' performs a replacement according to the passed replacedescriptor. |
Line 78: | Line 77: | ||
For completed information of search and replace, here is the good place to look at: | For completed information of search and replace, here is the good place to look at: | ||
− | [ | + | [https://wiki.openoffice.org/wiki/Documentation/DevGuide/Text/Navigating#Search_and_Replace Search and Replace in Text Document] |
− | + | ||
− | + | ||
+ | [https://wiki.openoffice.org/wiki/Documentation/DevGuide/Spreadsheets/Search_and_Replace Search and Replace in Cells] | ||
== Example project download == | == Example project download == | ||
[[Image:TextReplace.zip]] | [[Image:TextReplace.zip]] |
Latest revision as of 13:22, 5 March 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 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