157c10a96SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 357c10a96SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 457c10a96SAndrew Rist * or more contributor license agreements. See the NOTICE file 557c10a96SAndrew Rist * distributed with this work for additional information 657c10a96SAndrew Rist * regarding copyright ownership. The ASF licenses this file 757c10a96SAndrew Rist * to you under the Apache License, Version 2.0 (the 857c10a96SAndrew Rist * "License"); you may not use this file except in compliance 957c10a96SAndrew Rist * with the License. You may obtain a copy of the License at 1057c10a96SAndrew Rist * 1157c10a96SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 1257c10a96SAndrew Rist * 1357c10a96SAndrew Rist * Unless required by applicable law or agreed to in writing, 1457c10a96SAndrew Rist * software distributed under the License is distributed on an 1557c10a96SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 1657c10a96SAndrew Rist * KIND, either express or implied. See the License for the 1757c10a96SAndrew Rist * specific language governing permissions and limitations 1857c10a96SAndrew Rist * under the License. 1957c10a96SAndrew Rist * 2057c10a96SAndrew Rist *************************************************************/ 2157c10a96SAndrew Rist 2257c10a96SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir package com.sun.star.help; 25cdf0e10cSrcweir 26cdf0e10cSrcweir import com.sun.star.lib.uno.helper.Factory; 27cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory; 28cdf0e10cSrcweir import com.sun.star.lang.XSingleComponentFactory; 29cdf0e10cSrcweir import com.sun.star.lib.uno.helper.WeakBase; 30cdf0e10cSrcweir import com.sun.star.uno.XComponentContext; 31cdf0e10cSrcweir import com.sun.star.registry.XRegistryKey; 32cdf0e10cSrcweir import com.sun.star.lang.XServiceInfo; 33cdf0e10cSrcweir import com.sun.star.uno.Type; 34cdf0e10cSrcweir import com.sun.star.uno.Any; 35cdf0e10cSrcweir import com.sun.star.uno.AnyConverter; 36cdf0e10cSrcweir 37cdf0e10cSrcweir import org.apache.lucene.analysis.Analyzer; 38cdf0e10cSrcweir import org.apache.lucene.analysis.standard.StandardAnalyzer; 39cdf0e10cSrcweir import org.apache.lucene.analysis.cjk.CJKAnalyzer; 40cdf0e10cSrcweir import org.apache.lucene.document.Document; 41cdf0e10cSrcweir import org.apache.lucene.index.IndexReader; 42cdf0e10cSrcweir import org.apache.lucene.index.Term; 437fb4469bSPedro Giffuni import org.apache.lucene.search.TopDocs; 44cdf0e10cSrcweir import org.apache.lucene.search.IndexSearcher; 45cdf0e10cSrcweir import org.apache.lucene.search.Query; 46cdf0e10cSrcweir import org.apache.lucene.search.Searcher; 477fb4469bSPedro Giffuni import org.apache.lucene.search.ScoreDoc; 48cdf0e10cSrcweir import org.apache.lucene.search.TermQuery; 49cdf0e10cSrcweir import org.apache.lucene.search.WildcardQuery; 507fb4469bSPedro Giffuni import org.apache.lucene.util.Version; 517fb4469bSPedro Giffuni import org.apache.lucene.store.NIOFSDirectory; 52cdf0e10cSrcweir 53cdf0e10cSrcweir import com.sun.star.script.XInvocation; 54cdf0e10cSrcweir import com.sun.star.beans.XIntrospectionAccess; 55cdf0e10cSrcweir 567fb4469bSPedro Giffuni import java.io.File; 577fb4469bSPedro Giffuni 58cdf0e10cSrcweir /** This class capsulates the class, that implements the minimal component and a 59cdf0e10cSrcweir * factory for creating the service (<CODE>__getComponentFactory</CODE>). 60cdf0e10cSrcweir */ 61cdf0e10cSrcweir public class HelpSearch 62cdf0e10cSrcweir { 63cdf0e10cSrcweir /** This class implements the component. At least the interfaces XServiceInfo, 64cdf0e10cSrcweir * XTypeProvider, and XInitialization should be provided by the service. 65cdf0e10cSrcweir */ 66cdf0e10cSrcweir public static class _HelpSearch extends WeakBase 67cdf0e10cSrcweir implements XServiceInfo, XInvocation 68cdf0e10cSrcweir { 69cdf0e10cSrcweir /** The service name, that must be used to get an instance of this service. 70cdf0e10cSrcweir */ 71cdf0e10cSrcweir static private final String __serviceName = 72cdf0e10cSrcweir "com.sun.star.help.HelpSearch"; 73cdf0e10cSrcweir static private final String aSearchMethodName = "search"; 74cdf0e10cSrcweir 75cdf0e10cSrcweir /** The initial component contextr, that gives access to 76cdf0e10cSrcweir * the service manager, supported singletons, ... 77cdf0e10cSrcweir * It's often later used 78cdf0e10cSrcweir */ 79cdf0e10cSrcweir private XComponentContext m_cmpCtx; 80cdf0e10cSrcweir 81cdf0e10cSrcweir /** The service manager, that gives access to all registered services. 82cdf0e10cSrcweir * It's often later used 83cdf0e10cSrcweir */ 84cdf0e10cSrcweir private XMultiComponentFactory m_xMCF; 85cdf0e10cSrcweir 86cdf0e10cSrcweir /** The constructor of the inner class has a XMultiServiceFactory parameter. 87cdf0e10cSrcweir * @param xmultiservicefactoryInitialization A special service factory 88cdf0e10cSrcweir * could be introduced while initializing. 89cdf0e10cSrcweir */ _HelpSearch(XComponentContext xCompContext)90cdf0e10cSrcweir public _HelpSearch(XComponentContext xCompContext) 91cdf0e10cSrcweir { 92cdf0e10cSrcweir try { 93cdf0e10cSrcweir m_cmpCtx = xCompContext; 94cdf0e10cSrcweir m_xMCF = m_cmpCtx.getServiceManager(); 95cdf0e10cSrcweir } 96cdf0e10cSrcweir catch( Exception e ) { 97cdf0e10cSrcweir e.printStackTrace(); 98cdf0e10cSrcweir } 99cdf0e10cSrcweir } 100cdf0e10cSrcweir 101cdf0e10cSrcweir /** This method returns an array of all supported service names. 102cdf0e10cSrcweir * @return Array of supported service names. 103cdf0e10cSrcweir */ getSupportedServiceNames()104cdf0e10cSrcweir public String[] getSupportedServiceNames() 105cdf0e10cSrcweir { 106cdf0e10cSrcweir return getServiceNames(); 107cdf0e10cSrcweir } 108cdf0e10cSrcweir 109cdf0e10cSrcweir /** This method is a simple helper function to used in the 110cdf0e10cSrcweir * static component initialisation functions as well as in 111cdf0e10cSrcweir * getSupportedServiceNames. 112cdf0e10cSrcweir */ getServiceNames()113cdf0e10cSrcweir public static String[] getServiceNames() 114cdf0e10cSrcweir { 115cdf0e10cSrcweir String[] sSupportedServiceNames = { __serviceName }; 116cdf0e10cSrcweir return sSupportedServiceNames; 117cdf0e10cSrcweir } 118cdf0e10cSrcweir 119cdf0e10cSrcweir /** This method returns true, if the given service will be 120cdf0e10cSrcweir * supported by the component. 121cdf0e10cSrcweir * @param sServiceName Service name. 122cdf0e10cSrcweir * @return True, if the given service name will be supported. 123cdf0e10cSrcweir */ supportsService( String sServiceName )124cdf0e10cSrcweir public boolean supportsService( String sServiceName ) 125cdf0e10cSrcweir { 126cdf0e10cSrcweir return sServiceName.equals( __serviceName ); 127cdf0e10cSrcweir } 128cdf0e10cSrcweir 129cdf0e10cSrcweir /** Return the class name of the component. 130cdf0e10cSrcweir * @return Class name of the component. 131cdf0e10cSrcweir */ getImplementationName()132cdf0e10cSrcweir public String getImplementationName() 133cdf0e10cSrcweir { 134cdf0e10cSrcweir return _HelpSearch.class.getName(); 135cdf0e10cSrcweir } 136cdf0e10cSrcweir 137cdf0e10cSrcweir //=================================================== 138cdf0e10cSrcweir // XInvocation getIntrospection()139cdf0e10cSrcweir public XIntrospectionAccess getIntrospection() 140cdf0e10cSrcweir { 141cdf0e10cSrcweir return null; 142cdf0e10cSrcweir } 143cdf0e10cSrcweir invoke( String aFunctionName, java.lang.Object[] aParams, short[][] aOutParamIndex, java.lang.Object[][] aOutParam )144cdf0e10cSrcweir public Object invoke( String aFunctionName, java.lang.Object[] aParams, 145cdf0e10cSrcweir short[][] aOutParamIndex, java.lang.Object[][] aOutParam ) 146cdf0e10cSrcweir throws com.sun.star.lang.IllegalArgumentException, 147cdf0e10cSrcweir com.sun.star.script.CannotConvertException, 148cdf0e10cSrcweir com.sun.star.reflection.InvocationTargetException 149cdf0e10cSrcweir { 150cdf0e10cSrcweir String[] aRet = null; 151cdf0e10cSrcweir if( !aFunctionName.equals( aSearchMethodName ) ) 152cdf0e10cSrcweir throw new com.sun.star.lang.IllegalArgumentException(); 153cdf0e10cSrcweir 154cdf0e10cSrcweir Object[] aScoreOutArray = new Object[1]; 155cdf0e10cSrcweir aScoreOutArray[0] = null; 156cdf0e10cSrcweir try 157cdf0e10cSrcweir { 158cdf0e10cSrcweir aRet = doQuery( aParams, aScoreOutArray ); 159cdf0e10cSrcweir } 160cdf0e10cSrcweir catch( Exception e ) 161cdf0e10cSrcweir { 162cdf0e10cSrcweir aRet = null; 163cdf0e10cSrcweir } 164cdf0e10cSrcweir 165cdf0e10cSrcweir Object aScoreArray = aScoreOutArray[0]; 166cdf0e10cSrcweir if( aScoreArray == null ) 167cdf0e10cSrcweir { 168cdf0e10cSrcweir aOutParamIndex[0] = new short[0]; 169cdf0e10cSrcweir aOutParam[0] = new Object[0]; 170cdf0e10cSrcweir } 171cdf0e10cSrcweir else 172cdf0e10cSrcweir { 173cdf0e10cSrcweir short nInParamCount = (short)aParams.length; 174cdf0e10cSrcweir aOutParamIndex[0] = new short[1]; 175cdf0e10cSrcweir aOutParamIndex[0][0] = nInParamCount; 176cdf0e10cSrcweir aOutParam[0] = new Object[1]; 177cdf0e10cSrcweir aOutParam[0][0] = aScoreArray; 178cdf0e10cSrcweir } 179cdf0e10cSrcweir 180cdf0e10cSrcweir Any aRetAny = new Any( new Type( String[].class ), aRet ); 181cdf0e10cSrcweir return aRetAny; 182cdf0e10cSrcweir } 183cdf0e10cSrcweir setValue( String aPropertyName, java.lang.Object aValue )184cdf0e10cSrcweir public void setValue( String aPropertyName, java.lang.Object aValue ) 185cdf0e10cSrcweir throws com.sun.star.beans.UnknownPropertyException, 186cdf0e10cSrcweir com.sun.star.script.CannotConvertException, 187cdf0e10cSrcweir com.sun.star.reflection.InvocationTargetException { 188cdf0e10cSrcweir throw new com.sun.star.beans.UnknownPropertyException(); 189cdf0e10cSrcweir } 190cdf0e10cSrcweir getValue( String aPropertyName )191cdf0e10cSrcweir public Object getValue( String aPropertyName ) 192cdf0e10cSrcweir throws com.sun.star.beans.UnknownPropertyException { 193cdf0e10cSrcweir throw new com.sun.star.beans.UnknownPropertyException(); 194cdf0e10cSrcweir } 195cdf0e10cSrcweir hasMethod( String aMethodName )196cdf0e10cSrcweir public boolean hasMethod( String aMethodName ) { 197cdf0e10cSrcweir boolean bRet = (aMethodName.equals( aSearchMethodName ) ); 198cdf0e10cSrcweir return bRet; 199cdf0e10cSrcweir } hasProperty( String aName )200cdf0e10cSrcweir public boolean hasProperty( String aName ) { 201cdf0e10cSrcweir return false; 202cdf0e10cSrcweir } 203cdf0e10cSrcweir 204cdf0e10cSrcweir // Command line interface for testing doQuery( Object[] args, Object[] aScoreOutArray )205cdf0e10cSrcweir private static String[] doQuery( Object[] args, Object[] aScoreOutArray ) throws Exception 206cdf0e10cSrcweir { 207cdf0e10cSrcweir String aLanguageStr = ""; 208cdf0e10cSrcweir String aIndexStr = ""; 209cdf0e10cSrcweir String aQueryStr = ""; 210cdf0e10cSrcweir boolean bCaptionOnly = false; 211cdf0e10cSrcweir 212cdf0e10cSrcweir int nParamCount = args.length; 213cdf0e10cSrcweir String aStrs[] = new String[nParamCount]; 214cdf0e10cSrcweir for( int i = 0 ; i < nParamCount ; i++ ) 215cdf0e10cSrcweir { 216cdf0e10cSrcweir try 217cdf0e10cSrcweir { 218cdf0e10cSrcweir aStrs[i] = AnyConverter.toString( args[i] ); 219cdf0e10cSrcweir } 220cdf0e10cSrcweir catch( IllegalArgumentException e ) 221cdf0e10cSrcweir { 222cdf0e10cSrcweir aStrs[i] = ""; 223cdf0e10cSrcweir } 224cdf0e10cSrcweir } 225cdf0e10cSrcweir 226cdf0e10cSrcweir // TODO: Error handling 227cdf0e10cSrcweir for( int i = 0 ; i < nParamCount ; i++ ) 228cdf0e10cSrcweir { 229cdf0e10cSrcweir if ("-lang".equals(aStrs[i]) ) 230cdf0e10cSrcweir { 231cdf0e10cSrcweir aLanguageStr = aStrs[i + 1]; 232cdf0e10cSrcweir i++; 233cdf0e10cSrcweir } 234cdf0e10cSrcweir else if( "-index".equals(aStrs[i]) ) 235cdf0e10cSrcweir { 236cdf0e10cSrcweir aIndexStr = aStrs[i+1]; 237cdf0e10cSrcweir i++; 238cdf0e10cSrcweir } 239cdf0e10cSrcweir else if( "-query".equals(aStrs[i]) ) 240cdf0e10cSrcweir { 241cdf0e10cSrcweir aQueryStr = aStrs[i+1]; 242cdf0e10cSrcweir i++; 243cdf0e10cSrcweir } 244cdf0e10cSrcweir else if( "-caption".equals(aStrs[i]) ) 245cdf0e10cSrcweir { 246cdf0e10cSrcweir bCaptionOnly = true; 247cdf0e10cSrcweir } 248cdf0e10cSrcweir } 249cdf0e10cSrcweir String[] aDocs = queryImpl( aLanguageStr, aIndexStr, aQueryStr, bCaptionOnly, aScoreOutArray ); 250cdf0e10cSrcweir 251cdf0e10cSrcweir return aDocs; 252cdf0e10cSrcweir } 253cdf0e10cSrcweir queryImpl( String aLanguageStr, String aIndexStr, String aQueryStr, boolean bCaptionOnly, Object[] aScoreOutArray )254cdf0e10cSrcweir private static String[] queryImpl( String aLanguageStr, String aIndexStr, String aQueryStr, 255cdf0e10cSrcweir boolean bCaptionOnly, Object[] aScoreOutArray ) throws Exception 256cdf0e10cSrcweir { 2577fb4469bSPedro Giffuni File aIndexFile = new File( aIndexStr ); 2587fb4469bSPedro Giffuni IndexReader reader = IndexReader.open( NIOFSDirectory.open( aIndexFile ), true ); 259cdf0e10cSrcweir Searcher searcher = new IndexSearcher( reader ); 2607fb4469bSPedro Giffuni Analyzer analyzer = aLanguageStr.equals("ja") ? (Analyzer)new CJKAnalyzer(Version.LUCENE_29) : (Analyzer)new StandardAnalyzer(Version.LUCENE_29); 261cdf0e10cSrcweir 262cdf0e10cSrcweir String aField; 263cdf0e10cSrcweir if( bCaptionOnly ) 264cdf0e10cSrcweir aField = "caption"; 265cdf0e10cSrcweir else 266cdf0e10cSrcweir aField = "content"; 267cdf0e10cSrcweir 268cdf0e10cSrcweir Query aQuery; 269cdf0e10cSrcweir if( aQueryStr.endsWith( "*" ) ) 270cdf0e10cSrcweir aQuery = new WildcardQuery( new Term( aField, aQueryStr ) ); 271cdf0e10cSrcweir else 272cdf0e10cSrcweir aQuery = new TermQuery( new Term( aField, aQueryStr ) ); 273cdf0e10cSrcweir 274cdf0e10cSrcweir // Perform search 2757fb4469bSPedro Giffuni TopDocs aHits = searcher.search( aQuery, 100 ); 276*7a0fab5dSHerbert Dürr int nHitCount = aHits.scoreDocs.length; 277cdf0e10cSrcweir 278cdf0e10cSrcweir String aDocs[] = new String[nHitCount]; 279cdf0e10cSrcweir float aScores[] = null; 280cdf0e10cSrcweir aScores = new float[nHitCount]; 281cdf0e10cSrcweir for( int iHit = 0 ; iHit < nHitCount ; iHit++ ) 282cdf0e10cSrcweir { 2837fb4469bSPedro Giffuni ScoreDoc aDoc = aHits.scoreDocs[iHit]; 2847fb4469bSPedro Giffuni String aPath = searcher.doc(aDoc.doc).get( "path" ); 285cdf0e10cSrcweir aDocs[iHit] = ( aPath != null ) ? aPath : ""; 2867fb4469bSPedro Giffuni aScores[iHit] = aDoc.score; 287cdf0e10cSrcweir } 288cdf0e10cSrcweir aScoreOutArray[0] = aScores; 289cdf0e10cSrcweir 290cdf0e10cSrcweir reader.close(); 291cdf0e10cSrcweir 292cdf0e10cSrcweir return aDocs; 293cdf0e10cSrcweir } 294cdf0e10cSrcweir } 295cdf0e10cSrcweir 296cdf0e10cSrcweir /** 297cdf0e10cSrcweir * Gives a factory for creating the service. 298cdf0e10cSrcweir * This method is called by the <code>JavaLoader</code> 299cdf0e10cSrcweir * <p> 300cdf0e10cSrcweir * @return returns a <code>XSingleComponentFactory</code> for creating 301cdf0e10cSrcweir * the component 302cdf0e10cSrcweir * @param sImplName the name of the implementation for which a 303cdf0e10cSrcweir * service is desired 304cdf0e10cSrcweir * @see com.sun.star.comp.loader.JavaLoader 305cdf0e10cSrcweir */ __getComponentFactory(String sImplName)306cdf0e10cSrcweir public static XSingleComponentFactory __getComponentFactory(String sImplName) 307cdf0e10cSrcweir { 308cdf0e10cSrcweir XSingleComponentFactory xFactory = null; 309cdf0e10cSrcweir 310cdf0e10cSrcweir if ( sImplName.equals( _HelpSearch.class.getName() ) ) 311cdf0e10cSrcweir xFactory = Factory.createComponentFactory(_HelpSearch.class, 312cdf0e10cSrcweir _HelpSearch.getServiceNames()); 313cdf0e10cSrcweir 314cdf0e10cSrcweir return xFactory; 315cdf0e10cSrcweir } 316cdf0e10cSrcweir 317cdf0e10cSrcweir /** This method is a member of the interface for initializing an object 318cdf0e10cSrcweir * directly after its creation. 319cdf0e10cSrcweir * @param object This array of arbitrary objects will be passed to the 320cdf0e10cSrcweir * component after its creation. 321cdf0e10cSrcweir * @throws Exception Every exception will not be handled, but will be 322cdf0e10cSrcweir * passed to the caller. 323cdf0e10cSrcweir */ initialize( Object[] object )324cdf0e10cSrcweir public void initialize( Object[] object ) 325cdf0e10cSrcweir throws com.sun.star.uno.Exception { 326cdf0e10cSrcweir /* The component describes what arguments its expected and in which 327cdf0e10cSrcweir * order!At this point you can read the objects and can intialize 328cdf0e10cSrcweir * your component using these objects. 329cdf0e10cSrcweir */ 330cdf0e10cSrcweir } 331cdf0e10cSrcweir } 332