1*24acc546SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*24acc546SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*24acc546SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*24acc546SAndrew Rist  * distributed with this work for additional information
6*24acc546SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*24acc546SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*24acc546SAndrew Rist  * "License"); you may not use this file except in compliance
9*24acc546SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*24acc546SAndrew Rist  *
11*24acc546SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*24acc546SAndrew Rist  *
13*24acc546SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*24acc546SAndrew Rist  * software distributed under the License is distributed on an
15*24acc546SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*24acc546SAndrew Rist  * KIND, either express or implied.  See the License for the
17*24acc546SAndrew Rist  * specific language governing permissions and limitations
18*24acc546SAndrew Rist  * under the License.
19*24acc546SAndrew Rist  *
20*24acc546SAndrew Rist  *************************************************************/
21*24acc546SAndrew Rist 
22*24acc546SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_forms.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "computedexpression.hxx"
28cdf0e10cSrcweir #include "unohelper.hxx"
29cdf0e10cSrcweir #include "evaluationcontext.hxx"
30cdf0e10cSrcweir #include "NameContainer.hxx"
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include <com/sun/star/container/XNameContainer.hpp>
33cdf0e10cSrcweir #include <com/sun/star/uno/Sequence.hxx>
34cdf0e10cSrcweir #include <com/sun/star/xml/dom/NodeType.hpp>
35cdf0e10cSrcweir #include <com/sun/star/xml/dom/XNode.hpp>
36cdf0e10cSrcweir #include <com/sun/star/xml/xpath/XXPathAPI.hpp>
37cdf0e10cSrcweir #include <com/sun/star/xml/xpath/XXPathObject.hpp>
38cdf0e10cSrcweir #include <com/sun/star/xml/xpath/XXPathExtension.hpp>
39cdf0e10cSrcweir #include <com/sun/star/beans/NamedValue.hpp>
40cdf0e10cSrcweir #include <com/sun/star/lang/XInitialization.hpp>
41cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
42cdf0e10cSrcweir #include <com/sun/star/util/SearchAlgorithms.hpp>
43cdf0e10cSrcweir 
44cdf0e10cSrcweir #include <unotools/textsearch.hxx>
45cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
46cdf0e10cSrcweir 
47cdf0e10cSrcweir using rtl::OUString;
48cdf0e10cSrcweir using com::sun::star::beans::NamedValue;
49cdf0e10cSrcweir using com::sun::star::uno::Any;
50cdf0e10cSrcweir using com::sun::star::uno::Reference;
51cdf0e10cSrcweir using com::sun::star::uno::Sequence;
52cdf0e10cSrcweir using com::sun::star::lang::XInitialization;
53cdf0e10cSrcweir using com::sun::star::lang::XMultiServiceFactory;
54cdf0e10cSrcweir using com::sun::star::xml::dom::XNode;
55cdf0e10cSrcweir using com::sun::star::container::XNameContainer;
56cdf0e10cSrcweir using com::sun::star::xml::xpath::XXPathAPI;
57cdf0e10cSrcweir using com::sun::star::xml::xpath::XXPathExtension;
58cdf0e10cSrcweir using com::sun::star::xml::xpath::XXPathObject;
59cdf0e10cSrcweir using com::sun::star::uno::RuntimeException;
60cdf0e10cSrcweir using com::sun::star::uno::Exception;
61cdf0e10cSrcweir using com::sun::star::uno::UNO_QUERY_THROW;
62cdf0e10cSrcweir using com::sun::star::xml::xpath::XPathObjectType_XPATH_UNDEFINED;
63cdf0e10cSrcweir using com::sun::star::util::SearchOptions;
64cdf0e10cSrcweir using com::sun::star::util::SearchAlgorithms_REGEXP;
65cdf0e10cSrcweir 
66cdf0e10cSrcweir 
67cdf0e10cSrcweir namespace xforms
68cdf0e10cSrcweir {
69cdf0e10cSrcweir 
ComputedExpression()70cdf0e10cSrcweir ComputedExpression::ComputedExpression()
71cdf0e10cSrcweir     : msExpression(),
72cdf0e10cSrcweir       mbIsEmpty( true ),
73cdf0e10cSrcweir       mbIsSimple( true ),
74cdf0e10cSrcweir       mxResult()
75cdf0e10cSrcweir {
76cdf0e10cSrcweir }
77cdf0e10cSrcweir 
~ComputedExpression()78cdf0e10cSrcweir ComputedExpression::~ComputedExpression()
79cdf0e10cSrcweir {
80cdf0e10cSrcweir }
81cdf0e10cSrcweir 
82cdf0e10cSrcweir 
getExpression() const83cdf0e10cSrcweir OUString ComputedExpression::getExpression() const
84cdf0e10cSrcweir {
85cdf0e10cSrcweir     return msExpression;
86cdf0e10cSrcweir }
87cdf0e10cSrcweir 
setExpression(const OUString & rExpression)88cdf0e10cSrcweir void ComputedExpression::setExpression( const OUString& rExpression )
89cdf0e10cSrcweir {
90cdf0e10cSrcweir     // set new expression, and clear pre-computed results
91cdf0e10cSrcweir     msExpression = rExpression;
92cdf0e10cSrcweir     mbIsEmpty = _checkExpression( " *" );
93cdf0e10cSrcweir     mbIsSimple = false;
94cdf0e10cSrcweir     mxResult.clear();
95cdf0e10cSrcweir }
96cdf0e10cSrcweir 
97cdf0e10cSrcweir 
_checkExpression(const sal_Char * pExpression) const98cdf0e10cSrcweir bool ComputedExpression::_checkExpression( const sal_Char* pExpression ) const
99cdf0e10cSrcweir {
100cdf0e10cSrcweir     OSL_ENSURE( pExpression != NULL, "no expression?" );
101cdf0e10cSrcweir 
102cdf0e10cSrcweir     // call RegExp engine
103cdf0e10cSrcweir     SearchOptions aSearchOptions;
104cdf0e10cSrcweir     aSearchOptions.algorithmType = SearchAlgorithms_REGEXP;
105cdf0e10cSrcweir     aSearchOptions.searchString = String( pExpression, RTL_TEXTENCODING_ASCII_US );
106cdf0e10cSrcweir     utl::TextSearch aTextSearch( aSearchOptions );
107cdf0e10cSrcweir 
108cdf0e10cSrcweir     xub_StrLen nLength =
109cdf0e10cSrcweir         static_cast<xub_StrLen>( msExpression.getLength() );
110cdf0e10cSrcweir     xub_StrLen nStart = 0;
111cdf0e10cSrcweir     xub_StrLen nEnd = nLength;
112cdf0e10cSrcweir     int nSearch = aTextSearch.SearchFrwrd( msExpression, &nStart, &nEnd );
113cdf0e10cSrcweir 
114cdf0e10cSrcweir     // our expression is static only if 1) we found our regexp, and 2)
115cdf0e10cSrcweir     // the regexp goes from beginning to end.
116cdf0e10cSrcweir     return ( nLength == 0  ||  nSearch != 0 )
117cdf0e10cSrcweir         && ( nStart == 0  &&  nEnd == nLength );
118cdf0e10cSrcweir }
119cdf0e10cSrcweir 
120cdf0e10cSrcweir /// do we have an actual expression?
isEmptyExpression() const121cdf0e10cSrcweir bool ComputedExpression::isEmptyExpression() const
122cdf0e10cSrcweir {
123cdf0e10cSrcweir     return mbIsEmpty;
124cdf0e10cSrcweir }
125cdf0e10cSrcweir 
isSimpleExpression() const126cdf0e10cSrcweir bool ComputedExpression::isSimpleExpression() const
127cdf0e10cSrcweir {
128cdf0e10cSrcweir     // actual work is done by setExpression
129cdf0e10cSrcweir     return mbIsEmpty || mbIsSimple;
130cdf0e10cSrcweir }
131cdf0e10cSrcweir 
132cdf0e10cSrcweir 
_getExpressionForEvaluation() const133cdf0e10cSrcweir const OUString ComputedExpression::_getExpressionForEvaluation() const
134cdf0e10cSrcweir {
135cdf0e10cSrcweir     // the default implementation is to do nothing...
136cdf0e10cSrcweir     return msExpression;
137cdf0e10cSrcweir }
138cdf0e10cSrcweir 
_evaluate(const xforms::EvaluationContext & rContext,const OUString & sExpression)139cdf0e10cSrcweir bool ComputedExpression::_evaluate(
140cdf0e10cSrcweir     const xforms::EvaluationContext& rContext,
141cdf0e10cSrcweir     const OUString& sExpression )
142cdf0e10cSrcweir {
143cdf0e10cSrcweir     OSL_ENSURE( rContext.mxContextNode.is(), "no context node in context" );
144cdf0e10cSrcweir 
145cdf0e10cSrcweir     // obtain value by evaluating XPath expression
146cdf0e10cSrcweir     mxResult.clear();
147cdf0e10cSrcweir     try
148cdf0e10cSrcweir     {
149cdf0e10cSrcweir         mxResult = _getXPathAPI(rContext)->eval( rContext.mxContextNode,
150cdf0e10cSrcweir                                                  sExpression );
151cdf0e10cSrcweir     }
152cdf0e10cSrcweir     catch( const Exception& )
153cdf0e10cSrcweir     {
154cdf0e10cSrcweir         ; // ignore exception -> mxResult will be empty
155cdf0e10cSrcweir     }
156cdf0e10cSrcweir 
157cdf0e10cSrcweir     return hasValue();
158cdf0e10cSrcweir }
159cdf0e10cSrcweir 
evaluate(const EvaluationContext & rContext)160cdf0e10cSrcweir bool ComputedExpression::evaluate( const EvaluationContext& rContext )
161cdf0e10cSrcweir {
162cdf0e10cSrcweir     // for simple expression we don't need to re-evaluate (if we have
163cdf0e10cSrcweir     // an older result); neither for empty expressions
164cdf0e10cSrcweir     if( mbIsEmpty || (mxResult.is() && mbIsSimple) )
165cdf0e10cSrcweir         return true;
166cdf0e10cSrcweir 
167cdf0e10cSrcweir     return _evaluate( rContext, _getExpressionForEvaluation() );
168cdf0e10cSrcweir }
169cdf0e10cSrcweir 
170cdf0e10cSrcweir 
hasValue() const171cdf0e10cSrcweir bool ComputedExpression::hasValue() const
172cdf0e10cSrcweir {
173cdf0e10cSrcweir     return mxResult.is() &&
174cdf0e10cSrcweir            mxResult->getObjectType() != XPathObjectType_XPATH_UNDEFINED;
175cdf0e10cSrcweir }
176cdf0e10cSrcweir 
clear()177cdf0e10cSrcweir void ComputedExpression::clear()
178cdf0e10cSrcweir {
179cdf0e10cSrcweir     mxResult.clear();
180cdf0e10cSrcweir }
181cdf0e10cSrcweir 
getXPath()182cdf0e10cSrcweir Reference<XXPathObject> ComputedExpression::getXPath()
183cdf0e10cSrcweir {
184cdf0e10cSrcweir 	return mxResult;
185cdf0e10cSrcweir }
186cdf0e10cSrcweir 
getString(const rtl::OUString & rDefault) const187cdf0e10cSrcweir OUString ComputedExpression::getString( const rtl::OUString& rDefault ) const
188cdf0e10cSrcweir {
189cdf0e10cSrcweir     return mxResult.is() ? mxResult->getString() : rDefault;
190cdf0e10cSrcweir }
191cdf0e10cSrcweir 
getBool(bool bDefault) const192cdf0e10cSrcweir bool ComputedExpression::getBool( bool bDefault ) const
193cdf0e10cSrcweir {
194cdf0e10cSrcweir     return mxResult.is() ? mxResult->getBoolean() : bDefault;
195cdf0e10cSrcweir }
196cdf0e10cSrcweir 
197cdf0e10cSrcweir 
198cdf0e10cSrcweir 
199cdf0e10cSrcweir 
_getXPathAPI(const xforms::EvaluationContext & aContext)200cdf0e10cSrcweir Reference<XXPathAPI> ComputedExpression::_getXPathAPI(const xforms::EvaluationContext& aContext)
201cdf0e10cSrcweir {
202cdf0e10cSrcweir     // create XPath API, then register namespaces
203cdf0e10cSrcweir     Reference<XXPathAPI> xXPath( createInstance(
204cdf0e10cSrcweir                             OUSTRING( "com.sun.star.xml.xpath.XPathAPI" ) ),
205cdf0e10cSrcweir                                  UNO_QUERY_THROW );
206cdf0e10cSrcweir     OSL_ENSURE( xXPath.is(), "cannot get XPath API" );
207cdf0e10cSrcweir 
208cdf0e10cSrcweir     // register xforms extension#
209cdf0e10cSrcweir     Sequence< Any > aSequence(2);
210cdf0e10cSrcweir     NamedValue aValue;
211cdf0e10cSrcweir     aValue.Name = OUSTRING("Model");
212cdf0e10cSrcweir     aValue.Value <<= aContext.mxModel;
213cdf0e10cSrcweir     aSequence[0] <<= aValue;
214cdf0e10cSrcweir     aValue.Name = OUSTRING("ContextNode");
215cdf0e10cSrcweir     aValue.Value <<= aContext.mxContextNode;
216cdf0e10cSrcweir     aSequence[1] <<= aValue;
217cdf0e10cSrcweir     Reference<XMultiServiceFactory> aFactory = comphelper::getProcessServiceFactory();
218cdf0e10cSrcweir     Reference< XXPathExtension > aExtension( aFactory->createInstanceWithArguments(
219cdf0e10cSrcweir         OUSTRING( "com.sun.star.comp.xml.xpath.XFormsExtension"), aSequence), UNO_QUERY_THROW);
220cdf0e10cSrcweir     xXPath->registerExtensionInstance(aExtension);
221cdf0e10cSrcweir 
222cdf0e10cSrcweir     // register namespaces
223cdf0e10cSrcweir     if( aContext.mxNamespaces.is() )
224cdf0e10cSrcweir     {
225cdf0e10cSrcweir         Sequence<OUString> aPrefixes =aContext.mxNamespaces->getElementNames();
226cdf0e10cSrcweir         sal_Int32 nCount = aPrefixes.getLength();
227cdf0e10cSrcweir         const OUString* pPrefixes = aPrefixes.getConstArray();
228cdf0e10cSrcweir         for( sal_Int32 i = 0; i < nCount; i++ )
229cdf0e10cSrcweir         {
230cdf0e10cSrcweir             const OUString* pNamePrefix = &pPrefixes[i];
231cdf0e10cSrcweir             OUString sNameURL;
232cdf0e10cSrcweir             aContext.mxNamespaces->getByName( *pNamePrefix ) >>= sNameURL;
233cdf0e10cSrcweir             xXPath->registerNS( *pNamePrefix, sNameURL );
234cdf0e10cSrcweir         }
235cdf0e10cSrcweir     }
236cdf0e10cSrcweir 
237cdf0e10cSrcweir     // done, so return xXPath-object
238cdf0e10cSrcweir     return xXPath;
239cdf0e10cSrcweir }
240cdf0e10cSrcweir 
241cdf0e10cSrcweir 
242cdf0e10cSrcweir } // namespace xforms
243