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