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: get the Desktop object from the office
26 //          Step 2: open an empty text document
27 //          Step 3: enter a example text
28 //          Step 4: insert some bookmarks
29 //
30 //          Chapter 5.1.1.4 Inserting bookmarks
31 //***************************************************************************
32 
33 import com.sun.star.uno.UnoRuntime;
34 
35 public class BookmarkInsertion {
36 
main(String args[])37     public static void main(String args[]) {
38         // You need the desktop to create a document
39         // The getDesktop method does the UNO bootstrapping, gets the
40         // remote servie manager and the desktop object.
41         com.sun.star.frame.XDesktop xDesktop = null;
42         xDesktop = getDesktop();
43 
44         // create text document
45         com.sun.star.text.XTextDocument xTextDocument = null;
46         xTextDocument = createTextdocument(xDesktop);
47 
48         // put example text in document
49         createExampleData(xTextDocument);
50 
51 
52         String mOffending[] = { "negro(e|es)?","bor(ed|ing)?",
53                                 "bloody?", "bleed(ing)?" };
54         String mBad[] = { "possib(le|ilit(y|ies))", "real(ly)+", "brilliant" };
55 
56         String sOffendPrefix = "Offending";
57         String sBadPrefix = "BadStyle";
58 
59         markList(xTextDocument, mOffending, sOffendPrefix);
60         markList(xTextDocument, mBad, sBadPrefix);
61 
62         System.out.println("Done");
63 
64         System.exit(0);
65     }
66 
markList(com.sun.star.text.XTextDocument xTextDocument, String mList[], String sPrefix)67     public static void markList(com.sun.star.text.XTextDocument xTextDocument,
68                                 String mList[], String sPrefix) {
69         int iCounter=0;
70         com.sun.star.uno.XInterface xSearchInterface = null;
71         com.sun.star.text.XTextRange xSearchTextRange = null;
72 
73         try {
74             for( iCounter = 0; iCounter < mList.length; iCounter++ ) {
75                 // the findfirst returns a XInterface
76                 xSearchInterface = (com.sun.star.uno.XInterface)FindFirst(
77                     xTextDocument, mList[ iCounter ] );
78 
79                 if( xSearchInterface != null ) {
80                     // get the TextRange form the XInterface
81                     xSearchTextRange = (com.sun.star.text.XTextRange)
82                         UnoRuntime.queryInterface(
83                             com.sun.star.text.XTextRange.class, xSearchInterface);
84 
85                     InsertBookmark(xTextDocument, xSearchTextRange,
86                                    sPrefix + iCounter);
87                 }
88             }
89         }
90         catch( Exception e) {
91             e.printStackTrace(System.err);
92             System.exit(1);
93         }
94     }
95 
96 
InsertBookmark(com.sun.star.text.XTextDocument xTextDocument, com.sun.star.text.XTextRange xTextRange, String sBookName)97     public static void InsertBookmark(com.sun.star.text.XTextDocument xTextDocument,
98                                       com.sun.star.text.XTextRange xTextRange,
99                                       String sBookName) {
100         // create a bookmark on a TextRange
101         try {
102             // get the MultiServiceFactory from the text document
103             com.sun.star.lang.XMultiServiceFactory xDocMSF;
104             xDocMSF = (com.sun.star.lang.XMultiServiceFactory)
105                 UnoRuntime.queryInterface(
106                     com.sun.star.lang.XMultiServiceFactory.class, xTextDocument);
107 
108             // the bookmark service is a context dependend service, you need
109             // the MultiServiceFactory from the document
110             Object xObject = xDocMSF.createInstance("com.sun.star.text.Bookmark");
111 
112             // set the name from the bookmark
113             com.sun.star.container.XNamed xNameAccess = null;
114             xNameAccess = (com.sun.star.container.XNamed)
115                 UnoRuntime.queryInterface(
116                     com.sun.star.container.XNamed.class, xObject);
117 
118             xNameAccess.setName(sBookName);
119 
120             // create a XTextContent, for the method 'insertTextContent'
121             com.sun.star.text.XTextContent xTextContent = null;
122             xTextContent = (com.sun.star.text.XTextContent)
123                 UnoRuntime.queryInterface(
124                     com.sun.star.text.XTextContent.class, xNameAccess);
125 
126             // insertTextContent need a TextRange not a cursor to specify the
127             // position from the bookmark
128             xTextDocument.getText().insertTextContent(xTextRange, xTextContent, true);
129 
130             System.out.println("Insert bookmark: " + sBookName);
131         }
132         catch( Exception e) {
133             e.printStackTrace(System.err);
134         }
135     }
136 
FindFirst( com.sun.star.text.XTextDocument xTextDocument, String sSearchString)137     protected static com.sun.star.uno.XInterface FindFirst(
138         com.sun.star.text.XTextDocument xTextDocument, String sSearchString)
139     {
140         com.sun.star.util.XSearchDescriptor xSearchDescriptor = null;
141         com.sun.star.util.XSearchable xSearchable = null;
142         com.sun.star.uno.XInterface xSearchInterface = null;
143 
144         try {
145             xSearchable = (com.sun.star.util.XSearchable)
146                 UnoRuntime.queryInterface(
147                     com.sun.star.util.XSearchable.class, xTextDocument);
148             xSearchDescriptor = (com.sun.star.util.XSearchDescriptor)
149                 xSearchable.createSearchDescriptor();
150 
151             xSearchDescriptor.setSearchString(sSearchString);
152 
153             com.sun.star.beans.XPropertySet xPropertySet = null;
154             xPropertySet = (com.sun.star.beans.XPropertySet)
155                 UnoRuntime.queryInterface(
156                     com.sun.star.beans.XPropertySet.class, xSearchDescriptor);
157 
158             xPropertySet.setPropertyValue("SearchRegularExpression",
159                                           new Boolean( true ) );
160 
161             xSearchInterface = (com.sun.star.uno.XInterface)
162                 xSearchable.findFirst(xSearchDescriptor);
163         }
164         catch( Exception e) {
165             e.printStackTrace(System.err);
166         }
167 
168         return xSearchInterface;
169     }
170 
createExampleData( com.sun.star.text.XTextDocument xTextDocument )171     protected static void createExampleData(
172         com.sun.star.text.XTextDocument xTextDocument )
173     {
174         com.sun.star.text.XTextCursor xTextCursor = null;
175 
176         try {
177             xTextCursor = (com.sun.star.text.XTextCursor)
178                 xTextDocument.getText().createTextCursor();
179 
180             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?" );
181             xTextCursor.collapseToEnd();
182             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" );
183             xTextCursor.collapseToEnd();
184             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." );
185 
186             xTextCursor.gotoStart(false);
187         }
188         catch( Exception e) {
189             e.printStackTrace(System.err);
190         }
191 
192     }
193 
getDesktop()194     public static com.sun.star.frame.XDesktop getDesktop() {
195         com.sun.star.frame.XDesktop xDesktop = null;
196         com.sun.star.lang.XMultiComponentFactory xMCF = null;
197 
198         try {
199             com.sun.star.uno.XComponentContext xContext = null;
200 
201             // get the remote office component context
202             xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
203 
204             // get the remote office service manager
205             xMCF = xContext.getServiceManager();
206             if( xMCF != null ) {
207                 System.out.println("Connected to a running office ...");
208 
209                 Object oDesktop = xMCF.createInstanceWithContext(
210                     "com.sun.star.frame.Desktop", xContext);
211                 xDesktop = (com.sun.star.frame.XDesktop) UnoRuntime.queryInterface(
212                     com.sun.star.frame.XDesktop.class, oDesktop);
213             }
214             else
215                 System.out.println( "Can't create a desktop. No connection, no remote office servicemanager available!" );
216         }
217         catch( Exception e) {
218             e.printStackTrace(System.err);
219             System.exit(1);
220         }
221 
222 
223         return xDesktop;
224     }
225 
createTextdocument( com.sun.star.frame.XDesktop xDesktop )226     public static com.sun.star.text.XTextDocument createTextdocument(
227         com.sun.star.frame.XDesktop xDesktop )
228     {
229         com.sun.star.text.XTextDocument aTextDocument = null;
230 
231         try {
232             com.sun.star.lang.XComponent xComponent = CreateNewDocument(xDesktop,
233                                                                         "swriter");
234             aTextDocument = (com.sun.star.text.XTextDocument)
235                 UnoRuntime.queryInterface(
236                     com.sun.star.text.XTextDocument.class, xComponent);
237         }
238         catch( Exception e) {
239             e.printStackTrace(System.err);
240         }
241 
242         return aTextDocument;
243     }
244 
245 
CreateNewDocument( com.sun.star.frame.XDesktop xDesktop, String sDocumentType )246     protected static com.sun.star.lang.XComponent CreateNewDocument(
247         com.sun.star.frame.XDesktop xDesktop,
248         String sDocumentType )
249     {
250         String sURL = "private:factory/" + sDocumentType;
251 
252         com.sun.star.lang.XComponent xComponent = null;
253         com.sun.star.frame.XComponentLoader xComponentLoader = null;
254         com.sun.star.beans.PropertyValue xValues[] =
255             new com.sun.star.beans.PropertyValue[1];
256         com.sun.star.beans.PropertyValue xEmptyArgs[] =
257             new com.sun.star.beans.PropertyValue[0];
258 
259         try {
260             xComponentLoader = (com.sun.star.frame.XComponentLoader)
261                 UnoRuntime.queryInterface(
262                     com.sun.star.frame.XComponentLoader.class, xDesktop);
263 
264             xComponent  = xComponentLoader.loadComponentFromURL(
265                 sURL, "_blank", 0, xEmptyArgs);
266         }
267         catch( Exception e) {
268             e.printStackTrace(System.out);
269         }
270 
271         return xComponent ;
272     }
273 }
274