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",
86 aPropChgHelper
= new PropChgHelper_Hyph( this, aProps
);
87 aEvtListeners
= new ArrayList
<Object
>();
91 private boolean IsEqual( Locale aLoc1
, Locale aLoc2
)
93 return aLoc1
.Language
.equals( aLoc2
.Language
) &&
94 aLoc1
.Country
.equals( aLoc2
.Country
) &&
95 aLoc1
.Variant
.equals( aLoc2
.Variant
);
98 private boolean GetValueToUse(
101 PropertyValue
[] aProps
)
103 boolean bRes
= bDefaultVal
;
107 // use temporary value if supplied
108 for (int i
= 0; i
< aProps
.length
; ++i
)
110 if (aPropName
.equals( aProps
[i
].Name
))
112 Object aObj
= aProps
[i
].Value
;
113 if (AnyConverter
.isBoolean( aObj
))
115 bRes
= AnyConverter
.toBoolean( aObj
);
121 // otherwise use value from property set (if available)
122 XPropertySet xPropSet
= aPropChgHelper
.GetPropSet();
123 if (xPropSet
!= null) // should always be the case
125 Object aObj
= xPropSet
.getPropertyValue( aPropName
);
126 if (AnyConverter
.isBoolean( aObj
))
127 bRes
= AnyConverter
.toBoolean( aObj
);
130 catch (Exception e
) {
137 private short GetValueToUse(
140 PropertyValue
[] aProps
)
142 short nRes
= nDefaultVal
;
146 // use temporary value if supplied
147 for (int i
= 0; i
< aProps
.length
; ++i
)
149 if (aPropName
.equals( aProps
[i
].Name
))
151 Object aObj
= aProps
[i
].Value
;
152 if (AnyConverter
.isShort( aObj
))
154 nRes
= AnyConverter
.toShort( aObj
);
160 // otherwise use value from property set (if available)
161 XPropertySet xPropSet
= aPropChgHelper
.GetPropSet();
162 if (xPropSet
!= null) // should always be the case
164 Object aObj
= xPropSet
.getPropertyValue( aPropName
);
165 if (AnyConverter
.isShort( aObj
))
166 nRes
= AnyConverter
.toShort( aObj
);
169 catch (Exception e
) {
176 // __________ interface methods __________
182 public Locale
[] getLocales()
183 throws com
.sun
.star
.uno
.RuntimeException
187 new Locale( "de", "DE", "" ),
188 new Locale( "en", "US", "" )
194 public boolean hasLocale( Locale aLocale
)
195 throws com
.sun
.star
.uno
.RuntimeException
197 boolean bRes
= false;
198 if ( IsEqual( aLocale
, new Locale( "de", "DE", "" ) ) ||
199 IsEqual( aLocale
, new Locale( "en", "US", "" ) ))
207 public XHyphenatedWord
hyphenate(
208 String aWord
, Locale aLocale
,
209 short nMaxLeading
, PropertyValue
[] aProperties
)
210 throws com
.sun
.star
.uno
.RuntimeException
,
211 IllegalArgumentException
213 if (IsEqual( aLocale
, new Locale() ) || aWord
.length() == 0)
216 // linguistic is currently not allowed to throw exceptions
217 // thus we return null fwhich means 'word cannot be hyphenated'
218 if (!hasLocale( aLocale
))
221 // get values of relevant properties that may be used.
222 //! The values for 'IsIgnoreControlCharacters' and 'IsUseDictionaryList'
223 //! are handled by the dispatcher! Thus there is no need to access
225 boolean bIsIgnoreControlCharacters
= GetValueToUse( "IsIgnoreControlCharacters", true, aProperties
);
226 boolean bIsUseDictionaryList
= GetValueToUse( "IsUseDictionaryList", true, aProperties
);
227 short nHyphMinLeading
= GetValueToUse( "HyphMinLeading", (short)2, aProperties
);
228 short nHyphMinTrailing
= GetValueToUse( "HyphMinTrailing", (short)2, aProperties
);
229 short nHyphMinWordLen
= GetValueToUse( "HyphMinWordLength", (short)5, aProperties
);
231 XHyphenatedWord xRes
= null;
233 if (aWord
.length() >= nHyphMinWordLen
)
235 String aHyphenatedWord
= aWord
;
236 short nHyphenationPos
= -1;
237 short nHyphenPos
= -1;
239 //!! This code needs to be replaced by code calling the actual
240 //!! implementation of your hyphenator
241 if (IsEqual( aLocale
, new Locale( "de", "DE", "" ) ) )
243 if (aWord
.equals( "Schifffahrt" ))
245 nHyphenationPos
= nHyphenPos
= 5;
248 else if (IsEqual( aLocale
, new Locale( "en", "US", "" ) ) )
250 int nLast
= aWord
.length() - 1 - nHyphMinTrailing
;
252 if ( aWord
.equals( "waterfall" ) )
255 nHyphenationPos
= nHyphenPos
= 4;
257 nHyphenationPos
= nHyphenPos
= 1;
259 else if ( aWord
.equals( "driving" ) )
261 nHyphenationPos
= nHyphenPos
= 3;
265 // check if hyphenation pos is valid,
266 // a value of -1 indicates that hyphenation is not possible
267 if ( nHyphenationPos
!= -1 &&
268 (nHyphenationPos
> nHyphMinLeading
) &&
269 (nHyphenationPos
< aWord
.length() - nHyphMinTrailing
))
271 xRes
= new XHyphenatedWord_impl(aWord
, aLocale
,
272 nHyphenationPos
, aHyphenatedWord
, nHyphenPos
);
278 public XHyphenatedWord
queryAlternativeSpelling(
279 String aWord
, Locale aLocale
,
280 short nIndex
, PropertyValue
[] aProperties
)
281 throws com
.sun
.star
.uno
.RuntimeException
,
282 IllegalArgumentException
284 if (IsEqual( aLocale
, new Locale() ) || aWord
.length() == 0)
287 // linguistic is currently not allowed to throw exceptions
288 // thus we return null which means 'word cannot be hyphenated'
289 if (!hasLocale( aLocale
))
292 // get values of relevant properties that may be used.
293 //! The values for 'IsIgnoreControlCharacters' and 'IsUseDictionaryList'
294 //! are handled by the dispatcher! Thus there is no need to access
296 boolean bIsIgnoreControlCharacters
= GetValueToUse( "IsIgnoreControlCharacters", true, aProperties
);
297 boolean bIsUseDictionaryList
= GetValueToUse( "IsUseDictionaryList", true, aProperties
);
298 short nHyphMinLeading
= GetValueToUse( "HyphMinLeading", (short)2, aProperties
);
299 short nHyphMinTrailing
= GetValueToUse( "HyphMinTrailing", (short)2, aProperties
);
300 short nHyphMinWordLen
= GetValueToUse( "HyphMinWordLength", (short)5, aProperties
);
302 XHyphenatedWord xRes
= null;
304 //!! This code needs to be replaced by code calling the actual
305 //!! implementation of your hyphenator
306 if ( IsEqual( aLocale
, new Locale( "de", "DE", "" ) ) )
309 else if ( IsEqual( aLocale
, new Locale( "en", "US", "" ) ) )
311 // There are no alternative spellings in the English language
317 public XPossibleHyphens
createPossibleHyphens(
318 String aWord
, Locale aLocale
,
319 PropertyValue
[] aProperties
)
320 throws com
.sun
.star
.uno
.RuntimeException
,
321 IllegalArgumentException
323 if (IsEqual( aLocale
, new Locale() ) || aWord
.length() == 0)
326 // linguistic is currently not allowed to throw exceptions
327 // thus we return null which means 'word cannot be hyphenated'
328 if (!hasLocale( aLocale
))
331 // get values of relevant properties that may be used.
332 //! The values for 'IsIgnoreControlCharacters' and 'IsUseDictionaryList'
333 //! are handled by the dispatcher! Thus there is no need to access
335 boolean bIsIgnoreControlCharacters
= GetValueToUse( "IsIgnoreControlCharacters", true, aProperties
);
336 boolean bIsUseDictionaryList
= GetValueToUse( "IsUseDictionaryList", true, aProperties
);
337 short nHyphMinLeading
= GetValueToUse( "HyphMinLeading", (short)2, aProperties
);
338 short nHyphMinTrailing
= GetValueToUse( "HyphMinTrailing", (short)2, aProperties
);
339 short nHyphMinWordLen
= GetValueToUse( "HyphMinWordLength", (short)5, aProperties
);
341 XPossibleHyphens xRes
= null;
343 //!! This code needs to be replaced by code calling the actual
344 //!! implementation of your hyphenator
345 if ( IsEqual( aLocale
, new Locale( "de", "DE", "" ) ) )
347 if (aWord
.equals( "Schifffahrt" ))
349 short aPos
[] = new short[] { (short) 5 };
350 xRes
= new XPossibleHyphens_impl(aWord
, aLocale
,
351 "Schiff=fahrt", aPos
);
354 else if ( IsEqual( aLocale
, new Locale( "en", "US", "" ) ) )
356 if ( aWord
.equals( "waterfall" ) )
358 short aPos
[] = new short[]
359 { (short) 1, (short) 4 };
360 xRes
= new XPossibleHyphens_impl(aWord
, aLocale
,
361 "wa=ter=fall", aPos
);
363 else if ( aWord
.equals( "driving" ) )
365 short aPos
[] = new short[]
367 xRes
= new XPossibleHyphens_impl(aWord
, aLocale
,
376 //XLinguServiceEventBroadcaster
378 public boolean addLinguServiceEventListener (
379 XLinguServiceEventListener xLstnr
)
380 throws com
.sun
.star
.uno
.RuntimeException
382 boolean bRes
= false;
383 if (!bDisposing
&& xLstnr
!= null)
384 bRes
= aPropChgHelper
.addLinguServiceEventListener( xLstnr
);
388 public boolean removeLinguServiceEventListener(
389 XLinguServiceEventListener xLstnr
)
390 throws com
.sun
.star
.uno
.RuntimeException
392 boolean bRes
= false;
393 if (!bDisposing
&& xLstnr
!= null)
394 bRes
= aPropChgHelper
.removeLinguServiceEventListener( xLstnr
);
399 // XServiceDisplayName
401 public String
getServiceDisplayName( Locale aLocale
)
402 throws com
.sun
.star
.uno
.RuntimeException
404 return "Java Samples";
410 public void initialize( Object
[] aArguments
)
411 throws com
.sun
.star
.uno
.Exception
,
412 com
.sun
.star
.uno
.RuntimeException
414 int nLen
= aArguments
.length
;
417 XPropertySet xPropSet
= UnoRuntime
.queryInterface(
418 XPropertySet
.class, aArguments
[0]);
419 // start listening to property changes
420 aPropChgHelper
.AddAsListenerTo( xPropSet
);
428 public boolean supportsService( String aServiceName
)
429 throws com
.sun
.star
.uno
.RuntimeException
431 String
[] aServices
= getSupportedServiceNames_Static();
432 int i
, nLength
= aServices
.length
;
433 boolean bResult
= false;
435 for( i
= 0; !bResult
&& i
< nLength
; ++i
)
436 bResult
= aServiceName
.equals( aServices
[ i
] );
441 public String
getImplementationName()
442 throws com
.sun
.star
.uno
.RuntimeException
444 return _aSvcImplName
;
447 public String
[] getSupportedServiceNames()
448 throws com
.sun
.star
.uno
.RuntimeException
450 return getSupportedServiceNames_Static();
453 // __________ static things __________
455 public static String _aSvcImplName
= SampleHyphenator
.class.getName();
457 public static String
[] getSupportedServiceNames_Static()
459 String
[] aResult
= { "com.sun.star.linguistic2.Hyphenator" };
465 * Returns a factory for creating the service.
466 * This method is called by the <code>JavaLoader</code>
468 * @return returns a <code>XComponentServiceFactory</code> for creating the component
469 * @param aImplName the name of the implementation for which a service is desired
470 * @see com.sun.star.comp.loader.JavaLoader
472 public static XSingleComponentFactory
__getComponentFactory(
475 XSingleComponentFactory xSingleComponentFactory
= null;
476 if( aImplName
.equals( _aSvcImplName
) )
478 xSingleComponentFactory
= new OneInstanceFactory(
479 SampleHyphenator
.class, _aSvcImplName
,
480 getSupportedServiceNames_Static() );
482 return xSingleComponentFactory
;
486 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */