Update ooo320-m1
[ooovba.git] / forms / source / xforms / pathexpression.cxx
blob58601dfbaf2ca1070e0c6409b4d18173dff21606
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: pathexpression.cxx,v $
10 * $Revision: 1.7 $
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 "pathexpression.hxx"
35 #include "unohelper.hxx"
36 #include "evaluationcontext.hxx"
37 #include "NameContainer.hxx"
39 #include <com/sun/star/xml/dom/XNode.hpp>
40 #include <com/sun/star/xml/dom/XNodeList.hpp>
41 #include <com/sun/star/xml/dom/NodeType.hpp>
42 #include <com/sun/star/xml/dom/events/XEventListener.hpp>
43 #include <com/sun/star/xml/dom/events/XEventTarget.hpp>
44 #include <com/sun/star/xml/xpath/XXPathObject.hpp>
45 #include <com/sun/star/container/XNameContainer.hpp>
46 #include <com/sun/star/uno/Sequence.hxx>
47 #include <rtl/ustrbuf.hxx>
49 #include <unotools/textsearch.hxx>
51 #include <algorithm>
52 #include <functional>
55 using rtl::OUString;
56 using rtl::OUStringBuffer;
57 using com::sun::star::uno::Reference;
58 using com::sun::star::uno::Sequence;
59 using com::sun::star::xml::dom::XNode;
60 using com::sun::star::xml::dom::XNodeList;
61 using com::sun::star::xml::dom::events::XEventListener;
62 using com::sun::star::xml::dom::events::XEventTarget;
63 using com::sun::star::container::XNameContainer;
64 using com::sun::star::xml::xpath::XXPathObject;
65 using com::sun::star::uno::RuntimeException;
66 using com::sun::star::uno::UNO_QUERY;
67 using com::sun::star::uno::UNO_QUERY_THROW;
68 using com::sun::star::xml::dom::NodeType_TEXT_NODE;
69 using com::sun::star::xml::xpath::XPathObjectType_XPATH_UNDEFINED;
70 using namespace std;
75 namespace xforms
78 PathExpression::PathExpression()
79 : ComputedExpression(),
80 maNodes()
84 PathExpression::~PathExpression()
90 void PathExpression::setExpression( const OUString& rExpression )
92 // set new expression, and clear pre-computed results
93 ComputedExpression::setExpression( rExpression );
95 // check expression against regular expression to determine
96 // whether it contains only 'simple' (i.e. static) conditions. For
97 // now, we check whether it only contains number positions.
98 // (TODO: Only works for names containing only ASCII letters+digits.)
99 mbIsSimple =
100 _checkExpression( "( */@?[a-zA-Z0-9:]+( *\\[ *[0-9 ]+ *\\] *)?)+" );
102 maNodes.clear();
105 const rtl::OUString PathExpression::_getExpressionForEvaluation() const
107 OUString sExpr = ComputedExpression::_getExpressionForEvaluation();
108 if( sExpr.getLength() == 0 )
109 sExpr = OUSTRING(".");
110 return sExpr;
113 bool PathExpression::evaluate( const EvaluationContext& rContext )
115 // for simple expression we don't need to re-bind (if we were bound before)
116 // (we will evaluate empty expressions, since they are interpreted as ".")
117 if( mxResult.is() && isSimpleExpression() )
118 return true;
120 bool bResult = _evaluate( rContext, _getExpressionForEvaluation() );
122 // clear old result, and copy new
123 maNodes.clear();
124 if( mxResult.is() )
126 // copy node list
127 Reference<XNodeList> xNodeList = mxResult->getNodeList();
128 OSL_ENSURE( xNodeList.is(), "empty object (instead of empty list)" );
129 sal_Int32 nLength = xNodeList.is() ? xNodeList->getLength() : 0;
130 for( sal_Int32 n = 0; n < nLength; n++ )
131 maNodes.push_back( xNodeList->item( n ) );
134 return bResult;
138 Reference<XNode> PathExpression::getNode() const
140 Reference<XNode> xResult;
141 if( ! maNodes.empty() )
142 xResult = *maNodes.begin();
143 return xResult;
146 const PathExpression::NodeVector_t PathExpression::getNodeList() const
148 return maNodes;
151 Reference<XNodeList> PathExpression::getXNodeList() const
153 return mxResult.is() ? mxResult->getNodeList() : Reference<XNodeList>();
157 } // namespace xforms