1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
;
62 ComputedExpression::ComputedExpression()
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( " *" );
86 bool ComputedExpression::_checkExpression( const sal_Char
* pExpression
) const
88 assert(pExpression
&& "no expression?");
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();
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
124 mxResult
= _getXPathAPI(rContext
)->eval( rContext
.mxContextNode
,
127 catch( const Exception
& )
129 ; // ignore exception -> mxResult will be empty
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
) )
142 return _evaluate( rContext
, _getExpressionForEvaluation() );
146 bool ComputedExpression::hasValue() const
148 return mxResult
.is() &&
149 mxResult
->getObjectType() != XPathObjectType_XPATH_UNDEFINED
;
152 void ComputedExpression::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
];
191 aContext
.mxNamespaces
->getByName( *pNamePrefix
) >>= sNameURL
;
192 xXPath
->registerNS( *pNamePrefix
, sNameURL
);
196 // done, so return xXPath-object
201 } // namespace xforms
203 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */