Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / odk / examples / java / Text / BookmarkInsertion.java
bloba857b36c28742799832b67652d5e0e8bf224e926
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
5 * the BSD license.
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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: get the Desktop object from the office
38 // Step 2: open an empty text document
39 // Step 3: enter a example text
40 // Step 4: insert some bookmarks
42 // Chapter 5.1.1.4 Inserting bookmarks
45 import com.sun.star.uno.UnoRuntime;
47 public class BookmarkInsertion {
49 public static void main(String args[]) {
50 // You need the desktop to create a document
51 // The getDesktop method does the UNO bootstrapping, gets the
52 // remote servie manager and the desktop object.
53 com.sun.star.frame.XDesktop xDesktop = null;
54 xDesktop = getDesktop();
56 // create text document
57 com.sun.star.text.XTextDocument xTextDocument = null;
58 xTextDocument = createTextdocument(xDesktop);
60 // put example text in document
61 createExampleData(xTextDocument);
64 String mOffending[] = { "negro(e|es)?","bor(ed|ing)?",
65 "bloody?", "bleed(ing)?" };
66 String mBad[] = { "possib(le|ilit(y|ies))", "real(ly)+", "brilliant" };
68 String sOffendPrefix = "Offending";
69 String sBadPrefix = "BadStyle";
71 markList(xTextDocument, mOffending, sOffendPrefix);
72 markList(xTextDocument, mBad, sBadPrefix);
74 System.out.println("Done");
76 System.exit(0);
79 public static void markList(com.sun.star.text.XTextDocument xTextDocument,
80 String mList[], String sPrefix) {
81 int iCounter=0;
82 com.sun.star.uno.XInterface xSearchInterface = null;
83 com.sun.star.text.XTextRange xSearchTextRange = null;
85 try {
86 for( iCounter = 0; iCounter < mList.length; iCounter++ ) {
87 // the findfirst returns a XInterface
88 xSearchInterface = FindFirst(
89 xTextDocument, mList[ iCounter ] );
91 if( xSearchInterface != null ) {
92 // get the TextRange form the XInterface
93 xSearchTextRange = UnoRuntime.queryInterface(
94 com.sun.star.text.XTextRange.class, xSearchInterface);
96 InsertBookmark(xTextDocument, xSearchTextRange,
97 sPrefix + iCounter);
101 catch( Exception e) {
102 e.printStackTrace(System.err);
103 System.exit(1);
108 public static void InsertBookmark(com.sun.star.text.XTextDocument xTextDocument,
109 com.sun.star.text.XTextRange xTextRange,
110 String sBookName) {
111 // create a bookmark on a TextRange
112 try {
113 // get the MultiServiceFactory from the text document
114 com.sun.star.lang.XMultiServiceFactory xDocMSF;
115 xDocMSF = UnoRuntime.queryInterface(
116 com.sun.star.lang.XMultiServiceFactory.class, xTextDocument);
118 // the bookmark service is a context dependent service, you need
119 // the MultiServiceFactory from the document
120 Object xObject = xDocMSF.createInstance("com.sun.star.text.Bookmark");
122 // set the name from the bookmark
123 com.sun.star.container.XNamed xNameAccess = null;
124 xNameAccess = UnoRuntime.queryInterface(
125 com.sun.star.container.XNamed.class, xObject);
127 xNameAccess.setName(sBookName);
129 // create a XTextContent, for the method 'insertTextContent'
130 com.sun.star.text.XTextContent xTextContent = null;
131 xTextContent = UnoRuntime.queryInterface(
132 com.sun.star.text.XTextContent.class, xNameAccess);
134 // insertTextContent need a TextRange not a cursor to specify the
135 // position from the bookmark
136 xTextDocument.getText().insertTextContent(xTextRange, xTextContent, true);
138 System.out.println("Insert bookmark: " + sBookName);
140 catch( Exception e) {
141 e.printStackTrace(System.err);
145 protected static com.sun.star.uno.XInterface FindFirst(
146 com.sun.star.text.XTextDocument xTextDocument, String sSearchString)
148 com.sun.star.util.XSearchDescriptor xSearchDescriptor = null;
149 com.sun.star.util.XSearchable xSearchable = null;
150 com.sun.star.uno.XInterface xSearchInterface = null;
152 try {
153 xSearchable = UnoRuntime.queryInterface(
154 com.sun.star.util.XSearchable.class, xTextDocument);
155 xSearchDescriptor = xSearchable.createSearchDescriptor();
157 xSearchDescriptor.setSearchString(sSearchString);
159 com.sun.star.beans.XPropertySet xPropertySet = null;
160 xPropertySet = UnoRuntime.queryInterface(
161 com.sun.star.beans.XPropertySet.class, xSearchDescriptor);
163 xPropertySet.setPropertyValue("SearchRegularExpression",
164 Boolean.TRUE );
166 xSearchInterface = (com.sun.star.uno.XInterface)
167 xSearchable.findFirst(xSearchDescriptor);
169 catch( Exception e) {
170 e.printStackTrace(System.err);
173 return xSearchInterface;
176 protected static void createExampleData(
177 com.sun.star.text.XTextDocument xTextDocument )
179 com.sun.star.text.XTextCursor xTextCursor = null;
181 try {
182 xTextCursor = xTextDocument.getText().createTextCursor();
184 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?" );
185 xTextCursor.collapseToEnd();
186 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 lightning 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" );
187 xTextCursor.collapseToEnd();
188 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." );
190 xTextCursor.gotoStart(false);
192 catch( Exception e) {
193 e.printStackTrace(System.err);
198 public static com.sun.star.frame.XDesktop getDesktop() {
199 com.sun.star.frame.XDesktop xDesktop = null;
200 com.sun.star.lang.XMultiComponentFactory xMCF = null;
202 try {
203 com.sun.star.uno.XComponentContext xContext = null;
205 // get the remote office component context
206 xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
208 // get the remote office service manager
209 xMCF = xContext.getServiceManager();
210 if( xMCF != null ) {
211 System.out.println("Connected to a running office ...");
213 Object oDesktop = xMCF.createInstanceWithContext(
214 "com.sun.star.frame.Desktop", xContext);
215 xDesktop = UnoRuntime.queryInterface(
216 com.sun.star.frame.XDesktop.class, oDesktop);
218 else
219 System.out.println( "Can't create a desktop. No connection, no remote office servicemanager available!" );
221 catch( Exception e) {
222 e.printStackTrace(System.err);
223 System.exit(1);
227 return xDesktop;
230 public static com.sun.star.text.XTextDocument createTextdocument(
231 com.sun.star.frame.XDesktop xDesktop )
233 com.sun.star.text.XTextDocument aTextDocument = null;
235 try {
236 com.sun.star.lang.XComponent xComponent = CreateNewDocument(xDesktop,
237 "swriter");
238 aTextDocument = UnoRuntime.queryInterface(
239 com.sun.star.text.XTextDocument.class, xComponent);
241 catch( Exception e) {
242 e.printStackTrace(System.err);
245 return aTextDocument;
249 protected static com.sun.star.lang.XComponent CreateNewDocument(
250 com.sun.star.frame.XDesktop xDesktop,
251 String sDocumentType )
253 String sURL = "private:factory/" + sDocumentType;
255 com.sun.star.lang.XComponent xComponent = null;
256 com.sun.star.frame.XComponentLoader xComponentLoader = null;
257 com.sun.star.beans.PropertyValue xEmptyArgs[] =
258 new com.sun.star.beans.PropertyValue[0];
260 try {
261 xComponentLoader = UnoRuntime.queryInterface(
262 com.sun.star.frame.XComponentLoader.class, xDesktop);
264 xComponent = xComponentLoader.loadComponentFromURL(
265 sURL, "_blank", 0, xEmptyArgs);
267 catch( Exception e) {
268 e.printStackTrace(System.err);
271 return xComponent ;
275 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */