Iterating over Text

From Apache OpenOffice Wiki
Jump to: navigation, search



The second interface of com.sun.star.text.Text is XEnumerationAccess. A Text service enumerates all paragraphs in a text and returns objects which support com.sun.star.text.Paragraph. This includes tables, because writer sees tables as specialized paragraphs that support the com.sun.star.text.TextTable service.

Paragraphs also have an com.sun.star.container.XEnumerationAccess of their own. They can enumerate every single text portion that they contain. A text portion is a text range containing a uniform piece of information that appears within the text flow. An ordinary paragraph, formatted in a uniform manner and containing nothing but a string, enumerates just a single text portion. In a paragraph that has specially formatted words or other contents, the text portion enumeration returns one com.sun.star.text.TextPortion service for each differently formatted string, and for every other text content. Text portions include the service com.sun.star.text.TextRange and have the properties listed below:

Properties of com.sun.star.text.TextPortion
TextPortionType string - Contains the type of the text portion (see below).
ControlCharacter short - Returns the control character if the text portion contains a control character as defined in com.sun.star.text.ControlCharacter.
Bookmark com.sun.star.text.XTextContent. Contains the bookmark if the portion has TextPortionType="Bookmark".
DocumentIndexMark com.sun.star.text.XTextContent. Contains the document index mark if the portion has TextPortionType="DocumentIndexMark".
ReferenceMark com.sun.star.text.XTextContent. Contains the reference mark if the portion has TextPortionType="ReferenceMark".
Footnote com.sun.star.text.XFootnote. Contains the footnote if the portion has TextPortionType="Footnote".
TextField com.sun.star.text.XTextContent. Contains the text field if the portion has TextPortionType="TextField".
InContentMetadata com.sun.star.text.XTextContent. Contains the text range if the portion has TextPortionType="InContentMetadata".
IsCollapsed boolean - Contains whether the portion is a point only.
IsStart boolean - Contains whether the portion is a start portion if two portions are needed to include an object, for example, DocumentIndexMark.


Possible Values for TextPortionType are:

TextPortionType (String) Description
"Text" a portion with mere string content
"TextField" A com.sun.star.text.TextField content.
"TextContent" A text content supplied through the interface XContentEnumerationAccess.
"Footnote" A footnote or an endnote.
"ControlCharacter" A control character.
"ReferenceMark" A reference mark.
"DocumentIndexMark" A document index mark.
"Bookmark" A bookmark.
"Redline" A redline portion which is a result of the change tracking feature.
"Ruby" A ruby attribute which is used in Asian text.
"Frame" A frame supplied through the interface XContentEnumerationAccess.
"SoftPageBreak" A soft page break.
"InContentMetadata" A text range with attached metadata.

The text portion enumeration of a paragraph does not supply contents which do belong to the paragraph, but do not fuse together with the text flow. These could be text frames, graphic objects, embedded objects or drawing shapes anchored at the paragraph, characters or as character. The TextPortionType "TextContent" indicate if there is a content anchored at a character or as a character. If you have a TextContent portion type, you know that there are shape objects anchored at a character or as a character.

This last group of data contained in a text, Paragraphs and TextPortions in writer support the interface com.sun.star.container.XContentEnumerationAccess. This interface tells which text contents other than the text flow contents exist, and supplies them as an com.sun.star.container.XEnumeration:

 sequence< string > getAvailableServiceNames()
 com::sun::star::container::XEnumeration createContentEnumeration( [in] string aServiceName)

The XContentEnumerationAccess of the paragraph lists the shape objects anchored at the paragraph while the XContentEnumerationAccess of a text portion lists the shape objects anchored at a character or as a character.

Documentation note.png Precisely the same enumerations are available for the current text cursor selection. The text cursor enumerates paragraphs, text portions and text contents just like the service com.sun.star.text.Text itself.

The enumeration access to text through paragraphs and text portions is used if every single paragraph in a text needs to be touched. The application area for this enumeration are export filters, that uses this enumeration to go over the whole document, writing out the paragraphs to the target file. The following code snippet centers all paragraphs in a text.

<!--[SOURCE:Text/TextDocuments.java]-->
 
  /** This method demonstrates how to iterate over paragraphs */
  protected void ParagraphExample () {
      try {
          // The service 'com.sun.star.text.Text' supports the XEnumerationAccess interface to
          // provide an enumeration
          // of the paragraphs contained by the text the service refers to. 
 
          // Here, we access this interface
          XEnumerationAccess xParaAccess = (XEnumerationAccess) UnoRuntime.queryInterface(
              XEnumerationAccess.class, mxDocText);
          // Call the XEnumerationAccess's only method to access the actual Enumeration
          XEnumeration xParaEnum = xParaAccess.createEnumeration();
 
          // While there are paragraphs, do things to them
          while (xParaEnum.hasMoreElements()) {
              // Get a reference to the next paragraphs XServiceInfo interface. TextTables
              // are also part of this enumeration access, so we ask the element if it is
              // a TextTable, if it doesn't support the com.sun.star.text.TextTable
              // service, then it is safe to assume that it really is a paragraph
              XServiceInfo xInfo = (XServiceInfo) UnoRuntime.queryInterface(
                  XServiceInfo.class, xParaEnum.nextElement());
              if (!xInfo.supportsService("com.sun.star.text.TextTable")) {
                  // Access the paragraph's property set...the properties in this
                  // property set are listed
                  // in: com.sun.star.style.ParagraphProperties
                  XPropertySet xSet = (XPropertySet) UnoRuntime.queryInterface(
                      XPropertySet.class, xInfo);
                  // Set the justification to be center justified
                  xSet.setPropertyValue("ParaAdjust", com.sun.star.style.ParagraphAdjust.CENTER);
              }
          }
      } catch (Exception e) {
          e.printStackTrace (System.out);
      }
  }
Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages