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: replace some english spelled words with US spelled 29cdf0e10cSrcweir //*************************************************************************** 30cdf0e10cSrcweir 31cdf0e10cSrcweir 32cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 33cdf0e10cSrcweir 34cdf0e10cSrcweir public class TextReplace { 35cdf0e10cSrcweir main(String args[])36cdf0e10cSrcweir public static void main(String args[]) { 37cdf0e10cSrcweir // You need the desktop to create a document 38cdf0e10cSrcweir // The getDesktop method does the UNO bootstrapping, gets the 39cdf0e10cSrcweir // remote servie manager and the desktop object. 40cdf0e10cSrcweir com.sun.star.frame.XDesktop xDesktop = null; 41cdf0e10cSrcweir xDesktop = getDesktop(); 42cdf0e10cSrcweir 43cdf0e10cSrcweir com.sun.star.text.XTextDocument xTextDocument = 44cdf0e10cSrcweir createTextdocument( xDesktop ); 45cdf0e10cSrcweir 46cdf0e10cSrcweir createExampleData( xTextDocument ); 47cdf0e10cSrcweir 48cdf0e10cSrcweir String mBritishWords[] = {"colour", "neighbour", "centre", "behaviour", 49cdf0e10cSrcweir "metre", "through" }; 50cdf0e10cSrcweir String mUSWords[] = { "color", "neighbor", "center", "behavior", 51cdf0e10cSrcweir "meter", "thru" }; 52cdf0e10cSrcweir 53cdf0e10cSrcweir try { 54cdf0e10cSrcweir com.sun.star.util.XReplaceDescriptor xReplaceDescr = null; 55cdf0e10cSrcweir com.sun.star.util.XSearchDescriptor xSearchDescriptor = null; 56cdf0e10cSrcweir com.sun.star.util.XReplaceable xReplaceable = null; 57cdf0e10cSrcweir 58cdf0e10cSrcweir xReplaceable = (com.sun.star.util.XReplaceable) 59cdf0e10cSrcweir UnoRuntime.queryInterface( 60cdf0e10cSrcweir com.sun.star.util.XReplaceable.class, xTextDocument); 61cdf0e10cSrcweir 62cdf0e10cSrcweir // You need a descriptor to set properies for Replace 63cdf0e10cSrcweir xReplaceDescr = (com.sun.star.util.XReplaceDescriptor) 64cdf0e10cSrcweir xReplaceable.createReplaceDescriptor(); 65cdf0e10cSrcweir 66cdf0e10cSrcweir System.out.println("Change all occurrences of ..."); 67cdf0e10cSrcweir for( int iArrayCounter = 0; iArrayCounter < mBritishWords.length; 68cdf0e10cSrcweir iArrayCounter++ ) 69cdf0e10cSrcweir { 70cdf0e10cSrcweir System.out.println(mBritishWords[iArrayCounter] + 71cdf0e10cSrcweir " -> " + mUSWords[iArrayCounter]); 72cdf0e10cSrcweir // Set the properties the replace method need 73cdf0e10cSrcweir xReplaceDescr.setSearchString(mBritishWords[iArrayCounter] ); 74cdf0e10cSrcweir xReplaceDescr.setReplaceString(mUSWords[iArrayCounter] ); 75cdf0e10cSrcweir 76cdf0e10cSrcweir // Replace all words 77cdf0e10cSrcweir xReplaceable.replaceAll( xReplaceDescr ); 78cdf0e10cSrcweir } 79cdf0e10cSrcweir 80cdf0e10cSrcweir } 81cdf0e10cSrcweir catch( Exception e) { 82cdf0e10cSrcweir e.printStackTrace(System.err); 83cdf0e10cSrcweir } 84cdf0e10cSrcweir 85cdf0e10cSrcweir System.out.println("Done"); 86cdf0e10cSrcweir 87cdf0e10cSrcweir System.exit(0); 88cdf0e10cSrcweir 89cdf0e10cSrcweir } 90cdf0e10cSrcweir createExampleData( com.sun.star.text.XTextDocument xTextDocument )91cdf0e10cSrcweir protected static void createExampleData( 92cdf0e10cSrcweir com.sun.star.text.XTextDocument xTextDocument ) 93cdf0e10cSrcweir { 94cdf0e10cSrcweir // Create textdocument and insert example text 95cdf0e10cSrcweir com.sun.star.text.XTextCursor xTextCursor = null; 96cdf0e10cSrcweir 97cdf0e10cSrcweir try { 98cdf0e10cSrcweir xTextCursor = (com.sun.star.text.XTextCursor) 99cdf0e10cSrcweir xTextDocument.getText().createTextCursor(); 100cdf0e10cSrcweir com.sun.star.text.XText xText = (com.sun.star.text.XText) 101cdf0e10cSrcweir xTextDocument.getText(); 102cdf0e10cSrcweir 103cdf0e10cSrcweir xText.insertString( xTextCursor, 104cdf0e10cSrcweir "He nervously looked all around. Suddenly he saw his ", false ); 105cdf0e10cSrcweir 106cdf0e10cSrcweir xText.insertString( xTextCursor, "neighbour ", true ); 107cdf0e10cSrcweir com.sun.star.beans.XPropertySet xCPS = (com.sun.star.beans.XPropertySet) 108cdf0e10cSrcweir UnoRuntime.queryInterface( 109cdf0e10cSrcweir com.sun.star.beans.XPropertySet.class, xTextCursor); 110cdf0e10cSrcweir // Set the word blue 111cdf0e10cSrcweir xCPS.setPropertyValue( "CharColor", new Integer( 255 ) ); 112cdf0e10cSrcweir // Go to last character 113cdf0e10cSrcweir xTextCursor.gotoEnd(false); 114cdf0e10cSrcweir xCPS.setPropertyValue( "CharColor", new Integer( 0 ) ); 115cdf0e10cSrcweir 116cdf0e10cSrcweir xText.insertString( xTextCursor, "in 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 ", false ); 117cdf0e10cSrcweir 118cdf0e10cSrcweir xText.insertString( xTextCursor, "centre ", true ); 119cdf0e10cSrcweir xCPS = (com.sun.star.beans.XPropertySet)UnoRuntime.queryInterface( 120cdf0e10cSrcweir com.sun.star.beans.XPropertySet.class, xTextCursor); 121cdf0e10cSrcweir // Set the word blue 122cdf0e10cSrcweir xCPS.setPropertyValue( "CharColor", new Integer( 255 ) ); 123cdf0e10cSrcweir // Go to last character 124cdf0e10cSrcweir xTextCursor.gotoEnd(false); 125cdf0e10cSrcweir xCPS.setPropertyValue( "CharColor", new Integer( 0 ) ); 126cdf0e10cSrcweir 127cdf0e10cSrcweir xText.insertString( xTextCursor, "of the sidewalk.", false ); 128cdf0e10cSrcweir 129cdf0e10cSrcweir xText.insertControlCharacter( xTextCursor, 130cdf0e10cSrcweir com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, false ); 131cdf0e10cSrcweir xText.insertString( xTextCursor, "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.", false ); 132cdf0e10cSrcweir 133cdf0e10cSrcweir xTextCursor.gotoStart(false); 134cdf0e10cSrcweir } 135cdf0e10cSrcweir catch( Exception e) { 136cdf0e10cSrcweir e.printStackTrace(System.err); 137cdf0e10cSrcweir } 138cdf0e10cSrcweir 139cdf0e10cSrcweir } 140cdf0e10cSrcweir getDesktop()141cdf0e10cSrcweir public static com.sun.star.frame.XDesktop getDesktop() { 142cdf0e10cSrcweir com.sun.star.frame.XDesktop xDesktop = null; 143cdf0e10cSrcweir com.sun.star.lang.XMultiComponentFactory xMCF = null; 144cdf0e10cSrcweir 145cdf0e10cSrcweir try { 146cdf0e10cSrcweir com.sun.star.uno.XComponentContext xContext = null; 147cdf0e10cSrcweir 148cdf0e10cSrcweir // get the remote office component context 149cdf0e10cSrcweir xContext = com.sun.star.comp.helper.Bootstrap.bootstrap(); 150cdf0e10cSrcweir 151cdf0e10cSrcweir // get the remote office service manager 152cdf0e10cSrcweir xMCF = xContext.getServiceManager(); 153cdf0e10cSrcweir if( xMCF != null ) { 154cdf0e10cSrcweir System.out.println("Connected to a running office ..."); 155cdf0e10cSrcweir 156cdf0e10cSrcweir Object oDesktop = xMCF.createInstanceWithContext( 157cdf0e10cSrcweir "com.sun.star.frame.Desktop", xContext); 158cdf0e10cSrcweir xDesktop = (com.sun.star.frame.XDesktop) UnoRuntime.queryInterface( 159cdf0e10cSrcweir com.sun.star.frame.XDesktop.class, oDesktop); 160cdf0e10cSrcweir } 161cdf0e10cSrcweir else 162cdf0e10cSrcweir System.out.println( "Can't create a desktop. No connection, no remote office servicemanager available!" ); 163cdf0e10cSrcweir } 164cdf0e10cSrcweir catch( Exception e) { 165cdf0e10cSrcweir e.printStackTrace(System.err); 166cdf0e10cSrcweir System.exit(1); 167cdf0e10cSrcweir } 168cdf0e10cSrcweir 169cdf0e10cSrcweir 170cdf0e10cSrcweir return xDesktop; 171cdf0e10cSrcweir } 172cdf0e10cSrcweir createTextdocument( com.sun.star.frame.XDesktop xDesktop )173cdf0e10cSrcweir public static com.sun.star.text.XTextDocument createTextdocument( 174cdf0e10cSrcweir com.sun.star.frame.XDesktop xDesktop ) 175cdf0e10cSrcweir { 176cdf0e10cSrcweir com.sun.star.text.XTextDocument aTextDocument = null; 177cdf0e10cSrcweir 178cdf0e10cSrcweir try { 179cdf0e10cSrcweir com.sun.star.lang.XComponent xComponent = CreateNewDocument(xDesktop, 180cdf0e10cSrcweir "swriter"); 181cdf0e10cSrcweir aTextDocument = (com.sun.star.text.XTextDocument) 182cdf0e10cSrcweir UnoRuntime.queryInterface( 183cdf0e10cSrcweir com.sun.star.text.XTextDocument.class, xComponent); 184cdf0e10cSrcweir } 185cdf0e10cSrcweir catch( Exception e) { 186cdf0e10cSrcweir e.printStackTrace(System.err); 187cdf0e10cSrcweir } 188cdf0e10cSrcweir 189cdf0e10cSrcweir return aTextDocument; 190cdf0e10cSrcweir } 191cdf0e10cSrcweir 192cdf0e10cSrcweir CreateNewDocument( com.sun.star.frame.XDesktop xDesktop, String sDocumentType )193cdf0e10cSrcweir protected static com.sun.star.lang.XComponent CreateNewDocument( 194cdf0e10cSrcweir com.sun.star.frame.XDesktop xDesktop, 195cdf0e10cSrcweir String sDocumentType ) 196cdf0e10cSrcweir { 197cdf0e10cSrcweir String sURL = "private:factory/" + sDocumentType; 198cdf0e10cSrcweir 199cdf0e10cSrcweir com.sun.star.lang.XComponent xComponent = null; 200cdf0e10cSrcweir com.sun.star.frame.XComponentLoader xComponentLoader = null; 201cdf0e10cSrcweir com.sun.star.beans.PropertyValue xValues[] = 202cdf0e10cSrcweir new com.sun.star.beans.PropertyValue[1]; 203cdf0e10cSrcweir com.sun.star.beans.PropertyValue xEmptyArgs[] = 204cdf0e10cSrcweir new com.sun.star.beans.PropertyValue[0]; 205cdf0e10cSrcweir 206cdf0e10cSrcweir try { 207cdf0e10cSrcweir xComponentLoader = (com.sun.star.frame.XComponentLoader) 208cdf0e10cSrcweir UnoRuntime.queryInterface( 209cdf0e10cSrcweir com.sun.star.frame.XComponentLoader.class, xDesktop); 210cdf0e10cSrcweir 211cdf0e10cSrcweir xComponent = xComponentLoader.loadComponentFromURL( 212cdf0e10cSrcweir sURL, "_blank", 0, xEmptyArgs); 213cdf0e10cSrcweir } 214cdf0e10cSrcweir catch( Exception e) { 215cdf0e10cSrcweir e.printStackTrace(System.err); 216cdf0e10cSrcweir } 217cdf0e10cSrcweir 218cdf0e10cSrcweir return xComponent ; 219cdf0e10cSrcweir } 220cdf0e10cSrcweir } 221