tdf#154285 Check upper bound of arguments in SbRtl_Minute function
[LibreOffice.git] / odk / examples / DevelopersGuide / OfficeDev / Linguistic / SampleSpellChecker.java
blob9608929932fa0e23a265aea6a8208981787b6dee
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 // uno
37 import com.sun.star.lib.uno.helper.ComponentBase;
38 import com.sun.star.uno.UnoRuntime;
40 // factories
41 import com.sun.star.lang.XSingleComponentFactory;
43 // supported Interfaces
44 import com.sun.star.linguistic2.XSpellChecker;
45 import com.sun.star.linguistic2.XLinguServiceEventBroadcaster;
46 import com.sun.star.lang.XInitialization;
47 import com.sun.star.lang.XServiceInfo;
48 import com.sun.star.lang.XServiceDisplayName;
50 // Exceptions
51 import com.sun.star.uno.Exception;
52 import com.sun.star.lang.IllegalArgumentException;
54 //used Interfaces
55 import com.sun.star.linguistic2.XLinguServiceEventListener;
56 import com.sun.star.linguistic2.XSpellAlternatives;
57 import com.sun.star.linguistic2.SpellFailure;
58 import com.sun.star.lang.Locale;
59 import com.sun.star.beans.XPropertySet;
60 import com.sun.star.beans.PropertyValue;
61 import com.sun.star.uno.AnyConverter;
62 import java.util.ArrayList;
64 public class SampleSpellChecker extends ComponentBase implements
65 XSpellChecker,
66 XLinguServiceEventBroadcaster,
67 XInitialization,
68 XServiceDisplayName,
69 XServiceInfo
71 PropChgHelper_Spell aPropChgHelper;
72 ArrayList<?> aEvtListeners;
73 boolean bDisposing;
75 public SampleSpellChecker()
77 // names of relevant properties to be used
78 String[] aProps = new String[]
80 "IsIgnoreControlCharacters",
81 "IsUseDictionaryList",
82 "IsSpellUpperCase",
83 "IsSpellWithDigits",
85 aPropChgHelper = new PropChgHelper_Spell( this, aProps );
86 aEvtListeners = new ArrayList<Object>();
87 bDisposing = false;
90 private boolean IsEqual( Locale aLoc1, Locale aLoc2 )
92 return aLoc1.Language.equals( aLoc2.Language ) &&
93 aLoc1.Country .equals( aLoc2.Country ) &&
94 aLoc1.Variant .equals( aLoc2.Variant );
97 private boolean GetValueToUse(
98 String aPropName,
99 boolean bDefaultVal,
100 PropertyValue[] aProps )
102 boolean bRes = bDefaultVal;
106 // use temporary value if supplied
107 for (int i = 0; i < aProps.length; ++i)
109 if (aPropName.equals( aProps[i].Name ))
111 Object aObj = aProps[i].Value;
112 if (AnyConverter.isBoolean( aObj ))
114 bRes = AnyConverter.toBoolean( aObj );
115 return bRes;
120 // otherwise use value from property set (if available)
121 XPropertySet xPropSet = aPropChgHelper.GetPropSet();
122 if (xPropSet != null) // should always be the case
124 Object aObj = xPropSet.getPropertyValue( aPropName );
125 if (AnyConverter.isBoolean( aObj ))
126 bRes = AnyConverter.toBoolean( aObj );
129 catch (Exception e) {
130 bRes = bDefaultVal;
133 return bRes;
136 private boolean IsUpper( String aWord, Locale aLocale )
138 java.util.Locale aLang = new java.util.Locale(
139 aLocale.Language, aLocale.Country, aLocale.Variant );
140 return aWord.equals( aWord.toUpperCase( aLang ) );
143 private boolean HasDigits( String aWord )
145 int nLen = aWord.length();
146 for (int i = 0; i < nLen; ++i)
148 if (Character.isDigit( aWord.charAt(i) ))
149 return true;
151 return false;
154 private short GetSpellFailure(
155 String aWord,
156 Locale aLocale,
157 PropertyValue[] aProperties )
159 short nRes = -1;
161 //!! This code needs to be replaced by code calling the actual
162 //!! implementation of your spellchecker
163 if (IsEqual( aLocale, new Locale( "de", "DE", "" ) ))
165 if (aWord.equals( "Schiffahrt" ))
166 nRes = SpellFailure.SPELLING_ERROR;
168 else if (IsEqual( aLocale, new Locale( "en", "US", "" ) ))
170 // words with 'u', 'U' and 'arizona' are defined to be incorrect
171 boolean bIsValid = !(aWord.indexOf( "u" ) != -1 || aWord.indexOf( "U" ) != -1)
172 && !aWord.equals( "arizona" );
174 if (!bIsValid)
176 // default value (no other SpellFailure type is applicable)
177 nRes = SpellFailure.SPELLING_ERROR;
179 if (aWord.equals( "arizona" ))
180 nRes = SpellFailure.CAPTION_ERROR;
181 else if (aWord.equals( "house" ))
182 nRes = SpellFailure.SPELLING_ERROR;
183 else if (aWord.equals( "course" ))
184 nRes = SpellFailure.IS_NEGATIVE_WORD;
188 return nRes;
191 private XSpellAlternatives GetProposals(
192 String aWord,
193 Locale aLocale,
194 PropertyValue[] aProperties )
196 short nType = SpellFailure.SPELLING_ERROR;
197 String[] aProposals = null;
199 // get values of relevant properties that may be used.
200 //! The values for 'IsIgnoreControlCharacters' and 'IsUseDictionaryList'
201 //! are handled by the dispatcher! Thus there is no need to access
202 //! them here.
203 boolean bIsSpellWithDigits = GetValueToUse( "IsSpellWithDigits", false, aProperties );
204 boolean bIsSpellUpperCase = GetValueToUse( "IsSpellUpperCase", false, aProperties );
206 //!! This code needs to be replaced by code calling the actual
207 //!! implementation of your spellchecker
208 if (IsEqual( aLocale, new Locale( "de", "DE", "" ) ))
210 if (aWord.equals( "Schiffahrt" ))
212 nType = SpellFailure.SPELLING_ERROR;
213 aProposals = new String[]{ "Schifffahrt" };
216 else if (IsEqual( aLocale, new Locale( "en", "US", "" ) ))
218 if (aWord.equals( "arizona" ))
220 nType = SpellFailure.CAPTION_ERROR;
221 aProposals = new String[]{ "Arizona" };
223 else if (aWord.equals( "house" ))
225 nType = SpellFailure.SPELLING_ERROR;
226 aProposals = new String[]{ "horse", "home" };
228 else if (aWord.equals( "course" ))
230 nType = SpellFailure.IS_NEGATIVE_WORD;
231 aProposals = new String[]{ "line", "plan", "approach" };
235 // always return a result if word is incorrect,
236 // proposals may be empty though.
237 return new XSpellAlternatives_impl( aWord, aLocale,
238 nType, aProposals );
241 // __________ interface methods __________
245 //XSupportedLocales
247 public Locale[] getLocales()
248 throws com.sun.star.uno.RuntimeException
250 Locale aLocales[] =
252 new Locale( "de", "DE", "" ),
253 new Locale( "en", "US", "" )
256 return aLocales;
259 public boolean hasLocale( Locale aLocale )
260 throws com.sun.star.uno.RuntimeException
262 boolean bRes = false;
263 if ( IsEqual( aLocale, new Locale( "de", "DE", "" ) ) ||
264 IsEqual( aLocale, new Locale( "en", "US", "" ) ))
265 bRes = true;
266 return bRes;
271 //XSpellChecker
273 public boolean isValid(
274 String aWord, Locale aLocale,
275 PropertyValue[] aProperties )
276 throws com.sun.star.uno.RuntimeException,
277 IllegalArgumentException
279 if (IsEqual( aLocale, new Locale() ) || aWord.length() == 0)
280 return true;
282 // linguistic is currently not allowed to throw exceptions
283 // thus we return null which means 'word cannot be spelled'
284 if (!hasLocale( aLocale ))
285 return true;
287 // get values of relevant properties that may be used.
288 //! The values for 'IsIgnoreControlCharacters' and 'IsUseDictionaryList'
289 //! are handled by the dispatcher! Thus there is no need to access
290 //! them here.
291 boolean bIsSpellWithDigits = GetValueToUse( "IsSpellWithDigits", false, aProperties );
292 boolean bIsSpellUpperCase = GetValueToUse( "IsSpellUpperCase", false, aProperties );
294 short nFailure = GetSpellFailure( aWord, aLocale, aProperties );
295 if (nFailure != -1)
297 // postprocess result for errors that should be ignored
298 if ( (!bIsSpellUpperCase && IsUpper( aWord, aLocale ))
299 || (!bIsSpellWithDigits && HasDigits( aWord ))
301 nFailure = -1;
304 return nFailure == -1;
308 public XSpellAlternatives spell(
309 String aWord, Locale aLocale,
310 PropertyValue[] aProperties )
311 throws com.sun.star.uno.RuntimeException,
312 IllegalArgumentException
314 if (IsEqual( aLocale, new Locale() ) || aWord.length() == 0)
315 return null;
317 // linguistic is currently not allowed to throw exceptions
318 // thus we return null fwhich means 'word cannot be spelled'
319 if (!hasLocale( aLocale ))
320 return null;
322 XSpellAlternatives xRes = null;
323 if (!isValid( aWord, aLocale, aProperties ))
325 xRes = GetProposals( aWord, aLocale, aProperties );
327 return xRes;
332 //XLinguServiceEventBroadcaster
334 public boolean addLinguServiceEventListener (
335 XLinguServiceEventListener xLstnr )
336 throws com.sun.star.uno.RuntimeException
338 boolean bRes = false;
339 if (!bDisposing && xLstnr != null)
340 bRes = aPropChgHelper.addLinguServiceEventListener( xLstnr );
341 return bRes;
344 public boolean removeLinguServiceEventListener(
345 XLinguServiceEventListener xLstnr )
346 throws com.sun.star.uno.RuntimeException
348 boolean bRes = false;
349 if (!bDisposing && xLstnr != null)
350 bRes = aPropChgHelper.removeLinguServiceEventListener( xLstnr );
351 return bRes;
355 // XServiceDisplayName
357 public String getServiceDisplayName( Locale aLocale )
358 throws com.sun.star.uno.RuntimeException
360 return "Java Samples";
364 // XInitialization
366 public void initialize( Object[] aArguments )
367 throws com.sun.star.uno.Exception,
368 com.sun.star.uno.RuntimeException
370 int nLen = aArguments.length;
371 if (2 == nLen)
373 XPropertySet xPropSet = UnoRuntime.queryInterface(
374 XPropertySet.class, aArguments[0]);
375 // start listening to property changes
376 aPropChgHelper.AddAsListenerTo( xPropSet );
381 // XServiceInfo
383 public boolean supportsService( String aServiceName )
384 throws com.sun.star.uno.RuntimeException
386 String[] aServices = getSupportedServiceNames_Static();
387 int i, nLength = aServices.length;
388 boolean bResult = false;
390 for( i = 0; !bResult && i < nLength; ++i )
391 bResult = aServiceName.equals( aServices[ i ] );
393 return bResult;
396 public String getImplementationName()
397 throws com.sun.star.uno.RuntimeException
399 return _aSvcImplName;
402 public String[] getSupportedServiceNames()
403 throws com.sun.star.uno.RuntimeException
405 return getSupportedServiceNames_Static();
408 // __________ static things __________
410 public static String _aSvcImplName = SampleSpellChecker.class.getName();
412 public static String[] getSupportedServiceNames_Static()
414 String[] aResult = { "com.sun.star.linguistic2.SpellChecker" };
415 return aResult;
420 * Returns a factory for creating the service.
421 * This method is called by the <code>JavaLoader</code>
422 * <p>
423 * @return returns a <code>XSingleComponentFactory</code> for creating the component
424 * @param aImplName the name of the implementation for which a service is desired
425 * @see com.sun.star.comp.loader.JavaLoader
427 public static XSingleComponentFactory __getComponentFactory(
428 String aImplName )
430 XSingleComponentFactory xSingleComponentFactory = null;
431 if( aImplName.equals( _aSvcImplName ) )
433 xSingleComponentFactory = new OneInstanceFactory(
434 SampleSpellChecker.class, _aSvcImplName,
435 getSupportedServiceNames_Static() );
437 return xSingleComponentFactory;
441 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */