bump product version to 5.0.4.1
[LibreOffice.git] / forms / source / xforms / computedexpression.cxx
blobd51e71f8bed9edcf46e9d99459d0ce95331d150d
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/container/XNameContainer.hpp>
27 #include <com/sun/star/uno/Sequence.hxx>
28 #include <com/sun/star/xml/dom/NodeType.hpp>
29 #include <com/sun/star/xml/dom/XNode.hpp>
30 #include <com/sun/star/xml/xpath/XPathAPI.hpp>
31 #include <com/sun/star/xml/xpath/XXPathObject.hpp>
32 #include <com/sun/star/xml/xpath/XPathExtension.hpp>
33 #include <com/sun/star/beans/NamedValue.hpp>
34 #include <com/sun/star/lang/XInitialization.hpp>
35 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
36 #include <com/sun/star/util/SearchAlgorithms.hpp>
38 #include <osl/diagnose.h>
40 #include <unotools/textsearch.hxx>
41 #include <comphelper/processfactory.hxx>
43 using com::sun::star::beans::NamedValue;
44 using namespace com::sun::star::uno;
45 using com::sun::star::lang::XInitialization;
46 using com::sun::star::lang::XMultiServiceFactory;
47 using com::sun::star::xml::dom::XNode;
48 using com::sun::star::container::XNameContainer;
49 using com::sun::star::xml::xpath::XPathAPI;
50 using com::sun::star::xml::xpath::XXPathAPI;
51 using com::sun::star::xml::xpath::XPathExtension;
52 using com::sun::star::xml::xpath::XXPathExtension;
53 using com::sun::star::xml::xpath::XXPathObject;
54 using com::sun::star::xml::xpath::XPathObjectType_XPATH_UNDEFINED;
55 using com::sun::star::util::SearchOptions;
56 using com::sun::star::util::SearchAlgorithms_REGEXP;
59 namespace xforms
62 ComputedExpression::ComputedExpression()
63 : msExpression(),
64 mbIsEmpty( true ),
65 mbIsSimple( true ),
66 mxResult()
70 ComputedExpression::~ComputedExpression()
76 void ComputedExpression::setExpression( const OUString& rExpression )
78 // set new expression, and clear pre-computed results
79 msExpression = rExpression;
80 mbIsEmpty = _checkExpression( " *" );
81 mbIsSimple = false;
82 mxResult.clear();
86 bool ComputedExpression::_checkExpression( const sal_Char* pExpression ) const
88 assert(pExpression && "no expression?");
90 // call RegExp engine
91 SearchOptions aSearchOptions;
92 aSearchOptions.algorithmType = SearchAlgorithms_REGEXP;
93 aSearchOptions.searchString = OUString( pExpression, strlen(pExpression), RTL_TEXTENCODING_ASCII_US );
94 utl::TextSearch aTextSearch( aSearchOptions );
96 sal_Int32 nLength = msExpression.getLength();
97 sal_Int32 nStart = 0;
98 sal_Int32 nEnd = nLength;
99 bool nSearch = aTextSearch.SearchForward( msExpression, &nStart, &nEnd );
101 // our expression is static only if 1) we found our regexp, and 2)
102 // the regexp goes from beginning to end.
103 return ( nLength == 0 || nSearch )
104 && ( nStart == 0 && nEnd == nLength );
107 bool ComputedExpression::isSimpleExpression() const
109 // actual work is done by setExpression
110 return mbIsEmpty || mbIsSimple;
114 bool ComputedExpression::_evaluate(
115 const xforms::EvaluationContext& rContext,
116 const OUString& sExpression )
118 OSL_ENSURE( rContext.mxContextNode.is(), "no context node in context" );
120 // obtain value by evaluating XPath expression
121 mxResult.clear();
124 mxResult = _getXPathAPI(rContext)->eval( rContext.mxContextNode,
125 sExpression );
127 catch( const Exception& )
129 ; // ignore exception -> mxResult will be empty
132 return hasValue();
135 bool ComputedExpression::evaluate( const EvaluationContext& rContext )
137 // for simple expression we don't need to re-evaluate (if we have
138 // an older result); neither for empty expressions
139 if( mbIsEmpty || (mxResult.is() && mbIsSimple) )
140 return true;
142 return _evaluate( rContext, _getExpressionForEvaluation() );
146 bool ComputedExpression::hasValue() const
148 return mxResult.is() &&
149 mxResult->getObjectType() != XPathObjectType_XPATH_UNDEFINED;
152 void ComputedExpression::clear()
154 mxResult.clear();
158 OUString ComputedExpression::getString( const OUString& rDefault ) const
160 return mxResult.is() ? mxResult->getString() : rDefault;
163 bool ComputedExpression::getBool( bool bDefault ) const
165 return mxResult.is() ? mxResult->getBoolean() : bDefault;
171 Reference<XXPathAPI> ComputedExpression::_getXPathAPI(const xforms::EvaluationContext& aContext)
173 // create XPath API, then register namespaces
174 Reference<XXPathAPI> xXPath( XPathAPI::create( comphelper::getProcessComponentContext() ) );
176 // register xforms extension#
177 Reference< XComponentContext > aComponentContext = comphelper::getProcessComponentContext();
178 Reference< XXPathExtension > aExtension = XPathExtension::createWithModel(aComponentContext, aContext.mxModel, aContext.mxContextNode);
179 xXPath->registerExtensionInstance(aExtension);
181 // register namespaces
182 if( aContext.mxNamespaces.is() )
184 Sequence<OUString> aPrefixes =aContext.mxNamespaces->getElementNames();
185 sal_Int32 nCount = aPrefixes.getLength();
186 const OUString* pPrefixes = aPrefixes.getConstArray();
187 for( sal_Int32 i = 0; i < nCount; i++ )
189 const OUString* pNamePrefix = &pPrefixes[i];
190 OUString sNameURL;
191 aContext.mxNamespaces->getByName( *pNamePrefix ) >>= sNameURL;
192 xXPath->registerNS( *pNamePrefix, sNameURL );
196 // done, so return xXPath-object
197 return xXPath;
201 } // namespace xforms
203 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */