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