1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * The Contents of this file are made available subject to the terms of
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
34 *************************************************************************/
37 import com
.sun
.star
.lib
.uno
.helper
.ComponentBase
;
38 import com
.sun
.star
.uno
.UnoRuntime
;
41 import com
.sun
.star
.lang
.XSingleComponentFactory
;
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
;
50 import com
.sun
.star
.uno
.Exception
;
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
;
59 public class SampleThesaurus
extends ComponentBase
implements
65 PropChgHelper aPropChgHelper
;
66 ArrayList
<?
> aEvtListeners
;
69 public SampleThesaurus()
71 // names of relevant properties to be used
72 String
[] aProps
= new String
[]
74 "IsIgnoreControlCharacters",
75 "IsUseDictionaryList",
78 // this service has no listeners thus we may use the base class,
79 // which is here basically used only to keep track of the
80 // property set (and its lifetime) since it gets used in the
81 // 'GetValueToUse' function
82 aPropChgHelper
= new PropChgHelper( this, aProps
);
84 aEvtListeners
= new ArrayList
<Object
>();
88 private boolean IsEqual( Locale aLoc1
, Locale aLoc2
)
90 return aLoc1
.Language
.equals( aLoc2
.Language
) &&
91 aLoc1
.Country
.equals( aLoc2
.Country
) &&
92 aLoc1
.Variant
.equals( aLoc2
.Variant
);
95 private boolean GetValueToUse(
98 PropertyValue
[] aProps
)
100 boolean bRes
= bDefaultVal
;
104 // use temporary value if supplied
105 for (int i
= 0; i
< aProps
.length
; ++i
)
107 if (aPropName
.equals( aProps
[i
].Name
))
109 Object aObj
= aProps
[i
].Value
;
110 if (AnyConverter
.isBoolean( aObj
))
112 bRes
= AnyConverter
.toBoolean( aObj
);
118 // otherwise use value from property set (if available)
119 XPropertySet xPropSet
= aPropChgHelper
.GetPropSet();
120 if (xPropSet
!= null) // should always be the case
122 Object aObj
= xPropSet
.getPropertyValue( aPropName
);
123 if (AnyConverter
.isBoolean( aObj
))
124 bRes
= AnyConverter
.toBoolean( aObj
);
127 catch (Exception e
) {
134 // __________ interface methods __________
140 public Locale
[] getLocales()
141 throws com
.sun
.star
.uno
.RuntimeException
145 new Locale( "en", "US", "" )
151 public boolean hasLocale( Locale aLocale
)
152 throws com
.sun
.star
.uno
.RuntimeException
154 boolean bRes
= false;
155 if (IsEqual( aLocale
, new Locale( "en", "US", "" ) ))
163 public XMeaning
[] queryMeanings(
164 String aTerm
, Locale aLocale
,
165 PropertyValue
[] aProperties
)
166 throws com
.sun
.star
.lang
.IllegalArgumentException
,
167 com
.sun
.star
.uno
.RuntimeException
169 if (IsEqual( aLocale
, new Locale() ) || aTerm
.length() == 0)
172 // linguistic is currently not allowed to throw exceptions
173 // thus we return null fwhich means 'word cannot be looked up'
174 if (!hasLocale( aLocale
))
177 // get values of relevant properties that may be used.
178 //! The values for 'IsIgnoreControlCharacters' and 'IsUseDictionaryList'
179 //! are handled by the dispatcher! Thus there is no need to access
182 XMeaning
[] aRes
= null;
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", "" ) ) )
189 aRes
= new XMeaning
[]
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" } )
205 // XServiceDisplayName
207 public String
getServiceDisplayName( Locale aLocale
)
208 throws com
.sun
.star
.uno
.RuntimeException
210 return "Java Samples";
216 public void initialize( Object
[] aArguments
)
217 throws com
.sun
.star
.uno
.Exception
,
218 com
.sun
.star
.uno
.RuntimeException
220 int nLen
= aArguments
.length
;
223 XPropertySet xPropSet
= UnoRuntime
.queryInterface(
224 XPropertySet
.class, aArguments
[0]);
225 // start listening to property changes
226 aPropChgHelper
.AddAsListenerTo( xPropSet
);
233 public boolean supportsService( String aServiceName
)
234 throws com
.sun
.star
.uno
.RuntimeException
236 String
[] aServices
= getSupportedServiceNames_Static();
237 int i
, nLength
= aServices
.length
;
238 boolean bResult
= false;
240 for( i
= 0; !bResult
&& i
< nLength
; ++i
)
241 bResult
= aServiceName
.equals( aServices
[ i
] );
246 public String
getImplementationName()
247 throws com
.sun
.star
.uno
.RuntimeException
249 return _aSvcImplName
;
252 public String
[] getSupportedServiceNames()
253 throws com
.sun
.star
.uno
.RuntimeException
255 return getSupportedServiceNames_Static();
258 // __________ static things __________
260 public static String _aSvcImplName
= SampleThesaurus
.class.getName();
262 public static String
[] getSupportedServiceNames_Static()
264 String
[] aResult
= { "com.sun.star.linguistic2.Thesaurus" };
270 * Returns a factory for creating the service.
271 * This method is called by the <code>JavaLoader</code>
273 * @return returns a <code>XSingleComponentFactory</code> for creating the component
274 * @param aImplName the name of the implementation for which a service is desired
275 * @see com.sun.star.comp.loader.JavaLoader
277 public static XSingleComponentFactory
__getComponentFactory(
280 XSingleComponentFactory xSingleComponentFactory
= null;
281 if( aImplName
.equals( _aSvcImplName
) )
283 xSingleComponentFactory
= new OneInstanceFactory(
284 SampleThesaurus
.class, _aSvcImplName
,
285 getSupportedServiceNames_Static() );
287 return xSingleComponentFactory
;
291 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */