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