Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / forms / source / xforms / computedexpression.cxx
blob96d981224c09688f68a7f651578a6e700a01dce2
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 "unohelper.hxx"
23 #include "evaluationcontext.hxx"
24 #include "NameContainer.hxx"
26 #include <com/sun/star/uno/Sequence.hxx>
27 #include <com/sun/star/xml/dom/NodeType.hpp>
28 #include <com/sun/star/xml/xpath/XPathAPI.hpp>
29 #include <com/sun/star/xml/xpath/XPathExtension.hpp>
30 #include <com/sun/star/util/SearchAlgorithms2.hpp>
32 #include <osl/diagnose.h>
34 #include <i18nutil/searchopt.hxx>
35 #include <unotools/textsearch.hxx>
36 #include <comphelper/processfactory.hxx>
38 using namespace com::sun::star::uno;
39 using com::sun::star::xml::xpath::XPathAPI;
40 using com::sun::star::xml::xpath::XXPathAPI;
41 using com::sun::star::xml::xpath::XPathExtension;
42 using com::sun::star::xml::xpath::XXPathExtension;
43 using com::sun::star::xml::xpath::XPathObjectType_XPATH_UNDEFINED;
46 namespace xforms
49 ComputedExpression::ComputedExpression()
50 : msExpression(),
51 mbIsEmpty( true ),
52 mbIsSimple( true ),
53 mxResult()
57 ComputedExpression::~ComputedExpression()
62 void ComputedExpression::setExpression( const OUString& rExpression )
64 // set new expression, and clear pre-computed results
65 msExpression = rExpression;
66 mbIsEmpty = _checkExpression( " *" );
67 mbIsSimple = false;
68 mxResult.clear();
72 bool ComputedExpression::_checkExpression( const sal_Char* pExpression ) const
74 assert(pExpression && "no expression?");
76 // call RegExp engine
77 i18nutil::SearchOptions2 aSearchOptions;
78 aSearchOptions.AlgorithmType2 = css::util::SearchAlgorithms2::REGEXP;
79 aSearchOptions.searchString = OUString( pExpression, strlen(pExpression), RTL_TEXTENCODING_ASCII_US );
80 utl::TextSearch aTextSearch( aSearchOptions );
82 sal_Int32 nLength = msExpression.getLength();
83 sal_Int32 nStart = 0;
84 sal_Int32 nEnd = nLength;
85 bool bSearch = aTextSearch.SearchForward( msExpression, &nStart, &nEnd );
87 // our expression is static only if 1) we found our regexp, and 2)
88 // the regexp goes from beginning to end.
89 return ( nLength == 0 || bSearch )
90 && ( nStart == 0 && nEnd == nLength );
93 bool ComputedExpression::isSimpleExpression() const
95 // actual work is done by setExpression
96 return mbIsEmpty || mbIsSimple;
100 bool ComputedExpression::_evaluate(
101 const xforms::EvaluationContext& rContext,
102 const OUString& sExpression )
104 OSL_ENSURE( rContext.mxContextNode.is(), "no context node in context" );
106 // obtain value by evaluating XPath expression
107 mxResult.clear();
110 mxResult = _getXPathAPI(rContext)->eval( rContext.mxContextNode,
111 sExpression );
113 catch( const Exception& )
115 ; // ignore exception -> mxResult will be empty
118 return hasValue();
121 bool ComputedExpression::evaluate( const EvaluationContext& rContext )
123 // for simple expression we don't need to re-evaluate (if we have
124 // an older result); neither for empty expressions
125 if( mbIsEmpty || (mxResult.is() && mbIsSimple) )
126 return true;
128 return _evaluate( rContext, _getExpressionForEvaluation() );
132 bool ComputedExpression::hasValue() const
134 return mxResult.is() &&
135 mxResult->getObjectType() != XPathObjectType_XPATH_UNDEFINED;
138 void ComputedExpression::clear()
140 mxResult.clear();
144 OUString ComputedExpression::getString() const
146 return mxResult.is() ? mxResult->getString() : OUString();
149 bool ComputedExpression::getBool( bool bDefault ) const
151 return mxResult.is() ? mxResult->getBoolean() : bDefault;
155 Reference<XXPathAPI> ComputedExpression::_getXPathAPI(const xforms::EvaluationContext& aContext)
157 // create XPath API, then register namespaces
158 Reference<XXPathAPI> xXPath( XPathAPI::create( comphelper::getProcessComponentContext() ) );
160 // register xforms extension#
161 Reference< XComponentContext > aComponentContext = comphelper::getProcessComponentContext();
162 Reference< XXPathExtension > aExtension = XPathExtension::createWithModel(aComponentContext, aContext.mxModel, aContext.mxContextNode);
163 xXPath->registerExtensionInstance(aExtension);
165 // register namespaces
166 if( aContext.mxNamespaces.is() )
168 Sequence<OUString> aPrefixes =aContext.mxNamespaces->getElementNames();
169 sal_Int32 nCount = aPrefixes.getLength();
170 const OUString* pPrefixes = aPrefixes.getConstArray();
171 for( sal_Int32 i = 0; i < nCount; i++ )
173 const OUString* pNamePrefix = &pPrefixes[i];
174 OUString sNameURL;
175 aContext.mxNamespaces->getByName( *pNamePrefix ) >>= sNameURL;
176 xXPath->registerNS( *pNamePrefix, sNameURL );
180 // done, so return xXPath-object
181 return xXPath;
185 } // namespace xforms
187 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */