1 /*************************************************************************
3 * $RCSfile: SampleHyphenator.java,v $
7 * last change: $Author: hr $ $Date: 2003-06-30 15:40:57 $
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
.lib
.uno
.helper
.ComponentBase
;
43 import com
.sun
.star
.uno
.UnoRuntime
;
46 import com
.sun
.star
.lang
.XMultiServiceFactory
;
47 import com
.sun
.star
.lang
.XSingleServiceFactory
;
49 // supported Interfaces
50 import com
.sun
.star
.linguistic2
.XHyphenator
;
51 import com
.sun
.star
.linguistic2
.XLinguServiceEventBroadcaster
;
52 import com
.sun
.star
.lang
.XInitialization
;
53 import com
.sun
.star
.lang
.XComponent
;
54 import com
.sun
.star
.lang
.XServiceInfo
;
55 import com
.sun
.star
.lang
.XServiceDisplayName
;
58 import com
.sun
.star
.uno
.Exception
;
59 import com
.sun
.star
.uno
.RuntimeException
;
60 import com
.sun
.star
.lang
.IllegalArgumentException
;
63 import com
.sun
.star
.linguistic2
.XLinguServiceEventListener
;
64 import com
.sun
.star
.linguistic2
.XHyphenatedWord
;
65 import com
.sun
.star
.linguistic2
.XPossibleHyphens
;
66 import com
.sun
.star
.lang
.Locale
;
67 import com
.sun
.star
.lang
.XEventListener
;
68 import com
.sun
.star
.lang
.EventObject
;
69 import com
.sun
.star
.beans
.XPropertySet
;
70 import com
.sun
.star
.beans
.PropertyValue
;
71 import com
.sun
.star
.uno
.AnyConverter
;
72 import com
.sun
.star
.lang
.XTypeProvider
;
73 import com
.sun
.star
.uno
.XInterface
;
74 import com
.sun
.star
.uno
.Type
;
76 import java
.util
.ArrayList
;
78 public class SampleHyphenator
extends ComponentBase
implements
80 XLinguServiceEventBroadcaster
,
85 PropChgHelper_Hyph aPropChgHelper
;
86 ArrayList aEvtListeners
;
89 public SampleHyphenator()
91 // names of relevant properties to be used
92 String
[] aProps
= new String
[]
94 "IsIgnoreControlCharacters",
95 "IsUseDictionaryList",
101 aPropChgHelper
= new PropChgHelper_Hyph( (XHyphenator
) this, aProps
);
102 aEvtListeners
= new ArrayList();;
106 private boolean IsEqual( Locale aLoc1
, Locale aLoc2
)
108 return aLoc1
.Language
.equals( aLoc2
.Language
) &&
109 aLoc1
.Country
.equals( aLoc2
.Country
) &&
110 aLoc1
.Variant
.equals( aLoc2
.Variant
);
113 private boolean GetValueToUse(
116 PropertyValue
[] aProps
)
118 boolean bRes
= bDefaultVal
;
122 // use temporary value if supplied
123 for (int i
= 0; i
< aProps
.length
; ++i
)
125 if (aPropName
.equals( aProps
[i
].Name
))
127 Object aObj
= aProps
[i
].Value
;
128 if (AnyConverter
.isBoolean( aObj
))
130 bRes
= AnyConverter
.toBoolean( aObj
);
136 // otherwise use value from property set (if available)
137 XPropertySet xPropSet
= aPropChgHelper
.GetPropSet();
138 if (xPropSet
!= null) // should always be the case
140 Object aObj
= xPropSet
.getPropertyValue( aPropName
);
141 if (AnyConverter
.isBoolean( aObj
))
142 bRes
= AnyConverter
.toBoolean( aObj
);
145 catch (Exception e
) {
152 private short GetValueToUse(
155 PropertyValue
[] aProps
)
157 short nRes
= nDefaultVal
;
161 // use temporary value if supplied
162 for (int i
= 0; i
< aProps
.length
; ++i
)
164 if (aPropName
.equals( aProps
[i
].Name
))
166 Object aObj
= aProps
[i
].Value
;
167 if (AnyConverter
.isShort( aObj
))
169 nRes
= AnyConverter
.toShort( aObj
);
175 // otherwise use value from property set (if available)
176 XPropertySet xPropSet
= aPropChgHelper
.GetPropSet();
177 if (xPropSet
!= null) // should always be the case
179 Object aObj
= xPropSet
.getPropertyValue( aPropName
);
180 if (AnyConverter
.isShort( aObj
))
181 nRes
= AnyConverter
.toShort( aObj
);
184 catch (Exception e
) {
191 // __________ interface methods __________
197 public Locale
[] getLocales()
198 throws com
.sun
.star
.uno
.RuntimeException
202 new Locale( "de", "DE", "" ),
203 new Locale( "en", "US", "" )
209 public boolean hasLocale( Locale aLocale
)
210 throws com
.sun
.star
.uno
.RuntimeException
212 boolean bRes
= false;
213 if ( IsEqual( aLocale
, new Locale( "de", "DE", "" ) ) ||
214 IsEqual( aLocale
, new Locale( "en", "US", "" ) ))
222 public XHyphenatedWord
hyphenate(
223 String aWord
, Locale aLocale
,
224 short nMaxLeading
, PropertyValue
[] aProperties
)
225 throws com
.sun
.star
.uno
.RuntimeException
,
226 IllegalArgumentException
228 if (IsEqual( aLocale
, new Locale() ) || aWord
.length() == 0)
231 // linguistic is currently not allowed to throw exceptions
232 // thus we return null fwhich means 'word cannot be hyphenated'
233 if (!hasLocale( aLocale
))
236 // get values of relevant properties that may be used.
237 //! The values for 'IsIgnoreControlCharacters' and 'IsUseDictionaryList'
238 //! are handled by the dispatcher! Thus there is no need to access
240 boolean bIsIgnoreControlCharacters
= GetValueToUse( "IsIgnoreControlCharacters", true, aProperties
);
241 boolean bIsUseDictionaryList
= GetValueToUse( "IsUseDictionaryList", true, aProperties
);
242 boolean bIsGermanPreReform
= GetValueToUse( "IsGermanPreReform", false, aProperties
);
243 short nHyphMinLeading
= GetValueToUse( "HyphMinLeading", (short)2, aProperties
);
244 short nHyphMinTrailing
= GetValueToUse( "HyphMinTrailing", (short)2, aProperties
);
245 short nHyphMinWordLen
= GetValueToUse( "HyphMinWordLength", (short)5, aProperties
);
247 XHyphenatedWord xRes
= null;
249 if (aWord
.length() >= nHyphMinWordLen
)
251 String aHyphenatedWord
= aWord
;
252 short nHyphenationPos
= -1;
253 short nHyphenPos
= -1;
255 //!! This code needs to be replaced by code calling the actual
256 //!! implementation of your hyphenator
257 if (IsEqual( aLocale
, new Locale( "de", "DE", "" ) ) )
259 if (bIsGermanPreReform
&& aWord
.equals( "Schiffahrt" ))
261 // Note: there is only one position where the word
262 // can be hyphenated...
264 aHyphenatedWord
= "Schifffahrt";
268 else if (!bIsGermanPreReform
&& aWord
.equals( "Schifffahrt" ))
270 nHyphenationPos
= nHyphenPos
= 5;
273 else if (IsEqual( aLocale
, new Locale( "en", "US", "" ) ) )
275 int nLast
= aWord
.length() - 1 - nHyphMinTrailing
;
277 if ( aWord
.equals( "waterfall" ) )
280 nHyphenationPos
= nHyphenPos
= 4;
282 nHyphenationPos
= nHyphenPos
= 1;
284 else if ( aWord
.equals( "driving" ) )
286 nHyphenationPos
= nHyphenPos
= 3;
290 // check if hyphenation pos is valid,
291 // a value of -1 indicates that hyphenation is not possible
292 if ( nHyphenationPos
!= -1 &&
293 !(nHyphenationPos
< nHyphMinLeading
) &&
294 !(nHyphenationPos
>= aWord
.length() - nHyphMinTrailing
))
296 xRes
= new XHyphenatedWord_impl(aWord
, aLocale
,
297 nHyphenationPos
, aHyphenatedWord
, nHyphenPos
);
303 public XHyphenatedWord
queryAlternativeSpelling(
304 String aWord
, Locale aLocale
,
305 short nIndex
, PropertyValue
[] aProperties
)
306 throws com
.sun
.star
.uno
.RuntimeException
,
307 IllegalArgumentException
309 if (IsEqual( aLocale
, new Locale() ) || aWord
.length() == 0)
312 // linguistic is currently not allowed to throw exceptions
313 // thus we return null which means 'word cannot be hyphenated'
314 if (!hasLocale( aLocale
))
317 // get values of relevant properties that may be used.
318 //! The values for 'IsIgnoreControlCharacters' and 'IsUseDictionaryList'
319 //! are handled by the dispatcher! Thus there is no need to access
321 boolean bIsIgnoreControlCharacters
= GetValueToUse( "IsIgnoreControlCharacters", true, aProperties
);
322 boolean bIsUseDictionaryList
= GetValueToUse( "IsUseDictionaryList", true, aProperties
);
323 boolean bIsGermanPreReform
= GetValueToUse( "IsGermanPreReform", false, aProperties
);
324 short nHyphMinLeading
= GetValueToUse( "HyphMinLeading", (short)2, aProperties
);
325 short nHyphMinTrailing
= GetValueToUse( "HyphMinTrailing", (short)2, aProperties
);
326 short nHyphMinWordLen
= GetValueToUse( "HyphMinWordLength", (short)5, aProperties
);
328 XHyphenatedWord xRes
= null;
330 //!! This code needs to be replaced by code calling the actual
331 //!! implementation of your hyphenator
332 if ( IsEqual( aLocale
, new Locale( "de", "DE", "" ) ) )
334 // there is an alternative spelling only when the
335 // word is hyphenated between the "ff" and old german spelling
337 if (aWord
.equals( "Schiffahrt" ) &&
338 bIsGermanPreReform
&& nIndex
== 4)
340 xRes
= new XHyphenatedWord_impl(aWord
, aLocale
,
341 (short)4, "Schifffahrt", (short)5 );
344 else if ( IsEqual( aLocale
, new Locale( "en", "US", "" ) ) )
346 // There are no alternative spellings in the English language
352 public XPossibleHyphens
createPossibleHyphens(
353 String aWord
, Locale aLocale
,
354 PropertyValue
[] aProperties
)
355 throws com
.sun
.star
.uno
.RuntimeException
,
356 IllegalArgumentException
358 if (IsEqual( aLocale
, new Locale() ) || aWord
.length() == 0)
361 // linguistic is currently not allowed to throw exceptions
362 // thus we return null which means 'word cannot be hyphenated'
363 if (!hasLocale( aLocale
))
366 // get values of relevant properties that may be used.
367 //! The values for 'IsIgnoreControlCharacters' and 'IsUseDictionaryList'
368 //! are handled by the dispatcher! Thus there is no need to access
370 boolean bIsIgnoreControlCharacters
= GetValueToUse( "IsIgnoreControlCharacters", true, aProperties
);
371 boolean bIsUseDictionaryList
= GetValueToUse( "IsUseDictionaryList", true, aProperties
);
372 boolean bIsGermanPreReform
= GetValueToUse( "IsGermanPreReform", false, aProperties
);
373 short nHyphMinLeading
= GetValueToUse( "HyphMinLeading", (short)2, aProperties
);
374 short nHyphMinTrailing
= GetValueToUse( "HyphMinTrailing", (short)2, aProperties
);
375 short nHyphMinWordLen
= GetValueToUse( "HyphMinWordLength", (short)5, aProperties
);
377 XPossibleHyphens xRes
= null;
379 //!! This code needs to be replaced by code calling the actual
380 //!! implementation of your hyphenator
381 if ( IsEqual( aLocale
, new Locale( "de", "DE", "" ) ) )
383 if (bIsGermanPreReform
&& aWord
.equals( "Schiffahrt" ))
385 short aPos
[] = new short[] { (short) 4 };
386 xRes
= new XPossibleHyphens_impl(aWord
, aLocale
,
387 "Schiff=fahrt", aPos
);
389 else if (!bIsGermanPreReform
&& aWord
.equals( "Schifffahrt" ))
391 short aPos
[] = new short[] { (short) 5 };
392 xRes
= new XPossibleHyphens_impl(aWord
, aLocale
,
393 "Schiff=fahrt", aPos
);
396 else if ( IsEqual( aLocale
, new Locale( "en", "US", "" ) ) )
398 if ( aWord
.equals( "waterfall" ) )
400 short aPos
[] = new short[]
401 { (short) 1, (short) 4 };
402 xRes
= new XPossibleHyphens_impl(aWord
, aLocale
,
403 "wa=ter=fall", aPos
);
405 else if ( aWord
.equals( "driving" ) )
407 short aPos
[] = new short[]
409 xRes
= new XPossibleHyphens_impl(aWord
, aLocale
,
417 //*****************************
418 //XLinguServiceEventBroadcaster
419 //*****************************
420 public boolean addLinguServiceEventListener (
421 XLinguServiceEventListener xLstnr
)
422 throws com
.sun
.star
.uno
.RuntimeException
424 boolean bRes
= false;
425 if (!bDisposing
&& xLstnr
!= null)
426 bRes
= aPropChgHelper
.addLinguServiceEventListener( xLstnr
);
430 public boolean removeLinguServiceEventListener(
431 XLinguServiceEventListener xLstnr
)
432 throws com
.sun
.star
.uno
.RuntimeException
434 boolean bRes
= false;
435 if (!bDisposing
&& xLstnr
!= null)
436 bRes
= aPropChgHelper
.removeLinguServiceEventListener( xLstnr
);
440 //********************
441 // XServiceDisplayName
442 //********************
443 public String
getServiceDisplayName( Locale aLocale
)
444 throws com
.sun
.star
.uno
.RuntimeException
446 return "Java Samples";
452 public void initialize( Object
[] aArguments
)
453 throws com
.sun
.star
.uno
.Exception
,
454 com
.sun
.star
.uno
.RuntimeException
456 int nLen
= aArguments
.length
;
459 XPropertySet xPropSet
= (XPropertySet
)UnoRuntime
.queryInterface(
460 XPropertySet
.class, aArguments
[0]);
461 // start listening to property changes
462 aPropChgHelper
.AddAsListenerTo( xPropSet
);
470 public boolean supportsService( String aServiceName
)
471 throws com
.sun
.star
.uno
.RuntimeException
473 String
[] aServices
= getSupportedServiceNames_Static();
474 int i
, nLength
= aServices
.length
;
475 boolean bResult
= false;
477 for( i
= 0; !bResult
&& i
< nLength
; ++i
)
478 bResult
= aServiceName
.equals( aServices
[ i
] );
483 public String
getImplementationName()
484 throws com
.sun
.star
.uno
.RuntimeException
486 return _aSvcImplName
;
489 public String
[] getSupportedServiceNames()
490 throws com
.sun
.star
.uno
.RuntimeException
492 return getSupportedServiceNames_Static();
495 // __________ static things __________
497 public static String _aSvcImplName
= "com.sun.star.linguistic2.JavaSamples.SampleHyphenator";
499 public static String
[] getSupportedServiceNames_Static()
501 String
[] aResult
= { "com.sun.star.linguistic2.Hyphenator" };
507 * Returns a factory for creating the service.
508 * This method is called by the <code>JavaLoader</code>
510 * @return returns a <code>XSingleServiceFactory</code> for creating the component
511 * @param implName the name of the implementation for which a service is desired
512 * @param multiFactory the service manager to be used if needed
513 * @param regKey the registryKey
514 * @see com.sun.star.comp.loader.JavaLoader
516 public static XSingleServiceFactory
__getServiceFactory(
518 XMultiServiceFactory xMultiFactory
,
519 com
.sun
.star
.registry
.XRegistryKey xRegKey
)
521 XSingleServiceFactory xSingleServiceFactory
= null;
522 if( aImplName
.equals( _aSvcImplName
) )
524 xSingleServiceFactory
= new OneInstanceFactory(
525 SampleHyphenator
.class, _aSvcImplName
,
526 getSupportedServiceNames_Static(),
529 return xSingleServiceFactory
;
533 * Writes the service information into the given registry key.
534 * This method is called by the <code>JavaLoader</code>
536 * @return returns true if the operation succeeded
537 * @param xRegKey the registryKey
538 * @see com.sun.star.comp.loader.JavaLoader
540 public static boolean __writeRegistryServiceInfo(
541 com
.sun
.star
.registry
.XRegistryKey xRegKey
)
543 boolean bResult
= true;
544 String
[] aServices
= getSupportedServiceNames_Static();
545 int i
, nLength
= aServices
.length
;
546 for( i
= 0; i
< nLength
; ++i
)
548 bResult
= bResult
&& com
.sun
.star
.comp
.loader
.FactoryHelper
.writeRegistryServiceInfo(
549 _aSvcImplName
, aServices
[i
], xRegKey
);