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 // used interfaces 25 import com.sun.star.uno.XComponentContext; 26 import com.sun.star.lang.XMultiComponentFactory; 27 import com.sun.star.linguistic2.XLinguServiceManager; 28 import com.sun.star.linguistic2.XSpellChecker; 29 import com.sun.star.linguistic2.XHyphenator; 30 import com.sun.star.linguistic2.XThesaurus; 31 import com.sun.star.linguistic2.XSpellAlternatives; 32 import com.sun.star.linguistic2.XHyphenatedWord; 33 import com.sun.star.linguistic2.XPossibleHyphens; 34 import com.sun.star.linguistic2.XMeaning; 35 import com.sun.star.linguistic2.XSearchableDictionaryList; 36 import com.sun.star.linguistic2.XLinguServiceEventListener; 37 import com.sun.star.linguistic2.LinguServiceEvent; 38 import com.sun.star.beans.XPropertySet; 39 import com.sun.star.beans.PropertyValue; 40 import com.sun.star.uno.XComponentContext; 41 import com.sun.star.uno.XNamingService; 42 import com.sun.star.lang.XMultiComponentFactory; 43 import com.sun.star.lang.EventObject; 44 import com.sun.star.lang.Locale; 45 import com.sun.star.bridge.XUnoUrlResolver; 46 import com.sun.star.uno.UnoRuntime; 47 import com.sun.star.uno.Any; 48 import com.sun.star.lang.XComponent; 49 50 public class LinguisticExamples 51 { 52 // The remote office ocntext 53 protected XComponentContext mxRemoteContext = null; 54 // The MultiServiceFactory interface of the Office 55 protected XMultiComponentFactory mxRemoteServiceManager = null; 56 57 // The LinguServiceManager interface 58 protected XLinguServiceManager mxLinguSvcMgr = null; 59 60 // The SpellChecker interface 61 protected XSpellChecker mxSpell = null; 62 63 // The Hyphenator interface 64 protected XHyphenator mxHyph = null; 65 66 // The Thesaurus interface 67 protected XThesaurus mxThes = null; 68 69 // The DictionaryList interface 70 protected XSearchableDictionaryList mxDicList = null; 71 72 // The LinguProperties interface 73 protected XPropertySet mxLinguProps = null; 74 75 main(String args[])76 public static void main(String args[]) 77 { 78 // Create an instance of the class and call it's begin method 79 try { 80 LinguisticExamples aExample = new LinguisticExamples(); 81 aExample.Connect(); 82 aExample.Run(); 83 } catch (Exception e) { 84 System.err.println("failed to run examples"); 85 e.printStackTrace(); 86 } 87 } 88 89 Connect()90 public void Connect() 91 throws java.lang.Exception 92 { 93 // get the remote office context. If necessary a new office 94 // process is started 95 mxRemoteContext = com.sun.star.comp.helper.Bootstrap.bootstrap(); 96 System.out.println("Connected to a running office ..."); 97 mxRemoteServiceManager = mxRemoteContext.getServiceManager(); 98 } 99 100 101 /** Get the LinguServiceManager to be used. For example to access spell 102 checker, thesaurus and hyphenator, also the component may choose to 103 register itself as listener to it in order to get notified of relevant 104 events. */ GetLinguSvcMgr()105 public boolean GetLinguSvcMgr() 106 throws com.sun.star.uno.Exception 107 { 108 if (mxRemoteContext != null && mxRemoteServiceManager != null) { 109 Object aObj = mxRemoteServiceManager.createInstanceWithContext( 110 "com.sun.star.linguistic2.LinguServiceManager", mxRemoteContext ); 111 mxLinguSvcMgr = (XLinguServiceManager) 112 UnoRuntime.queryInterface(XLinguServiceManager.class, aObj); 113 } 114 return mxLinguSvcMgr != null; 115 } 116 117 118 /** Get the SpellChecker to be used. 119 */ GetSpell()120 public boolean GetSpell() 121 throws com.sun.star.uno.Exception, 122 com.sun.star.uno.RuntimeException 123 { 124 if (mxLinguSvcMgr != null) 125 mxSpell = mxLinguSvcMgr.getSpellChecker(); 126 return mxSpell != null; 127 } 128 129 /** Get the Hyphenator to be used. 130 */ GetHyph()131 public boolean GetHyph() 132 throws com.sun.star.uno.Exception, 133 com.sun.star.uno.RuntimeException 134 { 135 if (mxLinguSvcMgr != null) 136 mxHyph = mxLinguSvcMgr.getHyphenator(); 137 return mxHyph != null; 138 } 139 140 /** Get the Thesaurus to be used. 141 */ GetThes()142 public boolean GetThes() 143 throws com.sun.star.uno.Exception, 144 com.sun.star.uno.RuntimeException 145 { 146 if (mxLinguSvcMgr != null) 147 mxThes = mxLinguSvcMgr.getThesaurus(); 148 return mxThes != null; 149 } 150 151 Run()152 public void Run() 153 throws Exception 154 { 155 GetLinguSvcMgr(); 156 157 158 // list of property values to used in function calls below. 159 // Only properties with values different from the (default) values 160 // in the LinguProperties property set need to be supllied. 161 // Thus we may stay with an empty list in order to use the ones 162 // form the property set. 163 PropertyValue[] aEmptyProps = new PropertyValue[0]; 164 165 // use american english as language 166 Locale aLocale = new Locale("en","US",""); 167 168 169 170 // another list of property values to used in function calls below. 171 // Only properties with values different from the (default) values 172 // in the LinguProperties property set need to be supllied. 173 PropertyValue[] aProps = new PropertyValue[1]; 174 aProps[0] = new PropertyValue(); 175 aProps[0].Name = "IsGermanPreReform"; 176 aProps[0].Value = new Boolean( true ); 177 178 179 GetSpell(); 180 if (mxSpell != null) 181 { 182 // test with correct word 183 String aWord = "horseback"; 184 boolean bIsCorrect = mxSpell.isValid( aWord, aLocale, aEmptyProps ); 185 System.out.println( aWord + ": " + bIsCorrect ); 186 187 // test with incorrect word 188 aWord = "course"; 189 bIsCorrect = mxSpell.isValid( aWord, aLocale , aEmptyProps ); 190 System.out.println( aWord + ": " + bIsCorrect ); 191 192 193 aWord = "house"; 194 XSpellAlternatives xAlt = mxSpell.spell( aWord, aLocale, aEmptyProps ); 195 if (xAlt == null) 196 System.out.println( aWord + " is correct." ); 197 else 198 { 199 System.out.println( aWord + " is not correct. A list of proposals follows." ); 200 String[] aAlternatives = xAlt.getAlternatives(); 201 if (aAlternatives.length == 0) 202 System.out.println( "no proposal found." ); 203 else 204 { 205 for (int i = 0; i < aAlternatives.length; ++i) 206 System.out.println( aAlternatives[i] ); 207 } 208 } 209 } 210 211 212 GetHyph(); 213 if (mxHyph != null) 214 { 215 // maximum number of characters to remain before the hyphen 216 // character in the resulting word of the hyphenation 217 short nMaxLeading = 6; 218 219 XHyphenatedWord xHyphWord = mxHyph.hyphenate( "waterfall", 220 aLocale, nMaxLeading , 221 aEmptyProps ); 222 if (xHyphWord == null) 223 System.out.println( "no valid hyphenation position found" ); 224 else 225 { 226 System.out.println( "valid hyphenation pos found at " 227 + xHyphWord.getHyphenationPos() 228 + " in " + xHyphWord.getWord() ); 229 System.out.println( "hyphenation char will be after char " 230 + xHyphWord.getHyphenPos() 231 + " in " + xHyphWord.getHyphenatedWord() ); 232 } 233 234 235 //! Note: 'aProps' needs to have set 'IsGermanPreReform' to true! 236 xHyphWord = mxHyph.queryAlternativeSpelling( "Schiffahrt", 237 new Locale("de","DE",""), (short)4, aProps ); 238 if (xHyphWord == null) 239 System.out.println( "no alternative spelling found at specified position." ); 240 else 241 { 242 if (xHyphWord.isAlternativeSpelling()) 243 System.out.println( "alternative spelling detectetd!" ); 244 System.out.println( "valid hyphenation pos found at " 245 + xHyphWord.getHyphenationPos() 246 + " in " + xHyphWord.getWord() ); 247 System.out.println( "hyphenation char will be after char " 248 + xHyphWord.getHyphenPos() 249 + " in " + xHyphWord.getHyphenatedWord() ); 250 } 251 252 253 XPossibleHyphens xPossHyph = mxHyph.createPossibleHyphens("waterfall", 254 aLocale, 255 aEmptyProps ); 256 if (xPossHyph == null) 257 System.out.println( "no hyphenation positions found." ); 258 else 259 System.out.println( xPossHyph.getPossibleHyphens() ); 260 } 261 262 263 GetThes(); 264 if (mxThes != null) 265 { 266 XMeaning[] xMeanings = mxThes.queryMeanings("house", aLocale, 267 aEmptyProps ); 268 if (xMeanings == null) 269 System.out.println( "nothing found." ); 270 else 271 { 272 for (int i = 0; i < xMeanings.length; ++i) 273 { 274 System.out.println( "Meaning: " + xMeanings[i].getMeaning() ); 275 String[] aSynonyms = xMeanings[i].querySynonyms(); 276 for (int k = 0; k < aSynonyms.length; ++k) 277 System.out.println( " Synonym: " + aSynonyms[k] ); 278 } 279 } 280 } 281 282 283 284 XLinguServiceEventListener aClient = new Client(); 285 286 // get access to LinguProperties property set 287 Object aObj = mxRemoteServiceManager.createInstanceWithContext( 288 "com.sun.star.linguistic2.LinguProperties", mxRemoteContext); 289 XPropertySet aLinguProps = (XPropertySet) UnoRuntime.queryInterface( 290 XPropertySet.class,aObj); 291 292 // set a spellchecker and hyphenator property value to a defined state 293 try { 294 aLinguProps.setPropertyValue("IsGermanPreReform", new Boolean(true)); 295 } catch (Exception e) { 296 } 297 298 // now add the client as listener to the service manager to 299 // get informed when spellchecking or hyphenation may produce 300 // different results then before. 301 mxLinguSvcMgr.addLinguServiceManagerListener(aClient); 302 303 // change that property value in order to trigger a property change 304 // event that eventually results in the listeners 305 // 'processLinguServiceEvent' function being called 306 try { 307 aLinguProps.setPropertyValue("IsGermanPreReform", new Boolean(false)); 308 } catch (Exception e) { 309 } 310 311 //! keep the listener and the program alive until the event will 312 //! be launched. 313 //! There is a voluntary delay before launching the event! 314 // Of course this code would usually not be in a *real* client 315 // its 316 synchronized(this) { 317 try { 318 this.wait(4000); 319 } catch(Exception e) { 320 321 } 322 } 323 324 //! remove listener before programm termination. 325 //! should not be omitted. 326 mxLinguSvcMgr.removeLinguServiceManagerListener(aClient); 327 328 329 System.exit(0); 330 } 331 332 /** simple sample implementation of a clients XLinguServiceEventListener 333 * interface implementation 334 */ 335 public class Client 336 implements XLinguServiceEventListener 337 { disposing( EventObject aEventObj )338 public void disposing ( EventObject aEventObj ) 339 { 340 //! any references to the EventObjects source have to be 341 //! released here now! 342 343 System.out.println("object listened to will be disposed"); 344 } 345 processLinguServiceEvent( LinguServiceEvent aServiceEvent )346 public void processLinguServiceEvent( LinguServiceEvent aServiceEvent ) 347 { 348 //! do here whatever you think needs to be done depending 349 //! on the event received (e.g. trigger background spellchecking 350 //! or hyphenation again.) 351 352 System.out.println("Listener called"); 353 } 354 }; 355 356 } 357 358