Difference between revisions of "API/Samples/Java/Writer/TextReplace"

From Apache OpenOffice Wiki
< API‎ | Samples‎ | Java
Jump to: navigation, search
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 '''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 [http://api.openoffice.org/docs/common/ref/com/sun/star/util/SearchDescriptor.html Here].
+
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 [http://api.openoffice.org/docs/common/ref/com/sun/star/util/XReplaceDescriptor.html com.sun.star.util.XReplaceDescriptor] of this descriptor.  
+
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 :
<source lang="cpp">
+
<syntaxhighlight lang="cpp">
 
  XReplaceDescriptor
 
  XReplaceDescriptor
 
   ┗ XSearchDescriptor
 
   ┗ XSearchDescriptor
 
     ┗ ::com::sun::star::beans::XPropertySet
 
     ┗ ::com::sun::star::beans::XPropertySet
</source>
+
</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 defined the searching behaviors('''certain rules'''), such as:
+
Interfaces from XpropertySet are used to set properties of SearchDescriptor Service to define the searching behaviors('''certain rules'''), such as:
  
<source lang="java">
+
<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));
</source>
+
</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:
  
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.
+
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 replace descriptor.
+
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:
  
[http://doc.services.openoffice.org/wiki/Documentation/DevGuide/Text/Navigating#Search_and_Replace Search and Replace in Text Document]
+
[https://wiki.openoffice.org/wiki/Documentation/DevGuide/Text/Navigating#Search_and_Replace Search and Replace in Text Document]
 
+
[http://doc.services.openoffice.org/wiki/Documentation/DevGuide/Spreadsheets/Search_and_Replace Search and Replace in Cells]
+
  
 +
[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.


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

Search and Replace in Cells

Example project download

File:TextReplace.zip

Personal tools