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 "pathexpression.hxx"
28cdf0e10cSrcweir #include "unohelper.hxx"
29cdf0e10cSrcweir #include "evaluationcontext.hxx"
30cdf0e10cSrcweir #include "NameContainer.hxx"
31cdf0e10cSrcweir
32cdf0e10cSrcweir #include <com/sun/star/xml/dom/XNode.hpp>
33cdf0e10cSrcweir #include <com/sun/star/xml/dom/XNodeList.hpp>
34cdf0e10cSrcweir #include <com/sun/star/xml/dom/NodeType.hpp>
35cdf0e10cSrcweir #include <com/sun/star/xml/dom/events/XEventListener.hpp>
36cdf0e10cSrcweir #include <com/sun/star/xml/dom/events/XEventTarget.hpp>
37cdf0e10cSrcweir #include <com/sun/star/xml/xpath/XXPathObject.hpp>
38cdf0e10cSrcweir #include <com/sun/star/container/XNameContainer.hpp>
39cdf0e10cSrcweir #include <com/sun/star/uno/Sequence.hxx>
40cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
41cdf0e10cSrcweir
42cdf0e10cSrcweir #include <unotools/textsearch.hxx>
43cdf0e10cSrcweir
44cdf0e10cSrcweir #include <algorithm>
45cdf0e10cSrcweir #include <functional>
46cdf0e10cSrcweir
47cdf0e10cSrcweir
48cdf0e10cSrcweir using rtl::OUString;
49cdf0e10cSrcweir using rtl::OUStringBuffer;
50cdf0e10cSrcweir using com::sun::star::uno::Reference;
51cdf0e10cSrcweir using com::sun::star::uno::Sequence;
52cdf0e10cSrcweir using com::sun::star::xml::dom::XNode;
53cdf0e10cSrcweir using com::sun::star::xml::dom::XNodeList;
54cdf0e10cSrcweir using com::sun::star::xml::dom::events::XEventListener;
55cdf0e10cSrcweir using com::sun::star::xml::dom::events::XEventTarget;
56cdf0e10cSrcweir using com::sun::star::container::XNameContainer;
57cdf0e10cSrcweir using com::sun::star::xml::xpath::XXPathObject;
58cdf0e10cSrcweir using com::sun::star::uno::RuntimeException;
59cdf0e10cSrcweir using com::sun::star::uno::UNO_QUERY;
60cdf0e10cSrcweir using com::sun::star::uno::UNO_QUERY_THROW;
61cdf0e10cSrcweir using com::sun::star::xml::dom::NodeType_TEXT_NODE;
62cdf0e10cSrcweir using com::sun::star::xml::xpath::XPathObjectType_XPATH_UNDEFINED;
63cdf0e10cSrcweir using namespace std;
64cdf0e10cSrcweir
65cdf0e10cSrcweir
66cdf0e10cSrcweir
67cdf0e10cSrcweir
68cdf0e10cSrcweir namespace xforms
69cdf0e10cSrcweir {
70cdf0e10cSrcweir
PathExpression()71cdf0e10cSrcweir PathExpression::PathExpression()
72cdf0e10cSrcweir : ComputedExpression(),
73cdf0e10cSrcweir maNodes()
74cdf0e10cSrcweir {
75cdf0e10cSrcweir }
76cdf0e10cSrcweir
~PathExpression()77cdf0e10cSrcweir PathExpression::~PathExpression()
78cdf0e10cSrcweir {
79cdf0e10cSrcweir }
80cdf0e10cSrcweir
81cdf0e10cSrcweir
82cdf0e10cSrcweir
setExpression(const OUString & rExpression)83cdf0e10cSrcweir void PathExpression::setExpression( const OUString& rExpression )
84cdf0e10cSrcweir {
85cdf0e10cSrcweir // set new expression, and clear pre-computed results
86cdf0e10cSrcweir ComputedExpression::setExpression( rExpression );
87cdf0e10cSrcweir
88cdf0e10cSrcweir // check expression against regular expression to determine
89cdf0e10cSrcweir // whether it contains only 'simple' (i.e. static) conditions. For
90cdf0e10cSrcweir // now, we check whether it only contains number positions.
91cdf0e10cSrcweir // (TODO: Only works for names containing only ASCII letters+digits.)
92cdf0e10cSrcweir mbIsSimple =
93cdf0e10cSrcweir _checkExpression( "( */@?[a-zA-Z0-9:]+( *\\[ *[0-9 ]+ *\\] *)?)+" );
94cdf0e10cSrcweir
95cdf0e10cSrcweir maNodes.clear();
96cdf0e10cSrcweir }
97cdf0e10cSrcweir
_getExpressionForEvaluation() const98cdf0e10cSrcweir const rtl::OUString PathExpression::_getExpressionForEvaluation() const
99cdf0e10cSrcweir {
100cdf0e10cSrcweir OUString sExpr = ComputedExpression::_getExpressionForEvaluation();
101cdf0e10cSrcweir if( sExpr.getLength() == 0 )
102cdf0e10cSrcweir sExpr = OUSTRING(".");
103cdf0e10cSrcweir return sExpr;
104cdf0e10cSrcweir }
105cdf0e10cSrcweir
evaluate(const EvaluationContext & rContext)106cdf0e10cSrcweir bool PathExpression::evaluate( const EvaluationContext& rContext )
107cdf0e10cSrcweir {
108cdf0e10cSrcweir // for simple expression we don't need to re-bind (if we were bound before)
109cdf0e10cSrcweir // (we will evaluate empty expressions, since they are interpreted as ".")
110cdf0e10cSrcweir if( mxResult.is() && isSimpleExpression() )
111cdf0e10cSrcweir return true;
112cdf0e10cSrcweir
113cdf0e10cSrcweir bool bResult = _evaluate( rContext, _getExpressionForEvaluation() );
114cdf0e10cSrcweir
115cdf0e10cSrcweir // clear old result, and copy new
116cdf0e10cSrcweir maNodes.clear();
117cdf0e10cSrcweir if( mxResult.is() )
118cdf0e10cSrcweir {
119cdf0e10cSrcweir // copy node list
120cdf0e10cSrcweir Reference<XNodeList> xNodeList = mxResult->getNodeList();
121cdf0e10cSrcweir OSL_ENSURE( xNodeList.is(), "empty object (instead of empty list)" );
122cdf0e10cSrcweir sal_Int32 nLength = xNodeList.is() ? xNodeList->getLength() : 0;
123cdf0e10cSrcweir for( sal_Int32 n = 0; n < nLength; n++ )
124cdf0e10cSrcweir maNodes.push_back( xNodeList->item( n ) );
125cdf0e10cSrcweir }
126cdf0e10cSrcweir
127cdf0e10cSrcweir return bResult;
128cdf0e10cSrcweir }
129cdf0e10cSrcweir
130cdf0e10cSrcweir
getNode() const131cdf0e10cSrcweir Reference<XNode> PathExpression::getNode() const
132cdf0e10cSrcweir {
133cdf0e10cSrcweir Reference<XNode> xResult;
134cdf0e10cSrcweir if( ! maNodes.empty() )
135cdf0e10cSrcweir xResult = *maNodes.begin();
136cdf0e10cSrcweir return xResult;
137cdf0e10cSrcweir }
138cdf0e10cSrcweir
getNodeList() const139cdf0e10cSrcweir const PathExpression::NodeVector_t PathExpression::getNodeList() const
140cdf0e10cSrcweir {
141cdf0e10cSrcweir return maNodes;
142cdf0e10cSrcweir }
143cdf0e10cSrcweir
getXNodeList() const144cdf0e10cSrcweir Reference<XNodeList> PathExpression::getXNodeList() const
145cdf0e10cSrcweir {
146cdf0e10cSrcweir return mxResult.is() ? mxResult->getNodeList() : Reference<XNodeList>();
147cdf0e10cSrcweir }
148cdf0e10cSrcweir
149cdf0e10cSrcweir
150cdf0e10cSrcweir } // namespace xforms
151