1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 //***************************************************************************
25 // comment: Step 1: bootstrap UNO and get the remote component context
26 //          Step 2: open an empty text document
27 //          Step 3: create an enumeration of all paragraphs
28 //          Step 4: create an enumeration of all text portions
29 //***************************************************************************
30 
31 import com.sun.star.uno.UnoRuntime;
32 
33 public class TextDocumentStructure {
34 
main(String args[])35     public static void main(String args[]) {
36         com.sun.star.uno.XComponentContext xContext = null;
37 
38         try {
39             // get the remote office component context
40             xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
41             System.out.println("Connected to a running office ...");
42 
43             // get the rmeote service manger
44             com.sun.star.lang.XMultiComponentFactory xMCF =
45                 xContext.getServiceManager();
46 
47             // create a new instance of the desktop
48             Object oDesktop = xMCF.createInstanceWithContext(
49                                         "com.sun.star.frame.Desktop", xContext);
50 
51             // get the component laoder from the desktop to create a new
52             // text document
53             com.sun.star.frame.XComponentLoader xCLoader =
54                 (com.sun.star.frame.XComponentLoader)
55                 UnoRuntime.queryInterface(
56                     com.sun.star.frame.XComponentLoader.class,oDesktop);
57             com.sun.star.beans.PropertyValue [] szEmptyArgs =
58                 new com.sun.star.beans.PropertyValue [0];
59             String strDoc = "private:factory/swriter";
60 
61             System.out.println("create new text document");
62 
63             com.sun.star.lang.XComponent xComp = xCLoader.loadComponentFromURL(
64                 strDoc, "_blank", 0, szEmptyArgs);
65 
66             // query the new document for the XTextDocument interface
67             com.sun.star.text.XTextDocument xTextDocument =
68                 (com.sun.star.text.XTextDocument)UnoRuntime.queryInterface(
69                     com.sun.star.text.XTextDocument.class, xComp);
70 
71             // create some example data
72             com.sun.star.text.XText xText = xTextDocument.getText();
73             createExampleData( xText );
74 
75             // Begin section 'The structure of text documents' of the Tutorial
76 
77             com.sun.star.container.XEnumeration xParagraphEnumeration = null;
78             com.sun.star.container.XEnumerationAccess xParaEnumerationAccess = null;
79             com.sun.star.container.XEnumeration xPortionEnumeration = null;
80             com.sun.star.container.XEnumeration xTextPortionEnum;
81             com.sun.star.text.XTextContent xTextElement = null;
82 
83             System.out.println("create an enumeration of all paragraphs");
84             // create an enumeration access of all paragraphs of a document
85             com.sun.star.container.XEnumerationAccess xEnumerationAccess =
86                 (com.sun.star.container.XEnumerationAccess)
87                     UnoRuntime.queryInterface(
88                         com.sun.star.container.XEnumerationAccess.class, xText);
89             xParagraphEnumeration = xEnumerationAccess.createEnumeration();
90 
91             // Loop through all paragraphs of the document
92             while ( xParagraphEnumeration.hasMoreElements() ) {
93                 xTextElement = (com.sun.star.text.XTextContent)
94                     UnoRuntime.queryInterface(
95                         com.sun.star.text.XTextContent.class,
96                         xParagraphEnumeration.nextElement());
97                 com.sun.star.lang.XServiceInfo xServiceInfo =
98                     (com.sun.star.lang.XServiceInfo)UnoRuntime.queryInterface(
99                         com.sun.star.lang.XServiceInfo.class, xTextElement);
100 
101                 // check ifs the current paragraph really a paragraph or an
102                 // anchor of a frame or picture
103                 if( xServiceInfo.supportsService("com.sun.star.text.Paragraph") ) {
104                     com.sun.star.text.XTextRange xTextRange =
105                         xTextElement.getAnchor();
106                     System.out.println( "This is a Paragraph" );
107 
108                     // create another enumeration to get all text portions of
109                     // the paragraph
110                     xParaEnumerationAccess =
111                         (com.sun.star.container.XEnumerationAccess)
112                             UnoRuntime.queryInterface(
113                                 com.sun.star.container.XEnumerationAccess.class,
114                                 xTextElement);
115                     xTextPortionEnum = xParaEnumerationAccess.createEnumeration();
116 
117                     while ( xTextPortionEnum.hasMoreElements() ) {
118                         com.sun.star.text.XTextRange xTextPortion =
119                             (com.sun.star.text.XTextRange)UnoRuntime.queryInterface(
120                                 com.sun.star.text.XTextRange.class,
121                                 xTextPortionEnum.nextElement());
122                         System.out.println( "Text from the portion : "
123                                             + xTextPortion.getString() );
124 
125                         com.sun.star.beans.XPropertySet xPropertySet =
126                             (com.sun.star.beans.XPropertySet)
127                                  UnoRuntime.queryInterface(
128                                      com.sun.star.beans.XPropertySet.class,
129                                      xTextPortion);
130                         System.out.println( "Name of the font : "
131                             + xPropertySet.getPropertyValue( "CharFontName" ) );
132 
133                         // PropertyState status of each text portion.
134                         com.sun.star.beans.XPropertyState xPropertyState =
135                             (com.sun.star.beans.XPropertyState)
136                                 UnoRuntime.queryInterface(
137                                     com.sun.star.beans.XPropertyState.class,
138                                     xTextPortion);
139 
140                         if( xPropertyState.getPropertyState("CharWeight").equals(
141                                 com.sun.star.beans.PropertyState.AMBIGUOUS_VALUE) )
142                             System.out.println( "-  The text range contains more than one different attributes" );
143 
144                         if( xPropertyState.getPropertyState( "CharWeight" ).equals(
145                                 com.sun.star.beans.PropertyState.DIRECT_VALUE ) )
146                             System.out.println( " - The text range contains hard formats" );
147 
148                         if( xPropertyState.getPropertyState( "CharWeight" ).equals(
149                                 com.sun.star.beans.PropertyState.DEFAULT_VALUE ) )
150                             System.out.println( " - The text range doesn't contains hard formats" );
151                     }
152                 }
153                 else
154                     System.out.println( "The text portion isn't a text paragraph" );
155                 // End section 'The structure of text documents' of the Tutorial
156             }
157         }
158         catch( Exception e) {
159             e.printStackTrace(System.err);
160             System.exit(1);
161         }
162 
163         System.out.println("done");
164         System.exit(0);
165     }
166 
createExampleData( com.sun.star.text.XText xText )167     public static void createExampleData( com.sun.star.text.XText xText ) {
168 
169         try {
170             xText.setString( "This is an example sentence" );
171 
172             com.sun.star.text.XWordCursor xWordCursor =
173                 (com.sun.star.text.XWordCursor)UnoRuntime.queryInterface(
174                     com.sun.star.text.XWordCursor.class, xText.getStart());
175 
176             xWordCursor.gotoNextWord(false);
177             xWordCursor.gotoNextWord(false);
178             xWordCursor.gotoEndOfWord(true);
179 
180             com.sun.star.beans.XPropertySet xPropertySet =
181                 (com.sun.star.beans.XPropertySet)UnoRuntime.queryInterface(
182                     com.sun.star.beans.XPropertySet.class, xWordCursor );
183             xPropertySet.setPropertyValue("CharWeight",
184                              new Float( com.sun.star.awt.FontWeight.BOLD ));
185 
186             System.out.println("create example data");
187         }
188         catch( Exception e) {
189             e.printStackTrace(System.err);
190         }
191 
192 
193     }
194 }
195