LCOV - code coverage report
Current view: top level - forms/source/xforms - computedexpression.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 76 0.0 %
Date: 2012-08-25 Functions: 0 16 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 114 0.0 %

           Branch data     Line data    Source code
       1                 :            : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2                 :            : /*************************************************************************
       3                 :            :  *
       4                 :            :  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       5                 :            :  *
       6                 :            :  * Copyright 2000, 2010 Oracle and/or its affiliates.
       7                 :            :  *
       8                 :            :  * OpenOffice.org - a multi-platform office productivity suite
       9                 :            :  *
      10                 :            :  * This file is part of OpenOffice.org.
      11                 :            :  *
      12                 :            :  * OpenOffice.org is free software: you can redistribute it and/or modify
      13                 :            :  * it under the terms of the GNU Lesser General Public License version 3
      14                 :            :  * only, as published by the Free Software Foundation.
      15                 :            :  *
      16                 :            :  * OpenOffice.org is distributed in the hope that it will be useful,
      17                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      18                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      19                 :            :  * GNU Lesser General Public License version 3 for more details
      20                 :            :  * (a copy is included in the LICENSE file that accompanied this code).
      21                 :            :  *
      22                 :            :  * You should have received a copy of the GNU Lesser General Public License
      23                 :            :  * version 3 along with OpenOffice.org.  If not, see
      24                 :            :  * <http://www.openoffice.org/license.html>
      25                 :            :  * for a copy of the LGPLv3 License.
      26                 :            :  *
      27                 :            :  ************************************************************************/
      28                 :            : 
      29                 :            : 
      30                 :            : #include "computedexpression.hxx"
      31                 :            : #include "unohelper.hxx"
      32                 :            : #include "evaluationcontext.hxx"
      33                 :            : #include "NameContainer.hxx"
      34                 :            : 
      35                 :            : #include <com/sun/star/container/XNameContainer.hpp>
      36                 :            : #include <com/sun/star/uno/Sequence.hxx>
      37                 :            : #include <com/sun/star/xml/dom/NodeType.hpp>
      38                 :            : #include <com/sun/star/xml/dom/XNode.hpp>
      39                 :            : #include <com/sun/star/xml/xpath/XXPathAPI.hpp>
      40                 :            : #include <com/sun/star/xml/xpath/XXPathObject.hpp>
      41                 :            : #include <com/sun/star/xml/xpath/XXPathExtension.hpp>
      42                 :            : #include <com/sun/star/beans/NamedValue.hpp>
      43                 :            : #include <com/sun/star/lang/XInitialization.hpp>
      44                 :            : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      45                 :            : #include <com/sun/star/util/SearchAlgorithms.hpp>
      46                 :            : 
      47                 :            : #include <unotools/textsearch.hxx>
      48                 :            : #include <comphelper/processfactory.hxx>
      49                 :            : 
      50                 :            : using rtl::OUString;
      51                 :            : using com::sun::star::beans::NamedValue;
      52                 :            : using com::sun::star::uno::Any;
      53                 :            : using com::sun::star::uno::Reference;
      54                 :            : using com::sun::star::uno::Sequence;
      55                 :            : using com::sun::star::lang::XInitialization;
      56                 :            : using com::sun::star::lang::XMultiServiceFactory;
      57                 :            : using com::sun::star::xml::dom::XNode;
      58                 :            : using com::sun::star::container::XNameContainer;
      59                 :            : using com::sun::star::xml::xpath::XXPathAPI;
      60                 :            : using com::sun::star::xml::xpath::XXPathExtension;
      61                 :            : using com::sun::star::xml::xpath::XXPathObject;
      62                 :            : using com::sun::star::uno::RuntimeException;
      63                 :            : using com::sun::star::uno::Exception;
      64                 :            : using com::sun::star::uno::UNO_QUERY_THROW;
      65                 :            : using com::sun::star::xml::xpath::XPathObjectType_XPATH_UNDEFINED;
      66                 :            : using com::sun::star::util::SearchOptions;
      67                 :            : using com::sun::star::util::SearchAlgorithms_REGEXP;
      68                 :            : 
      69                 :            : 
      70                 :            : namespace xforms
      71                 :            : {
      72                 :            : 
      73                 :          0 : ComputedExpression::ComputedExpression()
      74                 :            :     : msExpression(),
      75                 :            :       mbIsEmpty( true ),
      76                 :            :       mbIsSimple( true ),
      77                 :          0 :       mxResult()
      78                 :            : {
      79                 :          0 : }
      80                 :            : 
      81                 :          0 : ComputedExpression::~ComputedExpression()
      82                 :            : {
      83                 :          0 : }
      84                 :            : 
      85                 :            : 
      86                 :          0 : OUString ComputedExpression::getExpression() const
      87                 :            : {
      88                 :          0 :     return msExpression;
      89                 :            : }
      90                 :            : 
      91                 :          0 : void ComputedExpression::setExpression( const OUString& rExpression )
      92                 :            : {
      93                 :            :     // set new expression, and clear pre-computed results
      94                 :          0 :     msExpression = rExpression;
      95                 :          0 :     mbIsEmpty = _checkExpression( " *" );
      96                 :          0 :     mbIsSimple = false;
      97                 :          0 :     mxResult.clear();
      98                 :          0 : }
      99                 :            : 
     100                 :            : 
     101                 :          0 : bool ComputedExpression::_checkExpression( const sal_Char* pExpression ) const
     102                 :            : {
     103                 :            :     OSL_ENSURE( pExpression != NULL, "no expression?" );
     104                 :            : 
     105                 :            :     // call RegExp engine
     106                 :          0 :     SearchOptions aSearchOptions;
     107                 :          0 :     aSearchOptions.algorithmType = SearchAlgorithms_REGEXP;
     108 [ #  # ][ #  # ]:          0 :     aSearchOptions.searchString = String( pExpression, RTL_TEXTENCODING_ASCII_US );
                 [ #  # ]
     109         [ #  # ]:          0 :     utl::TextSearch aTextSearch( aSearchOptions );
     110                 :            : 
     111                 :            :     xub_StrLen nLength =
     112                 :          0 :         static_cast<xub_StrLen>( msExpression.getLength() );
     113                 :          0 :     xub_StrLen nStart = 0;
     114                 :          0 :     xub_StrLen nEnd = nLength;
     115 [ #  # ][ #  # ]:          0 :     int nSearch = aTextSearch.SearchFrwrd( msExpression, &nStart, &nEnd );
                 [ #  # ]
     116                 :            : 
     117                 :            :     // our expression is static only if 1) we found our regexp, and 2)
     118                 :            :     // the regexp goes from beginning to end.
     119                 :            :     return ( nLength == 0  ||  nSearch != 0 )
     120 [ #  # ][ #  # ]:          0 :         && ( nStart == 0  &&  nEnd == nLength );
         [ #  # ][ #  # ]
                 [ #  # ]
     121                 :            : }
     122                 :            : 
     123                 :            : /// do we have an actual expression?
     124                 :          0 : bool ComputedExpression::isEmptyExpression() const
     125                 :            : {
     126                 :          0 :     return mbIsEmpty;
     127                 :            : }
     128                 :            : 
     129                 :          0 : bool ComputedExpression::isSimpleExpression() const
     130                 :            : {
     131                 :            :     // actual work is done by setExpression
     132 [ #  # ][ #  # ]:          0 :     return mbIsEmpty || mbIsSimple;
     133                 :            : }
     134                 :            : 
     135                 :            : 
     136                 :          0 : const OUString ComputedExpression::_getExpressionForEvaluation() const
     137                 :            : {
     138                 :            :     // the default implementation is to do nothing...
     139                 :          0 :     return msExpression;
     140                 :            : }
     141                 :            : 
     142                 :          0 : bool ComputedExpression::_evaluate(
     143                 :            :     const xforms::EvaluationContext& rContext,
     144                 :            :     const OUString& sExpression )
     145                 :            : {
     146                 :            :     OSL_ENSURE( rContext.mxContextNode.is(), "no context node in context" );
     147                 :            : 
     148                 :            :     // obtain value by evaluating XPath expression
     149                 :          0 :     mxResult.clear();
     150                 :            :     try
     151                 :            :     {
     152   [ #  #  #  # ]:          0 :         mxResult = _getXPathAPI(rContext)->eval( rContext.mxContextNode,
                 [ #  # ]
     153 [ #  # ][ #  # ]:          0 :                                                  sExpression );
     154                 :            :     }
     155                 :          0 :     catch( const Exception& )
     156                 :            :     {
     157                 :            :         ; // ignore exception -> mxResult will be empty
     158                 :            :     }
     159                 :            : 
     160                 :          0 :     return hasValue();
     161                 :            : }
     162                 :            : 
     163                 :          0 : bool ComputedExpression::evaluate( const EvaluationContext& rContext )
     164                 :            : {
     165                 :            :     // for simple expression we don't need to re-evaluate (if we have
     166                 :            :     // an older result); neither for empty expressions
     167 [ #  # ][ #  # ]:          0 :     if( mbIsEmpty || (mxResult.is() && mbIsSimple) )
         [ #  # ][ #  # ]
     168                 :          0 :         return true;
     169                 :            : 
     170         [ #  # ]:          0 :     return _evaluate( rContext, _getExpressionForEvaluation() );
     171                 :            : }
     172                 :            : 
     173                 :            : 
     174                 :          0 : bool ComputedExpression::hasValue() const
     175                 :            : {
     176                 :          0 :     return mxResult.is() &&
     177 [ #  # ][ #  # ]:          0 :            mxResult->getObjectType() != XPathObjectType_XPATH_UNDEFINED;
     178                 :            : }
     179                 :            : 
     180                 :          0 : void ComputedExpression::clear()
     181                 :            : {
     182                 :          0 :     mxResult.clear();
     183                 :          0 : }
     184                 :            : 
     185                 :          0 : Reference<XXPathObject> ComputedExpression::getXPath() const
     186                 :            : {
     187                 :          0 :     return mxResult;
     188                 :            : }
     189                 :            : 
     190                 :          0 : OUString ComputedExpression::getString( const rtl::OUString& rDefault ) const
     191                 :            : {
     192         [ #  # ]:          0 :     return mxResult.is() ? mxResult->getString() : rDefault;
     193                 :            : }
     194                 :            : 
     195                 :          0 : bool ComputedExpression::getBool( bool bDefault ) const
     196                 :            : {
     197         [ #  # ]:          0 :     return mxResult.is() ? mxResult->getBoolean() : bDefault;
     198                 :            : }
     199                 :            : 
     200                 :            : 
     201                 :            : 
     202                 :            : 
     203                 :          0 : Reference<XXPathAPI> ComputedExpression::_getXPathAPI(const xforms::EvaluationContext& aContext)
     204                 :            : {
     205                 :            :     // create XPath API, then register namespaces
     206                 :            :     Reference<XXPathAPI> xXPath( createInstance(
     207                 :            :                             OUSTRING( "com.sun.star.xml.xpath.XPathAPI" ) ),
     208 [ #  # ][ #  # ]:          0 :                                  UNO_QUERY_THROW );
                 [ #  # ]
     209                 :            :     OSL_ENSURE( xXPath.is(), "cannot get XPath API" );
     210                 :            : 
     211                 :            :     // register xforms extension#
     212         [ #  # ]:          0 :     Sequence< Any > aSequence(2);
     213                 :          0 :     NamedValue aValue;
     214         [ #  # ]:          0 :     aValue.Name = OUSTRING("Model");
     215         [ #  # ]:          0 :     aValue.Value <<= aContext.mxModel;
     216 [ #  # ][ #  # ]:          0 :     aSequence[0] <<= aValue;
     217         [ #  # ]:          0 :     aValue.Name = OUSTRING("ContextNode");
     218         [ #  # ]:          0 :     aValue.Value <<= aContext.mxContextNode;
     219 [ #  # ][ #  # ]:          0 :     aSequence[1] <<= aValue;
     220         [ #  # ]:          0 :     Reference<XMultiServiceFactory> aFactory = comphelper::getProcessServiceFactory();
     221         [ #  # ]:          0 :     Reference< XXPathExtension > aExtension( aFactory->createInstanceWithArguments(
     222 [ #  # ][ #  # ]:          0 :         OUSTRING( "com.sun.star.comp.xml.xpath.XFormsExtension"), aSequence), UNO_QUERY_THROW);
                 [ #  # ]
     223 [ #  # ][ #  # ]:          0 :     xXPath->registerExtensionInstance(aExtension);
     224                 :            : 
     225                 :            :     // register namespaces
     226         [ #  # ]:          0 :     if( aContext.mxNamespaces.is() )
     227                 :            :     {
     228 [ #  # ][ #  # ]:          0 :         Sequence<OUString> aPrefixes =aContext.mxNamespaces->getElementNames();
     229                 :          0 :         sal_Int32 nCount = aPrefixes.getLength();
     230                 :          0 :         const OUString* pPrefixes = aPrefixes.getConstArray();
     231         [ #  # ]:          0 :         for( sal_Int32 i = 0; i < nCount; i++ )
     232                 :            :         {
     233                 :          0 :             const OUString* pNamePrefix = &pPrefixes[i];
     234                 :          0 :             OUString sNameURL;
     235 [ #  # ][ #  # ]:          0 :             aContext.mxNamespaces->getByName( *pNamePrefix ) >>= sNameURL;
     236 [ #  # ][ #  # ]:          0 :             xXPath->registerNS( *pNamePrefix, sNameURL );
     237         [ #  # ]:          0 :         }
     238                 :            :     }
     239                 :            : 
     240                 :            :     // done, so return xXPath-object
     241         [ #  # ]:          0 :     return xXPath;
     242                 :            : }
     243                 :            : 
     244                 :            : 
     245                 :            : } // namespace xforms
     246                 :            : 
     247                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10