Difference between revisions of "Documentation/DevGuide/Text/Navigating"

From Apache OpenOffice Wiki
Jump to: navigation, search
(Search and Replace: added proprety SearchType)
(Page was somehow broken. Trying to restore it piece by piece.)
Line 17: Line 17:
 
<!--<idltopic>com.sun.star.text.XTextContent</idltopic>-->
 
<!--<idltopic>com.sun.star.text.XTextContent</idltopic>-->
 
The text document model has suppliers that yield all text contents in a document as collections. To find a particular text content, such as bookmarks or text fields, use the appropriate supplier interface. The following supplier interfaces are available at the model:
 
The text document model has suppliers that yield all text contents in a document as collections. To find a particular text content, such as bookmarks or text fields, use the appropriate supplier interface. The following supplier interfaces are available at the model:
 
{|border="1" cellpadding=4 style="border-collapse:collapse;"
 
|-bgcolor=#EDEDED
 
!Supplier interfaces
 
!Methods
 
|-
 
|<idls>com.sun.star.text.XTextTablesSupplier</idls>
 
|<code>com.sun.star.container.XNameAccess getTextTables()</code> 
 
|-
 
|<idls>com.sun.star.text.XTextFramesSupplier</idls>
 
|<code>com.sun.star.container.XNameAccess getTextFrames()</code>
 
|-
 
|<idls>com.sun.star.text.XTextGraphicObjectsSupplier</idls>
 
|<code>com.sun.star.container.XNameAccess getGraphicObjects()</code> 
 
|-
 
|<idls>com.sun.star.text.XTextEmbeddedObjectsSupplier</idls>
 
|<code>com.sun.star.container.XNameAccess getEmbeddedObjects()</code> 
 
|-
 
|<idls>com.sun.star.text.XTextFieldsSupplier</idls>
 
|<code>com.sun.star.container.XEnumerationAccess getTextFields()</code><br>
 
<code>com.sun.star.container.XNameAccess getTextFieldMasters()</code> 
 
|-
 
|<idls>com.sun.star.text.XBookmarksSupplier</idls>
 
|<code>com.sun.star.container.XNameAccess getBookmarks()</code>
 
|-
 
|<idls>com.sun.star.text.XReferenceMarksSupplier</idls>
 
|<code>com.sun.star.container.XNameAccess getReferenceMarks()</code> 
 
|-
 
|<idls>com.sun.star.text.XFootnotesSupplier</idls>
 
|<code>com.sun.star.container.XIndexAccess getFootnotes()</code><br> <code>com.sun.star.beans.XPropertySet getFootnoteSettings()</code>
 
|-
 
|<idls>com.sun.star.text.XEndnotesSupplier</idls>
 
|<code>com.sun.star.container.XIndexAccess getEndnotes()</code><br>
 
<code>com.sun.star.beans.XPropertySet getEndnoteSettings()</code>
 
|-
 
|<idls>com.sun.star.text.XTextSectionsSupplier</idls>
 
|<code>com.sun.star.container.XNameAccess getTextSections()</code> 
 
|-
 
|<idls>com.sun.star.text.XDocumentIndexesSupplier</idls>
 
|<code>com.sun.star.container.XIndexAccess getDocumentIndexes()</code>
 
|-
 
|<idls>com.sun.star.document.XRedlinesSupplier</idls>
 
|<code>com.sun.star.container.XEnumerationAccess getRedlines()</code>
 
|}
 
 
You can work with text content directly, set properties and use its interfaces, or find out where it is and do an action at the text content location in the text. To find out where a text content is located call the <code>getAnchor()</code> method at the interface <idl>com.sun.star.text.XTextContent</idl>, which every text content must support.
 
 
In addition, text contents located at the current text cursor position or the content where the cursor is currently located are provided in the <code>PropertySet</code> of the cursor. The corresponding cursor properties are:
 
 
* <code>DocumentIndexMark</code>
 
* <code>TextField</code>
 
* <code>ReferenceMark</code>
 
* <code>Footnote</code>
 
* <code>Endnote</code>
 
* <code>DocumentIndex</code>
 
* <code>TextTable</code>
 
* <code>TextFrame</code>
 
