2 * Copyright (c) 2002-2005 Nokia Corporation and/or its subsidiary(-ies).
4 * This component and the accompanying materials are made available
5 * under the terms of "Eclipse Public License v1.0"
6 * which accompanies this distribution, and is available
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
14 * Description: This class offers a set of utility functions for XML data
26 #ifndef SEN_XML_UTILS_H
27 #define SEN_XML_UTILS_H
31 #include <stringpool.h>
40 typedef RArray<RAttribute> RAttributeArray;
46 class CSenBaseAttribute;
52 const TInt KMaxEscapedLength = 8;
55 // Moved to SenXmlConstants.hß
57 // Five basic entities as descriptors
58 _LIT8(KSenEscapedApos, "'");
59 _LIT8(KSenEscapedDblQuot, """);
60 _LIT8(KSenEscapedGt, ">");
61 _LIT8(KSenEscapedLt, "<");
62 _LIT8(KSenEscapedAmp, "&");
64 // XML-escaping chars as descriptors
65 _LIT8(KSenAmpersandDesC8, "&");
66 _LIT8(KSenAposDesC8, "\'");
67 _LIT8(KSenDblQuotDesC8, "\"");
68 _LIT8(KSenGtDesC8, ">");
69 _LIT8(KSenLtDesC8, "<");
76 * This class offers a set of utility functions for XML data
77 * The helper methods include Unicode (UCS2) and UTF-8 encoding
78 * and decoding as well as convenience methods to encode and
79 * decode the five basic XML entities. There are functions
80 * for extracting XML prefixes and building of qualifiedname
81 * and a functionality for checking if an element name value
88 public: // New functions
90 * Helper function to convert unicode string to UTF-8 encoded.
92 * @param aUnicodeString string to be converted.
93 * @return buffer as UTF-8, caller takes ownership.
95 IMPORT_C static HBufC8* ToUtf8LC(const TDesC16& aUnicodeString);
98 * Helper function to convert UTF-8 string to unicode encoded.
100 * @param aUtf8String string to be converted.
101 * @return buffer as unicode, caller takes ownership.
103 IMPORT_C static HBufC16* ToUnicodeLC(const TDesC8& aUtf8String);
106 * Helper function to determine if a string starts with another string.
107 * @since Series60 3.0
108 * @param aDes string to be searched from.
109 * @param aPrefix the prefix to be searched.
110 * @return ETrue if given string starts with given prefix,
113 IMPORT_C static TBool StartsWith(const TDesC8& aDes,
114 const TDesC8& aPrefix);
117 * Helper function to determine if a string ends with another string.
118 * @since Series60 3.0
119 * @param aDes string to be searched from.
120 * @param aPrefix the prefix to be searched.
121 * @return ETrue if given string ends with given prefix,
124 IMPORT_C static TBool EndsWith(const TDesC8& aDes, const TDesC8& aPostfix);
127 * Helper function to find a value of a given attribute.
128 * @since Series60 3.0
129 * @param aAttributes Array which contains the attributes.
130 * @param aAttrName The name of the attribute which value is asked.
131 * @return The value of the wanted attribute, or KNullDesC8 if given
132 * attribute name was not found.
134 IMPORT_C static TPtrC8 AttrValue( const RAttributeArray& aAttributes,
135 const TDesC8& aAttrName);
138 * Get a newly allocated copy of the attribute.
139 * @since Series60 3.0
140 * @param apAttrs Array which contains the attributes.
141 * @param aAttrName The name of the attribute which value is asked.
142 * @return A buffer containing the value of the wanted attribute.
143 * Buffer is newly allocated and ownership is transferred
144 * to the caller. Can be NULL if attribute was not found.
146 IMPORT_C static HBufC8* AllocAttrValueL(const RAttributeArray& apAttrs,
147 const TDesC8& aAttrName);
150 * Helper function to construct a qualified name.
151 * @since Series60 3.0
152 * @param aPrefix the prefix to be used.
153 * @param aLocalName the local name to be used.
154 * @param aQName a ref-to-pointer which will contain the allocated
155 * qualified name. This param should be NULL when
156 * passed, otherwise memory leak will occur.
157 * Caller has the ownership of this buffer.
159 IMPORT_C static void BuildQNameL( const TDesC8& aPrefix,
160 const TDesC8& aLocalName,
164 * Encodes XML-escaping characters found from aOriginal
165 * to XML Basic Entities.
166 * Note, that aEncoded is not re-assigned IF there were NO
167 * XML-escaping characters - '&', ''', '"', '<' or '>' -found.
168 * Otherwise, when encoding has been done, the aEncoded points
169 * to newly allocated descriptor, which ownership belongs to
170 * the caller (who should have given aEncoded pointer as NULL
172 * @since Series60 3.0
173 * @param aOriginal the original descriptor.
174 * @param aEncoded a ref-to-pointer which will contain the allocated
175 * encoded string. This param should be NULL when
176 * passed, otherwise memory leak will occur.
177 * Caller has the ownership of this buffer.
179 IMPORT_C static TBool EncodeHttpCharactersL(const TDesC8& aOriginal,
182 * Method leaves if aCandidate contains illegal, XML-escaping characters.
183 * Those characters, which will cause a leave are:
184 * '&', ''', '"', '<' and '>'
185 * @since Series60 3.0
186 * @param aCandidate string to be checked.
188 * KErrSenInvalidCharacters if contains invalid characters.
190 IMPORT_C static void LeaveOnXmlEscapesL(const TDesC8& aCandidate);
193 * Method leaves if aCandidate contains illegal, XML-escaping characters
194 * or is an empty descriptor.
195 * Those characters, which will cause a leave are:
196 * '&', ''', '"', '<' and '>'
197 * @since Series60 3.0
198 * @param aCandidate string to be checked.
200 * KErrSenInvalidCharacters if contains invalid characters.
201 * KErrSenZeroLengthDescriptor if aCandidate is zero length
203 static void LeaveOnInvalidElementNameL(const TDesC8& aCandidate);
206 * Encodes XML-escaping characters found from aOriginal
207 * to XML Basic Entities.
208 * Example: '&' -> '&'
209 * Note! Function returns a copy of aOriginal descriptor,
210 * even if not a single illegal, XML-escaping character
212 * The returned pointer to heap allocated descriptor is
213 * pushed to the cleanup stack.
214 * @since Series60 3.0
215 * @param aOriginal the string to be encoded.
216 * @return a buffer containing the encoded string.
217 * Ownership is transferred to the caller.
219 IMPORT_C static HBufC8* EncodeHttpCharactersLC(const TDesC8& aOriginal);
222 * Method to provide functionality for decoding HTTP characters into
223 * XML escaping characters.
224 * @since Series60 3.0
225 * @param aOriginal the string to be decoded.
226 * @param aDecoded the buffer that will contain the decoded string
227 * on return. Caller has the ownership of this.
228 * Will be similar as aOriginal if nothing was
230 * @return ETrue if any XML escaping (some Basic Entity)
231 * character-sequence was decoded.
233 IMPORT_C static TBool DecodeHttpCharactersL(const TDesC8& aOriginal,
237 * Same as DecodeHttpCharactersL(), but the decoded string OR
238 * exact copy of the aOriginal descriptor is returned and pushed
240 * @since Series60 3.0
241 * @param aOriginal the string to be decoded.
242 * @return buffer located in the cleanup stack containing the decoded
245 IMPORT_C static HBufC8* DecodeHttpCharactersLC(const TDesC8& aOriginal);
248 * Helper function to get a prefix from a qualified name.
249 * @since Series60 3.0
250 * @param aQName the qualified name
251 * @return the namespace prefix for the element, e.g. 'soap' for
252 * 'soap:Body'. The returned pointer is empty if there is no prefix.
254 IMPORT_C static TPtrC8 NsPrefix(const TDesC8& aQName);
257 * Helper function to get a local name from a qualified name.
258 * @since Series60 3.0
259 * @param aQName the qualified name
260 * @return the local name for the element, e.g. 'Body' for
263 IMPORT_C static TPtrC8 LocalName(const TDesC8& aQName);
266 * Removes certain attribute from given element. May also be used when
267 * removing namespaces from the element.
268 * @param aElement Element from which attribute will be removed.
269 * @param aAttrName Attribute's local name
270 * @return pointer to removed attribute, which ownership is transferred to
271 * caller or NULL, if no matching attribute was found from this element.
273 IMPORT_C static CSenBaseAttribute* RemoveAttributeL(CSenElement& aElement,
274 const TDesC8& aAttrName);
277 * Removes attribute from this element.
278 * @param aElement Element from which attribute will be removed.
279 * @param apAttribute is the attribute to be removed.
280 * transferred to this element.
281 * @return pointer to removed attribute, which ownership is transferred to
282 * caller or NULL, if no matching attribute was found from this element.
284 IMPORT_C static CSenBaseAttribute* RemoveAttributeL(CSenElement& aElement,
285 CSenBaseAttribute* apAttribute);
288 * Adds an attribute into this element. Used also adding new namespaces
290 * @param aQName Attribute's qualified name
291 * @param aLocalName Attribute's local name
292 * @param aValue Attribute's value
293 * @return value of the attribute as string (TDesC&)
295 * KErrSenInvalidCharacters if aLocalName or aQName contain illegal
297 * KErrSenZeroLengthDescriptor if aLocalName or aQName is zero length.
299 IMPORT_C static const TDesC8& AddAttributeL(CSenElement& aElement,
300 const TDesC8& aQName,
301 const TDesC8& aLocalName,
302 const TDesC8& aValue);
305 * Adds an attribute into this element. Used also adding new namespaces
307 * @param aLocalName Attribute's local name
308 * @param aValue Attribute's value
309 * @return value of the attribute as string (TDesC&)
311 * KErrSenInvalidCharacters if aLocalName contains illegal characters.
312 * KErrSenZeroLengthDescriptor if aAttrName is zero length, or
313 * if the local name part of it is zero length.
315 IMPORT_C static const TDesC8& AddAttributeL(CSenElement& aElement,
316 const TDesC8& aAttrName,
317 const TDesC8& aValue);
319 * Adds an attribute into this element.
320 * @param apAttribute Attribute to be added. Ownership is transferred
322 * @return attribute value as a string (TDesC8&)
324 IMPORT_C static const TDesC8& AddAttributeL(CSenElement& aElement,
325 CSenBaseAttribute* apAttribute);
330 * C++ default constructor.
335 * Prohibit copy constructor if not deriving from CBase.
337 SenXmlUtils( const SenXmlUtils& );
340 * Prohibit assignment operator if not deriving from CBase.
342 SenXmlUtils& operator=( const SenXmlUtils& );
345 * Replaces the destination with the given values
347 static TBool ReplaceAll( TPtr8 aDestination,
352 * Finds the attribute with the given name
354 static CSenBaseAttribute* FindAttrL( CSenElement& aElement,
355 const TDesC8& aName );
358 #endif // SEN_XML_UTILS_H