update dev300-m58
[ooovba.git] / forms / source / xforms / computedexpression.cxx
blob15287379c784a4df47e91060da7e51a15a149dc9
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: computedexpression.cxx,v $
10 * $Revision: 1.8 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_forms.hxx"
34 #include "computedexpression.hxx"
35 #include "unohelper.hxx"
36 #include "evaluationcontext.hxx"
37 #include "NameContainer.hxx"
39 #include <com/sun/star/container/XNameContainer.hpp>
40 #include <com/sun/star/uno/Sequence.hxx>
41 #include <com/sun/star/xml/dom/NodeType.hpp>
42 #include <com/sun/star/xml/dom/XNode.hpp>
43 #include <com/sun/star/xml/xpath/XXPathAPI.hpp>
44 #include <com/sun/star/xml/xpath/XXPathObject.hpp>
45 #include <com/sun/star/xml/xpath/XXPathExtension.hpp>
46 #include <com/sun/star/beans/NamedValue.hpp>
47 #include <com/sun/star/lang/XInitialization.hpp>
48 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
49 #include <com/sun/star/util/SearchAlgorithms.hpp>
51 #include <unotools/textsearch.hxx>
52 #include <comphelper/processfactory.hxx>
54 using rtl::OUString;
55 using com::sun::star::beans::NamedValue;
56 using com::sun::star::uno::Any;
57 using com::sun::star::uno::Reference;
58 using com::sun::star::uno::Sequence;
59 using com::sun::star::lang::XInitialization;
60 using com::sun::star::lang::XMultiServiceFactory;
61 using com::sun::star::xml::dom::XNode;
62 using com::sun::star::container::XNameContainer;
63 using com::sun::star::xml::xpath::XXPathAPI;
64 using com::sun::star::xml::xpath::XXPathExtension;
65 using com::sun::star::xml::xpath::XXPathObject;
66 using com::sun::star::uno::RuntimeException;
67 using com::sun::star::uno::Exception;
68 using com::sun::star::uno::UNO_QUERY_THROW;
69 using com::sun::star::xml::xpath::XPathObjectType_XPATH_UNDEFINED;
70 using com::sun::star::util::SearchOptions;
71 using com::sun::star::util::SearchAlgorithms_REGEXP;
74 namespace xforms
77 ComputedExpression::ComputedExpression()
78 : msExpression(),
79 mbIsEmpty( true ),
80 mbIsSimple( true ),
81 mxResult()
85 ComputedExpression::~ComputedExpression()
90 OUString ComputedExpression::getExpression() const
92 return msExpression;
95 void ComputedExpression::setExpression( const OUString& rExpression )
97 // set new expression, and clear pre-computed results
98 msExpression = rExpression;
99 mbIsEmpty = _checkExpression( " *" );
100 mbIsSimple = false;
101 mxResult.clear();
105 bool ComputedExpression::_checkExpression( const sal_Char* pExpression ) const
107 OSL_ENSURE( pExpression != NULL, "no expression?" );
109 // call RegExp engine
110 SearchOptions aSearchOptions;
111 aSearchOptions.algorithmType = SearchAlgorithms_REGEXP;
112 aSearchOptions.searchString = String( pExpression, RTL_TEXTENCODING_ASCII_US );
113 utl::TextSearch aTextSearch( aSearchOptions );
115 xub_StrLen nLength =
116 static_cast<xub_StrLen>( msExpression.getLength() );
117 xub_StrLen nStart = 0;
118 xub_StrLen nEnd = nLength;
119 int nSearch = aTextSearch.SearchFrwrd( msExpression, &nStart, &nEnd );
121 // our expression is static only if 1) we found our regexp, and 2)
122 // the regexp goes from beginning to end.
123 return ( nLength == 0 || nSearch != 0 )
124 && ( nStart == 0 && nEnd == nLength );
127 /// do we have an actual expression?
128 bool ComputedExpression::isEmptyExpression() const
130 return mbIsEmpty;
133 bool ComputedExpression::isSimpleExpression() const
135 // actual work is done by setExpression
136 return mbIsEmpty || mbIsSimple;
140 const OUString ComputedExpression::_getExpressionForEvaluation() const
142 // the default implementation is to do nothing...
143 return msExpression;
146 bool ComputedExpression::_evaluate(
147 const xforms::EvaluationContext& rContext,
148 const OUString& sExpression )
150 OSL_ENSURE( rContext.mxContextNode.is(), "no context node in context" );
152 // obtain value by evaluating XPath expression
153 mxResult.clear();
156 mxResult = _getXPathAPI(rContext)->eval( rContext.mxContextNode,
157 sExpression );
159 catch( const Exception& )
161 ; // ignore exception -> mxResult will be empty
164 return hasValue();
167 bool ComputedExpression::evaluate( const EvaluationContext& rContext )
169 // for simple expression we don't need to re-evaluate (if we have
170 // an older result); neither for empty expressions
171 if( mbIsEmpty || (mxResult.is() && mbIsSimple) )
172 return true;
174 return _evaluate( rContext, _getExpressionForEvaluation() );
178 bool ComputedExpression::hasValue() const
180 return mxResult.is() &&
181 mxResult->getObjectType() != XPathObjectType_XPATH_UNDEFINED;
184 void ComputedExpression::clear()
186 mxResult.clear();
189 Reference<XXPathObject> ComputedExpression::getXPath()
191 return mxResult;
194 OUString ComputedExpression::getString( const rtl::OUString& rDefault ) const
196 return mxResult.is() ? mxResult->getString() : rDefault;
199 bool ComputedExpression::getBool( bool bDefault ) const
201 return mxResult.is() ? mxResult->getBoolean() : bDefault;
207 Reference<XXPathAPI> ComputedExpression::_getXPathAPI(const xforms::EvaluationContext& aContext)
209 // create XPath API, then register namespaces
210 Reference<XXPathAPI> xXPath( createInstance(
211 OUSTRING( "com.sun.star.xml.xpath.XPathAPI" ) ),
212 UNO_QUERY_THROW );
213 OSL_ENSURE( xXPath.is(), "cannot get XPath API" );
215 // register xforms extension#
216 Sequence< Any > aSequence(2);
217 NamedValue aValue;
218 aValue.Name = OUSTRING("Model");
219 aValue.Value <<= aContext.mxModel;
220 aSequence[0] <<= aValue;
221 aValue.Name = OUSTRING("ContextNode");
222 aValue.Value <<= aContext.mxContextNode;
223 aSequence[1] <<= aValue;
224 Reference<XMultiServiceFactory> aFactory = comphelper::getProcessServiceFactory();
225 Reference< XXPathExtension > aExtension( aFactory->createInstanceWithArguments(
226 OUSTRING( "com.sun.star.comp.xml.xpath.XFormsExtension"), aSequence), UNO_QUERY_THROW);
227 xXPath->registerExtensionInstance(aExtension);
229 // register namespaces
230 if( aContext.mxNamespaces.is() )
232 Sequence<OUString> aPrefixes =aContext.mxNamespaces->getElementNames();
233 sal_Int32 nCount = aPrefixes.getLength();
234 const OUString* pPrefixes = aPrefixes.getConstArray();
235 for( sal_Int32 i = 0; i < nCount; i++ )
237 const OUString* pNamePrefix = &pPrefixes[i];
238 OUString sNameURL;
239 aContext.mxNamespaces->getByName( *pNamePrefix ) >>= sNameURL;
240 xXPath->registerNS( *pNamePrefix, sNameURL );
244 // done, so return xXPath-object
245 return xXPath;
249 } // namespace xforms