LCOV - code coverage report
Current view: top level - xmlhelp/source/cxxhelp/provider - resultsetforquery.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 0 155 0.0 %
Date: 2015-06-13 12:38:46 Functions: 0 7 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include <comphelper/processfactory.hxx>
      21             : #include <com/sun/star/ucb/Command.hpp>
      22             : #include <com/sun/star/ucb/XCommandEnvironment.hpp>
      23             : #include <com/sun/star/i18n/Transliteration.hpp>
      24             : #include <com/sun/star/ucb/XCommandProcessor.hpp>
      25             : #include <com/sun/star/lang/Locale.hpp>
      26             : #include <com/sun/star/script/XInvocation.hpp>
      27             : 
      28             : #include <helpcompiler/HelpSearch.hxx>
      29             : 
      30             : #if defined _MSC_VER
      31             : #pragma warning(push)
      32             : #pragma warning(disable : 4068 4263 4264 4266)
      33             : #endif
      34             : 
      35             : #if defined(__GNUC__) && defined(HAVE_GCC_VISIBILITY_FEATURE)
      36             : #  pragma GCC visibility push (default)
      37             : #endif
      38             : #include <CLucene.h>
      39             : #if defined(__GNUC__) && defined(HAVE_GCC_VISIBILITY_FEATURE)
      40             : #  pragma GCC visibility pop
      41             : #endif
      42             : 
      43             : #if defined _MSC_VER
      44             : #pragma warning(pop)
      45             : #endif
      46             : 
      47             : #include <rtl/ustring.hxx>
      48             : 
      49             : #include <algorithm>
      50             : #include <set>
      51             : #include <qe/Query.hxx>
      52             : #include <qe/DocGenerator.hxx>
      53             : #include "resultsetforquery.hxx"
      54             : #include "databases.hxx"
      55             : 
      56             : using namespace std;
      57             : using namespace chelp;
      58             : using namespace xmlsearch::excep;
      59             : using namespace xmlsearch::qe;
      60             : using namespace com::sun::star;
      61             : using namespace com::sun::star::ucb;
      62             : using namespace com::sun::star::i18n;
      63             : using namespace com::sun::star::uno;
      64             : using namespace com::sun::star::lang;
      65             : 
      66           0 : struct HitItem
      67             : {
      68             :     OUString   m_aURL;
      69             :     float      m_fScore;
      70             : 
      71           0 :     HitItem(const OUString& aURL, float fScore)
      72             :         : m_aURL(aURL)
      73           0 :         , m_fScore(fScore)
      74           0 :     {}
      75           0 :     bool operator < ( const HitItem& rHitItem ) const
      76             :     {
      77           0 :         return rHitItem.m_fScore < m_fScore;
      78             :     }
      79             : };
      80             : 
      81           0 : ResultSetForQuery::ResultSetForQuery( const uno::Reference< uno::XComponentContext >& rxContext,
      82             :                                       const uno::Reference< XContentProvider >&  xProvider,
      83             :                                       sal_Int32 nOpenMode,
      84             :                                       const uno::Sequence< beans::Property >& seq,
      85             :                                       const uno::Sequence< NumberedSortingInfo >& seqSort,
      86             :                                       URLParameter& aURLParameter,
      87             :                                       Databases* pDatabases )
      88             :     : ResultSetBase( rxContext,xProvider,nOpenMode,seq,seqSort ),
      89           0 :       m_aURLParameter( aURLParameter )
      90             : {
      91           0 :     Reference< XExtendedTransliteration > xTrans = Transliteration::create( rxContext );
      92             :     Locale aLocale( aURLParameter.get_language(),
      93             :                     OUString(),
      94           0 :                     OUString() );
      95           0 :     xTrans->loadModule(TransliterationModules_UPPERCASE_LOWERCASE,
      96           0 :                        aLocale );
      97             : 
      98           0 :     vector< vector< OUString > > queryList;
      99             :     {
     100             :         sal_Int32 idx;
     101           0 :         OUString query = m_aURLParameter.get_query();
     102           0 :         while( !query.isEmpty() )
     103             :         {
     104           0 :             idx = query.indexOf( ' ' );
     105           0 :             if( idx == -1 )
     106           0 :                 idx = query.getLength();
     107             : 
     108           0 :             vector< OUString > currentQuery;
     109           0 :             OUString tmp(query.copy( 0,idx ));
     110           0 :             Sequence<sal_Int32> aSeq;
     111           0 :             OUString toliterate = xTrans->transliterate(
     112           0 :                 tmp,0,tmp.getLength(),aSeq);
     113             : 
     114           0 :             currentQuery.push_back( toliterate );
     115           0 :             queryList.push_back( currentQuery );
     116             : 
     117           0 :             int nCpy = 1 + idx;
     118           0 :             if( nCpy >= query.getLength() )
     119           0 :                 query.clear();
     120             :             else
     121           0 :                 query = query.copy( 1 + idx );
     122           0 :         }
     123             :     }
     124             : 
     125           0 :     vector< OUString > aCompleteResultVector;
     126           0 :     OUString scope = m_aURLParameter.get_scope();
     127           0 :     bool bCaptionsOnly = scope == "Heading";
     128           0 :     sal_Int32 hitCount = m_aURLParameter.get_hitCount();
     129             : 
     130           0 :     IndexFolderIterator aIndexFolderIt( *pDatabases, m_aURLParameter.get_module(), m_aURLParameter.get_language() );
     131           0 :     OUString idxDir;
     132           0 :     bool bExtension = false;
     133           0 :     int iDir = 0;
     134           0 :     vector< vector<HitItem>* > aIndexFolderResultVectorVector;
     135             : 
     136             :     bool bTemporary;
     137           0 :     while( !(idxDir = aIndexFolderIt.nextIndexFolder( bExtension, bTemporary )).isEmpty() )
     138             :     {
     139           0 :         vector<HitItem> aIndexFolderResultVector;
     140             : 
     141             :         try
     142             :         {
     143           0 :             vector< vector<HitItem>* > aQueryListResultVectorVector;
     144           0 :             set< OUString > aSet,aCurrent,aResultSet;
     145             : 
     146           0 :             int nQueryListSize = queryList.size();
     147           0 :             if( nQueryListSize > 1 )
     148           0 :                 hitCount = 2000;
     149             : 
     150           0 :             for( int i = 0; i < nQueryListSize; ++i )
     151             :             {
     152             :                 vector<HitItem>* pQueryResultVector;
     153           0 :                 if( nQueryListSize > 1 )
     154             :                 {
     155           0 :                     pQueryResultVector = new vector<HitItem>();
     156           0 :                     aQueryListResultVectorVector.push_back( pQueryResultVector );
     157             :                 }
     158             :                 else
     159             :                 {
     160           0 :                     pQueryResultVector = &aIndexFolderResultVector;
     161             :                 }
     162           0 :                 pQueryResultVector->reserve( hitCount );
     163             : 
     164           0 :                 const std::vector< OUString >& aListItem = queryList[i];
     165           0 :                 OUString aNewQueryStr = aListItem[0];
     166             : 
     167           0 :                 vector<float> aScoreVector;
     168           0 :                 vector<OUString> aPathVector;
     169             : 
     170             :                 try
     171             :                 {
     172           0 :                     HelpSearch searcher(idxDir);
     173           0 :                     searcher.query(aNewQueryStr, bCaptionsOnly, aPathVector, aScoreVector);
     174             :                 }
     175           0 :                 catch (CLuceneError &e)
     176             :                 {
     177             :                     SAL_WARN("xmlhelp", "CLuceneError: " << e.what());
     178             :                 }
     179             : 
     180           0 :                 if( nQueryListSize > 1 )
     181           0 :                     aSet.clear();
     182             : 
     183           0 :                 for (size_t j = 0; j < aPathVector.size(); ++j) {
     184           0 :                     pQueryResultVector->push_back(HitItem(aPathVector[j], aScoreVector[j]));
     185           0 :                     if (nQueryListSize > 1)
     186           0 :                         aSet.insert(aPathVector[j]);
     187             :                 }
     188             : 
     189             :                 // intersect
     190           0 :                 if( nQueryListSize > 1 )
     191             :                 {
     192           0 :                     if( i == 0 )
     193             :                     {
     194           0 :                         aResultSet = aSet;
     195             :                     }
     196             :                     else
     197             :                     {
     198           0 :                         aCurrent = aResultSet;
     199           0 :                         aResultSet.clear();
     200             :                         set_intersection( aSet.begin(),aSet.end(),
     201             :                                           aCurrent.begin(),aCurrent.end(),
     202           0 :                                           inserter(aResultSet,aResultSet.begin()));
     203             :                     }
     204             :                 }
     205           0 :             }
     206             : 
     207             :             // Combine results in aIndexFolderResultVector
     208           0 :             if( nQueryListSize > 1 )
     209             :             {
     210           0 :                 for( int n = 0 ; n < nQueryListSize ; ++n )
     211             :                 {
     212           0 :                     vector<HitItem>* pQueryResultVector = aQueryListResultVectorVector[n];
     213           0 :                     vector<HitItem>& rQueryResultVector = *pQueryResultVector;
     214             : 
     215           0 :                     int nItemCount = rQueryResultVector.size();
     216           0 :                     for( int i = 0 ; i < nItemCount ; ++i )
     217             :                     {
     218           0 :                         const HitItem& rItem = rQueryResultVector[ i ];
     219           0 :                         if( (aResultSet.find( rItem.m_aURL )) != aResultSet.end() )
     220             :                         {
     221           0 :                             HitItem aItemCopy( rItem );
     222           0 :                             aItemCopy.m_fScore /= nQueryListSize;   // To get average score
     223           0 :                             if( n == 0 )
     224             :                             {
     225             :                                 // Use first pass to create entry
     226           0 :                                 aIndexFolderResultVector.push_back( aItemCopy );
     227             :                             }
     228             :                             else
     229             :                             {
     230             :                                 // Find entry in vector
     231           0 :                                 int nCount = aIndexFolderResultVector.size();
     232           0 :                                 for( int j = 0 ; j < nCount ; ++j )
     233             :                                 {
     234           0 :                                     HitItem& rFindItem = aIndexFolderResultVector[ j ];
     235           0 :                                     if( rFindItem.m_aURL.equals( aItemCopy.m_aURL ) )
     236             :                                     {
     237           0 :                                         rFindItem.m_fScore += aItemCopy.m_fScore;
     238           0 :                                         break;
     239             :                                     }
     240             :                                 }
     241           0 :                             }
     242             :                         }
     243             :                     }
     244             : 
     245           0 :                     delete pQueryResultVector;
     246             :                 }
     247             : 
     248           0 :                 sort( aIndexFolderResultVector.begin(), aIndexFolderResultVector.end() );
     249             :             }
     250             : 
     251           0 :             vector<HitItem>* pIndexFolderHitItemVector = new vector<HitItem>( aIndexFolderResultVector );
     252           0 :             aIndexFolderResultVectorVector.push_back( pIndexFolderHitItemVector );
     253           0 :             aIndexFolderResultVector.clear();
     254             :         }
     255           0 :         catch (const Exception &e)
     256             :         {
     257             :             SAL_WARN("xmlhelp", "Exception: " << e.Message);
     258             :         }
     259             : 
     260           0 :         ++iDir;
     261             : 
     262           0 :         if( bTemporary )
     263           0 :             aIndexFolderIt.deleteTempIndexFolder( idxDir );
     264             : 
     265           0 :     }   // Iterator
     266             : 
     267             : 
     268           0 :     int nVectorCount = aIndexFolderResultVectorVector.size();
     269           0 :     vector<HitItem>::size_type* pCurrentVectorIndex = new vector<HitItem>::size_type[nVectorCount];
     270           0 :     for( int j = 0 ; j < nVectorCount ; ++j )
     271           0 :         pCurrentVectorIndex[j] = 0;
     272             : 
     273           0 :     sal_Int32 nTotalHitCount = m_aURLParameter.get_hitCount();
     274           0 :     sal_Int32 nHitCount = 0;
     275           0 :     while( nHitCount < nTotalHitCount )
     276             :     {
     277           0 :         int iVectorWithBestScore = -1;
     278           0 :         float fBestScore = 0.0;
     279           0 :         for( int k = 0 ; k < nVectorCount ; ++k )
     280             :         {
     281           0 :             vector<HitItem>& rIndexFolderVector = *aIndexFolderResultVectorVector[k];
     282           0 :             if( pCurrentVectorIndex[k] < rIndexFolderVector.size() )
     283             :             {
     284           0 :                 const HitItem& rItem = rIndexFolderVector[ pCurrentVectorIndex[k] ];
     285             : 
     286           0 :                 if( fBestScore < rItem.m_fScore )
     287             :                 {
     288           0 :                     fBestScore = rItem.m_fScore;
     289           0 :                     iVectorWithBestScore = k;
     290             :                 }
     291             :             }
     292             :         }
     293             : 
     294           0 :         if( iVectorWithBestScore == -1 )    // No item left at all
     295           0 :             break;
     296             : 
     297           0 :         vector<HitItem>& rIndexFolderVector = *aIndexFolderResultVectorVector[iVectorWithBestScore];
     298           0 :         const HitItem& rItem = rIndexFolderVector[ pCurrentVectorIndex[iVectorWithBestScore] ];
     299             : 
     300           0 :         pCurrentVectorIndex[iVectorWithBestScore]++;
     301             : 
     302           0 :         aCompleteResultVector.push_back( rItem.m_aURL );
     303           0 :         ++nHitCount;
     304             :     }
     305             : 
     306           0 :     delete[] pCurrentVectorIndex;
     307           0 :     for( int n = 0 ; n < nVectorCount ; ++n )
     308             :     {
     309           0 :         vector<HitItem>* pIndexFolderVector = aIndexFolderResultVectorVector[n];
     310           0 :         delete pIndexFolderVector;
     311             :     }
     312             : 
     313           0 :     sal_Int32 replIdx = OUString( "#HLP#" ).getLength();
     314           0 :     OUString replWith = "vnd.sun.star.help://";
     315             : 
     316           0 :     int nResultCount = aCompleteResultVector.size();
     317           0 :     for( int r = 0 ; r < nResultCount ; ++r )
     318             :     {
     319           0 :         OUString aURL = aCompleteResultVector[r];
     320           0 :         OUString aResultStr = replWith + aURL.copy(replIdx);
     321           0 :           m_aPath.push_back( aResultStr );
     322           0 :     }
     323             : 
     324           0 :     m_aItems.resize( m_aPath.size() );
     325           0 :     m_aIdents.resize( m_aPath.size() );
     326             : 
     327           0 :     Command aCommand;
     328           0 :     aCommand.Name = "getPropertyValues";
     329           0 :     aCommand.Argument <<= m_sProperty;
     330             : 
     331           0 :     for( m_nRow = 0; sal::static_int_cast<sal_uInt32>( m_nRow ) < m_aPath.size(); ++m_nRow )
     332             :     {
     333           0 :         m_aPath[m_nRow] =
     334           0 :             m_aPath[m_nRow] +
     335           0 :             "?Language=" +
     336           0 :             m_aURLParameter.get_language() +
     337           0 :             "&System=" +
     338           0 :             m_aURLParameter.get_system();
     339             : 
     340           0 :         uno::Reference< XContent > content = queryContent();
     341           0 :         if( content.is() )
     342             :         {
     343           0 :             uno::Reference< XCommandProcessor > cmd( content,uno::UNO_QUERY );
     344           0 :             cmd->execute( aCommand,0,uno::Reference< XCommandEnvironment >( 0 ) ) >>= m_aItems[m_nRow]; //TODO: check return value of operator >>=
     345             :         }
     346           0 :     }
     347           0 :     m_nRow = 0xffffffff;
     348           0 : }
     349             : 
     350             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11