|
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 // Methods for attribute node |
|
15 // |
|
16 |
|
17 |
|
18 #include <xml/dom/xmlengelement.h> |
|
19 #include <xml/dom/xmlengdocument.h> |
|
20 #include <stdapis/libxml2/libxml2_tree.h> |
|
21 #include "xmlengdomdefs.h" |
|
22 #include <stdapis/libxml2/libxml2_globals.h> |
|
23 #include <stdapis/libxml2/libxml2_parserinternals.h> |
|
24 #include <xml/utils/xmlengmem.h> |
|
25 #include <xml/utils/xmlengxestrings.h> |
|
26 |
|
27 #define LIBXML_ATTRIBUTE (static_cast<xmlAttrPtr>(iInternal)) |
|
28 |
|
29 // --------------------------------------------------------------------------------------------- |
|
30 // Clones attribute node |
|
31 // |
|
32 // @return A copy of the attribute with its value |
|
33 // @note |
|
34 // Namespace of the attribute is reset; use TXmlEngNode::CopyToL(TXmlEngNode), which |
|
35 // finds appropriate or creates new namespace declaration on the new |
|
36 // parent node (argument should be an TXmlEngElement handle) |
|
37 // @see CopyToL(TXmlEngNode) |
|
38 // --------------------------------------------------------------------------------------------- |
|
39 // |
|
40 EXPORT_C TXmlEngAttr TXmlEngAttr::CopyL() const |
|
41 { |
|
42 if (iInternal) |
|
43 { |
|
44 xmlAttrPtr attr = xmlCopyProp(NULL, LIBXML_ATTRIBUTE); |
|
45 OOM_IF_NULL(attr); |
|
46 TXmlEngAttr ncopy(attr); |
|
47 attr->doc = NULL; |
|
48 OwnerDocument().TakeOwnership(ncopy); |
|
49 return attr; |
|
50 } |
|
51 return NULL; |
|
52 } |
|
53 |
|
54 // --------------------------------------------------------------------------------------------- |
|
55 // @return Attribute's contents |
|
56 // |
|
57 // @note For values consisting of more then one TXmlEngTextNode node (as attribute's child) |
|
58 // returns only the begining of the value; this happens when the value is |
|
59 // represented by list of TXmlEngTextNode and TXmlEngEntityReference nodes. |
|
60 // @see IsSimpleContents(), WholeValueCopyL() |
|
61 // --------------------------------------------------------------------------------------------- |
|
62 // |
|
63 EXPORT_C TPtrC8 TXmlEngAttr::Value() const |
|
64 { |
|
65 if(LIBXML_ATTRIBUTE && LIBXML_ATTRIBUTE->children && |
|
66 LIBXML_ATTRIBUTE->children->type == XML_TEXT_NODE) |
|
67 { |
|
68 return ((TXmlEngConstString)CAST_XMLCHAR_TO_DOMSTRING(LIBXML_ATTRIBUTE->children->content)).PtrC8(); |
|
69 } |
|
70 return KNullDesC8(); // the value starts with entity reference or is NULL |
|
71 } |
|
72 |
|
73 // --------------------------------------------------------------------------------------------- |
|
74 // @return Local name of the attribute |
|
75 // |
|
76 // @note Equal to TXmlEngNode::NodeName(), but works faster. |
|
77 // |
|
78 // <b>Never call this on NULL object!</b> |
|
79 // --------------------------------------------------------------------------------------------- |
|
80 // |
|
81 EXPORT_C TPtrC8 TXmlEngAttr::Name() const |
|
82 { |
|
83 if(LIBXML_ATTRIBUTE) |
|
84 { |
|
85 return ((TXmlEngConstString)CAST_XMLCHAR_TO_DOMSTRING(LIBXML_ATTRIBUTE->name)).PtrC8(); |
|
86 } |
|
87 return KNullDesC8(); |
|
88 } |
|
89 |
|
90 // --------------------------------------------------------------------------------------------- |
|
91 // @return Compex value of the attribute, |
|
92 // probably consisting of text nodes and entity references |
|
93 // |
|
94 // Since the value may be composed from a set of TXmlEngTextNode and EntityRefernce nodes, |
|
95 // the returned result is newly allocated string, which should be freed by caller. |
|
96 // |
|
97 // <B style="color: red">BE SURE TO FREE THE RESULT STRING!!!</B> |
|
98 // |
|
99 // On Symbian: |
|
100 // @code |
|
101 // // Note the use of AllocAndFreeLC(), which converts value to |
|
102 // // UTF-16 descriptor, pushes it to the cleanup stack and |
|
103 // // DEALLOCATES string in the returned TDOMString object |
|
104 // HBufC* value = attr.WholeValueCopyL().AllocAndFreeLC(); |
|
105 // ... |
|
106 // CleanupStack::PopAndDestroy(); // value; |
|
107 // @endcode |
|
108 // |
|
109 // @see TXmlEngAttr::Value(), TXmlEngNode::Value(), |
|
110 // TXmlEngNode::IsSimpleTextContents(), TXmlEngNode::WholeTextContentsCopyL() |
|
111 // |
|
112 // @note In most cases using Value() is enough (and it needs no memory allocation). |
|
113 // Use IsSimpleTextContents() if there doubts can Value() be used or not safely. |
|
114 // --------------------------------------------------------------------------------------------- |
|
115 // |
|
116 EXPORT_C void TXmlEngAttr::WholeValueCopyL(RBuf8& aBuffer) const |
|
117 { |
|
118 if (!LIBXML_ATTRIBUTE) |
|
119 { |
|
120 User::Leave(KXmlEngErrNullNode); |
|
121 } |
|
122 // |
|
123 xmlChar* value = xmlNodeListGetString( |
|
124 LIBXML_ATTRIBUTE->doc, |
|
125 LIBXML_ATTRIBUTE->children, |
|
126 1); |
|
127 TEST_OOM_FLAG; |
|
128 xmlCharAssignToRbuf8L(aBuffer,value); |
|
129 } |
|
130 |
|
131 // ------------------------------------------------------------------------------------- |
|
132 // Sets new value of the attribute. Provided new value will be escaped as needed. |
|
133 // |
|
134 // @param aNewValue A string value for the attribute |
|
135 // |
|
136 // The new value should not contain entity references. Entity references are not expanded, |
|
137 // but used as text, because the ampersand (&) character of reference is escaped. |
|
138 // |
|
139 // @see SetRawValueL(TDOMString) |
|
140 // ------------------------------------------------------------------------------------- |
|
141 // |
|
142 EXPORT_C void TXmlEngAttr::SetValueL( |
|
143 const TDesC8& aNewValue) |
|
144 { |
|
145 if (!LIBXML_ATTRIBUTE) |
|
146 { |
|
147 User::Leave(KXmlEngErrNullNode); |
|
148 } |
|
149 // |
|
150 xmlAttrPtr prop = LIBXML_ATTRIBUTE; |
|
151 xmlNodePtr new_ch; |
|
152 if (!aNewValue.Length()) |
|
153 { |
|
154 new_ch = NULL; |
|
155 } |
|
156 else |
|
157 { |
|
158 xmlChar* value = xmlCharFromDesC8L(aNewValue); |
|
159 new_ch = xmlNewText(NULL); |
|
160 if(!new_ch) |
|
161 { |
|
162 delete value; |
|
163 OOM_HAPPENED; |
|
164 }; |
|
165 new_ch->content = value; |
|
166 new_ch->parent = reinterpret_cast<xmlNodePtr>(prop); |
|
167 new_ch->doc = prop->doc; |
|
168 } |
|
169 xmlFreeNodeList(prop->children); |
|
170 prop->children = new_ch; |
|
171 prop->last = new_ch; |
|
172 } |
|
173 |
|
174 // ------------------------------------------------------------------------------------- |
|
175 // Sets new value from escaped XML character data that may contain entity references. |
|
176 // |
|
177 // The value as if it is an escaped contents from XML file. |
|
178 // If the value contains entity references, then the resulting |
|
179 // content of the attribute is a list of TXmlEngTextNode and EntityRefeerence nodes. |
|
180 // Predefined entities are converted into characters they represent. |
|
181 // |
|
182 // @see TXmlEngAttr::SetValueL(TStringArg) |
|
183 // ------------------------------------------------------------------------------------- |
|
184 // |
|
185 EXPORT_C void TXmlEngAttr::SetEscapedValueL( |
|
186 const TDesC8& aNewValue) |
|
187 { |
|
188 if (!LIBXML_ATTRIBUTE) |
|
189 { |
|
190 User::Leave(KXmlEngErrNullNode); |
|
191 } |
|
192 // |
|
193 xmlAttrPtr prop = LIBXML_ATTRIBUTE; |
|
194 xmlNodePtr new_ch; |
|
195 if (!aNewValue.Length()) |
|
196 { |
|
197 prop->last = NULL; |
|
198 new_ch = NULL; |
|
199 } |
|
200 else |
|
201 { |
|
202 xmlChar* value = xmlCharFromDesC8L(aNewValue); |
|
203 new_ch = xmlStringGetNodeList(LIBXML_ATTRIBUTE->doc, value); |
|
204 delete value; |
|
205 TEST_OOM_FLAG; |
|
206 |
|
207 // Set parent property on all child nodes and find pointer to the last node |
|
208 xmlNodePtr tmp = new_ch; |
|
209 while (tmp) |
|
210 { |
|
211 tmp->parent = (xmlNodePtr) prop; |
|
212 if (!(tmp->next)) |
|
213 prop->last = tmp; |
|
214 tmp = tmp->next; |
|
215 } |
|
216 } |
|
217 xmlFreeNodeList(prop->children); |
|
218 prop->children = new_ch; |
|
219 } |
|
220 |
|
221 // ------------------------------------------------------------------------------------- |
|
222 // Sets new attribute value exactly as presented in the string. |
|
223 // |
|
224 // Predefined entities are not converted into characters they represent. |
|
225 // |
|
226 // @see TXmlEngAttr::SetValueL() |
|
227 // ------------------------------------------------------------------------------------- |
|
228 // |
|
229 EXPORT_C void TXmlEngAttr::SetValueNoEncL( const TDesC8& aNewValue ) |
|
230 { |
|
231 if (!LIBXML_ATTRIBUTE) |
|
232 { |
|
233 User::Leave(KXmlEngErrNullNode); |
|
234 } |
|
235 // |
|
236 xmlAttrPtr prop = LIBXML_ATTRIBUTE; |
|
237 xmlNodePtr new_ch; |
|
238 if (!aNewValue.Length()) |
|
239 { |
|
240 prop->last = NULL; |
|
241 new_ch = NULL; |
|
242 } |
|
243 else |
|
244 { |
|
245 xmlChar* value = xmlCharFromDesC8L(aNewValue); |
|
246 new_ch = xmlNewText(NULL); |
|
247 if(!new_ch) |
|
248 { |
|
249 delete value; |
|
250 OOM_HAPPENED; |
|
251 }; |
|
252 |
|
253 new_ch->name = xmlStringTextNoenc; |
|
254 new_ch->content = value; |
|
255 new_ch->parent = (xmlNodePtr) prop; |
|
256 prop->last = new_ch; |
|
257 } |
|
258 xmlFreeNodeList(prop->children); |
|
259 prop->children = new_ch; |
|
260 } |
|
261 |
|
262 // --------------------------------------------------------------------------------------------- |
|
263 // @return TXmlEngElement that contains the attribute |
|
264 // |
|
265 // Same as TXmlEngNode::ParentNode() but returns TXmlEngElement instead of TXmlEngNode |
|
266 // |
|
267 // @note Copies of attributes [TXmlEngAttr::CopyL()] and newly created attribute nodes |
|
268 // [RXmlEngDocument::CreateAttributeL(..)] do not have parent element until they are attached |
|
269 // to some element. |
|
270 // --------------------------------------------------------------------------------------------- |
|
271 // |
|
272 EXPORT_C const TXmlEngElement TXmlEngAttr::OwnerElement() const |
|
273 { |
|
274 XE_ASSERT_DEBUG(LIBXML_ATTRIBUTE); |
|
275 return TXmlEngElement(LIBXML_ATTRIBUTE->parent); |
|
276 } |
|
277 |