1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_forms.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "pathexpression.hxx"
32*cdf0e10cSrcweir #include "unohelper.hxx"
33*cdf0e10cSrcweir #include "evaluationcontext.hxx"
34*cdf0e10cSrcweir #include "NameContainer.hxx"
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir #include <com/sun/star/xml/dom/XNode.hpp>
37*cdf0e10cSrcweir #include <com/sun/star/xml/dom/XNodeList.hpp>
38*cdf0e10cSrcweir #include <com/sun/star/xml/dom/NodeType.hpp>
39*cdf0e10cSrcweir #include <com/sun/star/xml/dom/events/XEventListener.hpp>
40*cdf0e10cSrcweir #include <com/sun/star/xml/dom/events/XEventTarget.hpp>
41*cdf0e10cSrcweir #include <com/sun/star/xml/xpath/XXPathObject.hpp>
42*cdf0e10cSrcweir #include <com/sun/star/container/XNameContainer.hpp>
43*cdf0e10cSrcweir #include <com/sun/star/uno/Sequence.hxx>
44*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir #include <unotools/textsearch.hxx>
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir #include <algorithm>
49*cdf0e10cSrcweir #include <functional>
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir using rtl::OUString;
53*cdf0e10cSrcweir using rtl::OUStringBuffer;
54*cdf0e10cSrcweir using com::sun::star::uno::Reference;
55*cdf0e10cSrcweir using com::sun::star::uno::Sequence;
56*cdf0e10cSrcweir using com::sun::star::xml::dom::XNode;
57*cdf0e10cSrcweir using com::sun::star::xml::dom::XNodeList;
58*cdf0e10cSrcweir using com::sun::star::xml::dom::events::XEventListener;
59*cdf0e10cSrcweir using com::sun::star::xml::dom::events::XEventTarget;
60*cdf0e10cSrcweir using com::sun::star::container::XNameContainer;
61*cdf0e10cSrcweir using com::sun::star::xml::xpath::XXPathObject;
62*cdf0e10cSrcweir using com::sun::star::uno::RuntimeException;
63*cdf0e10cSrcweir using com::sun::star::uno::UNO_QUERY;
64*cdf0e10cSrcweir using com::sun::star::uno::UNO_QUERY_THROW;
65*cdf0e10cSrcweir using com::sun::star::xml::dom::NodeType_TEXT_NODE;
66*cdf0e10cSrcweir using com::sun::star::xml::xpath::XPathObjectType_XPATH_UNDEFINED;
67*cdf0e10cSrcweir using namespace std;
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir namespace xforms
73*cdf0e10cSrcweir {
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir PathExpression::PathExpression()
76*cdf0e10cSrcweir     : ComputedExpression(),
77*cdf0e10cSrcweir       maNodes()
78*cdf0e10cSrcweir {
79*cdf0e10cSrcweir }
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir PathExpression::~PathExpression()
82*cdf0e10cSrcweir {
83*cdf0e10cSrcweir }
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir void PathExpression::setExpression( const OUString& rExpression )
88*cdf0e10cSrcweir {
89*cdf0e10cSrcweir     // set new expression, and clear pre-computed results
90*cdf0e10cSrcweir     ComputedExpression::setExpression( rExpression );
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir     // check expression against regular expression to determine
93*cdf0e10cSrcweir     // whether it contains only 'simple' (i.e. static) conditions. For
94*cdf0e10cSrcweir     // now, we check whether it only contains number positions.
95*cdf0e10cSrcweir     // (TODO: Only works for names containing only ASCII letters+digits.)
96*cdf0e10cSrcweir     mbIsSimple =
97*cdf0e10cSrcweir         _checkExpression( "( */@?[a-zA-Z0-9:]+( *\\[ *[0-9 ]+ *\\] *)?)+" );
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir     maNodes.clear();
100*cdf0e10cSrcweir }
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir const rtl::OUString PathExpression::_getExpressionForEvaluation() const
103*cdf0e10cSrcweir {
104*cdf0e10cSrcweir     OUString sExpr = ComputedExpression::_getExpressionForEvaluation();
105*cdf0e10cSrcweir     if( sExpr.getLength() == 0 )
106*cdf0e10cSrcweir         sExpr = OUSTRING(".");
107*cdf0e10cSrcweir     return sExpr;
108*cdf0e10cSrcweir }
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir bool PathExpression::evaluate( const EvaluationContext& rContext )
111*cdf0e10cSrcweir {
112*cdf0e10cSrcweir     // for simple expression we don't need to re-bind (if we were bound before)
113*cdf0e10cSrcweir     // (we will evaluate empty expressions, since they are interpreted as ".")
114*cdf0e10cSrcweir     if( mxResult.is() && isSimpleExpression() )
115*cdf0e10cSrcweir         return true;
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir     bool bResult = _evaluate( rContext, _getExpressionForEvaluation() );
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir     // clear old result, and copy new
120*cdf0e10cSrcweir     maNodes.clear();
121*cdf0e10cSrcweir     if( mxResult.is() )
122*cdf0e10cSrcweir     {
123*cdf0e10cSrcweir         // copy node list
124*cdf0e10cSrcweir         Reference<XNodeList> xNodeList = mxResult->getNodeList();
125*cdf0e10cSrcweir         OSL_ENSURE( xNodeList.is(), "empty object (instead of empty list)" );
126*cdf0e10cSrcweir         sal_Int32 nLength = xNodeList.is() ? xNodeList->getLength() : 0;
127*cdf0e10cSrcweir         for( sal_Int32 n = 0; n < nLength; n++ )
128*cdf0e10cSrcweir             maNodes.push_back( xNodeList->item( n ) );
129*cdf0e10cSrcweir     }
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir     return bResult;
132*cdf0e10cSrcweir }
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir Reference<XNode> PathExpression::getNode() const
136*cdf0e10cSrcweir {
137*cdf0e10cSrcweir     Reference<XNode> xResult;
138*cdf0e10cSrcweir     if( ! maNodes.empty() )
139*cdf0e10cSrcweir         xResult = *maNodes.begin();
140*cdf0e10cSrcweir     return xResult;
141*cdf0e10cSrcweir }
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir const PathExpression::NodeVector_t PathExpression::getNodeList() const
144*cdf0e10cSrcweir {
145*cdf0e10cSrcweir     return maNodes;
146*cdf0e10cSrcweir }
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir Reference<XNodeList> PathExpression::getXNodeList() const
149*cdf0e10cSrcweir {
150*cdf0e10cSrcweir     return mxResult.is() ? mxResult->getNodeList() : Reference<XNodeList>();
151*cdf0e10cSrcweir }
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir } // namespace xforms
155