* <code>Cell</code>
 
* <code>TextSection</code>
 
 
=== Search and Replace ===
 
 
<!--<idltopic>com.sun.star.util.XSearchable;com.sun.star.util.XReplaceable;com.sun.star.util.SearchDescriptor</idltopic>-->
 
The writer model supports the interface <idl>com.sun.star.util.XReplaceable</idl> that inherits from the interface <idl>com.sun.star.util.XSearchable</idl> for searching and replacing in text. It contains the following methods:
 
 
  com::sun::star::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)
 
 
To search or replace text, first create a descriptor service using <code>createSearchDescriptor()</code> or <code>createReplaceDescriptor()</code>. You receive a service that supports the interface <idl>com.sun.star.util.XPropertyReplace</idl> with methods to describe what you are searching for, what you want to replace with and what attributes you are looking for. It is described in detail below.
 
 
Pass in this descriptor to the methods <code>findFirst()</code>, <code>findNext()</code>, <code>findAll()</code> or <code>replaceAll()</code>.
 
 
The methods <code>findFirst()</code> and <code>findNext()</code> return a <idl>com.sun.star.uno.XInterface</idl> pointing to an object that contains the found item. If the search is not successful, a null reference to an <code>XInterface</code> is returned, that is, if you try to query other interfaces from it, null is returned. The method <code>findAll()</code> returns a <idl>com.sun.star.container.XIndexAccess</idl> containing one or more <idl>com.sun.star.uno.XInterface</idl> pointing to the found text ranges or if they failed an empty interface. The method <code>replaceAll()</code> returns the number of replaced occurrences only.
 
 
[[Image:XPropertyReplace.png|none|thumb|400px|XPropertyReplace]]
 
 
The interface <idl>com.sun.star.util.XPropertyReplace</idl> is required to describe your search. It is a powerful interface and inherits from <code>XReplaceDescriptor</code>, <code>XSearchDescriptor</code> and <code>XPropertySet</code>.
 
 
The target of your search is described by a string containing a search text or a style name using <code>setSearchString()</code>. Correspondingly, provide the text string or style name that should replace the found occurrence of the search target to the <code>XReplaceDescriptor</code> using <code>setReplaceString()</code>. Refine the search mode through the properties included in the service <idl>com.sun.star.util.SearchDescriptor</idl>:
 
 
{|border="1" cellpadding=4 style="border-collapse:collapse;"
 
|-bgcolor=#EDEDED
 
!colspan="2"|Properties of <idl>com.sun.star.util.SearchDescriptor</idl>
 
|-
 
|<idlm>com.sun.star.util.SearchDescriptor:SearchBackwards</idlm>
 
|<code>boolean</code> - Search backward
 
|-
 
|<idlm>com.sun.star.util.SearchDescriptor:SearchCaseSensitive</idlm>
 
|<code>boolean</code> - Search is case sensitive.
 
|-
 
|<idlm>com.sun.star.util.SearchDescriptor:SearchRegularExpression</idlm>
 
|<code>boolean</code> - Search interpreting the search string as a regular expression.
 
|-
 
|<idlm>com.sun.star.util.SearchDescriptor:SearchSimilarity</idlm>
 
|<code>boolean</code> - Use similarity search using the four following options:
 
|-
 
|<idlm>com.sun.star.util.SearchDescriptor:SearchSimilarityAdd</idlm>
 
|<code>short</code> - Determines the number of characters the word in the document may be longer than the search string for it to remain valid.
 
|-
 
|<idlm>com.sun.star.util.SearchDescriptor:SearchSimilarityExchange</idlm>
 
|<code>short</code> - Determines how many characters in the search term can be exchanged.
 
|-
 
|<idlm>com.sun.star.util.SearchDescriptor:SearchSimilarityRelax</idlm>
 
|<code>boolean</code> - If <code>true</code>, the values of added, exchanged, and removed characters are combined The search term is then found if the word in the document can be generated through any combination of these three conditions.
 
|-
 
|<idlm>com.sun.star.util.SearchDescriptor:SearchSimilarityRemove</idlm>
 
|<code>short</code> - Determines how many characters the word in the document may be shorter than the search string for it to remain valid. The characters may be removed from the word at any position.
 
|-
 
|<idlm>com.sun.star.util.SearchDescriptor:SearchStyles</idlm>
 
|<code>boolean</code> - Determines if the search and replace string should be interpreted as paragraph style names. Note that the Display Name of the style has to be used.
 
|-
 
|<idlm>com.sun.star.util.SearchDescriptor:SearchWords</idlm>
 
|<code>boolean</code> - Determines if the search should find complete words only.
 
|-
 
|SearchType (not included in [http://api.openoffice.org/docs/common/ref/com/sun/star/util/SearchDescriptor.html API-reference] )
 
|<code>integer</code> - Determines if the search should be done in cell formulas (0) values (1) or notes (2). [Calc only]
 
|}
 
 
In <code>XPropertyReplace</code>, the methods to get and set search attributes, and replace attributes allow the attributes to search for to be defined and the attributes to insert instead of the existing attributes. All of these methods expect a sequence of <idl>com.sun.star.beans.PropertyValue</idl> structs.
 
 
Any properties contained in the services <idl>com.sun.star.style.CharacterProperties</idl>, <idl>com.sun.star.style.CharacterPropertiesAsian</idl> and <idl>com.sun.star.style.ParagraphProperties</idl> can be used for an attribute search. If <code>setValueSearch(false)</code> is used, {{PRODUCTNAME}} checks if an attribute exists, whereas <code>setValueSearch(true)</code> finds specific attribute values. If only searching to see if an attribute exists at all, it is sufficient to pass a <code>PropertyValue</code> struct with the Name field set to the name of the required attribute.
 
 
The following code snippet replaces all occurrences of the text "random numbers" by the bold text "replaced numbers" in a given document <code>mxDoc</code>.
 
 
  XReplaceable xReplaceable = (XReplaceable) UnoRuntime.queryInterface(XReplaceable.class, mxDoc);
 
  XReplaceDescriptor xRepDesc = xReplaceable.createReplaceDescriptor();
 
 
 
  // set a string to search for
 
  xRepDesc.setSearchString("random numbers");
 
 
 
  // set the string to be inserted
 
  xRepDesc.setReplaceString("replaced numbers");
 
 
 
  // create an array of one property value for a CharWeight property
 
  PropertyValue[] aReplaceArgs = new PropertyValue[1];
 
 
 
  // create PropertyValue struct
 
  aReplaceArgs[0] = new PropertyValue();
 
  // CharWeight should be bold
 
  aReplaceArgs[0].Name = "CharWeight";
 
  aReplaceArgs[0].Value = new Float(com.sun.star.awt.FontWeight.BOLD);
 
 
 
  // set our sequence with one property value as ReplaceAttribute
 
  XPropertyReplace xPropRepl = (XPropertyReplace) UnoRuntime.queryInterface(
 
          XPropertyReplace.class, xRepDesc);
 
  xPropRepl.setReplaceAttributes(aReplaceArgs);
 
 
 
  // replace
 
  long nResult = xReplaceable.replaceAll(xRepDesc);
 
 
 
{{PDL1}}
 
 
[[Category:Documentation/Developer's Guide/Text Documents]]
 

Revision as of 14:08, 17 October 2019



Cursors

The text model cursor allows for free navigation over the model by character, words, sentences, or paragraphs. There can be several model cursors at the same time. Model cursor creation, movement and usage is discussed in the section Word Processing . The text model cursors are com.sun.star.text.TextCursor services that are based on the interface com.sun.star.text.XTextCursor, which is based on com.sun.star.text.XTextRange.

The text view cursor enables the user to travel over the document in the view by character, line, screen page and document page. There is only one text view cursor. Certain information about the current layout, such as the number of lines and page number must be retrieved at the view cursor. The chapter Text Document Controller below discusses the view cursor in detail. The text view cursor is a com.sun.star.text.TextViewCursor service that includes com.sun.star.text.TextLayoutCursor.

Locating Text Contents

The text document model has suppliers that yield all text contents in a document as collections. To find a particular text content, such as bookmarks or text fields, use the appropriate supplier interface. The following supplier interfaces are available at the model:

Personal tools
In other languages