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