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