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