1 /*************************************************************************
2  *
3  *  The Contents of this file are made available subject to the terms of
4  *  the BSD license.
5  *
6  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7  *  All rights reserved.
8  *
9  *  Redistribution and use in source and binary forms, with or without
10  *  modification, are permitted provided that the following conditions
11  *  are met:
12  *  1. Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *  2. Redistributions in binary form must reproduce the above copyright
15  *     notice, this list of conditions and the following disclaimer in the
16  *     documentation and/or other materials provided with the distribution.
17  *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
18  *     contributors may be used to endorse or promote products derived
19  *     from this software without specific prior written permission.
20  *
21  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  *************************************************************************/
34 
35 // uno
36 import com.sun.star.lib.uno.helper.ComponentBase;
37 import com.sun.star.uno.UnoRuntime;
38 
39 // factories
40 import com.sun.star.lang.XMultiServiceFactory;
41 import com.sun.star.lang.XSingleServiceFactory;
42 
43 // supported Interfaces
44 import com.sun.star.linguistic2.XThesaurus;
45 import com.sun.star.lang.XInitialization;
46 import com.sun.star.lang.XComponent;
47 import com.sun.star.lang.XServiceInfo;
48 import com.sun.star.lang.XServiceDisplayName;
49 
50 // Exceptions
51 import com.sun.star.uno.Exception;
52 import com.sun.star.uno.RuntimeException;
53 import com.sun.star.lang.IllegalArgumentException;
54 
55 //used Interfaces
56 import com.sun.star.linguistic2.XMeaning;
57 import com.sun.star.lang.Locale;
58 import com.sun.star.lang.XEventListener;
59 import com.sun.star.lang.EventObject;
60 import com.sun.star.beans.XPropertySet;
61 import com.sun.star.beans.PropertyValue;
62 import com.sun.star.uno.AnyConverter;
63 import com.sun.star.lang.XTypeProvider;
64 import com.sun.star.uno.Type;
65 
66 import java.util.ArrayList;
67 
68 public class SampleThesaurus extends ComponentBase implements
69         XThesaurus,
70         XInitialization,
71         XServiceDisplayName,
72         XServiceInfo
73 {
74     PropChgHelper               aPropChgHelper;
75     ArrayList                   aEvtListeners;
76     boolean                     bDisposing;
77 
78     public SampleThesaurus()
79     {
80         // names of relevant properties to be used
81         String[] aProps = new String[]
82             {
83                 "IsIgnoreControlCharacters",
84                 "IsUseDictionaryList",
85                 "IsGermanPreReform",
86             };
87 
88         // this service has no listeners thus we may use the base class,
89         // which is here basically used only to keep track of the
90         // property set (and it's lifetime) since it gets used in the
91         // 'GetValueToUse' function
92         aPropChgHelper  = new PropChgHelper( (XThesaurus) this, aProps );
93 
94         aEvtListeners   = new ArrayList();
95         bDisposing      = false;
96     }
97 
98     private boolean IsEqual( Locale aLoc1, Locale aLoc2 )
99     {
100         return aLoc1.Language.equals( aLoc2.Language ) &&
101                aLoc1.Country .equals( aLoc2.Country )  &&
102                aLoc1.Variant .equals( aLoc2.Variant );
103     }
104 
105     private boolean GetValueToUse(
106             String          aPropName,
107             boolean         bDefaultVal,
108             PropertyValue[] aProps )
109     {
110         boolean bRes = bDefaultVal;
111 
112         try
113         {
114             // use temporary value if supplied
115             for (int i = 0;  i < aProps.length;  ++i)
116             {
117                 if (aPropName.equals( aProps[i].Name ))
118                 {
119                     Object aObj = aProps[i].Value;
120                     if (AnyConverter.isBoolean( aObj ))
121                     {
122                         bRes = AnyConverter.toBoolean( aObj );
123                         return bRes;
124                     }
125                 }
126             }
127 
128             // otherwise use value from property set (if available)
129             XPropertySet xPropSet = aPropChgHelper.GetPropSet();
130             if (xPropSet != null)   // should always be the case
131             {
132                 Object aObj = xPropSet.getPropertyValue( aPropName );
133                 if (AnyConverter.isBoolean( aObj ))
134                     bRes = AnyConverter.toBoolean( aObj );
135             }
136         }
137         catch (Exception e) {
138             bRes = bDefaultVal;
139         }
140 
141         return bRes;
142     }
143 
144     // __________ interface methods __________
145 
146 
147     //*****************
148     //XSupportedLocales
149     //*****************
150     public Locale[] getLocales()
151         throws com.sun.star.uno.RuntimeException
152     {
153         Locale aLocales[] =
154         {
155             new Locale( "en", "US", "" )
156         };
157 
158         return aLocales;
159     }
160 
161     public boolean hasLocale( Locale aLocale )
162         throws com.sun.star.uno.RuntimeException
163     {
164         boolean bRes = false;
165         if (IsEqual( aLocale, new Locale( "en", "US", "" ) ))
166             bRes = true;
167         return bRes;
168     }
169 
170     //**********
171     //XThesaurus
172     //**********
173     public XMeaning[] queryMeanings(
174             String aTerm, Locale aLocale,
175             PropertyValue[] aProperties )
176         throws com.sun.star.lang.IllegalArgumentException,
177                com.sun.star.uno.RuntimeException
178     {
179         if (IsEqual( aLocale, new Locale() ) || aTerm.length() == 0)
180             return null;
181 
182         // linguistic is currently not allowed to throw exceptions
183         // thus we return null fwhich means 'word cannot be looked up'
184         if (!hasLocale( aLocale ))
185             return null;
186 
187         // get values of relevant properties that may be used.
188         //! The values for 'IsIgnoreControlCharacters' and 'IsUseDictionaryList'
189         //! are handled by the dispatcher! Thus there is no need to access
190         //! them here.
191         boolean bIsGermanPreReform      = GetValueToUse( "IsGermanPreReform", false, aProperties );
192 
193         XMeaning[] aRes = null;
194 
195         //!! This code needs to be replaced by code calling the actual
196         //!! implementation of your thesaurus
197         if (aTerm.equals( "house" ) &&
198             IsEqual( aLocale, new Locale( "en", "US", "" ) ) )
199         {
200             aRes = new XMeaning[]
201                 {
202                     new XMeaning_impl( "a building where one lives",
203                             new String[]{ "home", "place", "dwelling" } ),
204                     new XMeaning_impl( "a group of people sharing common ancestry",
205                             new String[]{ "family", "clan", "kindred" } ),
206                     new XMeaning_impl( "to provide with lodging",
207                             new String[]{ "room", "board", "put up" } )
208                 };
209         }
210 
211         return aRes;
212     }
213 
214 
215     //********************
216     // XServiceDisplayName
217     //********************
218     public String getServiceDisplayName( Locale aLocale )
219         throws com.sun.star.uno.RuntimeException
220     {
221         return "Java Samples";
222     }
223 
224     //****************
225     // XInitialization
226     //****************
227     public void initialize( Object[] aArguments )
228         throws com.sun.star.uno.Exception,
229                com.sun.star.uno.RuntimeException
230     {
231         int nLen = aArguments.length;
232         if (2 == nLen)
233         {
234             XPropertySet xPropSet = (XPropertySet)UnoRuntime.queryInterface(
235                                          XPropertySet.class, aArguments[0]);
236             // start listening to property changes
237             aPropChgHelper.AddAsListenerTo( xPropSet );
238         }
239     }
240 
241     //*************
242     // XServiceInfo
243     //*************
244     public boolean supportsService( String aServiceName )
245         throws com.sun.star.uno.RuntimeException
246     {
247         String[] aServices = getSupportedServiceNames_Static();
248         int i, nLength = aServices.length;
249         boolean bResult = false;
250 
251         for( i = 0; !bResult && i < nLength; ++i )
252             bResult = aServiceName.equals( aServices[ i ] );
253 
254         return bResult;
255     }
256 
257     public String getImplementationName()
258         throws com.sun.star.uno.RuntimeException
259     {
260         return _aSvcImplName;
261     }
262 
263     public String[] getSupportedServiceNames()
264         throws com.sun.star.uno.RuntimeException
265     {
266         return getSupportedServiceNames_Static();
267     }
268 
269     // __________ static things __________
270 
271     public static String _aSvcImplName = SampleThesaurus.class.getName();
272 
273     public static String[] getSupportedServiceNames_Static()
274     {
275         String[] aResult = { "com.sun.star.linguistic2.Thesaurus" };
276         return aResult;
277     }
278 
279 
280     /**
281      * Returns a factory for creating the service.
282      * This method is called by the <code>JavaLoader</code>
283      * <p>
284      * @return  returns a <code>XSingleServiceFactory</code> for creating the component
285      * @param   implName     the name of the implementation for which a service is desired
286      * @param   multiFactory the service manager to be used if needed
287      * @param   regKey       the registryKey
288      * @see                  com.sun.star.comp.loader.JavaLoader
289      */
290     public static XSingleServiceFactory __getServiceFactory(
291         String aImplName,
292         XMultiServiceFactory xMultiFactory,
293         com.sun.star.registry.XRegistryKey xRegKey )
294     {
295         XSingleServiceFactory xSingleServiceFactory = null;
296         if( aImplName.equals( _aSvcImplName ) )
297         {
298             xSingleServiceFactory = new OneInstanceFactory(
299                     SampleThesaurus.class, _aSvcImplName,
300                     getSupportedServiceNames_Static(),
301                     xMultiFactory );
302         }
303         return xSingleServiceFactory;
304     }
305 
306     /**
307      * Writes the service information into the given registry key.
308      * This method is called by the <code>JavaLoader</code>
309      * <p>
310      * @return  returns true if the operation succeeded
311      * @param   xRegKey       the registryKey
312      * @see                  com.sun.star.comp.loader.JavaLoader
313      */
314     // This method not longer necessary since OOo 3.4 where the component registration
315     // was changed to passive component registration. For more details see
316     // http://wiki.services.openoffice.org/wiki/Passive_Component_Registration
317 
318 //     public static boolean __writeRegistryServiceInfo(
319 //             com.sun.star.registry.XRegistryKey xRegKey )
320 //     {
321 //         boolean bResult = true;
322 //         String[] aServices = getSupportedServiceNames_Static();
323 //         int i, nLength = aServices.length;
324 //         for( i = 0; i < nLength; ++i )
325 //         {
326 //             bResult = bResult && com.sun.star.comp.loader.FactoryHelper.writeRegistryServiceInfo(
327 //                 _aSvcImplName, aServices[i], xRegKey );
328 //         }
329 //         return bResult;
330 //     }
331 }
332 
333