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