merge the formfield patch from ooo-build
[ooovba.git] / odk / examples / DevelopersGuide / OfficeDev / Linguistic / SampleHyphenator.java
blobc2a512a4096728bc383808f1704c0e927617a27a
1 /*************************************************************************
3 * $RCSfile: SampleHyphenator.java,v $
5 * $Revision: 1.3 $
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
10 * the BSD license.
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
17 * are met:
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 *************************************************************************/
41 // uno
42 import com.sun.star.lib.uno.helper.ComponentBase;
43 import com.sun.star.uno.UnoRuntime;
45 // factories
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;
57 // Exceptions
58 import com.sun.star.uno.Exception;
59 import com.sun.star.uno.RuntimeException;
60 import com.sun.star.lang.IllegalArgumentException;
62 //used Interfaces
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
79 XHyphenator,
80 XLinguServiceEventBroadcaster,
81 XInitialization,
82 XServiceDisplayName,
83 XServiceInfo
85 PropChgHelper_Hyph aPropChgHelper;
86 ArrayList aEvtListeners;
87 boolean bDisposing;
89 public SampleHyphenator()
91 // names of relevant properties to be used
92 String[] aProps = new String[]
94 "IsIgnoreControlCharacters",
95 "IsUseDictionaryList",
96 "IsGermanPreReform",
97 "HyphMinLeading",
98 "HyphMinTrailing",
99 "HyphMinWordLength"
101 aPropChgHelper = new PropChgHelper_Hyph( (XHyphenator) this, aProps );
102 aEvtListeners = new ArrayList();;
103 bDisposing = false;
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(
114 String aPropName,
115 boolean bDefaultVal,
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 );
131 return bRes;
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) {
146 bRes = bDefaultVal;
149 return bRes;
152 private short GetValueToUse(
153 String aPropName,
154 short nDefaultVal,
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 );
170 return nRes;
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) {
185 nRes = nDefaultVal;
188 return nRes;
191 // __________ interface methods __________
194 //*****************
195 //XSupportedLocales
196 //*****************
197 public Locale[] getLocales()
198 throws com.sun.star.uno.RuntimeException
200 Locale aLocales[] =
202 new Locale( "de", "DE", "" ),
203 new Locale( "en", "US", "" )
206 return aLocales;
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", "" ) ))
215 bRes = true;
216 return bRes;
219 //***********
220 //XHyphenator
221 //***********
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)
229 return null;
231 // linguistic is currently not allowed to throw exceptions
232 // thus we return null fwhich means 'word cannot be hyphenated'
233 if (!hasLocale( aLocale ))
234 return null;
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
239 //! them here.
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";
265 nHyphenationPos = 4;
266 nHyphenPos = 5;
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" ) )
279 if (4 <= nLast)
280 nHyphenationPos = nHyphenPos = 4;
281 else
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);
300 return xRes;
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)
310 return null;
312 // linguistic is currently not allowed to throw exceptions
313 // thus we return null which means 'word cannot be hyphenated'
314 if (!hasLocale( aLocale ))
315 return null;
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
320 //! them here.
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
336 // is set.
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
349 return xRes;
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)
359 return null;
361 // linguistic is currently not allowed to throw exceptions
362 // thus we return null which means 'word cannot be hyphenated'
363 if (!hasLocale( aLocale ))
364 return null;
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
369 //! them here.
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[]
408 { (short) 3 };
409 xRes = new XPossibleHyphens_impl(aWord, aLocale,
410 "driv=ing", aPos);
414 return xRes;
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 );
427 return bRes;
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 );
437 return bRes;
440 //********************
441 // XServiceDisplayName
442 //********************
443 public String getServiceDisplayName( Locale aLocale )
444 throws com.sun.star.uno.RuntimeException
446 return "Java Samples";
449 //****************
450 // XInitialization
451 //****************
452 public void initialize( Object[] aArguments )
453 throws com.sun.star.uno.Exception,
454 com.sun.star.uno.RuntimeException
456 int nLen = aArguments.length;
457 if (2 == nLen)
459 XPropertySet xPropSet = (XPropertySet)UnoRuntime.queryInterface(
460 XPropertySet.class, aArguments[0]);
461 // start listening to property changes
462 aPropChgHelper.AddAsListenerTo( xPropSet );
467 //*************
468 // XServiceInfo
469 //*************
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 ] );
480 return bResult;
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" };
502 return aResult;
507 * Returns a factory for creating the service.
508 * This method is called by the <code>JavaLoader</code>
509 * <p>
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(
517 String aImplName,
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(),
527 xMultiFactory );
529 return xSingleServiceFactory;
533 * Writes the service information into the given registry key.
534 * This method is called by the <code>JavaLoader</code>
535 * <p>
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 );
551 return bResult;