1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 //*************************************************************************** 25 // comment: Step 1: get the Desktop object from the office 26 // Step 2: open an empty text document 27 // Step 3: enter a example text 28 // Step 4: replace some english spelled words with US spelled 29 //*************************************************************************** 30 31 32 import com.sun.star.uno.UnoRuntime; 33 34 public class TextReplace { 35 main(String args[])36 public static void main(String args[]) { 37 // You need the desktop to create a document 38 // The getDesktop method does the UNO bootstrapping, gets the 39 // remote servie manager and the desktop object. 40 com.sun.star.frame.XDesktop xDesktop = null; 41 xDesktop = getDesktop(); 42 43 com.sun.star.text.XTextDocument xTextDocument = 44 createTextdocument( xDesktop ); 45 46 createExampleData( xTextDocument ); 47 48 String mBritishWords[] = {"colour", "neighbour", "centre", "behaviour", 49 "metre", "through" }; 50 String mUSWords[] = { "color", "neighbor", "center", "behavior", 51 "meter", "thru" }; 52 53 try { 54 com.sun.star.util.XReplaceDescriptor xReplaceDescr = null; 55 com.sun.star.util.XSearchDescriptor xSearchDescriptor = null; 56 com.sun.star.util.XReplaceable xReplaceable = null; 57 58 xReplaceable = (com.sun.star.util.XReplaceable) 59 UnoRuntime.queryInterface( 60 com.sun.star.util.XReplaceable.class, xTextDocument); 61 62 // You need a descriptor to set properies for Replace 63 xReplaceDescr = (com.sun.star.util.XReplaceDescriptor) 64 xReplaceable.createReplaceDescriptor(); 65 66 System.out.println("Change all occurrences of ..."); 67 for( int iArrayCounter = 0; iArrayCounter < mBritishWords.length; 68 iArrayCounter++ ) 69 { 70 System.out.println(mBritishWords[iArrayCounter] + 71 " -> " + mUSWords[iArrayCounter]); 72 // Set the properties the replace method need 73 xReplaceDescr.setSearchString(mBritishWords[iArrayCounter] ); 74 xReplaceDescr.setReplaceString(mUSWords[iArrayCounter] ); 75 76 // Replace all words 77 xReplaceable.replaceAll( xReplaceDescr ); 78 } 79 80 } 81 catch( Exception e) { 82 e.printStackTrace(System.err); 83 } 84 85 System.out.println("Done"); 86 87 System.exit(0); 88 89 } 90 createExampleData( com.sun.star.text.XTextDocument xTextDocument )91 protected static void createExampleData( 92 com.sun.star.text.XTextDocument xTextDocument ) 93 { 94 // Create textdocument and insert example text 95 com.sun.star.text.XTextCursor xTextCursor = null; 96 97 try { 98 xTextCursor = (com.sun.star.text.XTextCursor) 99 xTextDocument.getText().createTextCursor(); 100 com.sun.star.text.XText xText = (com.sun.star.text.XText) 101 xTextDocument.getText(); 102 103 xText.insertString( xTextCursor, 104 "He nervously looked all around. Suddenly he saw his ", false ); 105 106 xText.insertString( xTextCursor, "neighbour ", true ); 107 com.sun.star.beans.XPropertySet xCPS = (com.sun.star.beans.XPropertySet) 108 UnoRuntime.queryInterface( 109 com.sun.star.beans.XPropertySet.class, xTextCursor); 110 // Set the word blue 111 xCPS.setPropertyValue( "CharColor", new Integer( 255 ) ); 112 // Go to last character 113 xTextCursor.gotoEnd(false); 114 xCPS.setPropertyValue( "CharColor", new Integer( 0 ) ); 115 116 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 ); 117 118 xText.insertString( xTextCursor, "centre ", true ); 119 xCPS = (com.sun.star.beans.XPropertySet)UnoRuntime.queryInterface( 120 com.sun.star.beans.XPropertySet.class, xTextCursor); 121 // Set the word blue 122 xCPS.setPropertyValue( "CharColor", new Integer( 255 ) ); 123 // Go to last character 124 xTextCursor.gotoEnd(false); 125 xCPS.setPropertyValue( "CharColor", new Integer( 0 ) ); 126 127 xText.insertString( xTextCursor, "of the sidewalk.", false ); 128 129 xText.insertControlCharacter( xTextCursor, 130 com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, false ); 131 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 ); 132 133 xTextCursor.gotoStart(false); 134 } 135 catch( Exception e) { 136 e.printStackTrace(System.err); 137 } 138 139 } 140 getDesktop()141 public static com.sun.star.frame.XDesktop getDesktop() { 142 com.sun.star.frame.XDesktop xDesktop = null; 143 com.sun.star.lang.XMultiComponentFactory xMCF = null; 144 145 try { 146 com.sun.star.uno.XComponentContext xContext = null; 147 148 // get the remote office component context 149 xContext = com.sun.star.comp.helper.Bootstrap.bootstrap(); 150 151 // get the remote office service manager 152 xMCF = xContext.getServiceManager(); 153 if( xMCF != null ) { 154 System.out.println("Connected to a running office ..."); 155 156 Object oDesktop = xMCF.createInstanceWithContext( 157 "com.sun.star.frame.Desktop", xContext); 158 xDesktop = (com.sun.star.frame.XDesktop) UnoRuntime.queryInterface( 159 com.sun.star.frame.XDesktop.class, oDesktop); 160 } 161 else 162 System.out.println( "Can't create a desktop. No connection, no remote office servicemanager available!" ); 163 } 164 catch( Exception e) { 165 e.printStackTrace(System.err); 166 System.exit(1); 167 } 168 169 170 return xDesktop; 171 } 172 createTextdocument( com.sun.star.frame.XDesktop xDesktop )173 public static com.sun.star.text.XTextDocument createTextdocument( 174 com.sun.star.frame.XDesktop xDesktop ) 175 { 176 com.sun.star.text.XTextDocument aTextDocument = null; 177 178 try { 179 com.sun.star.lang.XComponent xComponent = CreateNewDocument(xDesktop, 180 "swriter"); 181 aTextDocument = (com.sun.star.text.XTextDocument) 182 UnoRuntime.queryInterface( 183 com.sun.star.text.XTextDocument.class, xComponent); 184 } 185 catch( Exception e) { 186 e.printStackTrace(System.err); 187 } 188 189 return aTextDocument; 190 } 191 192 CreateNewDocument( com.sun.star.frame.XDesktop xDesktop, String sDocumentType )193 protected static com.sun.star.lang.XComponent CreateNewDocument( 194 com.sun.star.frame.XDesktop xDesktop, 195 String sDocumentType ) 196 { 197 String sURL = "private:factory/" + sDocumentType; 198 199 com.sun.star.lang.XComponent xComponent = null; 200 com.sun.star.frame.XComponentLoader xComponentLoader = null; 201 com.sun.star.beans.PropertyValue xValues[] = 202 new com.sun.star.beans.PropertyValue[1]; 203 com.sun.star.beans.PropertyValue xEmptyArgs[] = 204 new com.sun.star.beans.PropertyValue[0]; 205 206 try { 207 xComponentLoader = (com.sun.star.frame.XComponentLoader) 208 UnoRuntime.queryInterface( 209 com.sun.star.frame.XComponentLoader.class, xDesktop); 210 211 xComponent = xComponentLoader.loadComponentFromURL( 212 sURL, "_blank", 0, xEmptyArgs); 213 } 214 catch( Exception e) { 215 e.printStackTrace(System.err); 216 } 217 218 return xComponent ; 219 } 220 } 221