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