remove assert looking for new compatibilityMode DOCX
[LibreOffice.git] / odk / examples / DevelopersGuide / OfficeDev / Linguistic / SampleHyphenator.java
blob4fcd413a723b9ae9000cb890e74d4f5de501138a
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
5 * the BSD license.
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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 *************************************************************************/
36 // uno
37 import com.sun.star.lib.uno.helper.ComponentBase;
38 import com.sun.star.uno.UnoRuntime;
40 // factories
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;
50 // Exceptions
51 import com.sun.star.uno.Exception;
52 import com.sun.star.lang.IllegalArgumentException;
54 //used Interfaces
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
65 XHyphenator,
66 XLinguServiceEventBroadcaster,
67 XInitialization,
68 XServiceDisplayName,
69 XServiceInfo
71 PropChgHelper_Hyph aPropChgHelper;
72 ArrayList<?> aEvtListeners;
73 boolean bDisposing;
75 public SampleHyphenator()
77 // names of relevant properties to be used
78 String[] aProps = new String[]
80 "IsIgnoreControlCharacters",
81 "IsUseDictionaryList",
82 "HyphMinLeading",
83 "HyphMinTrailing",
84 "HyphMinWordLength"
86 aPropChgHelper = new PropChgHelper_Hyph( this, aProps );
87 aEvtListeners = new ArrayList<Object>();
88 bDisposing = false;
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(
99 String aPropName,
100 boolean bDefaultVal,
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 );
116 return bRes;
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) {
131 bRes = bDefaultVal;
134 return bRes;
137 private short GetValueToUse(
138 String aPropName,
139 short nDefaultVal,
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 );
155 return nRes;
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) {
170 nRes = nDefaultVal;
173 return nRes;
176 // __________ interface methods __________
180 //XSupportedLocales
182 public Locale[] getLocales()
183 throws com.sun.star.uno.RuntimeException
185 Locale aLocales[] =
187 new Locale( "de", "DE", "" ),
188 new Locale( "en", "US", "" )
191 return aLocales;
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", "" ) ))
200 bRes = true;
201 return bRes;
205 //XHyphenator
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)
214 return null;
216 // linguistic is currently not allowed to throw exceptions
217 // thus we return null fwhich means 'word cannot be hyphenated'
218 if (!hasLocale( aLocale ))
219 return null;
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
224 //! them here.
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" ) )
254 if (4 <= nLast)
255 nHyphenationPos = nHyphenPos = 4;
256 else
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);
275 return xRes;
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)
285 return null;
287 // linguistic is currently not allowed to throw exceptions
288 // thus we return null which means 'word cannot be hyphenated'
289 if (!hasLocale( aLocale ))
290 return null;
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
295 //! them here.
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
314 return xRes;
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)
324 return null;
326 // linguistic is currently not allowed to throw exceptions
327 // thus we return null which means 'word cannot be hyphenated'
328 if (!hasLocale( aLocale ))
329 return null;
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
334 //! them here.
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[]
366 { (short) 3 };
367 xRes = new XPossibleHyphens_impl(aWord, aLocale,
368 "driv=ing", aPos);
372 return xRes;
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 );
385 return bRes;
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 );
395 return bRes;
399 // XServiceDisplayName
401 public String getServiceDisplayName( Locale aLocale )
402 throws com.sun.star.uno.RuntimeException
404 return "Java Samples";
408 // XInitialization
410 public void initialize( Object[] aArguments )
411 throws com.sun.star.uno.Exception,
412 com.sun.star.uno.RuntimeException
414 int nLen = aArguments.length;
415 if (2 == nLen)
417 XPropertySet xPropSet = UnoRuntime.queryInterface(
418 XPropertySet.class, aArguments[0]);
419 // start listening to property changes
420 aPropChgHelper.AddAsListenerTo( xPropSet );
426 // XServiceInfo
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 ] );
438 return bResult;
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" };
460 return aResult;
465 * Returns a factory for creating the service.
466 * This method is called by the <code>JavaLoader</code>
467 * <p>
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(
473 String aImplName )
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: */