1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir package com.sun.star.help;
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir import com.sun.star.lib.uno.helper.Factory;
31*cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory;
32*cdf0e10cSrcweir import com.sun.star.lang.XSingleComponentFactory;
33*cdf0e10cSrcweir import com.sun.star.lib.uno.helper.WeakBase;
34*cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
35*cdf0e10cSrcweir import com.sun.star.registry.XRegistryKey;
36*cdf0e10cSrcweir import com.sun.star.lang.XServiceInfo;
37*cdf0e10cSrcweir import com.sun.star.uno.Type;
38*cdf0e10cSrcweir import com.sun.star.uno.Any;
39*cdf0e10cSrcweir import com.sun.star.uno.AnyConverter;
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir import org.apache.lucene.analysis.Analyzer;
42*cdf0e10cSrcweir import org.apache.lucene.analysis.standard.StandardAnalyzer;
43*cdf0e10cSrcweir import org.apache.lucene.analysis.cjk.CJKAnalyzer;
44*cdf0e10cSrcweir import org.apache.lucene.document.Document;
45*cdf0e10cSrcweir import org.apache.lucene.index.IndexReader;
46*cdf0e10cSrcweir import org.apache.lucene.index.Term;
47*cdf0e10cSrcweir import org.apache.lucene.search.Hits;
48*cdf0e10cSrcweir import org.apache.lucene.search.IndexSearcher;
49*cdf0e10cSrcweir import org.apache.lucene.search.Query;
50*cdf0e10cSrcweir import org.apache.lucene.search.Searcher;
51*cdf0e10cSrcweir import org.apache.lucene.search.TermQuery;
52*cdf0e10cSrcweir import org.apache.lucene.search.WildcardQuery;
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir import com.sun.star.script.XInvocation;
55*cdf0e10cSrcweir import com.sun.star.beans.XIntrospectionAccess;
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir /** This class capsulates the class, that implements the minimal component and a
58*cdf0e10cSrcweir  * factory for creating the service (<CODE>__getComponentFactory</CODE>).
59*cdf0e10cSrcweir  */
60*cdf0e10cSrcweir public class HelpSearch
61*cdf0e10cSrcweir {
62*cdf0e10cSrcweir     /** This class implements the component. At least the interfaces XServiceInfo,
63*cdf0e10cSrcweir      * XTypeProvider, and XInitialization should be provided by the service.
64*cdf0e10cSrcweir      */
65*cdf0e10cSrcweir     public static class _HelpSearch extends WeakBase
66*cdf0e10cSrcweir         implements XServiceInfo, XInvocation
67*cdf0e10cSrcweir 	{
68*cdf0e10cSrcweir         /** The service name, that must be used to get an instance of this service.
69*cdf0e10cSrcweir          */
70*cdf0e10cSrcweir 		static private final String __serviceName =
71*cdf0e10cSrcweir 	        "com.sun.star.help.HelpSearch";
72*cdf0e10cSrcweir 		static private final String aSearchMethodName = "search";
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir         /** The initial component contextr, that gives access to
75*cdf0e10cSrcweir          * the service manager, supported singletons, ...
76*cdf0e10cSrcweir          * It's often later used
77*cdf0e10cSrcweir          */
78*cdf0e10cSrcweir         private XComponentContext m_cmpCtx;
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir         /** The service manager, that gives access to all registered services.
81*cdf0e10cSrcweir          * It's often later used
82*cdf0e10cSrcweir          */
83*cdf0e10cSrcweir         private XMultiComponentFactory m_xMCF;
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir         /** The constructor of the inner class has a XMultiServiceFactory parameter.
86*cdf0e10cSrcweir          * @param xmultiservicefactoryInitialization A special service factory
87*cdf0e10cSrcweir          * could be introduced while initializing.
88*cdf0e10cSrcweir          */
89*cdf0e10cSrcweir         public _HelpSearch(XComponentContext xCompContext)
90*cdf0e10cSrcweir 		{
91*cdf0e10cSrcweir             try {
92*cdf0e10cSrcweir                 m_cmpCtx = xCompContext;
93*cdf0e10cSrcweir                 m_xMCF = m_cmpCtx.getServiceManager();
94*cdf0e10cSrcweir             }
95*cdf0e10cSrcweir             catch( Exception e ) {
96*cdf0e10cSrcweir                 e.printStackTrace();
97*cdf0e10cSrcweir             }
98*cdf0e10cSrcweir         }
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir         /** This method returns an array of all supported service names.
101*cdf0e10cSrcweir          * @return Array of supported service names.
102*cdf0e10cSrcweir          */
103*cdf0e10cSrcweir         public String[] getSupportedServiceNames()
104*cdf0e10cSrcweir 		{
105*cdf0e10cSrcweir             return getServiceNames();
106*cdf0e10cSrcweir         }
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir         /** This method is a simple helper function to used in the
109*cdf0e10cSrcweir          * static component initialisation functions as well as in
110*cdf0e10cSrcweir          * getSupportedServiceNames.
111*cdf0e10cSrcweir          */
112*cdf0e10cSrcweir         public static String[] getServiceNames()
113*cdf0e10cSrcweir 		{
114*cdf0e10cSrcweir             String[] sSupportedServiceNames = { __serviceName };
115*cdf0e10cSrcweir             return sSupportedServiceNames;
116*cdf0e10cSrcweir         }
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir         /** This method returns true, if the given service will be
119*cdf0e10cSrcweir          * supported by the component.
120*cdf0e10cSrcweir          * @param sServiceName Service name.
121*cdf0e10cSrcweir          * @return True, if the given service name will be supported.
122*cdf0e10cSrcweir          */
123*cdf0e10cSrcweir         public boolean supportsService( String sServiceName )
124*cdf0e10cSrcweir 		{
125*cdf0e10cSrcweir             return sServiceName.equals( __serviceName );
126*cdf0e10cSrcweir         }
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir         /** Return the class name of the component.
129*cdf0e10cSrcweir          * @return Class name of the component.
130*cdf0e10cSrcweir          */
131*cdf0e10cSrcweir         public String getImplementationName()
132*cdf0e10cSrcweir 		{
133*cdf0e10cSrcweir             return  _HelpSearch.class.getName();
134*cdf0e10cSrcweir         }
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir 		//===================================================
137*cdf0e10cSrcweir 		// XInvocation
138*cdf0e10cSrcweir 		public XIntrospectionAccess getIntrospection()
139*cdf0e10cSrcweir 		{
140*cdf0e10cSrcweir             return  null;
141*cdf0e10cSrcweir         }
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir 		public Object invoke( String aFunctionName, java.lang.Object[] aParams,
144*cdf0e10cSrcweir 			short[][] aOutParamIndex, java.lang.Object[][] aOutParam )
145*cdf0e10cSrcweir 				throws	com.sun.star.lang.IllegalArgumentException,
146*cdf0e10cSrcweir 						com.sun.star.script.CannotConvertException,
147*cdf0e10cSrcweir 						com.sun.star.reflection.InvocationTargetException
148*cdf0e10cSrcweir 		{
149*cdf0e10cSrcweir 			String[] aRet = null;
150*cdf0e10cSrcweir 			if( !aFunctionName.equals( aSearchMethodName ) )
151*cdf0e10cSrcweir 			    throw new com.sun.star.lang.IllegalArgumentException();
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir 			Object[] aScoreOutArray = new Object[1];
154*cdf0e10cSrcweir 			aScoreOutArray[0] = null;
155*cdf0e10cSrcweir 			try
156*cdf0e10cSrcweir 			{
157*cdf0e10cSrcweir 			    aRet =  doQuery( aParams, aScoreOutArray );
158*cdf0e10cSrcweir 			}
159*cdf0e10cSrcweir 			catch( Exception e )
160*cdf0e10cSrcweir 			{
161*cdf0e10cSrcweir 			    aRet = null;
162*cdf0e10cSrcweir 			}
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir 			Object aScoreArray = aScoreOutArray[0];
165*cdf0e10cSrcweir 			if( aScoreArray == null )
166*cdf0e10cSrcweir 			{
167*cdf0e10cSrcweir 				aOutParamIndex[0] = new short[0];
168*cdf0e10cSrcweir 				aOutParam[0] = new Object[0];
169*cdf0e10cSrcweir 			}
170*cdf0e10cSrcweir 			else
171*cdf0e10cSrcweir 			{
172*cdf0e10cSrcweir 				short nInParamCount = (short)aParams.length;
173*cdf0e10cSrcweir 				aOutParamIndex[0] = new short[1];
174*cdf0e10cSrcweir 				aOutParamIndex[0][0] = nInParamCount;
175*cdf0e10cSrcweir 				aOutParam[0] = new Object[1];
176*cdf0e10cSrcweir 				aOutParam[0][0] = aScoreArray;
177*cdf0e10cSrcweir 			}
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir 			Any aRetAny = new Any( new Type( String[].class ), aRet );
180*cdf0e10cSrcweir 			return aRetAny;
181*cdf0e10cSrcweir 		}
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir 		public void setValue( String aPropertyName, java.lang.Object aValue )
184*cdf0e10cSrcweir 			throws	com.sun.star.beans.UnknownPropertyException,
185*cdf0e10cSrcweir 					com.sun.star.script.CannotConvertException,
186*cdf0e10cSrcweir 					com.sun.star.reflection.InvocationTargetException {
187*cdf0e10cSrcweir 			throw new com.sun.star.beans.UnknownPropertyException();
188*cdf0e10cSrcweir 		}
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir 		public Object getValue( String aPropertyName )
191*cdf0e10cSrcweir 			throws com.sun.star.beans.UnknownPropertyException {
192*cdf0e10cSrcweir 			throw new com.sun.star.beans.UnknownPropertyException();
193*cdf0e10cSrcweir 		}
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir 		public boolean hasMethod( String aMethodName ) {
196*cdf0e10cSrcweir 			boolean bRet = (aMethodName.equals( aSearchMethodName ) );
197*cdf0e10cSrcweir 			return bRet;
198*cdf0e10cSrcweir 		}
199*cdf0e10cSrcweir 		public boolean hasProperty( String aName ) {
200*cdf0e10cSrcweir 			return false;
201*cdf0e10cSrcweir 		}
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir 		// Command line interface for testing
204*cdf0e10cSrcweir 		private static String[] doQuery( Object[] args, Object[] aScoreOutArray ) throws Exception
205*cdf0e10cSrcweir 		{
206*cdf0e10cSrcweir  			String aLanguageStr = "";
207*cdf0e10cSrcweir  			String aIndexStr = "";
208*cdf0e10cSrcweir 			String aQueryStr = "";
209*cdf0e10cSrcweir 			boolean bCaptionOnly = false;
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir 			int nParamCount = args.length;
212*cdf0e10cSrcweir 			String aStrs[] = new String[nParamCount];
213*cdf0e10cSrcweir 			for( int i = 0 ; i < nParamCount ; i++ )
214*cdf0e10cSrcweir 			{
215*cdf0e10cSrcweir 				try
216*cdf0e10cSrcweir 				{
217*cdf0e10cSrcweir 					aStrs[i] = AnyConverter.toString( args[i] );
218*cdf0e10cSrcweir 				}
219*cdf0e10cSrcweir 				catch( IllegalArgumentException e )
220*cdf0e10cSrcweir 				{
221*cdf0e10cSrcweir 					aStrs[i] = "";
222*cdf0e10cSrcweir 				}
223*cdf0e10cSrcweir 			}
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir 			// TODO: Error handling
226*cdf0e10cSrcweir 			for( int i = 0 ; i < nParamCount ; i++ )
227*cdf0e10cSrcweir 			{
228*cdf0e10cSrcweir                 if ("-lang".equals(aStrs[i]) )
229*cdf0e10cSrcweir 				{
230*cdf0e10cSrcweir                     aLanguageStr = aStrs[i + 1];
231*cdf0e10cSrcweir                     i++;
232*cdf0e10cSrcweir 				}
233*cdf0e10cSrcweir 				else if( "-index".equals(aStrs[i]) )
234*cdf0e10cSrcweir 				{
235*cdf0e10cSrcweir 					aIndexStr = aStrs[i+1];
236*cdf0e10cSrcweir 					i++;
237*cdf0e10cSrcweir 				}
238*cdf0e10cSrcweir 				else if( "-query".equals(aStrs[i]) )
239*cdf0e10cSrcweir 				{
240*cdf0e10cSrcweir 					aQueryStr = aStrs[i+1];
241*cdf0e10cSrcweir 					i++;
242*cdf0e10cSrcweir 				}
243*cdf0e10cSrcweir 				else if( "-caption".equals(aStrs[i]) )
244*cdf0e10cSrcweir 				{
245*cdf0e10cSrcweir 					bCaptionOnly = true;
246*cdf0e10cSrcweir 				}
247*cdf0e10cSrcweir 			}
248*cdf0e10cSrcweir 			String[] aDocs = queryImpl( aLanguageStr, aIndexStr, aQueryStr, bCaptionOnly, aScoreOutArray );
249*cdf0e10cSrcweir 
250*cdf0e10cSrcweir 			return aDocs;
251*cdf0e10cSrcweir         }
252*cdf0e10cSrcweir 
253*cdf0e10cSrcweir 		private static String[] queryImpl( String aLanguageStr, String aIndexStr, String aQueryStr,
254*cdf0e10cSrcweir 			boolean bCaptionOnly, Object[] aScoreOutArray ) throws Exception
255*cdf0e10cSrcweir 		{
256*cdf0e10cSrcweir 			IndexReader reader = IndexReader.open( aIndexStr );
257*cdf0e10cSrcweir 			Searcher searcher = new IndexSearcher( reader );
258*cdf0e10cSrcweir             Analyzer analyzer = aLanguageStr.equals("ja") ? (Analyzer)new CJKAnalyzer() : (Analyzer)new StandardAnalyzer();
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir 			String aField;
261*cdf0e10cSrcweir 			if( bCaptionOnly )
262*cdf0e10cSrcweir 				aField = "caption";
263*cdf0e10cSrcweir 			else
264*cdf0e10cSrcweir 				aField = "content";
265*cdf0e10cSrcweir 
266*cdf0e10cSrcweir 			Query aQuery;
267*cdf0e10cSrcweir 			if( aQueryStr.endsWith( "*" ) )
268*cdf0e10cSrcweir 				aQuery = new WildcardQuery( new Term( aField, aQueryStr ) );
269*cdf0e10cSrcweir 			else
270*cdf0e10cSrcweir 				aQuery = new TermQuery( new Term( aField, aQueryStr ) );
271*cdf0e10cSrcweir 
272*cdf0e10cSrcweir 			// Perform search
273*cdf0e10cSrcweir 			Hits aHits = searcher.search( aQuery );
274*cdf0e10cSrcweir 			int nHitCount = aHits.length();
275*cdf0e10cSrcweir 
276*cdf0e10cSrcweir 			String aDocs[] = new String[nHitCount];
277*cdf0e10cSrcweir 			float aScores[] = null;
278*cdf0e10cSrcweir 			aScores = new float[nHitCount];
279*cdf0e10cSrcweir 			for( int iHit = 0 ; iHit < nHitCount ; iHit++ )
280*cdf0e10cSrcweir 			{
281*cdf0e10cSrcweir 				Document aDoc = aHits.doc( iHit );
282*cdf0e10cSrcweir 				String aPath = aDoc.get( "path" );
283*cdf0e10cSrcweir 				aDocs[iHit] = ( aPath != null ) ? aPath : "";
284*cdf0e10cSrcweir 				aScores[iHit] = aHits.score( iHit );
285*cdf0e10cSrcweir 			}
286*cdf0e10cSrcweir 			aScoreOutArray[0] = aScores;
287*cdf0e10cSrcweir 
288*cdf0e10cSrcweir 			reader.close();
289*cdf0e10cSrcweir 
290*cdf0e10cSrcweir 			return aDocs;
291*cdf0e10cSrcweir 		}
292*cdf0e10cSrcweir     }
293*cdf0e10cSrcweir 
294*cdf0e10cSrcweir     /**
295*cdf0e10cSrcweir      * Gives a factory for creating the service.
296*cdf0e10cSrcweir      * This method is called by the <code>JavaLoader</code>
297*cdf0e10cSrcweir      * <p>
298*cdf0e10cSrcweir      * @return  returns a <code>XSingleComponentFactory</code> for creating
299*cdf0e10cSrcweir      *          the component
300*cdf0e10cSrcweir      * @param   sImplName the name of the implementation for which a
301*cdf0e10cSrcweir      *          service is desired
302*cdf0e10cSrcweir      * @see     com.sun.star.comp.loader.JavaLoader
303*cdf0e10cSrcweir      */
304*cdf0e10cSrcweir     public static XSingleComponentFactory __getComponentFactory(String sImplName)
305*cdf0e10cSrcweir     {
306*cdf0e10cSrcweir         XSingleComponentFactory xFactory = null;
307*cdf0e10cSrcweir 
308*cdf0e10cSrcweir         if ( sImplName.equals( _HelpSearch.class.getName() ) )
309*cdf0e10cSrcweir             xFactory = Factory.createComponentFactory(_HelpSearch.class,
310*cdf0e10cSrcweir                                              _HelpSearch.getServiceNames());
311*cdf0e10cSrcweir 
312*cdf0e10cSrcweir         return xFactory;
313*cdf0e10cSrcweir     }
314*cdf0e10cSrcweir 
315*cdf0e10cSrcweir         /** This method is a member of the interface for initializing an object
316*cdf0e10cSrcweir          * directly after its creation.
317*cdf0e10cSrcweir          * @param object This array of arbitrary objects will be passed to the
318*cdf0e10cSrcweir          * component after its creation.
319*cdf0e10cSrcweir          * @throws Exception Every exception will not be handled, but will be
320*cdf0e10cSrcweir          * passed to the caller.
321*cdf0e10cSrcweir          */
322*cdf0e10cSrcweir         public void initialize( Object[] object )
323*cdf0e10cSrcweir             throws com.sun.star.uno.Exception {
324*cdf0e10cSrcweir             /* The component describes what arguments its expected and in which
325*cdf0e10cSrcweir              * order!At this point you can read the objects and can intialize
326*cdf0e10cSrcweir              * your component using these objects.
327*cdf0e10cSrcweir              */
328*cdf0e10cSrcweir         }
329*cdf0e10cSrcweir }
330