Navigating

From Apache OpenOffice Wiki
Jump to: navigation, search



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:

Supplier interfaces Methods
com.sun.star.text.XTextTablesSupplier com.sun.star.container.XNameAccess getTextTables()
com.sun.star.text.XTextFramesSupplier com.sun.star.container.XNameAccess getTextFrames()
com.sun.star.text.XTextGraphicObjectsSupplier com.sun.star.container.XNameAccess getGraphicObjects()
com.sun.star.text.XTextEmbeddedObjectsSupplier com.sun.star.container.XNameAccess getEmbeddedObjects()
com.sun.star.text.XTextFieldsSupplier com.sun.star.container.XEnumerationAccess getTextFields()

com.sun.star.container.XNameAccess getTextFieldMasters()

com.sun.star.text.XBookmarksSupplier com.sun.star.container.XNameAccess getBookmarks()
com.sun.star.text.XReferenceMarksSupplier com.sun.star.container.XNameAccess getReferenceMarks()
com.sun.star.text.XFootnotesSupplier com.sun.star.container.XIndexAccess getFootnotes()
com.sun.star.beans.XPropertySet getFootnoteSettings()
com.sun.star.text.XEndnotesSupplier com.sun.star.container.XIndexAccess getEndnotes()

com.sun.star.beans.XPropertySet getEndnoteSettings()

com.sun.star.text.XTextSectionsSupplier com.sun.star.container.XNameAccess getTextSections()
com.sun.star.text.XDocumentIndexesSupplier com.sun.star.container.XIndexAccess getDocumentIndexes()
com.sun.star.document.XRedlinesSupplier com.sun.star.container.XEnumerationAccess getRedlines()

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 getAnchor() method at the interface com.sun.star.text.XTextContent, 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 PropertySet of the cursor. The corresponding cursor properties are:

  • DocumentIndexMark
  • TextField
  • ReferenceMark
  • Footnote
  • Endnote
  • DocumentIndex
  • TextTable
  • TextFrame
  • Cell
  • TextSection

Search and Replace

The writer model supports the interface com.sun.star.util.XReplaceable that inherits from the interface com.sun.star.util.XSearchable 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 createSearchDescriptor() or createReplaceDescriptor(). You receive a service that supports the interface com.sun.star.util.XPropertyReplace 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 findFirst(), findNext(), findAll() or replaceAll().

The methods findFirst() and findNext() return a com.sun.star.uno.XInterface pointing to an object that contains the found item. If the search is not successful, a null reference to an XInterface is returned, that is, if you try to query other interfaces from it, null is returned. The method findAll() returns a com.sun.star.container.XIndexAccess containing one or more com.sun.star.uno.XInterface pointing to the found text ranges or if they failed an empty interface. The method replaceAll() returns the number of replaced occurrences only.

XPropertyReplace

The interface com.sun.star.util.XPropertyReplace is required to describe your search. It is a powerful interface and inherits from XReplaceDescriptor, XSearchDescriptor and XPropertySet.

The target of your search is described by a string containing a search text or a style name using setSearchString(). Correspondingly, provide the text string or style name that should replace the found occurrence of the search target to the XReplaceDescriptor using setReplaceString(). Refine the search mode through the properties included in the service com.sun.star.util.SearchDescriptor:

Properties of com.sun.star.util.SearchDescriptor
com.sun.star.util.SearchDescriptor:SearchBackwards boolean - Search backward
com.sun.star.util.SearchDescriptor:SearchCaseSensitive boolean - Search is case sensitive.
com.sun.star.util.SearchDescriptor:SearchRegularExpression boolean - Search interpreting the search string as a regular expression.
com.sun.star.util.SearchDescriptor:SearchSimilarity boolean - Use similarity search using the four following options:
com.sun.star.util.SearchDescriptor:SearchSimilarityAdd short - Determines the number of characters the word in the document may be longer than the search string for it to remain valid.
com.sun.star.util.SearchDescriptor:SearchSimilarityExchange short - Determines how many characters in the search term can be exchanged.
com.sun.star.util.SearchDescriptor:SearchSimilarityRelax boolean - If true, 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.
com.sun.star.util.SearchDescriptor:SearchSimilarityRemove short - 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.
com.sun.star.util.SearchDescriptor:SearchStyles boolean - 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.
com.sun.star.util.SearchDescriptor:SearchWords boolean - Determines if the search should find complete words only.
SearchType (not included in API-reference ) integer - Determines if the search should be done in cell formulas (0) values (1) or notes (2). [Calc only]


In XPropertyReplace, 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 com.sun.star.beans.PropertyValue structs.

Any properties contained in the services com.sun.star.style.CharacterProperties, com.sun.star.style.CharacterPropertiesAsian and com.sun.star.style.ParagraphProperties can be used for an attribute search. If setValueSearch(false) is used, Apache OpenOffice checks if an attribute exists, whereas setValueSearch(true) finds specific attribute values. If only searching to see if an attribute exists at all, it is sufficient to pass a PropertyValue 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 mxDoc.

  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);
Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages