Difference between revisions of "Documentation/DevGuide/Text/Iterating over Text"

From Apache OpenOffice Wiki
Jump to: navigation, search
m (Robot: Changing Category:Documentation/Developers Guide/Text Documents)
m (typos)
Line 29: Line 29:
 
|-
 
|-
 
|<idlm>com.sun.star.text.TextPortion:IsStart</idlm>  
 
|<idlm>com.sun.star.text.TextPortion:IsStart</idlm>  
|<code>boolean</code> - Determines whether the portion is a start portion if two portions are needed to include an object, that is, <code>DocmentIndexMark</code>.  
+
|<code>boolean</code> - Determines whether the portion is a start portion if two portions are needed to include an object, that is, <code>DocumentIndexMark</code>.  
 
|}
 
|}
  
Line 73: Line 73:
 
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 <code>TextPortionType</code> "<code>TextContent</code>" indicate if there is a content anchored at a character or as a character. If you have a <code>TextContent</code> portion type, you know that there are shape objects anchored at a character or as a character.
 
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 <code>TextPortionType</code> "<code>TextContent</code>" indicate if there is a content anchored at a character or as a character. If you have a <code>TextContent</code> 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, <code>Paragraphs</code> and <code>TextPortions</code> in writer support the interface <idl>com.sun.star.container.XContentEnumerationAccess</idl>. This interface tells which text contents besides the text flow contents there are and supplies them as an <idl>com.sun.star.container.XEnumeration</idl>:
+
This last group of data contained in a text, <code>Paragraphs</code> and <code>TextPortions</code> in writer support the interface <idl>com.sun.star.container.XContentEnumerationAccess</idl>. This interface tells which text contents other than the text flow contents exist, and supplies them as an <idl>com.sun.star.container.XEnumeration</idl>:
  
 
   sequence< string > getAvailableServiceNames()
 
   sequence< string > getAvailableServiceNames()

Revision as of 17:20, 4 September 2008



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".
IsCollapsed boolean - Determines whether the portion is a point only.
IsStart boolean - Determines whether the portion is a start portion if two portions are needed to include an object, that is, 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.

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 lists the shape objects anchored at a character or as a character.

Template:Documentation/Note

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.

 /** 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