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