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
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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 *************************************************************************/
37 import com
.sun
.star
.lib
.uno
.helper
.ComponentBase
;
38 import com
.sun
.star
.uno
.UnoRuntime
;
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
;
51 import com
.sun
.star
.uno
.Exception
;
52 import com
.sun
.star
.lang
.IllegalArgumentException
;
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
66 XLinguServiceEventBroadcaster
,
71 PropChgHelper_Spell aPropChgHelper
;
72 ArrayList
<?
> aEvtListeners
;
75 public SampleSpellChecker()
77 // names of relevant properties to be used
78 String
[] aProps
= new String
[]
80 "IsIgnoreControlCharacters",
81 "IsUseDictionaryList",
85 aPropChgHelper
= new PropChgHelper_Spell( this, aProps
);
86 aEvtListeners
= new ArrayList
<Object
>();
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(
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
);
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
) {
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
) ))
154 private short GetSpellFailure(
157 PropertyValue
[] aProperties
)
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" );
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
;
191 private XSpellAlternatives
GetProposals(
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
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
,
241 // __________ interface methods __________
247 public Locale
[] getLocales()
248 throws com
.sun
.star
.uno
.RuntimeException
252 new Locale( "de", "DE", "" ),
253 new Locale( "en", "US", "" )
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", "" ) ))
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)
282 // linguistic is currently not allowed to throw exceptions
283 // thus we return null which means 'word cannot be spelled'
284 if (!hasLocale( aLocale
))
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
291 boolean bIsSpellWithDigits
= GetValueToUse( "IsSpellWithDigits", false, aProperties
);
292 boolean bIsSpellUpperCase
= GetValueToUse( "IsSpellUpperCase", false, aProperties
);
294 short nFailure
= GetSpellFailure( aWord
, aLocale
, aProperties
);
297 // postprocess result for errors that should be ignored
298 if ( (!bIsSpellUpperCase
&& IsUpper( aWord
, aLocale
))
299 || (!bIsSpellWithDigits
&& HasDigits( aWord
))
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)
317 // linguistic is currently not allowed to throw exceptions
318 // thus we return null fwhich means 'word cannot be spelled'
319 if (!hasLocale( aLocale
))
322 XSpellAlternatives xRes
= null;
323 if (!isValid( aWord
, aLocale
, aProperties
))
325 xRes
= GetProposals( aWord
, aLocale
, aProperties
);
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
);
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
);
355 // XServiceDisplayName
357 public String
getServiceDisplayName( Locale aLocale
)
358 throws com
.sun
.star
.uno
.RuntimeException
360 return "Java Samples";
366 public void initialize( Object
[] aArguments
)
367 throws com
.sun
.star
.uno
.Exception
,
368 com
.sun
.star
.uno
.RuntimeException
370 int nLen
= aArguments
.length
;
373 XPropertySet xPropSet
= UnoRuntime
.queryInterface(
374 XPropertySet
.class, aArguments
[0]);
375 // start listening to property changes
376 aPropChgHelper
.AddAsListenerTo( xPropSet
);
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
] );
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" };
420 * Returns a factory for creating the service.
421 * This method is called by the <code>JavaLoader</code>
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(
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: */