134dd1e25SAndrew Rist /**************************************************************
2*be8bac39Smseidel  *
334dd1e25SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
434dd1e25SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
534dd1e25SAndrew Rist  * distributed with this work for additional information
634dd1e25SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
734dd1e25SAndrew Rist  * to you under the Apache License, Version 2.0 (the
834dd1e25SAndrew Rist  * "License"); you may not use this file except in compliance
934dd1e25SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*be8bac39Smseidel  *
1134dd1e25SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*be8bac39Smseidel  *
1334dd1e25SAndrew Rist  * Unless required by applicable law or agreed to in writing,
1434dd1e25SAndrew Rist  * software distributed under the License is distributed on an
1534dd1e25SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1634dd1e25SAndrew Rist  * KIND, either express or implied.  See the License for the
1734dd1e25SAndrew Rist  * specific language governing permissions and limitations
1834dd1e25SAndrew Rist  * under the License.
19*be8bac39Smseidel  *
2034dd1e25SAndrew Rist  *************************************************************/
2134dd1e25SAndrew Rist 
2234dd1e25SAndrew 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.XThesaurus;
34*be8bac39Smseidel import com.sun.star.lang.XInitialization;
35cdf0e10cSrcweir import com.sun.star.lang.XComponent;
36*be8bac39Smseidel import com.sun.star.lang.XServiceInfo;
37*be8bac39Smseidel import com.sun.star.lang.XServiceDisplayName;
38cdf0e10cSrcweir 
39cdf0e10cSrcweir // Exceptions
40cdf0e10cSrcweir import com.sun.star.uno.Exception;
41cdf0e10cSrcweir import com.sun.star.uno.RuntimeException;
42cdf0e10cSrcweir import com.sun.star.lang.IllegalArgumentException;
43cdf0e10cSrcweir 
44cdf0e10cSrcweir //used Interfaces
45cdf0e10cSrcweir import com.sun.star.linguistic2.XMeaning;
46cdf0e10cSrcweir import com.sun.star.lang.Locale;
47cdf0e10cSrcweir import com.sun.star.lang.XEventListener;
48cdf0e10cSrcweir import com.sun.star.lang.EventObject;
49cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
50cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
51cdf0e10cSrcweir import com.sun.star.uno.AnyConverter;
52cdf0e10cSrcweir import com.sun.star.lang.XTypeProvider;
53cdf0e10cSrcweir import com.sun.star.uno.Type;
54cdf0e10cSrcweir 
55cdf0e10cSrcweir import java.util.ArrayList;
56cdf0e10cSrcweir 
57cdf0e10cSrcweir public class SampleThesaurus extends ComponentBase implements
58cdf0e10cSrcweir         XThesaurus,
59cdf0e10cSrcweir         XInitialization,
60cdf0e10cSrcweir         XServiceDisplayName,
61cdf0e10cSrcweir         XServiceInfo
62cdf0e10cSrcweir {
63cdf0e10cSrcweir     PropChgHelper               aPropChgHelper;
64cdf0e10cSrcweir     ArrayList                   aEvtListeners;
65cdf0e10cSrcweir     boolean                     bDisposing;
66*be8bac39Smseidel 
SampleThesaurus()67cdf0e10cSrcweir     public SampleThesaurus()
68cdf0e10cSrcweir     {
69cdf0e10cSrcweir         // names of relevant properties to be used
70cdf0e10cSrcweir         String[] aProps = new String[]
71cdf0e10cSrcweir             {
72cdf0e10cSrcweir                 "IsIgnoreControlCharacters",
73cdf0e10cSrcweir                 "IsUseDictionaryList",
74cdf0e10cSrcweir                 "IsGermanPreReform",
75cdf0e10cSrcweir             };
76*be8bac39Smseidel 
77cdf0e10cSrcweir         // this service has no listeners thus we may use the base class,
78*be8bac39Smseidel         // which is here basically used only to keep track of the
79cdf0e10cSrcweir         // property set (and it's lifetime) since it gets used in the
80cdf0e10cSrcweir         // 'GetValueToUse' function
81*be8bac39Smseidel         aPropChgHelper = new PropChgHelper( (XThesaurus) this, aProps );
82*be8bac39Smseidel 
83*be8bac39Smseidel         aEvtListeners  = new ArrayList();
84*be8bac39Smseidel         bDisposing     = false;
85cdf0e10cSrcweir     }
86cdf0e10cSrcweir 
IsEqual( Locale aLoc1, Locale aLoc2 )87cdf0e10cSrcweir     private boolean IsEqual( Locale aLoc1, Locale aLoc2 )
88cdf0e10cSrcweir     {
89cdf0e10cSrcweir         return aLoc1.Language.equals( aLoc2.Language ) &&
90*be8bac39Smseidel                aLoc1.Country .equals( aLoc2.Country ) &&
91*be8bac39Smseidel                aLoc1.Variant .equals( aLoc2.Variant );
92cdf0e10cSrcweir     }
93cdf0e10cSrcweir 
GetValueToUse( String aPropName, boolean bDefaultVal, PropertyValue[] aProps )94*be8bac39Smseidel     private boolean GetValueToUse(
95cdf0e10cSrcweir             String          aPropName,
96cdf0e10cSrcweir             boolean         bDefaultVal,
97cdf0e10cSrcweir             PropertyValue[] aProps )
98cdf0e10cSrcweir     {
99cdf0e10cSrcweir         boolean bRes = bDefaultVal;
100cdf0e10cSrcweir 
101cdf0e10cSrcweir         try
102cdf0e10cSrcweir         {
103cdf0e10cSrcweir             // use temporary value if supplied
104*be8bac39Smseidel             for (int i = 0; i < aProps.length; ++i)
105cdf0e10cSrcweir             {
106cdf0e10cSrcweir                 if (aPropName.equals( aProps[i].Name ))
107cdf0e10cSrcweir                 {
108cdf0e10cSrcweir                     Object aObj = aProps[i].Value;
109cdf0e10cSrcweir                     if (AnyConverter.isBoolean( aObj ))
110cdf0e10cSrcweir                     {
111cdf0e10cSrcweir                         bRes = AnyConverter.toBoolean( aObj );
112cdf0e10cSrcweir                         return bRes;
113cdf0e10cSrcweir                     }
114cdf0e10cSrcweir                 }
115cdf0e10cSrcweir             }
116cdf0e10cSrcweir 
117cdf0e10cSrcweir             // otherwise use value from property set (if available)
118cdf0e10cSrcweir             XPropertySet xPropSet = aPropChgHelper.GetPropSet();
119*be8bac39Smseidel             if (xPropSet != null) // should always be the case
120cdf0e10cSrcweir             {
121cdf0e10cSrcweir                 Object aObj = xPropSet.getPropertyValue( aPropName );
122cdf0e10cSrcweir                 if (AnyConverter.isBoolean( aObj ))
123cdf0e10cSrcweir                     bRes = AnyConverter.toBoolean( aObj );
124cdf0e10cSrcweir             }
125cdf0e10cSrcweir         }
126cdf0e10cSrcweir         catch (Exception e) {
127cdf0e10cSrcweir             bRes = bDefaultVal;
128cdf0e10cSrcweir         }
129*be8bac39Smseidel 
130cdf0e10cSrcweir         return bRes;
131cdf0e10cSrcweir     }
132*be8bac39Smseidel 
133cdf0e10cSrcweir     // __________ interface methods __________
134*be8bac39Smseidel 
135*be8bac39Smseidel 
136cdf0e10cSrcweir     //*****************
137cdf0e10cSrcweir     //XSupportedLocales
138cdf0e10cSrcweir     //*****************
getLocales()139cdf0e10cSrcweir     public Locale[] getLocales()
140cdf0e10cSrcweir         throws com.sun.star.uno.RuntimeException
141cdf0e10cSrcweir     {
142*be8bac39Smseidel         Locale aLocales[] =
143cdf0e10cSrcweir         {
144cdf0e10cSrcweir             new Locale( "en", "US", "" )
145cdf0e10cSrcweir         };
146cdf0e10cSrcweir 
147cdf0e10cSrcweir         return aLocales;
148cdf0e10cSrcweir     }
149*be8bac39Smseidel 
hasLocale( Locale aLocale )150*be8bac39Smseidel     public boolean hasLocale( Locale aLocale )
151cdf0e10cSrcweir         throws com.sun.star.uno.RuntimeException
152cdf0e10cSrcweir     {
153cdf0e10cSrcweir         boolean bRes = false;
154cdf0e10cSrcweir         if (IsEqual( aLocale, new Locale( "en", "US", "" ) ))
155cdf0e10cSrcweir             bRes = true;
156*be8bac39Smseidel         return bRes;
157cdf0e10cSrcweir     }
158*be8bac39Smseidel 
159cdf0e10cSrcweir     //**********
160cdf0e10cSrcweir     //XThesaurus
161cdf0e10cSrcweir     //**********
queryMeanings( String aTerm, Locale aLocale, PropertyValue[] aProperties )162*be8bac39Smseidel     public XMeaning[] queryMeanings(
163cdf0e10cSrcweir             String aTerm, Locale aLocale,
164cdf0e10cSrcweir             PropertyValue[] aProperties )
165cdf0e10cSrcweir         throws com.sun.star.lang.IllegalArgumentException,
166cdf0e10cSrcweir                com.sun.star.uno.RuntimeException
167cdf0e10cSrcweir     {
168cdf0e10cSrcweir         if (IsEqual( aLocale, new Locale() ) || aTerm.length() == 0)
169cdf0e10cSrcweir             return null;
170*be8bac39Smseidel 
171cdf0e10cSrcweir         // linguistic is currently not allowed to throw exceptions
172*be8bac39Smseidel         // thus we return null which means 'word cannot be looked up'
173cdf0e10cSrcweir         if (!hasLocale( aLocale ))
174cdf0e10cSrcweir             return null;
175cdf0e10cSrcweir 
176cdf0e10cSrcweir         // get values of relevant properties that may be used.
177cdf0e10cSrcweir         //! The values for 'IsIgnoreControlCharacters' and 'IsUseDictionaryList'
178cdf0e10cSrcweir         //! are handled by the dispatcher! Thus there is no need to access
179cdf0e10cSrcweir         //! them here.
180*be8bac39Smseidel         boolean bIsGermanPreReform = GetValueToUse( "IsGermanPreReform", false, aProperties );
181cdf0e10cSrcweir 
182cdf0e10cSrcweir         XMeaning[] aRes = null;
183cdf0e10cSrcweir 
184*be8bac39Smseidel         //!! This code needs to be replaced by code calling the actual
185cdf0e10cSrcweir         //!! implementation of your thesaurus
186cdf0e10cSrcweir         if (aTerm.equals( "house" ) &&
187cdf0e10cSrcweir             IsEqual( aLocale, new Locale( "en", "US", "" ) ) )
188cdf0e10cSrcweir         {
189cdf0e10cSrcweir             aRes = new XMeaning[]
190cdf0e10cSrcweir                 {
191*be8bac39Smseidel                     new XMeaning_impl( "a building where one lives",
192cdf0e10cSrcweir                             new String[]{ "home", "place", "dwelling" } ),
193*be8bac39Smseidel                     new XMeaning_impl( "a group of people sharing common ancestry",
194cdf0e10cSrcweir                             new String[]{ "family", "clan", "kindred" } ),
195*be8bac39Smseidel                     new XMeaning_impl( "to provide with lodging",
196cdf0e10cSrcweir                             new String[]{ "room", "board", "put up" } )
197cdf0e10cSrcweir                 };
198cdf0e10cSrcweir         }
199cdf0e10cSrcweir 
200cdf0e10cSrcweir         return aRes;
201cdf0e10cSrcweir     }
202*be8bac39Smseidel 
203*be8bac39Smseidel 
204cdf0e10cSrcweir     //********************
205cdf0e10cSrcweir     // XServiceDisplayName
206cdf0e10cSrcweir     //********************
getServiceDisplayName( Locale aLocale )207*be8bac39Smseidel     public String getServiceDisplayName( Locale aLocale )
208cdf0e10cSrcweir         throws com.sun.star.uno.RuntimeException
209cdf0e10cSrcweir     {
210*be8bac39Smseidel         return "Java Samples";
211cdf0e10cSrcweir     }
212cdf0e10cSrcweir 
213cdf0e10cSrcweir     //****************
214cdf0e10cSrcweir     // XInitialization
215cdf0e10cSrcweir     //****************
initialize( Object[] aArguments )216*be8bac39Smseidel     public void initialize( Object[] aArguments )
217cdf0e10cSrcweir         throws com.sun.star.uno.Exception,
218cdf0e10cSrcweir                com.sun.star.uno.RuntimeException
219cdf0e10cSrcweir     {
220cdf0e10cSrcweir         int nLen = aArguments.length;
221cdf0e10cSrcweir         if (2 == nLen)
222cdf0e10cSrcweir         {
223cdf0e10cSrcweir             XPropertySet xPropSet = (XPropertySet)UnoRuntime.queryInterface(
224cdf0e10cSrcweir                                          XPropertySet.class, aArguments[0]);
225cdf0e10cSrcweir             // start listening to property changes
226cdf0e10cSrcweir             aPropChgHelper.AddAsListenerTo( xPropSet );
227cdf0e10cSrcweir         }
228cdf0e10cSrcweir     }
229cdf0e10cSrcweir 
230cdf0e10cSrcweir     //*************
231cdf0e10cSrcweir     // XServiceInfo
232cdf0e10cSrcweir     //*************
supportsService( String aServiceName )233cdf0e10cSrcweir     public boolean supportsService( String aServiceName )
234cdf0e10cSrcweir         throws com.sun.star.uno.RuntimeException
235cdf0e10cSrcweir     {
236cdf0e10cSrcweir         String[] aServices = getSupportedServiceNames_Static();
237cdf0e10cSrcweir         int i, nLength = aServices.length;
238cdf0e10cSrcweir         boolean bResult = false;
239cdf0e10cSrcweir 
240cdf0e10cSrcweir         for( i = 0; !bResult && i < nLength; ++i )
241cdf0e10cSrcweir             bResult = aServiceName.equals( aServices[ i ] );
242cdf0e10cSrcweir 
243cdf0e10cSrcweir         return bResult;
244cdf0e10cSrcweir     }
245cdf0e10cSrcweir 
getImplementationName()246cdf0e10cSrcweir     public String getImplementationName()
247cdf0e10cSrcweir         throws com.sun.star.uno.RuntimeException
248cdf0e10cSrcweir     {
249cdf0e10cSrcweir         return _aSvcImplName;
250cdf0e10cSrcweir     }
251*be8bac39Smseidel 
getSupportedServiceNames()252cdf0e10cSrcweir     public String[] getSupportedServiceNames()
253cdf0e10cSrcweir         throws com.sun.star.uno.RuntimeException
254cdf0e10cSrcweir     {
255cdf0e10cSrcweir         return getSupportedServiceNames_Static();
256cdf0e10cSrcweir     }
257*be8bac39Smseidel 
258cdf0e10cSrcweir     // __________ static things __________
259cdf0e10cSrcweir 
260cdf0e10cSrcweir     public static String _aSvcImplName = SampleThesaurus.class.getName();
261*be8bac39Smseidel 
getSupportedServiceNames_Static()262cdf0e10cSrcweir     public static String[] getSupportedServiceNames_Static()
263cdf0e10cSrcweir     {
264cdf0e10cSrcweir         String[] aResult = { "com.sun.star.linguistic2.Thesaurus" };
265cdf0e10cSrcweir         return aResult;
266cdf0e10cSrcweir     }
267cdf0e10cSrcweir 
268cdf0e10cSrcweir 
269cdf0e10cSrcweir     /**
270cdf0e10cSrcweir      * Returns a factory for creating the service.
271cdf0e10cSrcweir      * This method is called by the <code>JavaLoader</code>
272cdf0e10cSrcweir      * <p>
273cdf0e10cSrcweir      * @return  returns a <code>XSingleServiceFactory</code> for creating the component
274cdf0e10cSrcweir      * @param   implName     the name of the implementation for which a service is desired
275cdf0e10cSrcweir      * @param   multiFactory the service manager to be used if needed
276cdf0e10cSrcweir      * @param   regKey       the registryKey
277cdf0e10cSrcweir      * @see                  com.sun.star.comp.loader.JavaLoader
278cdf0e10cSrcweir      */
__getServiceFactory( String aImplName, XMultiServiceFactory xMultiFactory, com.sun.star.registry.XRegistryKey xRegKey )279cdf0e10cSrcweir     public static XSingleServiceFactory __getServiceFactory(
280cdf0e10cSrcweir         String aImplName,
281cdf0e10cSrcweir         XMultiServiceFactory xMultiFactory,
282cdf0e10cSrcweir         com.sun.star.registry.XRegistryKey xRegKey )
283cdf0e10cSrcweir     {
284cdf0e10cSrcweir         XSingleServiceFactory xSingleServiceFactory = null;
285cdf0e10cSrcweir         if( aImplName.equals( _aSvcImplName ) )
286cdf0e10cSrcweir         {
287cdf0e10cSrcweir             xSingleServiceFactory = new OneInstanceFactory(
288cdf0e10cSrcweir                     SampleThesaurus.class, _aSvcImplName,
289cdf0e10cSrcweir                     getSupportedServiceNames_Static(),
290cdf0e10cSrcweir                     xMultiFactory );
291cdf0e10cSrcweir         }
292cdf0e10cSrcweir         return xSingleServiceFactory;
293cdf0e10cSrcweir     }
294cdf0e10cSrcweir 
295cdf0e10cSrcweir     /**
296cdf0e10cSrcweir      * Writes the service information into the given registry key.
297cdf0e10cSrcweir      * This method is called by the <code>JavaLoader</code>
298cdf0e10cSrcweir      * <p>
299cdf0e10cSrcweir      * @return  returns true if the operation succeeded
300cdf0e10cSrcweir      * @param   xRegKey       the registryKey
301cdf0e10cSrcweir      * @see                  com.sun.star.comp.loader.JavaLoader
302cdf0e10cSrcweir      */
303cdf0e10cSrcweir     // This method not longer necessary since OOo 3.4 where the component registration
304cdf0e10cSrcweir     // was changed to passive component registration. For more details see
305*be8bac39Smseidel     // https://wiki.openoffice.org/wiki/Passive_Component_Registration
306cdf0e10cSrcweir 
307*be8bac39Smseidel //     public static boolean __writeRegistryServiceInfo(
308cdf0e10cSrcweir //             com.sun.star.registry.XRegistryKey xRegKey )
309cdf0e10cSrcweir //     {
310cdf0e10cSrcweir //         boolean bResult = true;
311cdf0e10cSrcweir //         String[] aServices = getSupportedServiceNames_Static();
312cdf0e10cSrcweir //         int i, nLength = aServices.length;
313cdf0e10cSrcweir //         for( i = 0; i < nLength; ++i )
314cdf0e10cSrcweir //         {
315cdf0e10cSrcweir //             bResult = bResult && com.sun.star.comp.loader.FactoryHelper.writeRegistryServiceInfo(
316cdf0e10cSrcweir //                 _aSvcImplName, aServices[i], xRegKey );
317cdf0e10cSrcweir //         }
318cdf0e10cSrcweir //         return bResult;
319cdf0e10cSrcweir //     }
320cdf0e10cSrcweir }
321cdf0e10cSrcweir 
322