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
.XHyphenator
;
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
.XHyphenatedWord
;
57 import com
.sun
.star
.linguistic2
.XPossibleHyphens
;
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 SampleHyphenator
extends ComponentBase
implements
66 XLinguServiceEventBroadcaster
,
71 PropChgHelper_Hyph aPropChgHelper
;
72 ArrayList
<?
> aEvtListeners
;
75 public SampleHyphenator()
77 // names of relevant properties to be used
78 String
[] aProps
= new String
[]
80 "IsIgnoreControlCharacters",
81 "IsUseDictionaryList",
87 aPropChgHelper
= new PropChgHelper_Hyph( this, aProps
);
88 aEvtListeners
= new ArrayList
<Object
>();
92 private boolean IsEqual( Locale aLoc1
, Locale aLoc2
)
94 return aLoc1
.Language
.equals( aLoc2
.Language
) &&
95 aLoc1
.Country
.equals( aLoc2
.Country
) &&
96 aLoc1
.Variant
.equals( aLoc2
.Variant
);
99 private boolean GetValueToUse(
102 PropertyValue
[] aProps
)
104 boolean bRes
= bDefaultVal
;
108 // use temporary value if supplied
109 for (int i
= 0; i
< aProps
.length
; ++i
)
111 if (aPropName
.equals( aProps
[i
].Name
))
113 Object aObj
= aProps
[i
].Value
;
114 if (AnyConverter
.isBoolean( aObj
))
116 bRes
= AnyConverter
.toBoolean( aObj
);
122 // otherwise use value from property set (if available)
123 XPropertySet xPropSet
= aPropChgHelper
.GetPropSet();
124 if (xPropSet
!= null) // should always be the case
126 Object aObj
= xPropSet
.getPropertyValue( aPropName
);
127 if (AnyConverter
.isBoolean( aObj
))
128 bRes
= AnyConverter
.toBoolean( aObj
);
131 catch (Exception e
) {
138 private short GetValueToUse(
141 PropertyValue
[] aProps
)
143 short nRes
= nDefaultVal
;
147 // use temporary value if supplied
148 for (int i
= 0; i
< aProps
.length
; ++i
)
150 if (aPropName
.equals( aProps
[i
].Name
))
152 Object aObj
= aProps
[i
].Value
;
153 if (AnyConverter
.isShort( aObj
))
155 nRes
= AnyConverter
.toShort( aObj
);
161 // otherwise use value from property set (if available)
162 XPropertySet xPropSet
= aPropChgHelper
.GetPropSet();
163 if (xPropSet
!= null) // should always be the case
165 Object aObj
= xPropSet
.getPropertyValue( aPropName
);
166 if (AnyConverter
.isShort( aObj
))
167 nRes
= AnyConverter
.toShort( aObj
);
170 catch (Exception e
) {
177 // __________ interface methods __________
183 public Locale
[] getLocales()
184 throws com
.sun
.star
.uno
.RuntimeException
188 new Locale( "de", "DE", "" ),
189 new Locale( "en", "US", "" )
195 public boolean hasLocale( Locale aLocale
)
196 throws com
.sun
.star
.uno
.RuntimeException
198 boolean bRes
= false;
199 if ( IsEqual( aLocale
, new Locale( "de", "DE", "" ) ) ||
200 IsEqual( aLocale
, new Locale( "en", "US", "" ) ))
208 public XHyphenatedWord
hyphenate(
209 String aWord
, Locale aLocale
,
210 short nMaxLeading
, PropertyValue
[] aProperties
)
211 throws com
.sun
.star
.uno
.RuntimeException
,
212 IllegalArgumentException
214 if (IsEqual( aLocale
, new Locale() ) || aWord
.length() == 0)
217 // linguistic is currently not allowed to throw exceptions
218 // thus we return null fwhich means 'word cannot be hyphenated'
219 if (!hasLocale( aLocale
))
222 // get values of relevant properties that may be used.
223 //! The values for 'IsIgnoreControlCharacters' and 'IsUseDictionaryList'
224 //! are handled by the dispatcher! Thus there is no need to access
226 boolean bIsIgnoreControlCharacters
= GetValueToUse( "IsIgnoreControlCharacters", true, aProperties
);
227 boolean bIsUseDictionaryList
= GetValueToUse( "IsUseDictionaryList", true, aProperties
);
228 boolean bIsGermanPreReform
= GetValueToUse( "IsGermanPreReform", false, aProperties
);
229 short nHyphMinLeading
= GetValueToUse( "HyphMinLeading", (short)2, aProperties
);
230 short nHyphMinTrailing
= GetValueToUse( "HyphMinTrailing", (short)2, aProperties
);
231 short nHyphMinWordLen
= GetValueToUse( "HyphMinWordLength", (short)5, aProperties
);
233 XHyphenatedWord xRes
= null;
235 if (aWord
.length() >= nHyphMinWordLen
)
237 String aHyphenatedWord
= aWord
;
238 short nHyphenationPos
= -1;
239 short nHyphenPos
= -1;
241 //!! This code needs to be replaced by code calling the actual
242 //!! implementation of your hyphenator
243 if (IsEqual( aLocale
, new Locale( "de", "DE", "" ) ) )
245 if (bIsGermanPreReform
&& aWord
.equals( "Schiffahrt" ))
247 // Note: there is only one position where the word
248 // can be hyphenated...
250 aHyphenatedWord
= "Schifffahrt";
254 else if (!bIsGermanPreReform
&& aWord
.equals( "Schifffahrt" ))
256 nHyphenationPos
= nHyphenPos
= 5;
259 else if (IsEqual( aLocale
, new Locale( "en", "US", "" ) ) )
261 int nLast
= aWord
.length() - 1 - nHyphMinTrailing
;
263 if ( aWord
.equals( "waterfall" ) )
266 nHyphenationPos
= nHyphenPos
= 4;
268 nHyphenationPos
= nHyphenPos
= 1;
270 else if ( aWord
.equals( "driving" ) )
272 nHyphenationPos
= nHyphenPos
= 3;
276 // check if hyphenation pos is valid,
277 // a value of -1 indicates that hyphenation is not possible
278 if ( nHyphenationPos
!= -1 &&
279 (nHyphenationPos
> nHyphMinLeading
) &&
280 (nHyphenationPos
< aWord
.length() - nHyphMinTrailing
))
282 xRes
= new XHyphenatedWord_impl(aWord
, aLocale
,
283 nHyphenationPos
, aHyphenatedWord
, nHyphenPos
);
289 public XHyphenatedWord
queryAlternativeSpelling(
290 String aWord
, Locale aLocale
,
291 short nIndex
, PropertyValue
[] aProperties
)
292 throws com
.sun
.star
.uno
.RuntimeException
,
293 IllegalArgumentException
295 if (IsEqual( aLocale
, new Locale() ) || aWord
.length() == 0)
298 // linguistic is currently not allowed to throw exceptions
299 // thus we return null which means 'word cannot be hyphenated'
300 if (!hasLocale( aLocale
))
303 // get values of relevant properties that may be used.
304 //! The values for 'IsIgnoreControlCharacters' and 'IsUseDictionaryList'
305 //! are handled by the dispatcher! Thus there is no need to access
307 boolean bIsIgnoreControlCharacters
= GetValueToUse( "IsIgnoreControlCharacters", true, aProperties
);
308 boolean bIsUseDictionaryList
= GetValueToUse( "IsUseDictionaryList", true, aProperties
);
309 boolean bIsGermanPreReform
= GetValueToUse( "IsGermanPreReform", false, aProperties
);
310 short nHyphMinLeading
= GetValueToUse( "HyphMinLeading", (short)2, aProperties
);
311 short nHyphMinTrailing
= GetValueToUse( "HyphMinTrailing", (short)2, aProperties
);
312 short nHyphMinWordLen
= GetValueToUse( "HyphMinWordLength", (short)5, aProperties
);
314 XHyphenatedWord xRes
= null;
316 //!! This code needs to be replaced by code calling the actual
317 //!! implementation of your hyphenator
318 if ( IsEqual( aLocale
, new Locale( "de", "DE", "" ) ) )
320 // there is an alternative spelling only when the
321 // word is hyphenated between the "ff" and old german spelling
323 if (aWord
.equals( "Schiffahrt" ) &&
324 bIsGermanPreReform
&& nIndex
== 4)
326 xRes
= new XHyphenatedWord_impl(aWord
, aLocale
,
327 (short)4, "Schifffahrt", (short)5 );
330 else if ( IsEqual( aLocale
, new Locale( "en", "US", "" ) ) )
332 // There are no alternative spellings in the English language
338 public XPossibleHyphens
createPossibleHyphens(
339 String aWord
, Locale aLocale
,
340 PropertyValue
[] aProperties
)
341 throws com
.sun
.star
.uno
.RuntimeException
,
342 IllegalArgumentException
344 if (IsEqual( aLocale
, new Locale() ) || aWord
.length() == 0)
347 // linguistic is currently not allowed to throw exceptions
348 // thus we return null which means 'word cannot be hyphenated'
349 if (!hasLocale( aLocale
))
352 // get values of relevant properties that may be used.
353 //! The values for 'IsIgnoreControlCharacters' and 'IsUseDictionaryList'
354 //! are handled by the dispatcher! Thus there is no need to access
356 boolean bIsIgnoreControlCharacters
= GetValueToUse( "IsIgnoreControlCharacters", true, aProperties
);
357 boolean bIsUseDictionaryList
= GetValueToUse( "IsUseDictionaryList", true, aProperties
);
358 boolean bIsGermanPreReform
= GetValueToUse( "IsGermanPreReform", false, aProperties
);
359 short nHyphMinLeading
= GetValueToUse( "HyphMinLeading", (short)2, aProperties
);
360 short nHyphMinTrailing
= GetValueToUse( "HyphMinTrailing", (short)2, aProperties
);
361 short nHyphMinWordLen
= GetValueToUse( "HyphMinWordLength", (short)5, aProperties
);
363 XPossibleHyphens xRes
= null;
365 //!! This code needs to be replaced by code calling the actual
366 //!! implementation of your hyphenator
367 if ( IsEqual( aLocale
, new Locale( "de", "DE", "" ) ) )
369 if (bIsGermanPreReform
&& aWord
.equals( "Schiffahrt" ))
371 short aPos
[] = new short[] { (short) 4 };
372 xRes
= new XPossibleHyphens_impl(aWord
, aLocale
,
373 "Schiff=fahrt", aPos
);
375 else if (!bIsGermanPreReform
&& aWord
.equals( "Schifffahrt" ))
377 short aPos
[] = new short[] { (short) 5 };
378 xRes
= new XPossibleHyphens_impl(aWord
, aLocale
,
379 "Schiff=fahrt", aPos
);
382 else if ( IsEqual( aLocale
, new Locale( "en", "US", "" ) ) )
384 if ( aWord
.equals( "waterfall" ) )
386 short aPos
[] = new short[]
387 { (short) 1, (short) 4 };
388 xRes
= new XPossibleHyphens_impl(aWord
, aLocale
,
389 "wa=ter=fall", aPos
);
391 else if ( aWord
.equals( "driving" ) )
393 short aPos
[] = new short[]
395 xRes
= new XPossibleHyphens_impl(aWord
, aLocale
,
404 //XLinguServiceEventBroadcaster
406 public boolean addLinguServiceEventListener (
407 XLinguServiceEventListener xLstnr
)
408 throws com
.sun
.star
.uno
.RuntimeException
410 boolean bRes
= false;
411 if (!bDisposing
&& xLstnr
!= null)
412 bRes
= aPropChgHelper
.addLinguServiceEventListener( xLstnr
);
416 public boolean removeLinguServiceEventListener(
417 XLinguServiceEventListener xLstnr
)
418 throws com
.sun
.star
.uno
.RuntimeException
420 boolean bRes
= false;
421 if (!bDisposing
&& xLstnr
!= null)
422 bRes
= aPropChgHelper
.removeLinguServiceEventListener( xLstnr
);
427 // XServiceDisplayName
429 public String
getServiceDisplayName( Locale aLocale
)
430 throws com
.sun
.star
.uno
.RuntimeException
432 return "Java Samples";
438 public void initialize( Object
[] aArguments
)
439 throws com
.sun
.star
.uno
.Exception
,
440 com
.sun
.star
.uno
.RuntimeException
442 int nLen
= aArguments
.length
;
445 XPropertySet xPropSet
= UnoRuntime
.queryInterface(
446 XPropertySet
.class, aArguments
[0]);
447 // start listening to property changes
448 aPropChgHelper
.AddAsListenerTo( xPropSet
);
456 public boolean supportsService( String aServiceName
)
457 throws com
.sun
.star
.uno
.RuntimeException
459 String
[] aServices
= getSupportedServiceNames_Static();
460 int i
, nLength
= aServices
.length
;
461 boolean bResult
= false;
463 for( i
= 0; !bResult
&& i
< nLength
; ++i
)
464 bResult
= aServiceName
.equals( aServices
[ i
] );
469 public String
getImplementationName()
470 throws com
.sun
.star
.uno
.RuntimeException
472 return _aSvcImplName
;
475 public String
[] getSupportedServiceNames()
476 throws com
.sun
.star
.uno
.RuntimeException
478 return getSupportedServiceNames_Static();
481 // __________ static things __________
483 public static String _aSvcImplName
= SampleHyphenator
.class.getName();
485 public static String
[] getSupportedServiceNames_Static()
487 String
[] aResult
= { "com.sun.star.linguistic2.Hyphenator" };
493 * Returns a factory for creating the service.
494 * This method is called by the <code>JavaLoader</code>
496 * @return returns a <code>XComponentServiceFactory</code> for creating the component
497 * @param aImplName the name of the implementation for which a service is desired
498 * @see com.sun.star.comp.loader.JavaLoader
500 public static XSingleComponentFactory
__getComponentFactory(
503 XSingleComponentFactory xSingleComponentFactory
= null;
504 if( aImplName
.equals( _aSvcImplName
) )
506 xSingleComponentFactory
= new OneInstanceFactory(
507 SampleHyphenator
.class, _aSvcImplName
,
508 getSupportedServiceNames_Static() );
510 return xSingleComponentFactory
;
514 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */