1 /*************************************************************************
2  *
3  *  The Contents of this file are made available subject to the terms of
4  *  the BSD license.
5  *
6  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7  *  All rights reserved.
8  *
9  *  Redistribution and use in source and binary forms, with or without
10  *  modification, are permitted provided that the following conditions
11  *  are met:
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.
20  *
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.
32  *
33  *************************************************************************/
34 
35 //***************************************************************************
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
40 //***************************************************************************
41 
42 import com.sun.star.uno.UnoRuntime;
43 
44 public class TextDocumentStructure {
45 
46     public static void main(String args[]) {
47         com.sun.star.uno.XComponentContext xContext = null;
48 
49         try {
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 ...");
53 
54             // get the rmeote service manger
55             com.sun.star.lang.XMultiComponentFactory xMCF =
56                 xContext.getServiceManager();
57 
58             // create a new instance of the desktop
59             Object oDesktop = xMCF.createInstanceWithContext(
60                                         "com.sun.star.frame.Desktop", xContext);
61 
62             // get the component laoder from the desktop to create a new
63             // text document
64             com.sun.star.frame.XComponentLoader xCLoader =
65                 (com.sun.star.frame.XComponentLoader)
66                 UnoRuntime.queryInterface(
67                     com.sun.star.frame.XComponentLoader.class,oDesktop);
68             com.sun.star.beans.PropertyValue [] szEmptyArgs =
69                 new com.sun.star.beans.PropertyValue [0];
70             String strDoc = "private:factory/swriter";
71 
72             System.out.println("create new text document");
73 
74             com.sun.star.lang.XComponent xComp = xCLoader.loadComponentFromURL(
75                 strDoc, "_blank", 0, szEmptyArgs);
76 
77             // query the new document for the XTextDocument interface
78             com.sun.star.text.XTextDocument xTextDocument =
79                 (com.sun.star.text.XTextDocument)UnoRuntime.queryInterface(
80                     com.sun.star.text.XTextDocument.class, xComp);
81 
82             // create some example data
83             com.sun.star.text.XText xText = xTextDocument.getText();
84             createExampleData( xText );
85 
86             // Begin section 'The structure of text documents' of the Tutorial
87 
88             com.sun.star.container.XEnumeration xParagraphEnumeration = null;
89             com.sun.star.container.XEnumerationAccess xParaEnumerationAccess = null;
90             com.sun.star.container.XEnumeration xPortionEnumeration = null;
91             com.sun.star.container.XEnumeration xTextPortionEnum;
92             com.sun.star.text.XTextContent xTextElement = null;
93 
94             System.out.println("create an enumeration of all paragraphs");
95             // create an enumeration access of all paragraphs of a document
96             com.sun.star.container.XEnumerationAccess xEnumerationAccess =
97                 (com.sun.star.container.XEnumerationAccess)
98                     UnoRuntime.queryInterface(
99                         com.sun.star.container.XEnumerationAccess.class, xText);
100             xParagraphEnumeration = xEnumerationAccess.createEnumeration();
101 
102             // Loop through all paragraphs of the document
103             while ( xParagraphEnumeration.hasMoreElements() ) {
104                 xTextElement = (com.sun.star.text.XTextContent)
105                     UnoRuntime.queryInterface(
106                         com.sun.star.text.XTextContent.class,
107                         xParagraphEnumeration.nextElement());
108                 com.sun.star.lang.XServiceInfo xServiceInfo =
109                     (com.sun.star.lang.XServiceInfo)UnoRuntime.queryInterface(
110                         com.sun.star.lang.XServiceInfo.class, xTextElement);
111 
112                 // check ifs the current paragraph really a paragraph or an
113                 // anchor of a frame or picture
114                 if( xServiceInfo.supportsService("com.sun.star.text.Paragraph") ) {
115                     com.sun.star.text.XTextRange xTextRange =
116                         xTextElement.getAnchor();
117                     System.out.println( "This is a Paragraph" );
118 
119                     // create another enumeration to get all text portions of
120                     // the paragraph
121                     xParaEnumerationAccess =
122                         (com.sun.star.container.XEnumerationAccess)
123                             UnoRuntime.queryInterface(
124                                 com.sun.star.container.XEnumerationAccess.class,
125                                 xTextElement);
126                     xTextPortionEnum = xParaEnumerationAccess.createEnumeration();
127 
128                     while ( xTextPortionEnum.hasMoreElements() ) {
129                         com.sun.star.text.XTextRange xTextPortion =
130                             (com.sun.star.text.XTextRange)UnoRuntime.queryInterface(
131                                 com.sun.star.text.XTextRange.class,
132                                 xTextPortionEnum.nextElement());
133                         System.out.println( "Text from the portion : "
134                                             + xTextPortion.getString() );
135 
136                         com.sun.star.beans.XPropertySet xPropertySet =
137                             (com.sun.star.beans.XPropertySet)
138                                  UnoRuntime.queryInterface(
139                                      com.sun.star.beans.XPropertySet.class,
140                                      xTextPortion);
141                         System.out.println( "Name of the font : "
142                             + xPropertySet.getPropertyValue( "CharFontName" ) );
143 
144                         // PropertyState status of each text portion.
145                         com.sun.star.beans.XPropertyState xPropertyState =
146                             (com.sun.star.beans.XPropertyState)
147                                 UnoRuntime.queryInterface(
148                                     com.sun.star.beans.XPropertyState.class,
149                                     xTextPortion);
150 
151                         if( xPropertyState.getPropertyState("CharWeight").equals(
152                                 com.sun.star.beans.PropertyState.AMBIGUOUS_VALUE) )
153                             System.out.println( "-  The text range contains more than one different attributes" );
154 
155                         if( xPropertyState.getPropertyState( "CharWeight" ).equals(
156                                 com.sun.star.beans.PropertyState.DIRECT_VALUE ) )
157                             System.out.println( " - The text range contains hard formats" );
158 
159                         if( xPropertyState.getPropertyState( "CharWeight" ).equals(
160                                 com.sun.star.beans.PropertyState.DEFAULT_VALUE ) )
161                             System.out.println( " - The text range doesn't contains hard formats" );
162                     }
163                 }
164                 else
165                     System.out.println( "The text portion isn't a text paragraph" );
166                 // End section 'The structure of text documents' of the Tutorial
167             }
168         }
169         catch( Exception e) {
170             e.printStackTrace(System.err);
171             System.exit(1);
172         }
173 
174         System.out.println("done");
175         System.exit(0);
176     }
177 
178     public static void createExampleData( com.sun.star.text.XText xText ) {
179 
180         try {
181             xText.setString( "This is an example sentence" );
182 
183             com.sun.star.text.XWordCursor xWordCursor =
184                 (com.sun.star.text.XWordCursor)UnoRuntime.queryInterface(
185                     com.sun.star.text.XWordCursor.class, xText.getStart());
186 
187             xWordCursor.gotoNextWord(false);
188             xWordCursor.gotoNextWord(false);
189             xWordCursor.gotoEndOfWord(true);
190 
191             com.sun.star.beans.XPropertySet xPropertySet =
192                 (com.sun.star.beans.XPropertySet)UnoRuntime.queryInterface(
193                     com.sun.star.beans.XPropertySet.class, xWordCursor );
194             xPropertySet.setPropertyValue("CharWeight",
195                              new Float( com.sun.star.awt.FontWeight.BOLD ));
196 
197             System.out.println("create example data");
198         }
199         catch( Exception e) {
200             e.printStackTrace(System.err);
201         }
202 
203 
204     }
205 }
206