bump product version to 5.0.4.1
[LibreOffice.git] / forms / source / xforms / pathexpression.cxx
blobc8273224830b5f619968307c0e93704ad8a569e9
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 "pathexpression.hxx"
22 #include "unohelper.hxx"
23 #include "evaluationcontext.hxx"
24 #include "NameContainer.hxx"
26 #include <com/sun/star/xml/dom/XNode.hpp>
27 #include <com/sun/star/xml/dom/XNodeList.hpp>
28 #include <com/sun/star/xml/dom/NodeType.hpp>
29 #include <com/sun/star/xml/dom/events/XEventListener.hpp>
30 #include <com/sun/star/xml/dom/events/XEventTarget.hpp>
31 #include <com/sun/star/xml/xpath/XXPathObject.hpp>
32 #include <com/sun/star/container/XNameContainer.hpp>
33 #include <com/sun/star/uno/Sequence.hxx>
34 #include <rtl/ustrbuf.hxx>
36 #include <unotools/textsearch.hxx>
38 #include <algorithm>
39 #include <functional>
42 using com::sun::star::uno::Reference;
43 using com::sun::star::uno::Sequence;
44 using com::sun::star::xml::dom::XNode;
45 using com::sun::star::xml::dom::XNodeList;
46 using com::sun::star::xml::dom::events::XEventListener;
47 using com::sun::star::xml::dom::events::XEventTarget;
48 using com::sun::star::container::XNameContainer;
49 using com::sun::star::xml::xpath::XXPathObject;
50 using com::sun::star::uno::RuntimeException;
51 using com::sun::star::uno::UNO_QUERY;
52 using com::sun::star::uno::UNO_QUERY_THROW;
53 using com::sun::star::xml::dom::NodeType_TEXT_NODE;
54 using com::sun::star::xml::xpath::XPathObjectType_XPATH_UNDEFINED;
55 using namespace std;
60 namespace xforms
63 PathExpression::PathExpression()
64 : ComputedExpression(),
65 maNodes()
69 PathExpression::~PathExpression()
75 void PathExpression::setExpression( const OUString& rExpression )
77 // set new expression, and clear pre-computed results
78 ComputedExpression::setExpression( rExpression );
80 // check expression against regular expression to determine
81 // whether it contains only 'simple' (i.e. static) conditions. For
82 // now, we check whether it only contains number positions.
83 // (TODO: Only works for names containing only ASCII letters+digits.)
84 mbIsSimple =
85 _checkExpression( "( */@?[a-zA-Z0-9:]+( *\\[ *[0-9 ]+ *\\] *)?)+" );
87 maNodes.clear();
90 const OUString PathExpression::_getExpressionForEvaluation() const
92 OUString sExpr = ComputedExpression::_getExpressionForEvaluation();
93 if( sExpr.isEmpty())
94 sExpr = ".";
95 return sExpr;
98 bool PathExpression::evaluate( const EvaluationContext& rContext )
100 // for simple expression we don't need to re-bind (if we were bound before)
101 // (we will evaluate empty expressions, since they are interpreted as ".")
102 if( mxResult.is() && isSimpleExpression() )
103 return true;
105 bool bResult = _evaluate( rContext, _getExpressionForEvaluation() );
107 // clear old result, and copy new
108 maNodes.clear();
109 if( mxResult.is() )
111 // copy node list
112 Reference<XNodeList> xNodeList = mxResult->getNodeList();
113 OSL_ENSURE( xNodeList.is(), "empty object (instead of empty list)" );
114 sal_Int32 nLength = xNodeList.is() ? xNodeList->getLength() : 0;
115 for( sal_Int32 n = 0; n < nLength; n++ )
116 maNodes.push_back( xNodeList->item( n ) );
119 return bResult;
123 Reference<XNode> PathExpression::getNode() const
125 Reference<XNode> xResult;
126 if( ! maNodes.empty() )
127 xResult = *maNodes.begin();
128 return xResult;
132 Reference<XNodeList> PathExpression::getXNodeList() const
134 return mxResult.is() ? mxResult->getNodeList() : Reference<XNodeList>();
138 } // namespace xforms
140 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */