|
1 // Copyright (c) 2006-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 utils |
|
15 // |
|
16 |
|
17 #include <xml/dom/xmlengxpathutils.h> |
|
18 #include <stdapis/libxml2/libxml2_globals.h> |
|
19 #include <stdapis/libxml2/libxml2_xpath.h> |
|
20 #include <stdapis/libxml2/libxml2_xpathinternals.h> |
|
21 #include "xmlengdomdefs.h" |
|
22 #include <xml/dom/xmlengnode.h> |
|
23 #include <xml/utils/xmlengmem.h> |
|
24 #include <xml/utils/xmlengxestrings.h> |
|
25 |
|
26 #define XPATH_OBJ (static_cast<xmlXPathObjectPtr>(iInternal)) |
|
27 |
|
28 // --------------------------------------------------------------------------- |
|
29 // Converts node to number |
|
30 // --------------------------------------------------------------------------- |
|
31 // |
|
32 EXPORT_C TReal XmlEngXPathUtils::ToNumberL( |
|
33 const TXmlEngNode& aNode ) |
|
34 { |
|
35 // construct a fake node set with one node |
|
36 TXmlEngNode node = aNode; |
|
37 xmlNodeSet nset; |
|
38 xmlNodePtr pNode = INTERNAL_NODEPTR(node); |
|
39 nset.nodeNr = 1; |
|
40 nset.nodeMax = 1; |
|
41 nset.nodeTab = &pNode; |
|
42 // |
|
43 TReal number = xmlXPathCastNodeSetToNumber(&nset); |
|
44 TEST_OOM_FLAG; |
|
45 return number; |
|
46 } |
|
47 |
|
48 // --------------------------------------------------------------------------- |
|
49 // Converts node-set to number |
|
50 // --------------------------------------------------------------------------- |
|
51 // |
|
52 EXPORT_C TReal XmlEngXPathUtils::ToNumberL( |
|
53 const RXmlEngNodeSet& aNodeSet ) |
|
54 { |
|
55 TReal number = xmlXPathCastNodeSetToNumber(INTERNAL_NODESETPTR(aNodeSet)); |
|
56 TEST_OOM_FLAG; |
|
57 return number; |
|
58 } |
|
59 |
|
60 // --------------------------------------------------------------------------- |
|
61 // Converts TBool value to number |
|
62 // --------------------------------------------------------------------------- |
|
63 // |
|
64 EXPORT_C TReal XmlEngXPathUtils::ToNumberL( |
|
65 TBool aValue ) |
|
66 { |
|
67 return aValue ? (1.0) :(0.0); |
|
68 } |
|
69 |
|
70 // --------------------------------------------------------------------------- |
|
71 // Converts string to number |
|
72 // --------------------------------------------------------------------------- |
|
73 // |
|
74 EXPORT_C TReal XmlEngXPathUtils::ToNumberL( |
|
75 const TDesC8& aString ) |
|
76 { |
|
77 xmlChar* str = xmlCharFromDesC8L(aString); |
|
78 TReal number = (TReal)xmlXPathStringEvalNumber(str); |
|
79 delete str; |
|
80 TEST_OOM_FLAG; |
|
81 return number; |
|
82 } |
|
83 |
|
84 // --------------------------------------------------------------------------- |
|
85 // Converts node to string |
|
86 // --------------------------------------------------------------------------- |
|
87 // |
|
88 EXPORT_C void XmlEngXPathUtils::ToStringL( |
|
89 const TXmlEngNode& aNode, RBuf8& aOutput) |
|
90 { |
|
91 aNode.WholeTextContentsCopyL(aOutput); |
|
92 } |
|
93 |
|
94 // --------------------------------------------------------------------------- |
|
95 // Converts node-set to string |
|
96 // --------------------------------------------------------------------------- |
|
97 // |
|
98 EXPORT_C void XmlEngXPathUtils::ToStringL( |
|
99 const RXmlEngNodeSet& aNodeSet, RBuf8& aOutput) |
|
100 { |
|
101 xmlChar* text= xmlXPathCastNodeSetToString(INTERNAL_NODESETPTR(aNodeSet)); |
|
102 OOM_IF_NULL(text); |
|
103 xmlCharAssignToRbuf8L(aOutput,text); |
|
104 } |
|
105 |
|
106 // --------------------------------------------------------------------------- |
|
107 // Converts TBool value to string |
|
108 // --------------------------------------------------------------------------- |
|
109 // |
|
110 EXPORT_C void XmlEngXPathUtils::ToStringL( |
|
111 TBool aValue, RBuf8& aOutput) |
|
112 { |
|
113 xmlChar* text = xmlStrdup((const xmlChar *)(aValue ? "true" : "false")); |
|
114 OOM_IF_NULL(text); |
|
115 xmlCharAssignToRbuf8L(aOutput,text); |
|
116 } |
|
117 |
|
118 // --------------------------------------------------------------------------- |
|
119 // Converts TReal value to string |
|
120 // --------------------------------------------------------------------------- |
|
121 // |
|
122 EXPORT_C void XmlEngXPathUtils::ToStringL( |
|
123 TReal aValue, RBuf8& aOutput ) |
|
124 { |
|
125 xmlChar* text = xmlXPathCastNumberToString(aValue); |
|
126 OOM_IF_NULL(text); |
|
127 xmlCharAssignToRbuf8L(aOutput,text); |
|
128 } |
|
129 |
|
130 // --------------------------------------------------------------------------- |
|
131 // Converts node to boolean |
|
132 // --------------------------------------------------------------------------- |
|
133 // |
|
134 EXPORT_C TBool XmlEngXPathUtils::ToBoolean( |
|
135 const TXmlEngNode& aNode ) |
|
136 { |
|
137 return aNode.NotNull(); |
|
138 } |
|
139 |
|
140 // --------------------------------------------------------------------------- |
|
141 // Converts node-set to boolean |
|
142 // --------------------------------------------------------------------------- |
|
143 // |
|
144 EXPORT_C TBool XmlEngXPathUtils::ToBoolean( |
|
145 const RXmlEngNodeSet& aNodeSet ) |
|
146 { |
|
147 return xmlXPathCastNodeSetToBoolean(INTERNAL_NODESETPTR(aNodeSet)); |
|
148 } |
|
149 |
|
150 // --------------------------------------------------------------------------- |
|
151 // Converts string to boolean |
|
152 // --------------------------------------------------------------------------- |
|
153 // |
|
154 EXPORT_C TBool XmlEngXPathUtils::ToBoolean( |
|
155 const TDesC8& aValue ) |
|
156 { |
|
157 return aValue.Length() ? 1 : 0; |
|
158 } |
|
159 |
|
160 // --------------------------------------------------------------------------- |
|
161 // Converts stringArg to boolean |
|
162 // @deprecated |
|
163 // --------------------------------------------------------------------------- |
|
164 // |
|
165 EXPORT_C TBool XmlEngXPathUtils::ToBooleanL( |
|
166 const TDesC8& aValue ) |
|
167 { |
|
168 return ToBoolean(aValue); |
|
169 } |
|
170 |
|
171 // --------------------------------------------------------------------------- |
|
172 // Converts TReal value to boolean |
|
173 // --------------------------------------------------------------------------- |
|
174 // |
|
175 EXPORT_C TBool XmlEngXPathUtils::ToBoolean( |
|
176 TReal aValue ) |
|
177 { |
|
178 return xmlXPathIsNaN(aValue) || (aValue == 0.0) ? 0 : 1; |
|
179 } |
|
180 |