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