1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: HelpSearch.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 package com
.sun
.star
.help
;
33 import com
.sun
.star
.lib
.uno
.helper
.Factory
;
34 import com
.sun
.star
.lang
.XMultiComponentFactory
;
35 import com
.sun
.star
.lang
.XSingleComponentFactory
;
36 import com
.sun
.star
.lib
.uno
.helper
.WeakBase
;
37 import com
.sun
.star
.uno
.XComponentContext
;
38 import com
.sun
.star
.registry
.XRegistryKey
;
39 import com
.sun
.star
.lang
.XServiceInfo
;
40 import com
.sun
.star
.uno
.Type
;
41 import com
.sun
.star
.uno
.Any
;
42 import com
.sun
.star
.uno
.AnyConverter
;
44 import org
.apache
.lucene
.analysis
.Analyzer
;
45 import org
.apache
.lucene
.analysis
.standard
.StandardAnalyzer
;
46 import org
.apache
.lucene
.analysis
.cjk
.CJKAnalyzer
;
47 import org
.apache
.lucene
.document
.Document
;
48 import org
.apache
.lucene
.index
.IndexReader
;
49 import org
.apache
.lucene
.index
.Term
;
50 import org
.apache
.lucene
.search
.Hits
;
51 import org
.apache
.lucene
.search
.IndexSearcher
;
52 import org
.apache
.lucene
.search
.Query
;
53 import org
.apache
.lucene
.search
.Searcher
;
54 import org
.apache
.lucene
.search
.TermQuery
;
55 import org
.apache
.lucene
.search
.WildcardQuery
;
57 import com
.sun
.star
.script
.XInvocation
;
58 import com
.sun
.star
.beans
.XIntrospectionAccess
;
60 /** This class capsulates the class, that implements the minimal component, a
61 * factory for creating the service (<CODE>__getComponentFactory</CODE>) and a
62 * method, that writes the information into the given registry key
63 * (<CODE>__writeRegistryServiceInfo</CODE>).
65 public class HelpSearch
67 /** This class implements the component. At least the interfaces XServiceInfo,
68 * XTypeProvider, and XInitialization should be provided by the service.
70 public static class _HelpSearch
extends WeakBase
71 implements XServiceInfo
, XInvocation
73 /** The service name, that must be used to get an instance of this service.
75 static private final String __serviceName
=
76 "com.sun.star.help.HelpSearch";
77 static private final String aSearchMethodName
= "search";
79 /** The initial component contextr, that gives access to
80 * the service manager, supported singletons, ...
81 * It's often later used
83 private XComponentContext m_cmpCtx
;
85 /** The service manager, that gives access to all registered services.
86 * It's often later used
88 private XMultiComponentFactory m_xMCF
;
90 /** The constructor of the inner class has a XMultiServiceFactory parameter.
91 * @param xmultiservicefactoryInitialization A special service factory
92 * could be introduced while initializing.
94 public _HelpSearch(XComponentContext xCompContext
)
97 m_cmpCtx
= xCompContext
;
98 m_xMCF
= m_cmpCtx
.getServiceManager();
100 catch( Exception e
) {
105 /** This method returns an array of all supported service names.
106 * @return Array of supported service names.
108 public String
[] getSupportedServiceNames()
110 return getServiceNames();
113 /** This method is a simple helper function to used in the
114 * static component initialisation functions as well as in
115 * getSupportedServiceNames.
117 public static String
[] getServiceNames()
119 String
[] sSupportedServiceNames
= { __serviceName
};
120 return sSupportedServiceNames
;
123 /** This method returns true, if the given service will be
124 * supported by the component.
125 * @param sServiceName Service name.
126 * @return True, if the given service name will be supported.
128 public boolean supportsService( String sServiceName
)
130 return sServiceName
.equals( __serviceName
);
133 /** Return the class name of the component.
134 * @return Class name of the component.
136 public String
getImplementationName()
138 return _HelpSearch
.class.getName();
141 //===================================================
143 public XIntrospectionAccess
getIntrospection()
148 public Object
invoke( String aFunctionName
, java
.lang
.Object
[] aParams
,
149 short[][] aOutParamIndex
, java
.lang
.Object
[][] aOutParam
)
150 throws com
.sun
.star
.lang
.IllegalArgumentException
,
151 com
.sun
.star
.script
.CannotConvertException
,
152 com
.sun
.star
.reflection
.InvocationTargetException
154 String
[] aRet
= null;
155 if( !aFunctionName
.equals( aSearchMethodName
) )
156 throw new com
.sun
.star
.lang
.IllegalArgumentException();
158 Object
[] aScoreOutArray
= new Object
[1];
159 aScoreOutArray
[0] = null;
162 aRet
= doQuery( aParams
, aScoreOutArray
);
169 Object aScoreArray
= aScoreOutArray
[0];
170 if( aScoreArray
== null )
172 aOutParamIndex
[0] = new short[0];
173 aOutParam
[0] = new Object
[0];
177 short nInParamCount
= (short)aParams
.length
;
178 aOutParamIndex
[0] = new short[1];
179 aOutParamIndex
[0][0] = nInParamCount
;
180 aOutParam
[0] = new Object
[1];
181 aOutParam
[0][0] = aScoreArray
;
184 Any aRetAny
= new Any( new Type( String
[].class ), aRet
);
188 public void setValue( String aPropertyName
, java
.lang
.Object aValue
)
189 throws com
.sun
.star
.beans
.UnknownPropertyException
,
190 com
.sun
.star
.script
.CannotConvertException
,
191 com
.sun
.star
.reflection
.InvocationTargetException
{
192 throw new com
.sun
.star
.beans
.UnknownPropertyException();
195 public Object
getValue( String aPropertyName
)
196 throws com
.sun
.star
.beans
.UnknownPropertyException
{
197 throw new com
.sun
.star
.beans
.UnknownPropertyException();
200 public boolean hasMethod( String aMethodName
) {
201 boolean bRet
= (aMethodName
.equals( aSearchMethodName
) );
204 public boolean hasProperty( String aName
) {
208 // Command line interface for testing
209 private static String
[] doQuery( Object
[] args
, Object
[] aScoreOutArray
) throws Exception
211 String aLanguageStr
= "";
212 String aIndexStr
= "";
213 String aQueryStr
= "";
214 boolean bCaptionOnly
= false;
216 int nParamCount
= args
.length
;
217 String aStrs
[] = new String
[nParamCount
];
218 for( int i
= 0 ; i
< nParamCount
; i
++ )
222 aStrs
[i
] = AnyConverter
.toString( args
[i
] );
224 catch( IllegalArgumentException e
)
230 // TODO: Error handling
231 for( int i
= 0 ; i
< nParamCount
; i
++ )
233 if ("-lang".equals(aStrs
[i
]) )
235 aLanguageStr
= aStrs
[i
+ 1];
238 else if( "-index".equals(aStrs
[i
]) )
240 aIndexStr
= aStrs
[i
+1];
243 else if( "-query".equals(aStrs
[i
]) )
245 aQueryStr
= aStrs
[i
+1];
248 else if( "-caption".equals(aStrs
[i
]) )
253 String
[] aDocs
= queryImpl( aLanguageStr
, aIndexStr
, aQueryStr
, bCaptionOnly
, aScoreOutArray
);
258 private static String
[] queryImpl( String aLanguageStr
, String aIndexStr
, String aQueryStr
,
259 boolean bCaptionOnly
, Object
[] aScoreOutArray
) throws Exception
261 IndexReader reader
= IndexReader
.open( aIndexStr
);
262 Searcher searcher
= new IndexSearcher( reader
);
263 Analyzer analyzer
= aLanguageStr
.equals("ja") ?
(Analyzer
)new CJKAnalyzer() : (Analyzer
)new StandardAnalyzer();
272 if( aQueryStr
.endsWith( "*" ) )
273 aQuery
= new WildcardQuery( new Term( aField
, aQueryStr
) );
275 aQuery
= new TermQuery( new Term( aField
, aQueryStr
) );
278 Hits aHits
= searcher
.search( aQuery
);
279 int nHitCount
= aHits
.length();
281 String aDocs
[] = new String
[nHitCount
];
282 float aScores
[] = null;
283 aScores
= new float[nHitCount
];
284 for( int iHit
= 0 ; iHit
< nHitCount
; iHit
++ )
286 Document aDoc
= aHits
.doc( iHit
);
287 String aPath
= aDoc
.get( "path" );
288 aDocs
[iHit
] = ( aPath
!= null ) ? aPath
: "";
289 aScores
[iHit
] = aHits
.score( iHit
);
291 aScoreOutArray
[0] = aScores
;
300 * Gives a factory for creating the service.
301 * This method is called by the <code>JavaLoader</code>
303 * @return returns a <code>XSingleComponentFactory</code> for creating
305 * @param sImplName the name of the implementation for which a
307 * @see com.sun.star.comp.loader.JavaLoader
309 public static XSingleComponentFactory
__getComponentFactory(String sImplName
)
311 XSingleComponentFactory xFactory
= null;
313 if ( sImplName
.equals( _HelpSearch
.class.getName() ) )
314 xFactory
= Factory
.createComponentFactory(_HelpSearch
.class,
315 _HelpSearch
.getServiceNames());
321 * Writes the service information into the given registry key.
322 * This method is called by the <code>JavaLoader</code>
324 * @return returns true if the operation succeeded
325 * @param regKey the registryKey
326 * @see com.sun.star.comp.loader.JavaLoader
328 public static boolean __writeRegistryServiceInfo(XRegistryKey regKey
) {
329 return Factory
.writeRegistryServiceInfo(_HelpSearch
.class.getName(),
330 _HelpSearch
.getServiceNames(),
333 /** This method is a member of the interface for initializing an object
334 * directly after its creation.
335 * @param object This array of arbitrary objects will be passed to the
336 * component after its creation.
337 * @throws Exception Every exception will not be handled, but will be
338 * passed to the caller.
340 public void initialize( Object
[] object
)
341 throws com
.sun
.star
.uno
.Exception
{
342 /* The component describes what arguments its expected and in which
343 * order!At this point you can read the objects and can intialize
344 * your component using these objects.