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