1 /*************************************************************************
3 * The Contents of this file are made available subject to the terms of
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *************************************************************************/
36 // comment: Step 1: bootstrap UNO and get the remote component context
37 // Step 2: open an empty text document
38 // Step 3: create an enumeration of all paragraphs
39 // Step 4: create an enumeration of all text portions
42 import com
.sun
.star
.uno
.UnoRuntime
;
44 public class TextDocumentStructure
{
46 public static void main(String args
[]) {
47 com
.sun
.star
.uno
.XComponentContext xContext
= null;
50 // get the remote office component context
51 xContext
= com
.sun
.star
.comp
.helper
.Bootstrap
.bootstrap();
52 System
.out
.println("Connected to a running office ...");
54 // get the rmeote service manger
55 com
.sun
.star
.lang
.XMultiComponentFactory xMCF
=
56 xContext
.getServiceManager();
58 // create a new instance of the desktop
59 Object oDesktop
= xMCF
.createInstanceWithContext(
60 "com.sun.star.frame.Desktop", xContext
);
62 // get the component laoder from the desktop to create a new
64 com
.sun
.star
.frame
.XComponentLoader xCLoader
=
65 UnoRuntime
.queryInterface(
66 com
.sun
.star
.frame
.XComponentLoader
.class,oDesktop
);
67 com
.sun
.star
.beans
.PropertyValue
[] szEmptyArgs
=
68 new com
.sun
.star
.beans
.PropertyValue
[0];
69 String strDoc
= "private:factory/swriter";
71 System
.out
.println("create new text document");
73 com
.sun
.star
.lang
.XComponent xComp
= xCLoader
.loadComponentFromURL(
74 strDoc
, "_blank", 0, szEmptyArgs
);
76 // query the new document for the XTextDocument interface
77 com
.sun
.star
.text
.XTextDocument xTextDocument
=
78 UnoRuntime
.queryInterface(
79 com
.sun
.star
.text
.XTextDocument
.class, xComp
);
81 // create some example data
82 com
.sun
.star
.text
.XText xText
= xTextDocument
.getText();
83 createExampleData( xText
);
85 // Begin section 'The structure of text documents' of the Tutorial
87 com
.sun
.star
.container
.XEnumeration xParagraphEnumeration
= null;
88 com
.sun
.star
.container
.XEnumerationAccess xParaEnumerationAccess
= null;
89 com
.sun
.star
.container
.XEnumeration xTextPortionEnum
;
90 com
.sun
.star
.text
.XTextContent xTextElement
= null;
92 System
.out
.println("create an enumeration of all paragraphs");
93 // create an enumeration access of all paragraphs of a document
94 com
.sun
.star
.container
.XEnumerationAccess xEnumerationAccess
=
95 UnoRuntime
.queryInterface(
96 com
.sun
.star
.container
.XEnumerationAccess
.class, xText
);
97 xParagraphEnumeration
= xEnumerationAccess
.createEnumeration();
99 // Loop through all paragraphs of the document
100 while ( xParagraphEnumeration
.hasMoreElements() ) {
101 xTextElement
= UnoRuntime
.queryInterface(
102 com
.sun
.star
.text
.XTextContent
.class,
103 xParagraphEnumeration
.nextElement());
104 com
.sun
.star
.lang
.XServiceInfo xServiceInfo
=
105 UnoRuntime
.queryInterface(
106 com
.sun
.star
.lang
.XServiceInfo
.class, xTextElement
);
108 // check ifs the current paragraph really a paragraph or an
109 // anchor of a frame or picture
110 if( xServiceInfo
.supportsService("com.sun.star.text.Paragraph") ) {
111 com
.sun
.star
.text
.XTextRange xTextRange
=
112 xTextElement
.getAnchor();
113 System
.out
.println( "This is a Paragraph" );
115 // create another enumeration to get all text portions of
117 xParaEnumerationAccess
=
118 UnoRuntime
.queryInterface(
119 com
.sun
.star
.container
.XEnumerationAccess
.class,
121 xTextPortionEnum
= xParaEnumerationAccess
.createEnumeration();
123 while ( xTextPortionEnum
.hasMoreElements() ) {
124 com
.sun
.star
.text
.XTextRange xTextPortion
=
125 UnoRuntime
.queryInterface(
126 com
.sun
.star
.text
.XTextRange
.class,
127 xTextPortionEnum
.nextElement());
128 System
.out
.println( "Text from the portion : "
129 + xTextPortion
.getString() );
131 com
.sun
.star
.beans
.XPropertySet xPropertySet
=
132 UnoRuntime
.queryInterface(
133 com
.sun
.star
.beans
.XPropertySet
.class,
135 System
.out
.println( "Name of the font : "
136 + xPropertySet
.getPropertyValue( "CharFontName" ) );
138 // PropertyState status of each text portion.
139 com
.sun
.star
.beans
.XPropertyState xPropertyState
=
140 UnoRuntime
.queryInterface(
141 com
.sun
.star
.beans
.XPropertyState
.class,
144 if( xPropertyState
.getPropertyState("CharWeight").equals(
145 com
.sun
.star
.beans
.PropertyState
.AMBIGUOUS_VALUE
) )
146 System
.out
.println( "- The text range contains more than one different attributes" );
148 if( xPropertyState
.getPropertyState( "CharWeight" ).equals(
149 com
.sun
.star
.beans
.PropertyState
.DIRECT_VALUE
) )
150 System
.out
.println( " - The text range contains hard formats" );
152 if( xPropertyState
.getPropertyState( "CharWeight" ).equals(
153 com
.sun
.star
.beans
.PropertyState
.DEFAULT_VALUE
) )
154 System
.out
.println( " - The text range doesn't contains hard formats" );
158 System
.out
.println( "The text portion isn't a text paragraph" );
159 // End section 'The structure of text documents' of the Tutorial
162 catch( Exception e
) {
163 e
.printStackTrace(System
.err
);
167 System
.out
.println("done");
171 public static void createExampleData( com
.sun
.star
.text
.XText xText
) {
174 xText
.setString( "This is an example sentence" );
176 com
.sun
.star
.text
.XWordCursor xWordCursor
=
177 UnoRuntime
.queryInterface(
178 com
.sun
.star
.text
.XWordCursor
.class, xText
.getStart());
180 xWordCursor
.gotoNextWord(false);
181 xWordCursor
.gotoNextWord(false);
182 xWordCursor
.gotoEndOfWord(true);
184 com
.sun
.star
.beans
.XPropertySet xPropertySet
=
185 UnoRuntime
.queryInterface(
186 com
.sun
.star
.beans
.XPropertySet
.class, xWordCursor
);
187 xPropertySet
.setPropertyValue("CharWeight",
188 new Float( com
.sun
.star
.awt
.FontWeight
.BOLD
));
190 System
.out
.println("create example data");
192 catch( Exception e
) {
193 e
.printStackTrace(System
.err
);