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