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.XSpellChecker;
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.XSpellAlternatives;
48cdf0e10cSrcweir import com.sun.star.linguistic2.SpellFailure;
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 SampleSpellChecker extends ComponentBase implements
62cdf0e10cSrcweir         XSpellChecker,
63cdf0e10cSrcweir         XLinguServiceEventBroadcaster,
64cdf0e10cSrcweir         XInitialization,
65cdf0e10cSrcweir         XServiceDisplayName,
66cdf0e10cSrcweir         XServiceInfo
67cdf0e10cSrcweir {
68cdf0e10cSrcweir     PropChgHelper_Spell         aPropChgHelper;
69cdf0e10cSrcweir     ArrayList                   aEvtListeners;
70cdf0e10cSrcweir     boolean                     bDisposing;
71cdf0e10cSrcweir 
SampleSpellChecker()72cdf0e10cSrcweir     public SampleSpellChecker()
73cdf0e10cSrcweir     {
74cdf0e10cSrcweir         // names of relevant properties to be used
75cdf0e10cSrcweir         String[] aProps = new String[]
76cdf0e10cSrcweir             {
77cdf0e10cSrcweir                 "IsIgnoreControlCharacters",
78cdf0e10cSrcweir                 "IsUseDictionaryList",
79cdf0e10cSrcweir                 "IsGermanPreReform",
80cdf0e10cSrcweir                 "IsSpellUpperCase",
81cdf0e10cSrcweir                 "IsSpellWithDigits",
82cdf0e10cSrcweir                 "IsSpellCapitalization"
83cdf0e10cSrcweir             };
84cdf0e10cSrcweir         aPropChgHelper  = new PropChgHelper_Spell( (XSpellChecker) 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 
IsUpper( String aWord, Locale aLocale )135cdf0e10cSrcweir     private boolean IsUpper( String aWord, Locale aLocale )
136cdf0e10cSrcweir     {
137cdf0e10cSrcweir         java.util.Locale aLang = new java.util.Locale(
138cdf0e10cSrcweir                 aLocale.Language, aLocale.Country, aLocale.Variant );
139cdf0e10cSrcweir         return aWord.equals( aWord.toUpperCase( aLang ) );
140cdf0e10cSrcweir     }
141cdf0e10cSrcweir 
HasDigits( String aWord )142cdf0e10cSrcweir     private boolean HasDigits( String aWord )
143cdf0e10cSrcweir     {
144cdf0e10cSrcweir         int nLen = aWord.length();
145cdf0e10cSrcweir         for (int i = 0;  i < nLen;  ++i)
146cdf0e10cSrcweir         {
147cdf0e10cSrcweir             if (Character.isDigit( aWord.charAt(i) ))
148cdf0e10cSrcweir                 return true;
149cdf0e10cSrcweir         }
150cdf0e10cSrcweir         return false;
151cdf0e10cSrcweir     }
152cdf0e10cSrcweir 
GetSpellFailure( String aWord, Locale aLocale, PropertyValue[] aProperties )153cdf0e10cSrcweir     private short GetSpellFailure(
154cdf0e10cSrcweir             String aWord,
155cdf0e10cSrcweir             Locale aLocale,
156cdf0e10cSrcweir             PropertyValue[] aProperties )
157cdf0e10cSrcweir     {
158cdf0e10cSrcweir         short nRes = -1;
159cdf0e10cSrcweir 
160cdf0e10cSrcweir         //!! This code needs to be replaced by code calling the actual
161cdf0e10cSrcweir         //!! implementation of your spellchecker
162cdf0e10cSrcweir         boolean bIsGermanPreReform      = GetValueToUse( "IsGermanPreReform", false, aProperties );
163cdf0e10cSrcweir         if (IsEqual( aLocale, new Locale( "de", "DE", "" ) ))
164cdf0e10cSrcweir         {
165cdf0e10cSrcweir             if (bIsGermanPreReform && aWord.equals( "Schifffahrt" ))
166cdf0e10cSrcweir                 nRes = SpellFailure.SPELLING_ERROR;
167cdf0e10cSrcweir             else if (!bIsGermanPreReform && aWord.equals( "Schiffahrt" ))
168cdf0e10cSrcweir                 nRes = SpellFailure.SPELLING_ERROR;
169cdf0e10cSrcweir         }
170cdf0e10cSrcweir         else if (IsEqual( aLocale, new Locale( "en", "US", "" ) ))
171cdf0e10cSrcweir         {
172cdf0e10cSrcweir             // words with 'u', 'U' and 'arizona' are defined to be incorrect
173cdf0e10cSrcweir             boolean bIsValid = !(aWord.indexOf( "u" ) != -1 || aWord.indexOf( "U" ) != -1)
174cdf0e10cSrcweir                                  && !aWord.equals( "arizona" );
175cdf0e10cSrcweir 
176cdf0e10cSrcweir             if (!bIsValid)
177cdf0e10cSrcweir             {
178cdf0e10cSrcweir                 // default value (no other SpellFailure type is applicable)
179cdf0e10cSrcweir                 nRes = SpellFailure.SPELLING_ERROR;
180cdf0e10cSrcweir 
181cdf0e10cSrcweir                 if (aWord.equals( "arizona" ))
182cdf0e10cSrcweir                     nRes = SpellFailure.CAPTION_ERROR;
183cdf0e10cSrcweir                 else if (aWord.equals( "house" ))
184cdf0e10cSrcweir                     nRes = SpellFailure.SPELLING_ERROR;
185cdf0e10cSrcweir                 else if (aWord.equals( "course" ))
186cdf0e10cSrcweir                     nRes = SpellFailure.IS_NEGATIVE_WORD;
187cdf0e10cSrcweir             }
188cdf0e10cSrcweir         }
189cdf0e10cSrcweir 
190cdf0e10cSrcweir         return nRes;
191cdf0e10cSrcweir     }
192cdf0e10cSrcweir 
GetProposals( String aWord, Locale aLocale, PropertyValue[] aProperties )193cdf0e10cSrcweir     private XSpellAlternatives GetProposals(
194cdf0e10cSrcweir             String aWord,
195cdf0e10cSrcweir             Locale aLocale,
196cdf0e10cSrcweir             PropertyValue[] aProperties )
197cdf0e10cSrcweir     {
198cdf0e10cSrcweir         short    nType = SpellFailure.SPELLING_ERROR;
199cdf0e10cSrcweir         String[] aProposals = null;
200cdf0e10cSrcweir 
201cdf0e10cSrcweir         // get values of relevant properties that may be used.
202cdf0e10cSrcweir         //! The values for 'IsIgnoreControlCharacters' and 'IsUseDictionaryList'
203cdf0e10cSrcweir         //! are handled by the dispatcher! Thus there is no need to access
204cdf0e10cSrcweir         //! them here.
205cdf0e10cSrcweir         boolean bIsGermanPreReform      = GetValueToUse( "IsGermanPreReform", false, aProperties );
206cdf0e10cSrcweir         boolean bIsSpellWithDigits      = GetValueToUse( "IsSpellWithDigits", false, aProperties );
207cdf0e10cSrcweir         boolean bIsSpellUpperCase       = GetValueToUse( "IsSpellUpperCase", false, aProperties );
208cdf0e10cSrcweir         boolean bIsSpellCapitalization  = GetValueToUse( "IsSpellCapitalization", true, aProperties );
209cdf0e10cSrcweir 
210cdf0e10cSrcweir         //!! This code needs to be replaced by code calling the actual
211cdf0e10cSrcweir         //!! implementation of your spellchecker
212cdf0e10cSrcweir         if (IsEqual( aLocale, new Locale( "de", "DE", "" ) ))
213cdf0e10cSrcweir         {
214cdf0e10cSrcweir             if (bIsGermanPreReform && aWord.equals( "Schifffahrt" ))
215cdf0e10cSrcweir             {
216cdf0e10cSrcweir                 nType = SpellFailure.SPELLING_ERROR;
217cdf0e10cSrcweir                 aProposals = new String[]{ "Schiffahrt" };
218cdf0e10cSrcweir             }
219cdf0e10cSrcweir             else if (!bIsGermanPreReform && aWord.equals( "Schiffahrt" ))
220cdf0e10cSrcweir             {
221cdf0e10cSrcweir                 nType = SpellFailure.SPELLING_ERROR;
222cdf0e10cSrcweir                 aProposals = new String[]{ "Schifffahrt" };
223cdf0e10cSrcweir             }
224cdf0e10cSrcweir         }
225cdf0e10cSrcweir         else if (IsEqual( aLocale, new Locale( "en", "US", "" ) ))
226cdf0e10cSrcweir         {
227cdf0e10cSrcweir             if (aWord.equals( "arizona" ))
228cdf0e10cSrcweir             {
229cdf0e10cSrcweir                 nType = SpellFailure.CAPTION_ERROR;
230cdf0e10cSrcweir                 aProposals = new String[]{ "Arizona" };
231cdf0e10cSrcweir             }
232cdf0e10cSrcweir             else if (aWord.equals( "house" ))
233cdf0e10cSrcweir             {
234cdf0e10cSrcweir                 nType = SpellFailure.SPELLING_ERROR;
235cdf0e10cSrcweir                 aProposals = new String[]{ "horse", "home" };
236cdf0e10cSrcweir             }
237cdf0e10cSrcweir             else if (aWord.equals( "course" ))
238cdf0e10cSrcweir             {
239cdf0e10cSrcweir                 nType = SpellFailure.IS_NEGATIVE_WORD;
240cdf0e10cSrcweir                 aProposals = new String[]{ "line", "plan", "approach" };
241cdf0e10cSrcweir             }
242cdf0e10cSrcweir         }
243cdf0e10cSrcweir 
244cdf0e10cSrcweir         // always return a result if word is incorrect,
245cdf0e10cSrcweir         // proposals may be empty though.
246cdf0e10cSrcweir         return new XSpellAlternatives_impl( aWord, aLocale,
247cdf0e10cSrcweir                                             nType, aProposals );
248cdf0e10cSrcweir     }
249cdf0e10cSrcweir 
250cdf0e10cSrcweir     // __________ interface methods __________
251cdf0e10cSrcweir 
252cdf0e10cSrcweir 
253cdf0e10cSrcweir     //*****************
254cdf0e10cSrcweir     //XSupportedLocales
255cdf0e10cSrcweir     //*****************
getLocales()256cdf0e10cSrcweir     public Locale[] getLocales()
257cdf0e10cSrcweir         throws com.sun.star.uno.RuntimeException
258cdf0e10cSrcweir     {
259cdf0e10cSrcweir         Locale aLocales[] =
260cdf0e10cSrcweir         {
261cdf0e10cSrcweir             new Locale( "de", "DE", "" ),
262cdf0e10cSrcweir             new Locale( "en", "US", "" )
263cdf0e10cSrcweir         };
264cdf0e10cSrcweir 
265cdf0e10cSrcweir         return aLocales;
266cdf0e10cSrcweir     }
267cdf0e10cSrcweir 
hasLocale( Locale aLocale )268cdf0e10cSrcweir     public boolean hasLocale( Locale aLocale )
269cdf0e10cSrcweir         throws com.sun.star.uno.RuntimeException
270cdf0e10cSrcweir     {
271cdf0e10cSrcweir         boolean bRes = false;
272cdf0e10cSrcweir         if ( IsEqual( aLocale, new Locale( "de", "DE", "" ) )  ||
273cdf0e10cSrcweir              IsEqual( aLocale, new Locale( "en", "US", "" ) ))
274cdf0e10cSrcweir             bRes = true;
275cdf0e10cSrcweir         return bRes;
276cdf0e10cSrcweir     }
277cdf0e10cSrcweir 
278cdf0e10cSrcweir 
279cdf0e10cSrcweir     //*************
280cdf0e10cSrcweir     //XSpellChecker
281cdf0e10cSrcweir     //*************
isValid( String aWord, Locale aLocale, PropertyValue[] aProperties )282cdf0e10cSrcweir     public boolean isValid(
283cdf0e10cSrcweir             String aWord, Locale aLocale,
284cdf0e10cSrcweir             PropertyValue[] aProperties )
285cdf0e10cSrcweir         throws com.sun.star.uno.RuntimeException,
286cdf0e10cSrcweir                IllegalArgumentException
287cdf0e10cSrcweir     {
288cdf0e10cSrcweir         if (IsEqual( aLocale, new Locale() ) || aWord.length() == 0)
289cdf0e10cSrcweir             return true;
290cdf0e10cSrcweir 
291cdf0e10cSrcweir         // linguistic is currently not allowed to throw exceptions
292cdf0e10cSrcweir         // thus we return null which means 'word cannot be spelled'
293cdf0e10cSrcweir         if (!hasLocale( aLocale ))
294cdf0e10cSrcweir             return true;
295cdf0e10cSrcweir 
296cdf0e10cSrcweir         // get values of relevant properties that may be used.
297cdf0e10cSrcweir         //! The values for 'IsIgnoreControlCharacters' and 'IsUseDictionaryList'
298cdf0e10cSrcweir         //! are handled by the dispatcher! Thus there is no need to access
299cdf0e10cSrcweir         //! them here.
300cdf0e10cSrcweir         boolean bIsGermanPreReform      = GetValueToUse( "IsGermanPreReform", false, aProperties );
301cdf0e10cSrcweir         boolean bIsSpellWithDigits      = GetValueToUse( "IsSpellWithDigits", false, aProperties );
302cdf0e10cSrcweir         boolean bIsSpellUpperCase       = GetValueToUse( "IsSpellUpperCase", false, aProperties );
303cdf0e10cSrcweir         boolean bIsSpellCapitalization  = GetValueToUse( "IsSpellCapitalization", true, aProperties );
304cdf0e10cSrcweir 
305cdf0e10cSrcweir         short nFailure = GetSpellFailure( aWord, aLocale, aProperties );
306cdf0e10cSrcweir         if (nFailure != -1)
307cdf0e10cSrcweir         {
308cdf0e10cSrcweir             // postprocess result for errors that should be ignored
309cdf0e10cSrcweir             if (   (!bIsSpellUpperCase  && IsUpper( aWord, aLocale ))
310cdf0e10cSrcweir                 || (!bIsSpellWithDigits && HasDigits( aWord ))
311cdf0e10cSrcweir                 || (!bIsSpellCapitalization
312cdf0e10cSrcweir                     &&  nFailure == SpellFailure.CAPTION_ERROR)
313cdf0e10cSrcweir             )
314cdf0e10cSrcweir                 nFailure = -1;
315cdf0e10cSrcweir         }
316cdf0e10cSrcweir 
317cdf0e10cSrcweir         return nFailure == -1;
318cdf0e10cSrcweir     }
319cdf0e10cSrcweir 
320cdf0e10cSrcweir 
spell( String aWord, Locale aLocale, PropertyValue[] aProperties )321cdf0e10cSrcweir     public XSpellAlternatives spell(
322cdf0e10cSrcweir             String aWord, Locale aLocale,
323cdf0e10cSrcweir             PropertyValue[] aProperties )
324cdf0e10cSrcweir         throws com.sun.star.uno.RuntimeException,
325cdf0e10cSrcweir                IllegalArgumentException
326cdf0e10cSrcweir     {
327cdf0e10cSrcweir         if (IsEqual( aLocale, new Locale() ) || aWord.length() == 0)
328cdf0e10cSrcweir             return null;
329cdf0e10cSrcweir 
330cdf0e10cSrcweir         // linguistic is currently not allowed to throw exceptions
331cdf0e10cSrcweir         // thus we return null fwhich means 'word cannot be spelled'
332cdf0e10cSrcweir         if (!hasLocale( aLocale ))
333cdf0e10cSrcweir             return null;
334cdf0e10cSrcweir 
335cdf0e10cSrcweir         XSpellAlternatives xRes = null;
336cdf0e10cSrcweir         if (!isValid( aWord, aLocale, aProperties ))
337cdf0e10cSrcweir         {
338cdf0e10cSrcweir             xRes = GetProposals( aWord, aLocale, aProperties );
339cdf0e10cSrcweir         }
340cdf0e10cSrcweir         return xRes;
341cdf0e10cSrcweir     }
342cdf0e10cSrcweir 
343cdf0e10cSrcweir 
344cdf0e10cSrcweir     //*****************************
345cdf0e10cSrcweir     //XLinguServiceEventBroadcaster
346cdf0e10cSrcweir     //*****************************
addLinguServiceEventListener( XLinguServiceEventListener xLstnr )347cdf0e10cSrcweir     public boolean addLinguServiceEventListener (
348cdf0e10cSrcweir             XLinguServiceEventListener xLstnr )
349cdf0e10cSrcweir         throws com.sun.star.uno.RuntimeException
350cdf0e10cSrcweir     {
351cdf0e10cSrcweir         boolean bRes = false;
352cdf0e10cSrcweir         if (!bDisposing && xLstnr != null)
353cdf0e10cSrcweir             bRes = aPropChgHelper.addLinguServiceEventListener( xLstnr );
354cdf0e10cSrcweir         return bRes;
355cdf0e10cSrcweir     }
356cdf0e10cSrcweir 
removeLinguServiceEventListener( XLinguServiceEventListener xLstnr )357cdf0e10cSrcweir     public boolean removeLinguServiceEventListener(
358cdf0e10cSrcweir             XLinguServiceEventListener xLstnr )
359cdf0e10cSrcweir         throws com.sun.star.uno.RuntimeException
360cdf0e10cSrcweir     {
361cdf0e10cSrcweir         boolean bRes = false;
362cdf0e10cSrcweir         if (!bDisposing && xLstnr != null)
363cdf0e10cSrcweir             bRes = aPropChgHelper.removeLinguServiceEventListener( xLstnr );
364cdf0e10cSrcweir         return bRes;
365cdf0e10cSrcweir     }
366cdf0e10cSrcweir 
367cdf0e10cSrcweir     //********************
368cdf0e10cSrcweir     // XServiceDisplayName
369cdf0e10cSrcweir     //********************
getServiceDisplayName( Locale aLocale )370cdf0e10cSrcweir     public String getServiceDisplayName( Locale aLocale )
371cdf0e10cSrcweir         throws com.sun.star.uno.RuntimeException
372cdf0e10cSrcweir     {
373cdf0e10cSrcweir         return "Java Samples";
374cdf0e10cSrcweir     }
375cdf0e10cSrcweir 
376cdf0e10cSrcweir     //****************
377cdf0e10cSrcweir     // XInitialization
378cdf0e10cSrcweir     //****************
initialize( Object[] aArguments )379cdf0e10cSrcweir     public void initialize( Object[] aArguments )
380cdf0e10cSrcweir         throws com.sun.star.uno.Exception,
381cdf0e10cSrcweir                com.sun.star.uno.RuntimeException
382cdf0e10cSrcweir     {
383cdf0e10cSrcweir         int nLen = aArguments.length;
384cdf0e10cSrcweir         if (2 == nLen)
385cdf0e10cSrcweir         {
386cdf0e10cSrcweir             XPropertySet xPropSet = (XPropertySet)UnoRuntime.queryInterface(
387cdf0e10cSrcweir                                          XPropertySet.class, aArguments[0]);
388cdf0e10cSrcweir             // start listening to property changes
389cdf0e10cSrcweir             aPropChgHelper.AddAsListenerTo( xPropSet );
390cdf0e10cSrcweir         }
391cdf0e10cSrcweir     }
392cdf0e10cSrcweir 
393cdf0e10cSrcweir     //*************
394cdf0e10cSrcweir     // XServiceInfo
395cdf0e10cSrcweir     //*************
supportsService( String aServiceName )396cdf0e10cSrcweir     public boolean supportsService( String aServiceName )
397cdf0e10cSrcweir         throws com.sun.star.uno.RuntimeException
398cdf0e10cSrcweir     {
399cdf0e10cSrcweir         String[] aServices = getSupportedServiceNames_Static();
400cdf0e10cSrcweir         int i, nLength = aServices.length;
401cdf0e10cSrcweir         boolean bResult = false;
402cdf0e10cSrcweir 
403cdf0e10cSrcweir         for( i = 0; !bResult && i < nLength; ++i )
404cdf0e10cSrcweir             bResult = aServiceName.equals( aServices[ i ] );
405cdf0e10cSrcweir 
406cdf0e10cSrcweir         return bResult;
407cdf0e10cSrcweir     }
408cdf0e10cSrcweir 
getImplementationName()409cdf0e10cSrcweir     public String getImplementationName()
410cdf0e10cSrcweir         throws com.sun.star.uno.RuntimeException
411cdf0e10cSrcweir     {
412cdf0e10cSrcweir         return _aSvcImplName;
413cdf0e10cSrcweir     }
414cdf0e10cSrcweir 
getSupportedServiceNames()415cdf0e10cSrcweir     public String[] getSupportedServiceNames()
416cdf0e10cSrcweir         throws com.sun.star.uno.RuntimeException
417cdf0e10cSrcweir     {
418cdf0e10cSrcweir         return getSupportedServiceNames_Static();
419cdf0e10cSrcweir     }
420cdf0e10cSrcweir 
421cdf0e10cSrcweir     // __________ static things __________
422cdf0e10cSrcweir 
423cdf0e10cSrcweir     public static String _aSvcImplName = SampleSpellChecker.class.getName();
424cdf0e10cSrcweir 
getSupportedServiceNames_Static()425cdf0e10cSrcweir     public static String[] getSupportedServiceNames_Static()
426cdf0e10cSrcweir     {
427cdf0e10cSrcweir         String[] aResult = { "com.sun.star.linguistic2.SpellChecker" };
428cdf0e10cSrcweir         return aResult;
429cdf0e10cSrcweir     }
430cdf0e10cSrcweir 
431cdf0e10cSrcweir 
432cdf0e10cSrcweir     /**
433cdf0e10cSrcweir      * Returns a factory for creating the service.
434cdf0e10cSrcweir      * This method is called by the <code>JavaLoader</code>
435cdf0e10cSrcweir      * <p>
436cdf0e10cSrcweir      * @return  returns a <code>XSingleServiceFactory</code> for creating the component
437cdf0e10cSrcweir      * @param   implName     the name of the implementation for which a service is desired
438cdf0e10cSrcweir      * @param   multiFactory the service manager to be used if needed
439cdf0e10cSrcweir      * @param   regKey       the registryKey
440cdf0e10cSrcweir      * @see                  com.sun.star.comp.loader.JavaLoader
441cdf0e10cSrcweir      */
__getServiceFactory( String aImplName, XMultiServiceFactory xMultiFactory, com.sun.star.registry.XRegistryKey xRegKey )442cdf0e10cSrcweir     public static XSingleServiceFactory __getServiceFactory(
443cdf0e10cSrcweir         String aImplName,
444cdf0e10cSrcweir         XMultiServiceFactory xMultiFactory,
445cdf0e10cSrcweir         com.sun.star.registry.XRegistryKey xRegKey )
446cdf0e10cSrcweir     {
447cdf0e10cSrcweir         XSingleServiceFactory xSingleServiceFactory = null;
448cdf0e10cSrcweir         if( aImplName.equals( _aSvcImplName ) )
449cdf0e10cSrcweir         {
450cdf0e10cSrcweir             xSingleServiceFactory = new OneInstanceFactory(
451cdf0e10cSrcweir                     SampleSpellChecker.class, _aSvcImplName,
452cdf0e10cSrcweir                     getSupportedServiceNames_Static(),
453cdf0e10cSrcweir                     xMultiFactory );
454cdf0e10cSrcweir         }
455cdf0e10cSrcweir         return xSingleServiceFactory;
456cdf0e10cSrcweir     }
457cdf0e10cSrcweir 
458cdf0e10cSrcweir     /**
459cdf0e10cSrcweir      * Writes the service information into the given registry key.
460cdf0e10cSrcweir      * This method is called by the <code>JavaLoader</code>
461cdf0e10cSrcweir      * <p>
462cdf0e10cSrcweir      * @return  returns true if the operation succeeded
463cdf0e10cSrcweir      * @param   xRegKey       the registryKey
464cdf0e10cSrcweir      * @see                  com.sun.star.comp.loader.JavaLoader
465cdf0e10cSrcweir      */
466cdf0e10cSrcweir     // This method not longer necessary since OOo 3.4 where the component registration
467cdf0e10cSrcweir     // was changed to passive component registration. For more details see
468cdf0e10cSrcweir     // http://wiki.services.openoffice.org/wiki/Passive_Component_Registration
469cdf0e10cSrcweir 
470cdf0e10cSrcweir //     public static boolean __writeRegistryServiceInfo(
471cdf0e10cSrcweir //             com.sun.star.registry.XRegistryKey xRegKey )
472cdf0e10cSrcweir //     {
473cdf0e10cSrcweir //         boolean bResult = true;
474cdf0e10cSrcweir //         String[] aServices = getSupportedServiceNames_Static();
475cdf0e10cSrcweir //         int i, nLength = aServices.length;
476cdf0e10cSrcweir //         for( i = 0; i < nLength; ++i )
477cdf0e10cSrcweir //         {
478cdf0e10cSrcweir //             bResult = bResult && com.sun.star.comp.loader.FactoryHelper.writeRegistryServiceInfo(
479cdf0e10cSrcweir //                 _aSvcImplName, aServices[i], xRegKey );
480cdf0e10cSrcweir //         }
481cdf0e10cSrcweir //         return bResult;
482cdf0e10cSrcweir //     }
483cdf0e10cSrcweir }
484cdf0e10cSrcweir 
485