1 /*************************************************************************
3 * $RCSfile: BookmarkInsertion.java,v $
7 * last change: $Author: rt $ $Date: 2005-01-31 17:16:38 $
9 * The Contents of this file are made available subject to the terms of
12 * Copyright (c) 2003 by Sun Microsystems, Inc.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *************************************************************************/
41 //***************************************************************************
42 // comment: Step 1: get the Desktop object from the office
43 // Step 2: open an empty text document
44 // Step 3: enter a example text
45 // Step 4: insert some bookmarks
47 // Chapter 5.1.1.4 Inserting bookmarks
48 //***************************************************************************
50 import com
.sun
.star
.uno
.UnoRuntime
;
52 public class BookmarkInsertion
{
54 public static void main(String args
[]) {
55 // You need the desktop to create a document
56 // The getDesktop method does the UNO bootstrapping, gets the
57 // remote servie manager and the desktop object.
58 com
.sun
.star
.frame
.XDesktop xDesktop
= null;
59 xDesktop
= getDesktop();
61 // create text document
62 com
.sun
.star
.text
.XTextDocument xTextDocument
= null;
63 xTextDocument
= createTextdocument(xDesktop
);
65 // put example text in document
66 createExampleData(xTextDocument
);
69 String mOffending
[] = { "negro(e|es)?","bor(ed|ing)?",
70 "bloody?", "bleed(ing)?" };
71 String mBad
[] = { "possib(le|ilit(y|ies))", "real(ly)+", "brilliant" };
73 String sOffendPrefix
= "Offending";
74 String sBadPrefix
= "BadStyle";
76 markList(xTextDocument
, mOffending
, sOffendPrefix
);
77 markList(xTextDocument
, mBad
, sBadPrefix
);
79 System
.out
.println("Done");
84 public static void markList(com
.sun
.star
.text
.XTextDocument xTextDocument
,
85 String mList
[], String sPrefix
) {
87 com
.sun
.star
.uno
.XInterface xSearchInterface
= null;
88 com
.sun
.star
.text
.XTextRange xSearchTextRange
= null;
91 for( iCounter
= 0; iCounter
< mList
.length
; iCounter
++ ) {
92 // the findfirst returns a XInterface
93 xSearchInterface
= (com
.sun
.star
.uno
.XInterface
)FindFirst(
94 xTextDocument
, mList
[ iCounter
] );
96 if( xSearchInterface
!= null ) {
97 // get the TextRange form the XInterface
98 xSearchTextRange
= (com
.sun
.star
.text
.XTextRange
)
99 UnoRuntime
.queryInterface(
100 com
.sun
.star
.text
.XTextRange
.class, xSearchInterface
);
102 InsertBookmark(xTextDocument
, xSearchTextRange
,
107 catch( Exception e
) {
108 e
.printStackTrace(System
.err
);
114 public static void InsertBookmark(com
.sun
.star
.text
.XTextDocument xTextDocument
,
115 com
.sun
.star
.text
.XTextRange xTextRange
,
117 // create a bookmark on a TextRange
119 // get the MultiServiceFactory from the text document
120 com
.sun
.star
.lang
.XMultiServiceFactory xDocMSF
;
121 xDocMSF
= (com
.sun
.star
.lang
.XMultiServiceFactory
)
122 UnoRuntime
.queryInterface(
123 com
.sun
.star
.lang
.XMultiServiceFactory
.class, xTextDocument
);
125 // the bookmark service is a context dependend service, you need
126 // the MultiServiceFactory from the document
127 Object xObject
= xDocMSF
.createInstance("com.sun.star.text.Bookmark");
129 // set the name from the bookmark
130 com
.sun
.star
.container
.XNamed xNameAccess
= null;
131 xNameAccess
= (com
.sun
.star
.container
.XNamed
)
132 UnoRuntime
.queryInterface(
133 com
.sun
.star
.container
.XNamed
.class, xObject
);
135 xNameAccess
.setName(sBookName
);
137 // create a XTextContent, for the method 'insertTextContent'
138 com
.sun
.star
.text
.XTextContent xTextContent
= null;
139 xTextContent
= (com
.sun
.star
.text
.XTextContent
)
140 UnoRuntime
.queryInterface(
141 com
.sun
.star
.text
.XTextContent
.class, xNameAccess
);
143 // insertTextContent need a TextRange not a cursor to specify the
144 // position from the bookmark
145 xTextDocument
.getText().insertTextContent(xTextRange
, xTextContent
, true);
147 System
.out
.println("Insert bookmark: " + sBookName
);
149 catch( Exception e
) {
150 e
.printStackTrace(System
.err
);
154 protected static com
.sun
.star
.uno
.XInterface
FindFirst(
155 com
.sun
.star
.text
.XTextDocument xTextDocument
, String sSearchString
)
157 com
.sun
.star
.util
.XSearchDescriptor xSearchDescriptor
= null;
158 com
.sun
.star
.util
.XSearchable xSearchable
= null;
159 com
.sun
.star
.uno
.XInterface xSearchInterface
= null;
162 xSearchable
= (com
.sun
.star
.util
.XSearchable
)
163 UnoRuntime
.queryInterface(
164 com
.sun
.star
.util
.XSearchable
.class, xTextDocument
);
165 xSearchDescriptor
= (com
.sun
.star
.util
.XSearchDescriptor
)
166 xSearchable
.createSearchDescriptor();
168 xSearchDescriptor
.setSearchString(sSearchString
);
170 com
.sun
.star
.beans
.XPropertySet xPropertySet
= null;
171 xPropertySet
= (com
.sun
.star
.beans
.XPropertySet
)
172 UnoRuntime
.queryInterface(
173 com
.sun
.star
.beans
.XPropertySet
.class, xSearchDescriptor
);
175 xPropertySet
.setPropertyValue("SearchRegularExpression",
176 new Boolean( true ) );
178 xSearchInterface
= (com
.sun
.star
.uno
.XInterface
)
179 xSearchable
.findFirst(xSearchDescriptor
);
181 catch( Exception e
) {
182 e
.printStackTrace(System
.err
);
185 return xSearchInterface
;
188 protected static void createExampleData(
189 com
.sun
.star
.text
.XTextDocument xTextDocument
)
191 com
.sun
.star
.text
.XTextCursor xTextCursor
= null;
194 xTextCursor
= (com
.sun
.star
.text
.XTextCursor
)
195 xTextDocument
.getText().createTextCursor();
197 xTextCursor
.setString( "He heard quiet steps behind him. That didn't bode well. Who could be following him this late at night and in this deadbeat part of town? And at this particular moment, just after he pulled off the big time and was making off with the greenbacks. Was there another crook who'd had the same idea, and was now watching him and waiting for a chance to grab the fruit of his labor?" );
198 xTextCursor
.collapseToEnd();
199 xTextCursor
.setString( "Or did the steps behind him mean that one of many bloody officers in town was on to him and just waiting to pounce and snap those cuffs on his wrists? He nervously looked all around. Suddenly he saw the alley. Like lightening he darted off to the left and disappeared between the two warehouses almost falling over the trash can lying in the middle of the sidewalk. He tried to nervously tap his way along in the inky darkness and suddenly stiffened: it was a dead-end, he would have to go back the way he had come" );
200 xTextCursor
.collapseToEnd();
201 xTextCursor
.setString( "The steps got louder and louder, he saw the black outline of a figure coming around the corner. Is this the end of the line? he thought pressing himself back against the wall trying to make himself invisible in the dark, was all that planning and energy wasted? He was dripping with sweat now, cold and wet, he could smell the brilliant fear coming off his clothes. Suddenly next to him, with a barely noticeable squeak, a door swung quietly to and fro in the night's breeze." );
203 xTextCursor
.gotoStart(false);
205 catch( Exception e
) {
206 e
.printStackTrace(System
.err
);
211 public static com
.sun
.star
.frame
.XDesktop
getDesktop() {
212 com
.sun
.star
.frame
.XDesktop xDesktop
= null;
213 com
.sun
.star
.lang
.XMultiComponentFactory xMCF
= null;
216 com
.sun
.star
.uno
.XComponentContext xContext
= null;
218 // get the remote office component context
219 xContext
= com
.sun
.star
.comp
.helper
.Bootstrap
.bootstrap();
221 // get the remote office service manager
222 xMCF
= xContext
.getServiceManager();
224 System
.out
.println("Connected to a running office ...");
226 Object oDesktop
= xMCF
.createInstanceWithContext(
227 "com.sun.star.frame.Desktop", xContext
);
228 xDesktop
= (com
.sun
.star
.frame
.XDesktop
) UnoRuntime
.queryInterface(
229 com
.sun
.star
.frame
.XDesktop
.class, oDesktop
);
232 System
.out
.println( "Can't create a desktop. No connection, no remote office servicemanager available!" );
234 catch( Exception e
) {
235 e
.printStackTrace(System
.err
);
243 public static com
.sun
.star
.text
.XTextDocument
createTextdocument(
244 com
.sun
.star
.frame
.XDesktop xDesktop
)
246 com
.sun
.star
.text
.XTextDocument aTextDocument
= null;
249 com
.sun
.star
.lang
.XComponent xComponent
= CreateNewDocument(xDesktop
,
251 aTextDocument
= (com
.sun
.star
.text
.XTextDocument
)
252 UnoRuntime
.queryInterface(
253 com
.sun
.star
.text
.XTextDocument
.class, xComponent
);
255 catch( Exception e
) {
256 e
.printStackTrace(System
.err
);
259 return aTextDocument
;
263 protected static com
.sun
.star
.lang
.XComponent
CreateNewDocument(
264 com
.sun
.star
.frame
.XDesktop xDesktop
,
265 String sDocumentType
)
267 String sURL
= "private:factory/" + sDocumentType
;
269 com
.sun
.star
.lang
.XComponent xComponent
= null;
270 com
.sun
.star
.frame
.XComponentLoader xComponentLoader
= null;
271 com
.sun
.star
.beans
.PropertyValue xValues
[] =
272 new com
.sun
.star
.beans
.PropertyValue
[1];
273 com
.sun
.star
.beans
.PropertyValue xEmptyArgs
[] =
274 new com
.sun
.star
.beans
.PropertyValue
[0];
277 xComponentLoader
= (com
.sun
.star
.frame
.XComponentLoader
)
278 UnoRuntime
.queryInterface(
279 com
.sun
.star
.frame
.XComponentLoader
.class, xDesktop
);
281 xComponent
= xComponentLoader
.loadComponentFromURL(
282 sURL
, "_blank", 0, xEmptyArgs
);
284 catch( Exception e
) {
285 e
.printStackTrace(System
.out
);