|
1 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // XPath evaluator functions |
|
15 // |
|
16 |
|
17 #include "xmlengdomdefs.h" |
|
18 #include <xml/dom/xmlengxpathevaluator.h> |
|
19 #include <xml/dom/xmlengxpathextensionfunction.h> |
|
20 #include <stdapis/libxml2/libxml2_xpath.h> |
|
21 #include <stdapis/libxml2/libxml2_globals.h> |
|
22 #include <xml/dom/xmlengxpatherrors.h> |
|
23 #include <xml/dom/xmlengnode.h> |
|
24 #include <xml/utils/xmlengmem.h> |
|
25 #include <xml/utils/xmlengxestrings.h> |
|
26 // Default constructor |
|
27 // -------------------------------------------------------------------------------------------- |
|
28 // |
|
29 EXPORT_C TXmlEngXPathEvaluator::TXmlEngXPathEvaluator() : iInstanceTrees(NULL) |
|
30 { |
|
31 } |
|
32 |
|
33 // -------------------------------------------------------------------------------------------- |
|
34 // Precompiles and stores an XPath expression, which is returned to caller. |
|
35 // |
|
36 // @param[in] aExpression The expression as a string. |
|
37 // @param[in] aResolver An object that can resolve of namespace prefixes into namespace URIs |
|
38 // @return Compiled XPath expression. |
|
39 // |
|
40 // May leave with KErrXPath code |
|
41 // -------------------------------------------------------------------------------------------- |
|
42 // |
|
43 EXPORT_C RXmlEngXPathExpression TXmlEngXPathEvaluator::CreateExpressionL( |
|
44 const TDesC8& aExpression, |
|
45 const MXmlEngNamespaceResolver* /* aResolver */) |
|
46 { |
|
47 xmlChar* exp = xmlCharFromDesC8L(aExpression); |
|
48 xmlXPathCompExprPtr cExpr = xmlXPathCtxtCompile(NULL, exp); |
|
49 delete exp; |
|
50 if(xmlOOMFlag()) |
|
51 { |
|
52 xmlXPathFreeCompExpr(cExpr); |
|
53 OOM_HAPPENED; |
|
54 } |
|
55 if(!cExpr) |
|
56 { |
|
57 XmlEngLeaveL(KXmlEngErrXPathSyntax); |
|
58 } |
|
59 |
|
60 return RXmlEngXPathExpression(cExpr, iInstanceTrees); |
|
61 } |
|
62 |
|
63 // -------------------------------------------------------------------------------------------- |
|
64 // Evaluates the XPath expression given as parameter. The result is returned. |
|
65 // |
|
66 // @param[in] aExpression The expression as a string. |
|
67 // @param[in] aContextNode The node relative to which the expression is evaluated. |
|
68 // @param[in] aResolver (for future use) |
|
69 // @return Result of evaluation as an RXmlEngXPathResult object. |
|
70 // May leave with KXmlEngErrXPathSyntax code for invalid XPathExpression and KXmlEngErrXPathResult |
|
71 // for undefined result of evaluation. |
|
72 // -------------------------------------------------------------------------------------------- |
|
73 // |
|
74 EXPORT_C RXmlEngXPathResult TXmlEngXPathEvaluator::EvaluateL( |
|
75 const TDesC8& aExpression, |
|
76 const TXmlEngNode aContextNode, |
|
77 const MXmlEngNamespaceResolver* /* aResolver */ ) |
|
78 { |
|
79 RXmlEngXPathExpression expr = CreateExpressionL(aExpression, NULL); |
|
80 CleanupClosePushL(expr); |
|
81 |
|
82 RXmlEngXPathResult result = expr.EvaluateL(aContextNode); |
|
83 CleanupStack::PopAndDestroy(&expr); |
|
84 return result; |
|
85 } |
|
86 |
|
87 // -------------------------------------------------------------------------------------------- |
|
88 // Does the same as #evaluate, but as a side-effect calculates the dependency list of the |
|
89 // expression. This is meant to be used by the XForms engine. |
|
90 // |
|
91 // @param[in] aExpression The expression as a string. |
|
92 // @param[in] aContextNode The node relative to which the expression is evaluated. |
|
93 // @param[in] aResolver (for future use) |
|
94 // @param[in,out] aDependents After the method has returned, contains set of nodes that |
|
95 // the expression is dependent on. |
|
96 // @return Result of evaluation as an RXmlEngXPathResult object. |
|
97 // |
|
98 // May leave with KXmlEngErrXPathResult or KXmlEngErrXPathSyntax codes. |
|
99 // -------------------------------------------------------------------------------------------- |
|
100 // |
|
101 EXPORT_C RXmlEngXPathResult TXmlEngXPathEvaluator::EvaluateWithDependenciesL( |
|
102 const TDesC8& aExpression, |
|
103 const TXmlEngNode aContextNode, |
|
104 const MXmlEngNamespaceResolver* /* aResolver */, |
|
105 RXmlEngNodeSet& aDependents ) |
|
106 { |
|
107 RXmlEngXPathExpression expr = CreateExpressionL(aExpression, NULL); |
|
108 CleanupClosePushL(expr); |
|
109 RXmlEngXPathResult result = expr.EvaluateWithDependenciesL(aContextNode, aDependents); |
|
110 CleanupStack::PopAndDestroy(&expr); |
|
111 return result; |
|
112 } |
|
113 |
|
114 // -------------------------------------------------------------------------------------------- |
|
115 // Registers the instance map to be used when evaluating XForms expressions. The map |
|
116 // contains DOM Documents and their names. For example, when evaluating the expression |
|
117 // "instance('a')" the evaluator looks up a Document that has the name "a" from the map. |
|
118 // Method does not take owership, caller has to free the map. |
|
119 // |
|
120 // @param[in] aInstanceTrees The instance map. |
|
121 // -------------------------------------------------------------------------------------------- |
|
122 // |
|
123 EXPORT_C void TXmlEngXPathEvaluator::SetInstanceMap( |
|
124 CXmlEngXFormsInstanceMap* aInstanceTrees ) |
|
125 { |
|
126 iInstanceTrees = aInstanceTrees; |
|
127 } |
|
128 |
|
129 // ----------------------------------------------------------------------------- |
|
130 // Returns the registered instance map. |
|
131 // @return pointer instance map or NULL if not registered. |
|
132 // ----------------------------------------------------------------------------- |
|
133 // |
|
134 EXPORT_C CXmlEngXFormsInstanceMap* TXmlEngXPathEvaluator::InstanceMap() |
|
135 { |
|
136 return iInstanceTrees; |
|
137 } |