1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  *  The Contents of this file are made available subject to the terms of
4*cdf0e10cSrcweir  *  the BSD license.
5*cdf0e10cSrcweir  *
6*cdf0e10cSrcweir  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7*cdf0e10cSrcweir  *  All rights reserved.
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  *  Redistribution and use in source and binary forms, with or without
10*cdf0e10cSrcweir  *  modification, are permitted provided that the following conditions
11*cdf0e10cSrcweir  *  are met:
12*cdf0e10cSrcweir  *  1. Redistributions of source code must retain the above copyright
13*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer.
14*cdf0e10cSrcweir  *  2. Redistributions in binary form must reproduce the above copyright
15*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer in the
16*cdf0e10cSrcweir  *     documentation and/or other materials provided with the distribution.
17*cdf0e10cSrcweir  *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
18*cdf0e10cSrcweir  *     contributors may be used to endorse or promote products derived
19*cdf0e10cSrcweir  *     from this software without specific prior written permission.
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22*cdf0e10cSrcweir  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23*cdf0e10cSrcweir  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24*cdf0e10cSrcweir  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25*cdf0e10cSrcweir  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26*cdf0e10cSrcweir  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27*cdf0e10cSrcweir  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28*cdf0e10cSrcweir  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29*cdf0e10cSrcweir  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30*cdf0e10cSrcweir  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31*cdf0e10cSrcweir  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*cdf0e10cSrcweir  *
33*cdf0e10cSrcweir  *************************************************************************/
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir // uno
36*cdf0e10cSrcweir import com.sun.star.lib.uno.helper.ComponentBase;
37*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir // factories
40*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
41*cdf0e10cSrcweir import com.sun.star.lang.XSingleServiceFactory;
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir // supported Interfaces
44*cdf0e10cSrcweir import com.sun.star.linguistic2.XHyphenator;
45*cdf0e10cSrcweir import com.sun.star.linguistic2.XLinguServiceEventBroadcaster;
46*cdf0e10cSrcweir import com.sun.star.lang.XInitialization;
47*cdf0e10cSrcweir import com.sun.star.lang.XComponent;
48*cdf0e10cSrcweir import com.sun.star.lang.XServiceInfo;
49*cdf0e10cSrcweir import com.sun.star.lang.XServiceDisplayName;
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir // Exceptions
52*cdf0e10cSrcweir import com.sun.star.uno.Exception;
53*cdf0e10cSrcweir import com.sun.star.uno.RuntimeException;
54*cdf0e10cSrcweir import com.sun.star.lang.IllegalArgumentException;
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir //used Interfaces
57*cdf0e10cSrcweir import com.sun.star.linguistic2.XLinguServiceEventListener;
58*cdf0e10cSrcweir import com.sun.star.linguistic2.XHyphenatedWord;
59*cdf0e10cSrcweir import com.sun.star.linguistic2.XPossibleHyphens;
60*cdf0e10cSrcweir import com.sun.star.lang.Locale;
61*cdf0e10cSrcweir import com.sun.star.lang.XEventListener;
62*cdf0e10cSrcweir import com.sun.star.lang.EventObject;
63*cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
64*cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
65*cdf0e10cSrcweir import com.sun.star.uno.AnyConverter;
66*cdf0e10cSrcweir import com.sun.star.lang.XTypeProvider;
67*cdf0e10cSrcweir import com.sun.star.uno.XInterface;
68*cdf0e10cSrcweir import com.sun.star.uno.Type;
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir import java.util.ArrayList;
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir public class SampleHyphenator extends ComponentBase implements
73*cdf0e10cSrcweir         XHyphenator,
74*cdf0e10cSrcweir         XLinguServiceEventBroadcaster,
75*cdf0e10cSrcweir         XInitialization,
76*cdf0e10cSrcweir         XServiceDisplayName,
77*cdf0e10cSrcweir         XServiceInfo
78*cdf0e10cSrcweir {
79*cdf0e10cSrcweir     PropChgHelper_Hyph          aPropChgHelper;
80*cdf0e10cSrcweir     ArrayList                   aEvtListeners;
81*cdf0e10cSrcweir     boolean                     bDisposing;
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir     public SampleHyphenator()
84*cdf0e10cSrcweir     {
85*cdf0e10cSrcweir         // names of relevant properties to be used
86*cdf0e10cSrcweir         String[] aProps = new String[]
87*cdf0e10cSrcweir             {
88*cdf0e10cSrcweir                 "IsIgnoreControlCharacters",
89*cdf0e10cSrcweir                 "IsUseDictionaryList",
90*cdf0e10cSrcweir                 "IsGermanPreReform",
91*cdf0e10cSrcweir                 "HyphMinLeading",
92*cdf0e10cSrcweir                 "HyphMinTrailing",
93*cdf0e10cSrcweir                 "HyphMinWordLength"
94*cdf0e10cSrcweir             };
95*cdf0e10cSrcweir         aPropChgHelper  = new PropChgHelper_Hyph( (XHyphenator) this, aProps );
96*cdf0e10cSrcweir         aEvtListeners   = new ArrayList();;
97*cdf0e10cSrcweir         bDisposing      = false;
98*cdf0e10cSrcweir     }
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir     private boolean IsEqual( Locale aLoc1, Locale aLoc2 )
101*cdf0e10cSrcweir     {
102*cdf0e10cSrcweir         return aLoc1.Language.equals( aLoc2.Language ) &&
103*cdf0e10cSrcweir                aLoc1.Country .equals( aLoc2.Country )  &&
104*cdf0e10cSrcweir                aLoc1.Variant .equals( aLoc2.Variant );
105*cdf0e10cSrcweir     }
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir     private boolean GetValueToUse(
108*cdf0e10cSrcweir             String          aPropName,
109*cdf0e10cSrcweir             boolean         bDefaultVal,
110*cdf0e10cSrcweir             PropertyValue[] aProps )
111*cdf0e10cSrcweir     {
112*cdf0e10cSrcweir         boolean bRes = bDefaultVal;
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir         try
115*cdf0e10cSrcweir         {
116*cdf0e10cSrcweir             // use temporary value if supplied
117*cdf0e10cSrcweir             for (int i = 0;  i < aProps.length;  ++i)
118*cdf0e10cSrcweir             {
119*cdf0e10cSrcweir                 if (aPropName.equals( aProps[i].Name ))
120*cdf0e10cSrcweir                 {
121*cdf0e10cSrcweir                     Object aObj = aProps[i].Value;
122*cdf0e10cSrcweir                     if (AnyConverter.isBoolean( aObj ))
123*cdf0e10cSrcweir                     {
124*cdf0e10cSrcweir                         bRes = AnyConverter.toBoolean( aObj );
125*cdf0e10cSrcweir                         return bRes;
126*cdf0e10cSrcweir                     }
127*cdf0e10cSrcweir                 }
128*cdf0e10cSrcweir             }
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir             // otherwise use value from property set (if available)
131*cdf0e10cSrcweir             XPropertySet xPropSet = aPropChgHelper.GetPropSet();
132*cdf0e10cSrcweir             if (xPropSet != null)   // should always be the case
133*cdf0e10cSrcweir             {
134*cdf0e10cSrcweir                 Object aObj = xPropSet.getPropertyValue( aPropName );
135*cdf0e10cSrcweir                 if (AnyConverter.isBoolean( aObj ))
136*cdf0e10cSrcweir                     bRes = AnyConverter.toBoolean( aObj );
137*cdf0e10cSrcweir             }
138*cdf0e10cSrcweir         }
139*cdf0e10cSrcweir         catch (Exception e) {
140*cdf0e10cSrcweir             bRes = bDefaultVal;
141*cdf0e10cSrcweir         }
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir         return bRes;
144*cdf0e10cSrcweir     }
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir     private short GetValueToUse(
147*cdf0e10cSrcweir             String          aPropName,
148*cdf0e10cSrcweir             short           nDefaultVal,
149*cdf0e10cSrcweir             PropertyValue[] aProps )
150*cdf0e10cSrcweir     {
151*cdf0e10cSrcweir         short nRes = nDefaultVal;
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir         try
154*cdf0e10cSrcweir         {
155*cdf0e10cSrcweir             // use temporary value if supplied
156*cdf0e10cSrcweir             for (int i = 0;  i < aProps.length;  ++i)
157*cdf0e10cSrcweir             {
158*cdf0e10cSrcweir                 if (aPropName.equals( aProps[i].Name ))
159*cdf0e10cSrcweir                 {
160*cdf0e10cSrcweir                     Object aObj = aProps[i].Value;
161*cdf0e10cSrcweir                     if (AnyConverter.isShort( aObj ))
162*cdf0e10cSrcweir                     {
163*cdf0e10cSrcweir                         nRes = AnyConverter.toShort( aObj );
164*cdf0e10cSrcweir                         return nRes;
165*cdf0e10cSrcweir                     }
166*cdf0e10cSrcweir                 }
167*cdf0e10cSrcweir             }
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir             // otherwise use value from property set (if available)
170*cdf0e10cSrcweir             XPropertySet xPropSet = aPropChgHelper.GetPropSet();
171*cdf0e10cSrcweir             if (xPropSet != null)   // should always be the case
172*cdf0e10cSrcweir             {
173*cdf0e10cSrcweir                     Object aObj = xPropSet.getPropertyValue( aPropName );
174*cdf0e10cSrcweir                     if (AnyConverter.isShort( aObj ))
175*cdf0e10cSrcweir                         nRes = AnyConverter.toShort( aObj );
176*cdf0e10cSrcweir             }
177*cdf0e10cSrcweir         }
178*cdf0e10cSrcweir         catch (Exception e) {
179*cdf0e10cSrcweir             nRes = nDefaultVal;
180*cdf0e10cSrcweir         }
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir         return nRes;
183*cdf0e10cSrcweir     }
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir     // __________ interface methods __________
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir     //*****************
189*cdf0e10cSrcweir     //XSupportedLocales
190*cdf0e10cSrcweir     //*****************
191*cdf0e10cSrcweir     public Locale[] getLocales()
192*cdf0e10cSrcweir         throws com.sun.star.uno.RuntimeException
193*cdf0e10cSrcweir     {
194*cdf0e10cSrcweir         Locale aLocales[] =
195*cdf0e10cSrcweir         {
196*cdf0e10cSrcweir             new Locale( "de", "DE", "" ),
197*cdf0e10cSrcweir             new Locale( "en", "US", "" )
198*cdf0e10cSrcweir         };
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir         return aLocales;
201*cdf0e10cSrcweir     }
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir     public boolean hasLocale( Locale aLocale )
204*cdf0e10cSrcweir         throws com.sun.star.uno.RuntimeException
205*cdf0e10cSrcweir     {
206*cdf0e10cSrcweir         boolean bRes = false;
207*cdf0e10cSrcweir         if ( IsEqual( aLocale, new Locale( "de", "DE", "" ) )  ||
208*cdf0e10cSrcweir              IsEqual( aLocale, new Locale( "en", "US", "" ) ))
209*cdf0e10cSrcweir             bRes = true;
210*cdf0e10cSrcweir         return bRes;
211*cdf0e10cSrcweir     }
212*cdf0e10cSrcweir 
213*cdf0e10cSrcweir     //***********
214*cdf0e10cSrcweir     //XHyphenator
215*cdf0e10cSrcweir     //***********
216*cdf0e10cSrcweir     public XHyphenatedWord hyphenate(
217*cdf0e10cSrcweir             String aWord, Locale aLocale,
218*cdf0e10cSrcweir             short nMaxLeading, PropertyValue[] aProperties )
219*cdf0e10cSrcweir         throws com.sun.star.uno.RuntimeException,
220*cdf0e10cSrcweir                IllegalArgumentException
221*cdf0e10cSrcweir     {
222*cdf0e10cSrcweir         if (IsEqual( aLocale, new Locale() ) || aWord.length() == 0)
223*cdf0e10cSrcweir             return null;
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir         // linguistic is currently not allowed to throw exceptions
226*cdf0e10cSrcweir         // thus we return null fwhich means 'word cannot be hyphenated'
227*cdf0e10cSrcweir         if (!hasLocale( aLocale ))
228*cdf0e10cSrcweir             return null;
229*cdf0e10cSrcweir 
230*cdf0e10cSrcweir         // get values of relevant properties that may be used.
231*cdf0e10cSrcweir         //! The values for 'IsIgnoreControlCharacters' and 'IsUseDictionaryList'
232*cdf0e10cSrcweir         //! are handled by the dispatcher! Thus there is no need to access
233*cdf0e10cSrcweir         //! them here.
234*cdf0e10cSrcweir         boolean bIsIgnoreControlCharacters  = GetValueToUse( "IsIgnoreControlCharacters", true, aProperties );
235*cdf0e10cSrcweir         boolean bIsUseDictionaryList        = GetValueToUse( "IsUseDictionaryList", true, aProperties );
236*cdf0e10cSrcweir         boolean bIsGermanPreReform          = GetValueToUse( "IsGermanPreReform", false, aProperties );
237*cdf0e10cSrcweir         short   nHyphMinLeading             = GetValueToUse( "HyphMinLeading", (short)2, aProperties );
238*cdf0e10cSrcweir         short   nHyphMinTrailing            = GetValueToUse( "HyphMinTrailing", (short)2, aProperties );
239*cdf0e10cSrcweir         short   nHyphMinWordLen             = GetValueToUse( "HyphMinWordLength", (short)5, aProperties );
240*cdf0e10cSrcweir 
241*cdf0e10cSrcweir         XHyphenatedWord xRes = null;
242*cdf0e10cSrcweir 
243*cdf0e10cSrcweir         if (aWord.length() >= nHyphMinWordLen)
244*cdf0e10cSrcweir         {
245*cdf0e10cSrcweir             String  aHyphenatedWord = aWord;
246*cdf0e10cSrcweir             short   nHyphenationPos = -1;
247*cdf0e10cSrcweir             short   nHyphenPos = -1;
248*cdf0e10cSrcweir 
249*cdf0e10cSrcweir             //!! This code needs to be replaced by code calling the actual
250*cdf0e10cSrcweir             //!! implementation of your hyphenator
251*cdf0e10cSrcweir             if (IsEqual( aLocale, new Locale( "de", "DE", "" ) ) )
252*cdf0e10cSrcweir             {
253*cdf0e10cSrcweir                 if (bIsGermanPreReform && aWord.equals( "Schiffahrt" ))
254*cdf0e10cSrcweir                 {
255*cdf0e10cSrcweir                     // Note: there is only one position where the word
256*cdf0e10cSrcweir                     // can be hyphenated...
257*cdf0e10cSrcweir 
258*cdf0e10cSrcweir                     aHyphenatedWord = "Schifffahrt";
259*cdf0e10cSrcweir                     nHyphenationPos = 4;
260*cdf0e10cSrcweir                     nHyphenPos = 5;
261*cdf0e10cSrcweir                 }
262*cdf0e10cSrcweir                 else if (!bIsGermanPreReform && aWord.equals( "Schifffahrt" ))
263*cdf0e10cSrcweir                 {
264*cdf0e10cSrcweir                     nHyphenationPos = nHyphenPos = 5;
265*cdf0e10cSrcweir                 }
266*cdf0e10cSrcweir             }
267*cdf0e10cSrcweir             else if (IsEqual( aLocale, new Locale( "en", "US", "" ) ) )
268*cdf0e10cSrcweir             {
269*cdf0e10cSrcweir                 int nLast = aWord.length() - 1 - nHyphMinTrailing;
270*cdf0e10cSrcweir 
271*cdf0e10cSrcweir                 if ( aWord.equals( "waterfall" ) )
272*cdf0e10cSrcweir                 {
273*cdf0e10cSrcweir                     if (4 <= nLast)
274*cdf0e10cSrcweir                         nHyphenationPos = nHyphenPos = 4;
275*cdf0e10cSrcweir                     else
276*cdf0e10cSrcweir                         nHyphenationPos = nHyphenPos = 1;
277*cdf0e10cSrcweir                 }
278*cdf0e10cSrcweir                 else if ( aWord.equals( "driving" ) )
279*cdf0e10cSrcweir                 {
280*cdf0e10cSrcweir                     nHyphenationPos = nHyphenPos = 3;
281*cdf0e10cSrcweir                 }
282*cdf0e10cSrcweir             }
283*cdf0e10cSrcweir 
284*cdf0e10cSrcweir             // check if hyphenation pos is valid,
285*cdf0e10cSrcweir             // a value of -1 indicates that hyphenation is not possible
286*cdf0e10cSrcweir             if (  nHyphenationPos != -1 &&
287*cdf0e10cSrcweir                 !(nHyphenationPos <  nHyphMinLeading) &&
288*cdf0e10cSrcweir                 !(nHyphenationPos >= aWord.length() - nHyphMinTrailing))
289*cdf0e10cSrcweir             {
290*cdf0e10cSrcweir                 xRes = new XHyphenatedWord_impl(aWord, aLocale,
291*cdf0e10cSrcweir                                 nHyphenationPos, aHyphenatedWord, nHyphenPos);
292*cdf0e10cSrcweir             }
293*cdf0e10cSrcweir         }
294*cdf0e10cSrcweir         return xRes;
295*cdf0e10cSrcweir     }
296*cdf0e10cSrcweir 
297*cdf0e10cSrcweir     public XHyphenatedWord queryAlternativeSpelling(
298*cdf0e10cSrcweir             String aWord, Locale aLocale,
299*cdf0e10cSrcweir             short nIndex, PropertyValue[] aProperties )
300*cdf0e10cSrcweir         throws com.sun.star.uno.RuntimeException,
301*cdf0e10cSrcweir                IllegalArgumentException
302*cdf0e10cSrcweir     {
303*cdf0e10cSrcweir         if (IsEqual( aLocale, new Locale() ) || aWord.length() == 0)
304*cdf0e10cSrcweir             return null;
305*cdf0e10cSrcweir 
306*cdf0e10cSrcweir         // linguistic is currently not allowed to throw exceptions
307*cdf0e10cSrcweir         // thus we return null which means 'word cannot be hyphenated'
308*cdf0e10cSrcweir         if (!hasLocale( aLocale ))
309*cdf0e10cSrcweir             return null;
310*cdf0e10cSrcweir 
311*cdf0e10cSrcweir         // get values of relevant properties that may be used.
312*cdf0e10cSrcweir         //! The values for 'IsIgnoreControlCharacters' and 'IsUseDictionaryList'
313*cdf0e10cSrcweir         //! are handled by the dispatcher! Thus there is no need to access
314*cdf0e10cSrcweir         //! them here.
315*cdf0e10cSrcweir         boolean bIsIgnoreControlCharacters  = GetValueToUse( "IsIgnoreControlCharacters", true, aProperties );
316*cdf0e10cSrcweir         boolean bIsUseDictionaryList        = GetValueToUse( "IsUseDictionaryList", true, aProperties );
317*cdf0e10cSrcweir         boolean bIsGermanPreReform          = GetValueToUse( "IsGermanPreReform", false, aProperties );
318*cdf0e10cSrcweir         short   nHyphMinLeading             = GetValueToUse( "HyphMinLeading", (short)2, aProperties );
319*cdf0e10cSrcweir         short   nHyphMinTrailing            = GetValueToUse( "HyphMinTrailing", (short)2, aProperties );
320*cdf0e10cSrcweir         short   nHyphMinWordLen             = GetValueToUse( "HyphMinWordLength", (short)5, aProperties );
321*cdf0e10cSrcweir 
322*cdf0e10cSrcweir         XHyphenatedWord xRes = null;
323*cdf0e10cSrcweir 
324*cdf0e10cSrcweir         //!! This code needs to be replaced by code calling the actual
325*cdf0e10cSrcweir         //!! implementation of your hyphenator
326*cdf0e10cSrcweir         if ( IsEqual( aLocale, new Locale( "de", "DE", "" ) ) )
327*cdf0e10cSrcweir         {
328*cdf0e10cSrcweir             // there is an alternative spelling only when the
329*cdf0e10cSrcweir             // word is hyphenated between the "ff" and old german spelling
330*cdf0e10cSrcweir             // is set.
331*cdf0e10cSrcweir             if (aWord.equals( "Schiffahrt" ) &&
332*cdf0e10cSrcweir                 bIsGermanPreReform && nIndex == 4)
333*cdf0e10cSrcweir             {
334*cdf0e10cSrcweir                 xRes = new XHyphenatedWord_impl(aWord, aLocale,
335*cdf0e10cSrcweir                               (short)4, "Schifffahrt", (short)5 );
336*cdf0e10cSrcweir             }
337*cdf0e10cSrcweir         }
338*cdf0e10cSrcweir         else if ( IsEqual( aLocale, new Locale( "en", "US", "" ) ) )
339*cdf0e10cSrcweir         {
340*cdf0e10cSrcweir             // There are no alternative spellings in the English language
341*cdf0e10cSrcweir         }
342*cdf0e10cSrcweir 
343*cdf0e10cSrcweir         return xRes;
344*cdf0e10cSrcweir     }
345*cdf0e10cSrcweir 
346*cdf0e10cSrcweir     public XPossibleHyphens createPossibleHyphens(
347*cdf0e10cSrcweir             String aWord, Locale aLocale,
348*cdf0e10cSrcweir             PropertyValue[] aProperties )
349*cdf0e10cSrcweir         throws com.sun.star.uno.RuntimeException,
350*cdf0e10cSrcweir                IllegalArgumentException
351*cdf0e10cSrcweir     {
352*cdf0e10cSrcweir         if (IsEqual( aLocale, new Locale() ) || aWord.length() == 0)
353*cdf0e10cSrcweir             return null;
354*cdf0e10cSrcweir 
355*cdf0e10cSrcweir         // linguistic is currently not allowed to throw exceptions
356*cdf0e10cSrcweir         // thus we return null which means 'word cannot be hyphenated'
357*cdf0e10cSrcweir         if (!hasLocale( aLocale ))
358*cdf0e10cSrcweir             return null;
359*cdf0e10cSrcweir 
360*cdf0e10cSrcweir         // get values of relevant properties that may be used.
361*cdf0e10cSrcweir         //! The values for 'IsIgnoreControlCharacters' and 'IsUseDictionaryList'
362*cdf0e10cSrcweir         //! are handled by the dispatcher! Thus there is no need to access
363*cdf0e10cSrcweir         //! them here.
364*cdf0e10cSrcweir         boolean bIsIgnoreControlCharacters  = GetValueToUse( "IsIgnoreControlCharacters", true, aProperties );
365*cdf0e10cSrcweir         boolean bIsUseDictionaryList        = GetValueToUse( "IsUseDictionaryList", true, aProperties );
366*cdf0e10cSrcweir         boolean bIsGermanPreReform          = GetValueToUse( "IsGermanPreReform", false, aProperties );
367*cdf0e10cSrcweir         short   nHyphMinLeading             = GetValueToUse( "HyphMinLeading", (short)2, aProperties );
368*cdf0e10cSrcweir         short   nHyphMinTrailing            = GetValueToUse( "HyphMinTrailing", (short)2, aProperties );
369*cdf0e10cSrcweir         short   nHyphMinWordLen             = GetValueToUse( "HyphMinWordLength", (short)5, aProperties );
370*cdf0e10cSrcweir 
371*cdf0e10cSrcweir         XPossibleHyphens xRes = null;
372*cdf0e10cSrcweir 
373*cdf0e10cSrcweir         //!! This code needs to be replaced by code calling the actual
374*cdf0e10cSrcweir         //!! implementation of your hyphenator
375*cdf0e10cSrcweir         if ( IsEqual( aLocale, new Locale( "de", "DE", "" ) ) )
376*cdf0e10cSrcweir         {
377*cdf0e10cSrcweir             if (bIsGermanPreReform && aWord.equals( "Schiffahrt" ))
378*cdf0e10cSrcweir             {
379*cdf0e10cSrcweir                 short aPos[] = new short[] { (short) 4 };
380*cdf0e10cSrcweir                 xRes = new XPossibleHyphens_impl(aWord, aLocale,
381*cdf0e10cSrcweir                             "Schiff=fahrt", aPos);
382*cdf0e10cSrcweir             }
383*cdf0e10cSrcweir             else if (!bIsGermanPreReform && aWord.equals( "Schifffahrt" ))
384*cdf0e10cSrcweir             {
385*cdf0e10cSrcweir                 short aPos[] = new short[] { (short) 5 };
386*cdf0e10cSrcweir                 xRes = new XPossibleHyphens_impl(aWord, aLocale,
387*cdf0e10cSrcweir                             "Schiff=fahrt", aPos);
388*cdf0e10cSrcweir             }
389*cdf0e10cSrcweir         }
390*cdf0e10cSrcweir         else if ( IsEqual( aLocale, new Locale( "en", "US", "" ) ) )
391*cdf0e10cSrcweir         {
392*cdf0e10cSrcweir             if ( aWord.equals( "waterfall" ) )
393*cdf0e10cSrcweir             {
394*cdf0e10cSrcweir                 short aPos[] = new short[]
395*cdf0e10cSrcweir                         { (short) 1, (short) 4 };
396*cdf0e10cSrcweir                 xRes = new XPossibleHyphens_impl(aWord, aLocale,
397*cdf0e10cSrcweir                             "wa=ter=fall", aPos);
398*cdf0e10cSrcweir             }
399*cdf0e10cSrcweir             else if ( aWord.equals( "driving" ) )
400*cdf0e10cSrcweir             {
401*cdf0e10cSrcweir                 short aPos[] = new short[]
402*cdf0e10cSrcweir                         { (short) 3 };
403*cdf0e10cSrcweir                 xRes = new XPossibleHyphens_impl(aWord, aLocale,
404*cdf0e10cSrcweir                             "driv=ing", aPos);
405*cdf0e10cSrcweir             }
406*cdf0e10cSrcweir         }
407*cdf0e10cSrcweir 
408*cdf0e10cSrcweir         return xRes;
409*cdf0e10cSrcweir     }
410*cdf0e10cSrcweir 
411*cdf0e10cSrcweir     //*****************************
412*cdf0e10cSrcweir     //XLinguServiceEventBroadcaster
413*cdf0e10cSrcweir     //*****************************
414*cdf0e10cSrcweir     public boolean addLinguServiceEventListener (
415*cdf0e10cSrcweir             XLinguServiceEventListener xLstnr )
416*cdf0e10cSrcweir         throws com.sun.star.uno.RuntimeException
417*cdf0e10cSrcweir     {
418*cdf0e10cSrcweir         boolean bRes = false;
419*cdf0e10cSrcweir         if (!bDisposing && xLstnr != null)
420*cdf0e10cSrcweir             bRes = aPropChgHelper.addLinguServiceEventListener( xLstnr );
421*cdf0e10cSrcweir         return bRes;
422*cdf0e10cSrcweir     }
423*cdf0e10cSrcweir 
424*cdf0e10cSrcweir     public boolean removeLinguServiceEventListener(
425*cdf0e10cSrcweir             XLinguServiceEventListener xLstnr )
426*cdf0e10cSrcweir         throws com.sun.star.uno.RuntimeException
427*cdf0e10cSrcweir     {
428*cdf0e10cSrcweir         boolean bRes = false;
429*cdf0e10cSrcweir         if (!bDisposing && xLstnr != null)
430*cdf0e10cSrcweir             bRes = aPropChgHelper.removeLinguServiceEventListener( xLstnr );
431*cdf0e10cSrcweir         return bRes;
432*cdf0e10cSrcweir     }
433*cdf0e10cSrcweir 
434*cdf0e10cSrcweir     //********************
435*cdf0e10cSrcweir     // XServiceDisplayName
436*cdf0e10cSrcweir     //********************
437*cdf0e10cSrcweir     public String getServiceDisplayName( Locale aLocale )
438*cdf0e10cSrcweir         throws com.sun.star.uno.RuntimeException
439*cdf0e10cSrcweir     {
440*cdf0e10cSrcweir         return "Java Samples";
441*cdf0e10cSrcweir     }
442*cdf0e10cSrcweir 
443*cdf0e10cSrcweir     //****************
444*cdf0e10cSrcweir     // XInitialization
445*cdf0e10cSrcweir     //****************
446*cdf0e10cSrcweir     public void initialize( Object[] aArguments )
447*cdf0e10cSrcweir         throws com.sun.star.uno.Exception,
448*cdf0e10cSrcweir                com.sun.star.uno.RuntimeException
449*cdf0e10cSrcweir     {
450*cdf0e10cSrcweir         int nLen = aArguments.length;
451*cdf0e10cSrcweir         if (2 == nLen)
452*cdf0e10cSrcweir         {
453*cdf0e10cSrcweir             XPropertySet xPropSet = (XPropertySet)UnoRuntime.queryInterface(
454*cdf0e10cSrcweir                                          XPropertySet.class, aArguments[0]);
455*cdf0e10cSrcweir             // start listening to property changes
456*cdf0e10cSrcweir             aPropChgHelper.AddAsListenerTo( xPropSet );
457*cdf0e10cSrcweir         }
458*cdf0e10cSrcweir     }
459*cdf0e10cSrcweir 
460*cdf0e10cSrcweir 
461*cdf0e10cSrcweir     //*************
462*cdf0e10cSrcweir     // XServiceInfo
463*cdf0e10cSrcweir     //*************
464*cdf0e10cSrcweir     public boolean supportsService( String aServiceName )
465*cdf0e10cSrcweir         throws com.sun.star.uno.RuntimeException
466*cdf0e10cSrcweir     {
467*cdf0e10cSrcweir         String[] aServices = getSupportedServiceNames_Static();
468*cdf0e10cSrcweir         int i, nLength = aServices.length;
469*cdf0e10cSrcweir         boolean bResult = false;
470*cdf0e10cSrcweir 
471*cdf0e10cSrcweir         for( i = 0; !bResult && i < nLength; ++i )
472*cdf0e10cSrcweir             bResult = aServiceName.equals( aServices[ i ] );
473*cdf0e10cSrcweir 
474*cdf0e10cSrcweir         return bResult;
475*cdf0e10cSrcweir     }
476*cdf0e10cSrcweir 
477*cdf0e10cSrcweir     public String getImplementationName()
478*cdf0e10cSrcweir         throws com.sun.star.uno.RuntimeException
479*cdf0e10cSrcweir     {
480*cdf0e10cSrcweir         return _aSvcImplName;
481*cdf0e10cSrcweir     }
482*cdf0e10cSrcweir 
483*cdf0e10cSrcweir     public String[] getSupportedServiceNames()
484*cdf0e10cSrcweir         throws com.sun.star.uno.RuntimeException
485*cdf0e10cSrcweir     {
486*cdf0e10cSrcweir         return getSupportedServiceNames_Static();
487*cdf0e10cSrcweir     }
488*cdf0e10cSrcweir 
489*cdf0e10cSrcweir     // __________ static things __________
490*cdf0e10cSrcweir 
491*cdf0e10cSrcweir     public static String _aSvcImplName = SampleHyphenator.class.getName();
492*cdf0e10cSrcweir 
493*cdf0e10cSrcweir     public static String[] getSupportedServiceNames_Static()
494*cdf0e10cSrcweir     {
495*cdf0e10cSrcweir         String[] aResult = { "com.sun.star.linguistic2.Hyphenator" };
496*cdf0e10cSrcweir         return aResult;
497*cdf0e10cSrcweir     }
498*cdf0e10cSrcweir 
499*cdf0e10cSrcweir 
500*cdf0e10cSrcweir     /**
501*cdf0e10cSrcweir      * Returns a factory for creating the service.
502*cdf0e10cSrcweir      * This method is called by the <code>JavaLoader</code>
503*cdf0e10cSrcweir      * <p>
504*cdf0e10cSrcweir      * @return  returns a <code>XSingleServiceFactory</code> for creating the component
505*cdf0e10cSrcweir      * @param   implName     the name of the implementation for which a service is desired
506*cdf0e10cSrcweir      * @param   multiFactory the service manager to be used if needed
507*cdf0e10cSrcweir      * @param   regKey       the registryKey
508*cdf0e10cSrcweir      * @see                  com.sun.star.comp.loader.JavaLoader
509*cdf0e10cSrcweir      */
510*cdf0e10cSrcweir     public static XSingleServiceFactory __getServiceFactory(
511*cdf0e10cSrcweir         String aImplName,
512*cdf0e10cSrcweir         XMultiServiceFactory xMultiFactory,
513*cdf0e10cSrcweir         com.sun.star.registry.XRegistryKey xRegKey )
514*cdf0e10cSrcweir     {
515*cdf0e10cSrcweir         XSingleServiceFactory xSingleServiceFactory = null;
516*cdf0e10cSrcweir         if( aImplName.equals( _aSvcImplName ) )
517*cdf0e10cSrcweir         {
518*cdf0e10cSrcweir             xSingleServiceFactory = new OneInstanceFactory(
519*cdf0e10cSrcweir                     SampleHyphenator.class, _aSvcImplName,
520*cdf0e10cSrcweir                     getSupportedServiceNames_Static(),
521*cdf0e10cSrcweir                     xMultiFactory );
522*cdf0e10cSrcweir         }
523*cdf0e10cSrcweir         return xSingleServiceFactory;
524*cdf0e10cSrcweir     }
525*cdf0e10cSrcweir 
526*cdf0e10cSrcweir     /**
527*cdf0e10cSrcweir      * Writes the service information into the given registry key.
528*cdf0e10cSrcweir      * This method is called by the <code>JavaLoader</code>
529*cdf0e10cSrcweir      * <p>
530*cdf0e10cSrcweir      * @return  returns true if the operation succeeded
531*cdf0e10cSrcweir      * @param   xRegKey       the registryKey
532*cdf0e10cSrcweir      * @see                  com.sun.star.comp.loader.JavaLoader
533*cdf0e10cSrcweir      */
534*cdf0e10cSrcweir     // This method not longer necessary since OOo 3.4 where the component registration
535*cdf0e10cSrcweir     // was changed to passive component registration. For more details see
536*cdf0e10cSrcweir     // http://wiki.services.openoffice.org/wiki/Passive_Component_Registration
537*cdf0e10cSrcweir 
538*cdf0e10cSrcweir //     public static boolean __writeRegistryServiceInfo(
539*cdf0e10cSrcweir //             com.sun.star.registry.XRegistryKey xRegKey )
540*cdf0e10cSrcweir //     {
541*cdf0e10cSrcweir //         boolean bResult = true;
542*cdf0e10cSrcweir //         String[] aServices = getSupportedServiceNames_Static();
543*cdf0e10cSrcweir //         int i, nLength = aServices.length;
544*cdf0e10cSrcweir //         for( i = 0; i < nLength; ++i )
545*cdf0e10cSrcweir //         {
546*cdf0e10cSrcweir //             bResult = bResult && com.sun.star.comp.loader.FactoryHelper.writeRegistryServiceInfo(
547*cdf0e10cSrcweir //                 _aSvcImplName, aServices[i], xRegKey );
548*cdf0e10cSrcweir //         }
549*cdf0e10cSrcweir //         return bResult;
550*cdf0e10cSrcweir //     }
551*cdf0e10cSrcweir }
552*cdf0e10cSrcweir 
553