1 /*************************************************************************
2  *
3  *  The Contents of this file are made available subject to the terms of
4  *  the BSD license.
5  *
6  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7  *  All rights reserved.
8  *
9  *  Redistribution and use in source and binary forms, with or without
10  *  modification, are permitted provided that the following conditions
11  *  are met:
12  *  1. Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *  2. Redistributions in binary form must reproduce the above copyright
15  *     notice, this list of conditions and the following disclaimer in the
16  *     documentation and/or other materials provided with the distribution.
17  *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
18  *     contributors may be used to endorse or promote products derived
19  *     from this software without specific prior written permission.
20  *
21  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  *************************************************************************/
34 
35 //***************************************************************************
36 // comment: Step 1: get the Desktop object from the office
37 //          Step 2: open an empty text document
38 //          Step 3: enter a example text
39 //          Step 4: insert some bookmarks
40 //
41 //          Chapter 5.1.1.4 Inserting bookmarks
42 //***************************************************************************
43 
44 import com.sun.star.uno.UnoRuntime;
45 
46 public class BookmarkInsertion {
47 
48     public static void main(String args[]) {
49         // You need the desktop to create a document
50         // The getDesktop method does the UNO bootstrapping, gets the
51         // remote servie manager and the desktop object.
52         com.sun.star.frame.XDesktop xDesktop = null;
53         xDesktop = getDesktop();
54 
55         // create text document
56         com.sun.star.text.XTextDocument xTextDocument = null;
57         xTextDocument = createTextdocument(xDesktop);
58 
59         // put example text in document
60         createExampleData(xTextDocument);
61 
62 
63         String mOffending[] = { "negro(e|es)?","bor(ed|ing)?",
64                                 "bloody?", "bleed(ing)?" };
65         String mBad[] = { "possib(le|ilit(y|ies))", "real(ly)+", "brilliant" };
66 
67         String sOffendPrefix = "Offending";
68         String sBadPrefix = "BadStyle";
69 
70         markList(xTextDocument, mOffending, sOffendPrefix);
71         markList(xTextDocument, mBad, sBadPrefix);
72 
73         System.out.println("Done");
74 
75         System.exit(0);
76     }
77 
78     public static void markList(com.sun.star.text.XTextDocument xTextDocument,
79                                 String mList[], String sPrefix) {
80         int iCounter=0;
81         com.sun.star.uno.XInterface xSearchInterface = null;
82         com.sun.star.text.XTextRange xSearchTextRange = null;
83 
84         try {
85             for( iCounter = 0; iCounter < mList.length; iCounter++ ) {
86                 // the findfirst returns a XInterface
87                 xSearchInterface = (com.sun.star.uno.XInterface)FindFirst(
88                     xTextDocument, mList[ iCounter ] );
89 
90                 if( xSearchInterface != null ) {
91                     // get the TextRange form the XInterface
92                     xSearchTextRange = (com.sun.star.text.XTextRange)
93                         UnoRuntime.queryInterface(
94                             com.sun.star.text.XTextRange.class, xSearchInterface);
95 
96                     InsertBookmark(xTextDocument, xSearchTextRange,
97                                    sPrefix + iCounter);
98                 }
99             }
100         }
101         catch( Exception e) {
102             e.printStackTrace(System.err);
103             System.exit(1);
104         }
105     }
106 
107 
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 = (com.sun.star.lang.XMultiServiceFactory)
116                 UnoRuntime.queryInterface(
117                     com.sun.star.lang.XMultiServiceFactory.class, xTextDocument);
118 
119             // the bookmark service is a context dependend service, you need
120             // the MultiServiceFactory from the document
121             Object xObject = xDocMSF.createInstance("com.sun.star.text.Bookmark");
122 
123             // set the name from the bookmark
124             com.sun.star.container.XNamed xNameAccess = null;
125             xNameAccess = (com.sun.star.container.XNamed)
126                 UnoRuntime.queryInterface(
127                     com.sun.star.container.XNamed.class, xObject);
128 
129             xNameAccess.setName(sBookName);
130 
131             // create a XTextContent, for the method 'insertTextContent'
132             com.sun.star.text.XTextContent xTextContent = null;
133             xTextContent = (com.sun.star.text.XTextContent)
134                 UnoRuntime.queryInterface(
135                     com.sun.star.text.XTextContent.class, xNameAccess);
136 
137             // insertTextContent need a TextRange not a cursor to specify the
138             // position from the bookmark
139             xTextDocument.getText().insertTextContent(xTextRange, xTextContent, true);
140 
141             System.out.println("Insert bookmark: " + sBookName);
142         }
143         catch( Exception e) {
144             e.printStackTrace(System.err);
145         }
146     }
147 
148     protected static com.sun.star.uno.XInterface FindFirst(
149         com.sun.star.text.XTextDocument xTextDocument, String sSearchString)
150     {
151         com.sun.star.util.XSearchDescriptor xSearchDescriptor = null;
152         com.sun.star.util.XSearchable xSearchable = null;
153         com.sun.star.uno.XInterface xSearchInterface = null;
154 
155         try {
156             xSearchable = (com.sun.star.util.XSearchable)
157                 UnoRuntime.queryInterface(
158                     com.sun.star.util.XSearchable.class, xTextDocument);
159             xSearchDescriptor = (com.sun.star.util.XSearchDescriptor)
160                 xSearchable.createSearchDescriptor();
161 
162             xSearchDescriptor.setSearchString(sSearchString);
163 
164             com.sun.star.beans.XPropertySet xPropertySet = null;
165             xPropertySet = (com.sun.star.beans.XPropertySet)
166                 UnoRuntime.queryInterface(
167                     com.sun.star.beans.XPropertySet.class, xSearchDescriptor);
168 
169             xPropertySet.setPropertyValue("SearchRegularExpression",
170                                           new Boolean( true ) );
171 
172             xSearchInterface = (com.sun.star.uno.XInterface)
173                 xSearchable.findFirst(xSearchDescriptor);
174         }
175         catch( Exception e) {
176             e.printStackTrace(System.err);
177         }
178 
179         return xSearchInterface;
180     }
181 
182     protected static void createExampleData(
183         com.sun.star.text.XTextDocument xTextDocument )
184     {
185         com.sun.star.text.XTextCursor xTextCursor = null;
186 
187         try {
188             xTextCursor = (com.sun.star.text.XTextCursor)
189                 xTextDocument.getText().createTextCursor();
190 
191             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?" );
192             xTextCursor.collapseToEnd();
193             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" );
194             xTextCursor.collapseToEnd();
195             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." );
196 
197             xTextCursor.gotoStart(false);
198         }
199         catch( Exception e) {
200             e.printStackTrace(System.err);
201         }
202 
203     }
204 
205     public static com.sun.star.frame.XDesktop getDesktop() {
206         com.sun.star.frame.XDesktop xDesktop = null;
207         com.sun.star.lang.XMultiComponentFactory xMCF = null;
208 
209         try {
210             com.sun.star.uno.XComponentContext xContext = null;
211 
212             // get the remote office component context
213             xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
214 
215             // get the remote office service manager
216             xMCF = xContext.getServiceManager();
217             if( xMCF != null ) {
218                 System.out.println("Connected to a running office ...");
219 
220                 Object oDesktop = xMCF.createInstanceWithContext(
221                     "com.sun.star.frame.Desktop", xContext);
222                 xDesktop = (com.sun.star.frame.XDesktop) UnoRuntime.queryInterface(
223                     com.sun.star.frame.XDesktop.class, oDesktop);
224             }
225             else
226                 System.out.println( "Can't create a desktop. No connection, no remote office servicemanager available!" );
227         }
228         catch( Exception e) {
229             e.printStackTrace(System.err);
230             System.exit(1);
231         }
232 
233 
234         return xDesktop;
235     }
236 
237     public static com.sun.star.text.XTextDocument createTextdocument(
238         com.sun.star.frame.XDesktop xDesktop )
239     {
240         com.sun.star.text.XTextDocument aTextDocument = null;
241 
242         try {
243             com.sun.star.lang.XComponent xComponent = CreateNewDocument(xDesktop,
244                                                                         "swriter");
245             aTextDocument = (com.sun.star.text.XTextDocument)
246                 UnoRuntime.queryInterface(
247                     com.sun.star.text.XTextDocument.class, xComponent);
248         }
249         catch( Exception e) {
250             e.printStackTrace(System.err);
251         }
252 
253         return aTextDocument;
254     }
255 
256 
257     protected static com.sun.star.lang.XComponent CreateNewDocument(
258         com.sun.star.frame.XDesktop xDesktop,
259         String sDocumentType )
260     {
261         String sURL = "private:factory/" + sDocumentType;
262 
263         com.sun.star.lang.XComponent xComponent = null;
264         com.sun.star.frame.XComponentLoader xComponentLoader = null;
265         com.sun.star.beans.PropertyValue xValues[] =
266             new com.sun.star.beans.PropertyValue[1];
267         com.sun.star.beans.PropertyValue xEmptyArgs[] =
268             new com.sun.star.beans.PropertyValue[0];
269 
270         try {
271             xComponentLoader = (com.sun.star.frame.XComponentLoader)
272                 UnoRuntime.queryInterface(
273                     com.sun.star.frame.XComponentLoader.class, xDesktop);
274 
275             xComponent  = xComponentLoader.loadComponentFromURL(
276                 sURL, "_blank", 0, xEmptyArgs);
277         }
278         catch( Exception e) {
279             e.printStackTrace(System.out);
280         }
281 
282         return xComponent ;
283     }
284 }
285