Update git submodules
[LibreOffice.git] / forms / source / xforms / computedexpression.cxx
blob4b9b43065acd95269c81906f83bc47a2f7885ce3
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include "computedexpression.hxx"
22 #include "evaluationcontext.hxx"
24 #include <com/sun/star/uno/Sequence.hxx>
25 #include <com/sun/star/xml/xpath/XPathAPI.hpp>
26 #include <com/sun/star/xml/xpath/XPathExtension.hpp>
27 #include <com/sun/star/util/SearchAlgorithms2.hpp>
29 #include <osl/diagnose.h>
31 #include <i18nutil/searchopt.hxx>
32 #include <unotools/textsearch.hxx>
33 #include <comphelper/processfactory.hxx>
35 using namespace com::sun::star::uno;
36 using com::sun::star::xml::xpath::XPathAPI;
37 using com::sun::star::xml::xpath::XXPathAPI;
38 using com::sun::star::xml::xpath::XPathExtension;
39 using com::sun::star::xml::xpath::XXPathExtension;
40 using com::sun::star::xml::xpath::XPathObjectType_XPATH_UNDEFINED;
43 namespace xforms
46 ComputedExpression::ComputedExpression()
47 : mbIsEmpty( true ),
48 mbIsSimple( true )
52 ComputedExpression::~ComputedExpression()
57 void ComputedExpression::setExpression( const OUString& rExpression )
59 // set new expression, and clear pre-computed results
60 msExpression = rExpression;
61 mbIsEmpty = _checkExpression( " *" );
62 mbIsSimple = false;
63 mxResult.clear();
67 bool ComputedExpression::_checkExpression( const char* pExpression ) const
69 assert(pExpression && "no expression?");
71 // call RegExp engine
72 i18nutil::SearchOptions2 aSearchOptions;
73 aSearchOptions.AlgorithmType2 = css::util::SearchAlgorithms2::REGEXP;
74 aSearchOptions.searchString = OUString( pExpression, strlen(pExpression), RTL_TEXTENCODING_ASCII_US );
75 utl::TextSearch aTextSearch( aSearchOptions );
77 sal_Int32 nLength = msExpression.getLength();
78 sal_Int32 nStart = 0;
79 sal_Int32 nEnd = nLength;
80 bool bSearch = aTextSearch.SearchForward( msExpression, &nStart, &nEnd );
82 // our expression is static only if 1) we found our regexp, and 2)
83 // the regexp goes from beginning to end.
84 return ( nLength == 0 || bSearch )
85 && ( nStart == 0 && nEnd == nLength );
88 bool ComputedExpression::isSimpleExpression() const
90 // actual work is done by setExpression
91 return mbIsEmpty || mbIsSimple;
95 bool ComputedExpression::_evaluate(
96 const xforms::EvaluationContext& rContext,
97 const OUString& sExpression )
99 OSL_ENSURE( rContext.mxContextNode.is(), "no context node in context" );
101 // obtain value by evaluating XPath expression
102 mxResult.clear();
105 mxResult = _getXPathAPI(rContext)->eval( rContext.mxContextNode,
106 sExpression );
108 catch( const Exception& )
110 ; // ignore exception -> mxResult will be empty
113 return hasValue();
116 bool ComputedExpression::evaluate( const EvaluationContext& rContext )
118 // for simple expression we don't need to re-evaluate (if we have
119 // an older result); neither for empty expressions
120 if( mbIsEmpty || (mxResult.is() && mbIsSimple) )
121 return true;
123 return _evaluate( rContext, _getExpressionForEvaluation() );
127 bool ComputedExpression::hasValue() const
129 return mxResult.is() &&
130 mxResult->getObjectType() != XPathObjectType_XPATH_UNDEFINED;
133 void ComputedExpression::clear()
135 mxResult.clear();
139 OUString ComputedExpression::getString() const
141 return mxResult.is() ? mxResult->getString() : OUString();
144 bool ComputedExpression::getBool( bool bDefault ) const
146 return mxResult.is() ? mxResult->getBoolean() : bDefault;
150 Reference<XXPathAPI> ComputedExpression::_getXPathAPI(const xforms::EvaluationContext& aContext)
152 // create XPath API, then register namespaces
153 Reference<XXPathAPI> xXPath( XPathAPI::create( comphelper::getProcessComponentContext() ) );
155 // register xforms extension#
156 const Reference< XComponentContext >& aComponentContext = comphelper::getProcessComponentContext();
157 Reference< XXPathExtension > aExtension = XPathExtension::createWithModel(aComponentContext, aContext.mxModel, aContext.mxContextNode);
158 xXPath->registerExtensionInstance(aExtension);
160 // register namespaces
161 if( aContext.mxNamespaces.is() )
163 Sequence<OUString> aPrefixes =aContext.mxNamespaces->getElementNames();
164 sal_Int32 nCount = aPrefixes.getLength();
165 const OUString* pPrefixes = aPrefixes.getConstArray();
166 for( sal_Int32 i = 0; i < nCount; i++ )
168 const OUString* pNamePrefix = &pPrefixes[i];
169 OUString sNameURL;
170 aContext.mxNamespaces->getByName( *pNamePrefix ) >>= sNameURL;
171 xXPath->registerNS( *pNamePrefix, sNameURL );
175 // done, so return xXPath-object
176 return xXPath;
180 } // namespace xforms
182 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */