Branch libreoffice-5-0-4
[LibreOffice.git] / odk / examples / java / Text / BookmarkInsertion.java
blob3358e23ebcbbea085de33ab06537bb778c04b4f6
1 /*************************************************************************
3 * The Contents of this file are made available subject to the terms of
4 * the BSD license.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * All rights reserved.
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.
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.
33 *************************************************************************/
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
41 // Chapter 5.1.1.4 Inserting bookmarks
44 import com.sun.star.uno.UnoRuntime;
46 public class BookmarkInsertion {
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();
55 // create text document
56 com.sun.star.text.XTextDocument xTextDocument = null;
57 xTextDocument = createTextdocument(xDesktop);
59 // put example text in document
60 createExampleData(xTextDocument);
63 String mOffending[] = { "negro(e|es)?","bor(ed|ing)?",
64 "bloody?", "bleed(ing)?" };
65 String mBad[] = { "possib(le|ilit(y|ies))", "real(ly)+", "brilliant" };
67 String sOffendPrefix = "Offending";
68 String sBadPrefix = "BadStyle";
70 markList(xTextDocument, mOffending, sOffendPrefix);
71 markList(xTextDocument, mBad, sBadPrefix);
73 System.out.println("Done");
75 System.exit(0);
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;
84 try {
85 for( iCounter = 0; iCounter < mList.length; iCounter++ ) {
86 // the findfirst returns a XInterface
87 xSearchInterface = FindFirst(
88 xTextDocument, mList[ iCounter ] );
90 if( xSearchInterface != null ) {
91 // get the TextRange form the XInterface
92 xSearchTextRange = UnoRuntime.queryInterface(
93 com.sun.star.text.XTextRange.class, xSearchInterface);
95 InsertBookmark(xTextDocument, xSearchTextRange,
96 sPrefix + iCounter);
100 catch( Exception e) {
101 e.printStackTrace(System.err);
102 System.exit(1);
107 public static void InsertBookmark(com.sun.star.text.XTextDocument xTextDocument,
108 com.sun.star.text.XTextRange xTextRange,
109 String sBookName) {
110 // create a bookmark on a TextRange
111 try {
112 // get the MultiServiceFactory from the text document
113 com.sun.star.lang.XMultiServiceFactory xDocMSF;
114 xDocMSF = UnoRuntime.queryInterface(
115 com.sun.star.lang.XMultiServiceFactory.class, xTextDocument);
117 // the bookmark service is a context dependent service, you need
118 // the MultiServiceFactory from the document
119 Object xObject = xDocMSF.createInstance("com.sun.star.text.Bookmark");
121 // set the name from the bookmark
122 com.sun.star.container.XNamed xNameAccess = null;
123 xNameAccess = UnoRuntime.queryInterface(
124 com.sun.star.container.XNamed.class, xObject);
126 xNameAccess.setName(sBookName);
128 // create a XTextContent, for the method 'insertTextContent'
129 com.sun.star.text.XTextContent xTextContent = null;
130 xTextContent = UnoRuntime.queryInterface(
131 com.sun.star.text.XTextContent.class, xNameAccess);
133 // insertTextContent need a TextRange not a cursor to specify the
134 // position from the bookmark
135 xTextDocument.getText().insertTextContent(xTextRange, xTextContent, true);
137 System.out.println("Insert bookmark: " + sBookName);
139 catch( Exception e) {
140 e.printStackTrace(System.err);
144 protected static com.sun.star.uno.XInterface FindFirst(
145 com.sun.star.text.XTextDocument xTextDocument, String sSearchString)
147 com.sun.star.util.XSearchDescriptor xSearchDescriptor = null;
148 com.sun.star.util.XSearchable xSearchable = null;
149 com.sun.star.uno.XInterface xSearchInterface = null;
151 try {
152 xSearchable = UnoRuntime.queryInterface(
153 com.sun.star.util.XSearchable.class, xTextDocument);
154 xSearchDescriptor = xSearchable.createSearchDescriptor();
156 xSearchDescriptor.setSearchString(sSearchString);
158 com.sun.star.beans.XPropertySet xPropertySet = null;
159 xPropertySet = UnoRuntime.queryInterface(
160 com.sun.star.beans.XPropertySet.class, xSearchDescriptor);
162 xPropertySet.setPropertyValue("SearchRegularExpression",
163 Boolean.TRUE );
165 xSearchInterface = (com.sun.star.uno.XInterface)
166 xSearchable.findFirst(xSearchDescriptor);
168 catch( Exception e) {
169 e.printStackTrace(System.err);
172 return xSearchInterface;
175 protected static void createExampleData(
176 com.sun.star.text.XTextDocument xTextDocument )
178 com.sun.star.text.XTextCursor xTextCursor = null;
180 try {
181 xTextCursor = xTextDocument.getText().createTextCursor();
183 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?" );
184 xTextCursor.collapseToEnd();
185 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" );
186 xTextCursor.collapseToEnd();
187 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." );
189 xTextCursor.gotoStart(false);
191 catch( Exception e) {
192 e.printStackTrace(System.err);
197 public static com.sun.star.frame.XDesktop getDesktop() {
198 com.sun.star.frame.XDesktop xDesktop = null;
199 com.sun.star.lang.XMultiComponentFactory xMCF = null;
201 try {
202 com.sun.star.uno.XComponentContext xContext = null;
204 // get the remote office component context
205 xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
207 // get the remote office service manager
208 xMCF = xContext.getServiceManager();
209 if( xMCF != null ) {
210 System.out.println("Connected to a running office ...");
212 Object oDesktop = xMCF.createInstanceWithContext(
213 "com.sun.star.frame.Desktop", xContext);
214 xDesktop = UnoRuntime.queryInterface(
215 com.sun.star.frame.XDesktop.class, oDesktop);
217 else
218 System.out.println( "Can't create a desktop. No connection, no remote office servicemanager available!" );
220 catch( Exception e) {
221 e.printStackTrace(System.err);
222 System.exit(1);
226 return xDesktop;
229 public static com.sun.star.text.XTextDocument createTextdocument(
230 com.sun.star.frame.XDesktop xDesktop )
232 com.sun.star.text.XTextDocument aTextDocument = null;
234 try {
235 com.sun.star.lang.XComponent xComponent = CreateNewDocument(xDesktop,
236 "swriter");
237 aTextDocument = UnoRuntime.queryInterface(
238 com.sun.star.text.XTextDocument.class, xComponent);
240 catch( Exception e) {
241 e.printStackTrace(System.err);
244 return aTextDocument;
248 protected static com.sun.star.lang.XComponent CreateNewDocument(
249 com.sun.star.frame.XDesktop xDesktop,
250 String sDocumentType )
252 String sURL = "private:factory/" + sDocumentType;
254 com.sun.star.lang.XComponent xComponent = null;
255 com.sun.star.frame.XComponentLoader xComponentLoader = null;
256 com.sun.star.beans.PropertyValue xEmptyArgs[] =
257 new com.sun.star.beans.PropertyValue[0];
259 try {
260 xComponentLoader = UnoRuntime.queryInterface(
261 com.sun.star.frame.XComponentLoader.class, xDesktop);
263 xComponent = xComponentLoader.loadComponentFromURL(
264 sURL, "_blank", 0, xEmptyArgs);
266 catch( Exception e) {
267 e.printStackTrace(System.err);
270 return xComponent ;