1 /*************************************************************************
3 * $RCSfile: LinguisticExamples.java,v $
7 * last change: $Author: rt $ $Date: 2005-01-31 16:47:11 $
9 * The Contents of this file are made available subject to the terms of
12 * Copyright (c) 2003 by Sun Microsystems, Inc.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *************************************************************************/
42 import com
.sun
.star
.uno
.XComponentContext
;
43 import com
.sun
.star
.lang
.XMultiComponentFactory
;
44 import com
.sun
.star
.linguistic2
.XLinguServiceManager
;
45 import com
.sun
.star
.linguistic2
.XSpellChecker
;
46 import com
.sun
.star
.linguistic2
.XHyphenator
;
47 import com
.sun
.star
.linguistic2
.XThesaurus
;
48 import com
.sun
.star
.linguistic2
.XSpellAlternatives
;
49 import com
.sun
.star
.linguistic2
.XHyphenatedWord
;
50 import com
.sun
.star
.linguistic2
.XPossibleHyphens
;
51 import com
.sun
.star
.linguistic2
.XMeaning
;
52 import com
.sun
.star
.linguistic2
.XSearchableDictionaryList
;
53 import com
.sun
.star
.linguistic2
.XLinguServiceEventListener
;
54 import com
.sun
.star
.linguistic2
.LinguServiceEvent
;
55 import com
.sun
.star
.beans
.XPropertySet
;
56 import com
.sun
.star
.beans
.PropertyValue
;
57 import com
.sun
.star
.uno
.XComponentContext
;
58 import com
.sun
.star
.uno
.XNamingService
;
59 import com
.sun
.star
.lang
.XMultiComponentFactory
;
60 import com
.sun
.star
.lang
.EventObject
;
61 import com
.sun
.star
.lang
.Locale
;
62 import com
.sun
.star
.bridge
.XUnoUrlResolver
;
63 import com
.sun
.star
.uno
.UnoRuntime
;
64 import com
.sun
.star
.uno
.Any
;
65 import com
.sun
.star
.lang
.XComponent
;
67 public class LinguisticExamples
69 // The remote office ocntext
70 protected XComponentContext mxRemoteContext
= null;
71 // The MultiServiceFactory interface of the Office
72 protected XMultiComponentFactory mxRemoteServiceManager
= null;
74 // The LinguServiceManager interface
75 protected XLinguServiceManager mxLinguSvcMgr
= null;
77 // The SpellChecker interface
78 protected XSpellChecker mxSpell
= null;
80 // The Hyphenator interface
81 protected XHyphenator mxHyph
= null;
83 // The Thesaurus interface
84 protected XThesaurus mxThes
= null;
86 // The DictionaryList interface
87 protected XSearchableDictionaryList mxDicList
= null;
89 // The LinguProperties interface
90 protected XPropertySet mxLinguProps
= null;
93 public static void main(String args
[])
95 // Create an instance of the class and call it's begin method
97 LinguisticExamples aExample
= new LinguisticExamples();
100 } catch (Exception e
) {
101 System
.err
.println("failed to run examples");
107 public void Connect()
108 throws java
.lang
.Exception
110 // get the remote office context. If necessary a new office
111 // process is started
112 mxRemoteContext
= com
.sun
.star
.comp
.helper
.Bootstrap
.bootstrap();
113 System
.out
.println("Connected to a running office ...");
114 mxRemoteServiceManager
= mxRemoteContext
.getServiceManager();
118 /** Get the LinguServiceManager to be used. For example to access spell
119 checker, thesaurus and hyphenator, also the component may choose to
120 register itself as listener to it in order to get notified of relevant
122 public boolean GetLinguSvcMgr()
123 throws com
.sun
.star
.uno
.Exception
125 if (mxRemoteContext
!= null && mxRemoteServiceManager
!= null) {
126 Object aObj
= mxRemoteServiceManager
.createInstanceWithContext(
127 "com.sun.star.linguistic2.LinguServiceManager", mxRemoteContext
);
128 mxLinguSvcMgr
= (XLinguServiceManager
)
129 UnoRuntime
.queryInterface(XLinguServiceManager
.class, aObj
);
131 return mxLinguSvcMgr
!= null;
135 /** Get the SpellChecker to be used.
137 public boolean GetSpell()
138 throws com
.sun
.star
.uno
.Exception
,
139 com
.sun
.star
.uno
.RuntimeException
141 if (mxLinguSvcMgr
!= null)
142 mxSpell
= mxLinguSvcMgr
.getSpellChecker();
143 return mxSpell
!= null;
146 /** Get the Hyphenator to be used.
148 public boolean GetHyph()
149 throws com
.sun
.star
.uno
.Exception
,
150 com
.sun
.star
.uno
.RuntimeException
152 if (mxLinguSvcMgr
!= null)
153 mxHyph
= mxLinguSvcMgr
.getHyphenator();
154 return mxHyph
!= null;
157 /** Get the Thesaurus to be used.
159 public boolean GetThes()
160 throws com
.sun
.star
.uno
.Exception
,
161 com
.sun
.star
.uno
.RuntimeException
163 if (mxLinguSvcMgr
!= null)
164 mxThes
= mxLinguSvcMgr
.getThesaurus();
165 return mxThes
!= null;
175 // list of property values to used in function calls below.
176 // Only properties with values different from the (default) values
177 // in the LinguProperties property set need to be supllied.
178 // Thus we may stay with an empty list in order to use the ones
179 // form the property set.
180 PropertyValue
[] aEmptyProps
= new PropertyValue
[0];
182 // use american english as language
183 Locale aLocale
= new Locale("en","US","");
187 // another list of property values to used in function calls below.
188 // Only properties with values different from the (default) values
189 // in the LinguProperties property set need to be supllied.
190 PropertyValue
[] aProps
= new PropertyValue
[1];
191 aProps
[0] = new PropertyValue();
192 aProps
[0].Name
= "IsGermanPreReform";
193 aProps
[0].Value
= new Boolean( true );
199 // test with correct word
200 String aWord
= "horseback";
201 boolean bIsCorrect
= mxSpell
.isValid( aWord
, aLocale
, aEmptyProps
);
202 System
.out
.println( aWord
+ ": " + bIsCorrect
);
204 // test with incorrect word
206 bIsCorrect
= mxSpell
.isValid( aWord
, aLocale
, aEmptyProps
);
207 System
.out
.println( aWord
+ ": " + bIsCorrect
);
211 XSpellAlternatives xAlt
= mxSpell
.spell( aWord
, aLocale
, aEmptyProps
);
213 System
.out
.println( aWord
+ " is correct." );
216 System
.out
.println( aWord
+ " is not correct. A list of proposals follows." );
217 String
[] aAlternatives
= xAlt
.getAlternatives();
218 if (aAlternatives
.length
== 0)
219 System
.out
.println( "no proposal found." );
222 for (int i
= 0; i
< aAlternatives
.length
; ++i
)
223 System
.out
.println( aAlternatives
[i
] );
232 // maximum number of characters to remain before the hyphen
233 // character in the resulting word of the hyphenation
234 short nMaxLeading
= 6;
236 XHyphenatedWord xHyphWord
= mxHyph
.hyphenate( "waterfall",
237 aLocale
, nMaxLeading
,
239 if (xHyphWord
== null)
240 System
.out
.println( "no valid hyphenation position found" );
243 System
.out
.println( "valid hyphenation pos found at "
244 + xHyphWord
.getHyphenationPos()
245 + " in " + xHyphWord
.getWord() );
246 System
.out
.println( "hyphenation char will be after char "
247 + xHyphWord
.getHyphenPos()
248 + " in " + xHyphWord
.getHyphenatedWord() );
252 //! Note: 'aProps' needs to have set 'IsGermanPreReform' to true!
253 xHyphWord
= mxHyph
.queryAlternativeSpelling( "Schiffahrt",
254 new Locale("de","DE",""), (short)4, aProps
);
255 if (xHyphWord
== null)
256 System
.out
.println( "no alternative spelling found at specified position." );
259 if (xHyphWord
.isAlternativeSpelling())
260 System
.out
.println( "alternative spelling detectetd!" );
261 System
.out
.println( "valid hyphenation pos found at "
262 + xHyphWord
.getHyphenationPos()
263 + " in " + xHyphWord
.getWord() );
264 System
.out
.println( "hyphenation char will be after char "
265 + xHyphWord
.getHyphenPos()
266 + " in " + xHyphWord
.getHyphenatedWord() );
270 XPossibleHyphens xPossHyph
= mxHyph
.createPossibleHyphens("waterfall",
273 if (xPossHyph
== null)
274 System
.out
.println( "no hyphenation positions found." );
276 System
.out
.println( xPossHyph
.getPossibleHyphens() );
283 XMeaning
[] xMeanings
= mxThes
.queryMeanings("house", aLocale
,
285 if (xMeanings
== null)
286 System
.out
.println( "nothing found." );
289 for (int i
= 0; i
< xMeanings
.length
; ++i
)
291 System
.out
.println( "Meaning: " + xMeanings
[i
].getMeaning() );
292 String
[] aSynonyms
= xMeanings
[i
].querySynonyms();
293 for (int k
= 0; k
< aSynonyms
.length
; ++k
)
294 System
.out
.println( " Synonym: " + aSynonyms
[k
] );
301 XLinguServiceEventListener aClient
= new Client();
303 // get access to LinguProperties property set
304 Object aObj
= mxRemoteServiceManager
.createInstanceWithContext(
305 "com.sun.star.linguistic2.LinguProperties", mxRemoteContext
);
306 XPropertySet aLinguProps
= (XPropertySet
) UnoRuntime
.queryInterface(
307 XPropertySet
.class,aObj
);
309 // set a spellchecker and hyphenator property value to a defined state
311 aLinguProps
.setPropertyValue("IsGermanPreReform", new Boolean(true));
312 } catch (Exception e
) {
315 // now add the client as listener to the service manager to
316 // get informed when spellchecking or hyphenation may produce
317 // different results then before.
318 mxLinguSvcMgr
.addLinguServiceManagerListener(aClient
);
320 // change that property value in order to trigger a property change
321 // event that eventually results in the listeners
322 // 'processLinguServiceEvent' function being called
324 aLinguProps
.setPropertyValue("IsGermanPreReform", new Boolean(false));
325 } catch (Exception e
) {
328 //! keep the listener and the program alive until the event will
330 //! There is a voluntary delay before launching the event!
331 // Of course this code would usually not be in a *real* client
336 } catch(Exception e
) {
341 //! remove listener before programm termination.
342 //! should not be omitted.
343 mxLinguSvcMgr
.removeLinguServiceManagerListener(aClient
);
349 /** simple sample implementation of a clients XLinguServiceEventListener
350 * interface implementation
353 implements XLinguServiceEventListener
355 public void disposing ( EventObject aEventObj
)
357 //! any references to the EventObjects source have to be
358 //! released here now!
360 System
.out
.println("object listened to will be disposed");
363 public void processLinguServiceEvent( LinguServiceEvent aServiceEvent
)
365 //! do here whatever you think needs to be done depending
366 //! on the event recieved (e.g. trigger background spellchecking
367 //! or hyphenation again.)
369 System
.out
.println("Listener called");