1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * The Contents of this file are made available subject to the terms of
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
31 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
32 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *************************************************************************/
37 // comment: Step 1: bootstrap UNO and get the remote component context
38 // Step 2: open an empty text document
39 // Step 3: create an enumeration of all paragraphs
40 // Step 4: create an enumeration of all text portions
43 import com
.sun
.star
.uno
.UnoRuntime
;
45 public class TextDocumentStructure
{
47 public static void main(String args
[]) {
48 com
.sun
.star
.uno
.XComponentContext xContext
= null;
51 // get the remote office component context
52 xContext
= com
.sun
.star
.comp
.helper
.Bootstrap
.bootstrap();
53 System
.out
.println("Connected to a running office ...");
55 // get the remote service manager
56 com
.sun
.star
.lang
.XMultiComponentFactory xMCF
=
57 xContext
.getServiceManager();
59 // create a new instance of the desktop
60 Object oDesktop
= xMCF
.createInstanceWithContext(
61 "com.sun.star.frame.Desktop", xContext
);
63 // get the component loader from the desktop to create a new
65 com
.sun
.star
.frame
.XComponentLoader xCLoader
=
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";
72 System
.out
.println("create new text document");
74 com
.sun
.star
.lang
.XComponent xComp
= xCLoader
.loadComponentFromURL(
75 strDoc
, "_blank", 0, szEmptyArgs
);
77 // query the new document for the XTextDocument interface
78 com
.sun
.star
.text
.XTextDocument xTextDocument
=
79 UnoRuntime
.queryInterface(
80 com
.sun
.star
.text
.XTextDocument
.class, xComp
);
82 // create some example data
83 com
.sun
.star
.text
.XText xText
= xTextDocument
.getText();
84 createExampleData( xText
);
86 // Begin section 'The structure of text documents' of the Tutorial
88 com
.sun
.star
.container
.XEnumeration xParagraphEnumeration
= null;
89 com
.sun
.star
.container
.XEnumerationAccess xParaEnumerationAccess
= null;
90 com
.sun
.star
.container
.XEnumeration xTextPortionEnum
;
91 com
.sun
.star
.text
.XTextContent xTextElement
= null;
93 System
.out
.println("create an enumeration of all paragraphs");
94 // create an enumeration access of all paragraphs of a document
95 com
.sun
.star
.container
.XEnumerationAccess xEnumerationAccess
=
96 UnoRuntime
.queryInterface(
97 com
.sun
.star
.container
.XEnumerationAccess
.class, xText
);
98 xParagraphEnumeration
= xEnumerationAccess
.createEnumeration();
100 // Loop through all paragraphs of the document
101 while ( xParagraphEnumeration
.hasMoreElements() ) {
102 xTextElement
= UnoRuntime
.queryInterface(
103 com
.sun
.star
.text
.XTextContent
.class,
104 xParagraphEnumeration
.nextElement());
105 com
.sun
.star
.lang
.XServiceInfo xServiceInfo
=
106 UnoRuntime
.queryInterface(
107 com
.sun
.star
.lang
.XServiceInfo
.class, xTextElement
);
109 // check ifs the current paragraph really a paragraph or an
110 // anchor of a frame or picture
111 if( xServiceInfo
.supportsService("com.sun.star.text.Paragraph") ) {
112 com
.sun
.star
.text
.XTextRange xTextRange
=
113 xTextElement
.getAnchor();
114 System
.out
.println( "This is a Paragraph" );
116 // create another enumeration to get all text portions of
118 xParaEnumerationAccess
=
119 UnoRuntime
.queryInterface(
120 com
.sun
.star
.container
.XEnumerationAccess
.class,
122 xTextPortionEnum
= xParaEnumerationAccess
.createEnumeration();
124 while ( xTextPortionEnum
.hasMoreElements() ) {
125 com
.sun
.star
.text
.XTextRange xTextPortion
=
126 UnoRuntime
.queryInterface(
127 com
.sun
.star
.text
.XTextRange
.class,
128 xTextPortionEnum
.nextElement());
129 System
.out
.println( "Text from the portion : "
130 + xTextPortion
.getString() );
132 com
.sun
.star
.beans
.XPropertySet xPropertySet
=
133 UnoRuntime
.queryInterface(
134 com
.sun
.star
.beans
.XPropertySet
.class,
136 System
.out
.println( "Name of the font : "
137 + xPropertySet
.getPropertyValue( "CharFontName" ) );
139 // PropertyState status of each text portion.
140 com
.sun
.star
.beans
.XPropertyState xPropertyState
=
141 UnoRuntime
.queryInterface(
142 com
.sun
.star
.beans
.XPropertyState
.class,
145 if( xPropertyState
.getPropertyState("CharWeight").equals(
146 com
.sun
.star
.beans
.PropertyState
.AMBIGUOUS_VALUE
) )
147 System
.out
.println( "- The text range contains more than one different attributes" );
149 if( xPropertyState
.getPropertyState( "CharWeight" ).equals(
150 com
.sun
.star
.beans
.PropertyState
.DIRECT_VALUE
) )
151 System
.out
.println( " - The text range contains hard formats" );
153 if( xPropertyState
.getPropertyState( "CharWeight" ).equals(
154 com
.sun
.star
.beans
.PropertyState
.DEFAULT_VALUE
) )
155 System
.out
.println( " - The text range doesn't contains hard formats" );
159 System
.out
.println( "The text portion isn't a text paragraph" );
160 // End section 'The structure of text documents' of the Tutorial
163 catch( Exception e
) {
164 e
.printStackTrace(System
.err
);
168 System
.out
.println("done");
172 public static void createExampleData( com
.sun
.star
.text
.XText xText
) {
175 xText
.setString( "This is an example sentence" );
177 com
.sun
.star
.text
.XWordCursor xWordCursor
=
178 UnoRuntime
.queryInterface(
179 com
.sun
.star
.text
.XWordCursor
.class, xText
.getStart());
181 xWordCursor
.gotoNextWord(false);
182 xWordCursor
.gotoNextWord(false);
183 xWordCursor
.gotoEndOfWord(true);
185 com
.sun
.star
.beans
.XPropertySet xPropertySet
=
186 UnoRuntime
.queryInterface(
187 com
.sun
.star
.beans
.XPropertySet
.class, xWordCursor
);
188 xPropertySet
.setPropertyValue("CharWeight",
189 Float
.valueOf( com
.sun
.star
.awt
.FontWeight
.BOLD
));
191 System
.out
.println("create example data");
193 catch( Exception e
) {
194 e
.printStackTrace(System
.err
);
201 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */