epoc32/include/mw/senxmlutils.h
author William Roberts <williamr@symbian.org>
Wed, 31 Mar 2010 12:33:34 +0100
branchSymbian3
changeset 4 837f303aceeb
parent 2 2fe1408b6811
permissions -rw-r--r--
Current Symbian^3 public API header files (from PDK 3.0.h)
This is the epoc32/include tree with the "platform" subtrees removed, and
all but a selected few mbg and rsg files removed.
williamr@2
     1
/*
williamr@2
     2
* Copyright (c) 2002-2005 Nokia Corporation and/or its subsidiary(-ies). 
williamr@2
     3
* All rights reserved.
williamr@2
     4
* This component and the accompanying materials are made available
williamr@4
     5
* under the terms of "Eclipse Public License v1.0"
williamr@2
     6
* which accompanies this distribution, and is available
williamr@4
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
williamr@2
     8
*
williamr@2
     9
* Initial Contributors:
williamr@2
    10
* Nokia Corporation - initial contribution.
williamr@2
    11
*
williamr@2
    12
* Contributors:
williamr@2
    13
*
williamr@2
    14
* Description:        This class offers a set of utility functions for XML data
williamr@2
    15
*
williamr@2
    16
*/
williamr@2
    17
williamr@2
    18
williamr@2
    19
williamr@2
    20
williamr@2
    21
williamr@2
    22
williamr@2
    23
williamr@2
    24
williamr@2
    25
williamr@2
    26
#ifndef SEN_XML_UTILS_H
williamr@2
    27
#define SEN_XML_UTILS_H
williamr@2
    28
williamr@2
    29
//  INCLUDES
williamr@2
    30
#include <e32base.h>
williamr@2
    31
#include <stringpool.h>
williamr@2
    32
williamr@2
    33
williamr@2
    34
namespace Xml
williamr@2
    35
    {
williamr@2
    36
    // FORWARD DECLARES
williamr@2
    37
    class RAttribute;
williamr@2
    38
williamr@2
    39
    // TYPE DEFINITIONS
williamr@2
    40
    typedef RArray<RAttribute> RAttributeArray;
williamr@2
    41
    }
williamr@2
    42
williamr@2
    43
using namespace Xml;
williamr@2
    44
williamr@2
    45
class CSenElement;
williamr@2
    46
class CSenBaseAttribute;
williamr@2
    47
williamr@2
    48
williamr@2
    49
// CONSTANTS
williamr@2
    50
namespace
williamr@2
    51
    {
williamr@2
    52
    const TInt KMaxEscapedLength = 8;
williamr@2
    53
williamr@2
    54
/*
williamr@2
    55
    // Moved to SenXmlConstants.hß
williamr@2
    56
williamr@2
    57
    // Five basic entities as descriptors
williamr@2
    58
    _LIT8(KSenEscapedApos,     "&apos;");
williamr@2
    59
    _LIT8(KSenEscapedDblQuot,  "&quot;");
williamr@2
    60
    _LIT8(KSenEscapedGt,       "&gt;");
williamr@2
    61
    _LIT8(KSenEscapedLt,       "&lt;");
williamr@2
    62
    _LIT8(KSenEscapedAmp,      "&amp;");
williamr@2
    63
williamr@2
    64
    // XML-escaping chars as descriptors
williamr@2
    65
    _LIT8(KSenAmpersandDesC8,  "&");
williamr@2
    66
    _LIT8(KSenAposDesC8,       "\'");
williamr@2
    67
    _LIT8(KSenDblQuotDesC8,    "\"");
williamr@2
    68
    _LIT8(KSenGtDesC8,         ">");
williamr@2
    69
    _LIT8(KSenLtDesC8,         "<");
williamr@2
    70
*/    
williamr@2
    71
    }
williamr@2
    72
    
williamr@2
    73
// CLASS DECLARATION
williamr@2
    74
williamr@2
    75
/**
williamr@2
    76
*  This class offers a set of utility functions for XML data
williamr@2
    77
*  The helper methods include Unicode (UCS2) and UTF-8 encoding
williamr@2
    78
*  and decoding as well as convenience methods to encode and
williamr@2
    79
*  decode the five basic XML entities. There are functions
williamr@2
    80
*  for extracting XML prefixes and building of qualifiedname
williamr@2
    81
*  and a functionality for checking if an element name value 
williamr@2
    82
*  is illegal or not.
williamr@2
    83
*  @lib SenXML.dll
williamr@2
    84
*  @since Series60 3.0
williamr@2
    85
*/
williamr@2
    86
class SenXmlUtils
williamr@2
    87
    {
williamr@2
    88
    public: // New functions
williamr@2
    89
        /**
williamr@2
    90
         * Helper function to convert unicode string to UTF-8 encoded.
williamr@2
    91
         * @since Series60 3.0
williamr@2
    92
         * @param aUnicodeString    string to be converted.
williamr@2
    93
         * @return buffer as UTF-8, caller takes ownership.
williamr@2
    94
        */
williamr@2
    95
        IMPORT_C static HBufC8* ToUtf8LC(const TDesC16& aUnicodeString);
williamr@2
    96
williamr@2
    97
        /**
williamr@2
    98
         * Helper function to convert UTF-8 string to unicode encoded.
williamr@2
    99
         * @since Series60 3.0
williamr@2
   100
         * @param aUtf8String   string to be converted.
williamr@2
   101
         * @return buffer as unicode, caller takes ownership.
williamr@2
   102
         */
williamr@2
   103
        IMPORT_C static HBufC16* ToUnicodeLC(const TDesC8& aUtf8String);
williamr@2
   104
williamr@2
   105
        /**
williamr@2
   106
         * Helper function to determine if a string starts with another string.
williamr@2
   107
         * @since Series60 3.0
williamr@2
   108
         * @param aDes  string to be searched from.
williamr@2
   109
         * @param aPrefix   the prefix to be searched.
williamr@2
   110
         * @return ETrue if given string starts with given prefix, 
williamr@2
   111
         *          otherwise EFalse.
williamr@2
   112
         */
williamr@2
   113
        IMPORT_C static TBool StartsWith(const TDesC8& aDes,
williamr@2
   114
                                         const TDesC8& aPrefix);
williamr@2
   115
williamr@2
   116
        /**
williamr@2
   117
         * Helper function to determine if a string ends with another string.
williamr@2
   118
         * @since Series60 3.0
williamr@2
   119
         * @param aDes  string to be searched from.
williamr@2
   120
         * @param aPrefix   the prefix to be searched.
williamr@2
   121
         * @return ETrue if given string ends with given prefix, 
williamr@2
   122
         *          otherwise EFalse.
williamr@2
   123
         */
williamr@2
   124
        IMPORT_C static TBool EndsWith(const TDesC8& aDes, const TDesC8& aPostfix);
williamr@2
   125
williamr@2
   126
        /**
williamr@2
   127
         * Helper function to find a value of a given attribute.
williamr@2
   128
         * @since Series60 3.0
williamr@2
   129
         * @param aAttributes   Array which contains the attributes.
williamr@2
   130
         * @param aAttrName The name of the attribute which value is asked.
williamr@2
   131
         * @return The value of the wanted attribute, or KNullDesC8 if given
williamr@2
   132
         *          attribute name was not found.
williamr@2
   133
         */
williamr@2
   134
        IMPORT_C static TPtrC8  AttrValue(  const RAttributeArray& aAttributes,
williamr@2
   135
                                            const TDesC8& aAttrName);
williamr@2
   136
williamr@2
   137
        /**
williamr@2
   138
         * Get a newly allocated copy of the attribute.
williamr@2
   139
         * @since Series60 3.0
williamr@2
   140
         * @param apAttrs       Array which contains the attributes.
williamr@2
   141
         * @param aAttrName The name of the attribute which value is asked.
williamr@2
   142
         * @return A buffer containing the value of the wanted attribute.
williamr@2
   143
         *          Buffer is newly allocated and ownership is transferred 
williamr@2
   144
         *          to the caller. Can be NULL if attribute was not found.
williamr@2
   145
         */
williamr@2
   146
        IMPORT_C static HBufC8* AllocAttrValueL(const RAttributeArray& apAttrs,
williamr@2
   147
                                                const TDesC8& aAttrName);
williamr@2
   148
williamr@2
   149
        /**
williamr@2
   150
         * Helper function to construct a qualified name.
williamr@2
   151
         * @since Series60 3.0
williamr@2
   152
         * @param aPrefix       the prefix to be used.
williamr@2
   153
         * @param aLocalName    the local name to be used.
williamr@2
   154
         * @param   aQName      a ref-to-pointer which will contain the allocated
williamr@2
   155
         *                      qualified name. This param should be NULL when
williamr@2
   156
         *                      passed, otherwise memory leak will occur.
williamr@2
   157
         *                      Caller has the ownership of this buffer.
williamr@2
   158
         */
williamr@2
   159
        IMPORT_C static void BuildQNameL(   const TDesC8& aPrefix, 
williamr@2
   160
                                            const TDesC8& aLocalName,
williamr@2
   161
                                            HBufC8*& aQName);
williamr@2
   162
williamr@2
   163
        /**
williamr@2
   164
         * Encodes XML-escaping characters found from aOriginal 
williamr@2
   165
         * to XML Basic Entities. 
williamr@2
   166
         * Note, that aEncoded is not re-assigned IF there were NO
williamr@2
   167
         * XML-escaping characters - '&', ''', '"', '<' or '>' -found.   
williamr@2
   168
         * Otherwise, when encoding has been done, the aEncoded points
williamr@2
   169
         * to newly allocated descriptor, which ownership belongs to
williamr@2
   170
         * the caller (who should have given aEncoded pointer as NULL
williamr@2
   171
         * in any case).
williamr@2
   172
         * @since Series60 3.0
williamr@2
   173
         * @param aOriginal the original descriptor.
williamr@2
   174
         * @param aEncoded      a ref-to-pointer which will contain the allocated
williamr@2
   175
         *                      encoded string. This param should be NULL when
williamr@2
   176
         *                      passed, otherwise memory leak will occur.
williamr@2
   177
         *                      Caller has the ownership of this buffer.
williamr@2
   178
         */
williamr@2
   179
        IMPORT_C static TBool EncodeHttpCharactersL(const TDesC8& aOriginal,
williamr@2
   180
                                                    HBufC8*& aEncoded);
williamr@2
   181
        /**
williamr@2
   182
         * Method leaves if aCandidate contains illegal, XML-escaping characters.
williamr@2
   183
         * Those characters, which will cause a leave are:
williamr@2
   184
         *                           '&', ''', '"', '<' and '>'
williamr@2
   185
         * @since Series60 3.0
williamr@2
   186
         * @param   aCandidate  string to be checked.
williamr@2
   187
         * Leave codes:
williamr@2
   188
         *      KErrSenInvalidCharacters    if contains invalid characters.
williamr@2
   189
         */
williamr@2
   190
        IMPORT_C static void LeaveOnXmlEscapesL(const TDesC8& aCandidate); 
williamr@2
   191
 
williamr@2
   192
        /**
williamr@2
   193
         * Method leaves if aCandidate contains illegal, XML-escaping characters
williamr@2
   194
         * or is an empty descriptor.
williamr@2
   195
         * Those characters, which will cause a leave are:
williamr@2
   196
         *                           '&', ''', '"', '<' and '>'
williamr@2
   197
         * @since Series60 3.0
williamr@2
   198
         * @param   aCandidate  string to be checked.
williamr@2
   199
         * Leave codes:
williamr@2
   200
         *      KErrSenInvalidCharacters    if contains invalid characters.
williamr@2
   201
         *      KErrSenZeroLengthDescriptor if aCandidate is zero length
williamr@2
   202
         */
williamr@2
   203
        static void LeaveOnInvalidElementNameL(const TDesC8& aCandidate);
williamr@2
   204
        
williamr@2
   205
        /**
williamr@2
   206
         * Encodes XML-escaping characters found from aOriginal 
williamr@2
   207
         * to XML Basic Entities. 
williamr@2
   208
         *          Example: '&' -> '&amp;'
williamr@2
   209
         * Note! Function returns a copy of aOriginal descriptor,
williamr@2
   210
         * even if not a single illegal, XML-escaping character
williamr@2
   211
         * was encoded.
williamr@2
   212
         * The returned pointer to heap allocated descriptor is
williamr@2
   213
         * pushed to the cleanup stack.
williamr@2
   214
         * @since Series60 3.0
williamr@2
   215
         * @param   aOriginal   the string to be encoded.
williamr@2
   216
         * @return  a buffer containing the encoded string. 
williamr@2
   217
         *          Ownership is transferred to the caller.
williamr@2
   218
         */
williamr@2
   219
        IMPORT_C static HBufC8* EncodeHttpCharactersLC(const TDesC8& aOriginal);
williamr@2
   220
williamr@2
   221
        /**
williamr@2
   222
         * Method to provide functionality for decoding HTTP characters into
williamr@2
   223
         * XML escaping characters.
williamr@2
   224
         * @since Series60 3.0
williamr@2
   225
         * @param   aOriginal   the string to be decoded.
williamr@2
   226
         * @param   aDecoded    the buffer that will contain the decoded string
williamr@2
   227
         *                      on return. Caller has the ownership of this.
williamr@2
   228
         *                      Will be similar as aOriginal if nothing was 
williamr@2
   229
         *                      replaced.
williamr@2
   230
         * @return ETrue if any XML escaping (some Basic Entity) 
williamr@2
   231
         *                  character-sequence was decoded.
williamr@2
   232
         */
williamr@2
   233
        IMPORT_C static TBool DecodeHttpCharactersL(const TDesC8& aOriginal,
williamr@2
   234
                                                HBufC8*& aDecoded);
williamr@2
   235
        
williamr@2
   236
        /**
williamr@2
   237
         * Same as DecodeHttpCharactersL(), but the decoded string OR
williamr@2
   238
         * exact copy of the aOriginal descriptor is returned and pushed
williamr@2
   239
         * to cleanup stack.
williamr@2
   240
         * @since Series60 3.0
williamr@2
   241
         * @param   aOriginal   the string to be decoded.
williamr@2
   242
         * @return buffer located in the cleanup stack containing the decoded 
williamr@2
   243
         *          string
williamr@2
   244
         */
williamr@2
   245
        IMPORT_C static HBufC8* DecodeHttpCharactersLC(const TDesC8& aOriginal);
williamr@2
   246
williamr@2
   247
        /**
williamr@2
   248
         * Helper function to get a prefix from a qualified name.
williamr@2
   249
         * @since Series60 3.0
williamr@2
   250
         * @param   aQName  the qualified name
williamr@2
   251
         * @return the namespace prefix for the element, e.g. 'soap' for
williamr@2
   252
         *      'soap:Body'. The returned pointer is empty if there is no prefix.
williamr@2
   253
         */
williamr@2
   254
        IMPORT_C static TPtrC8 NsPrefix(const TDesC8& aQName);
williamr@2
   255
williamr@2
   256
        /**
williamr@2
   257
         *  Helper function to get a local name from a qualified name.
williamr@2
   258
         *  @since Series60 3.0
williamr@2
   259
         *  @param  aQName  the qualified name
williamr@2
   260
         *  @return the local name for the element, e.g. 'Body' for
williamr@2
   261
         *      'soap:Body'.
williamr@2
   262
         */
williamr@2
   263
        IMPORT_C static TPtrC8 LocalName(const TDesC8& aQName);
williamr@2
   264
williamr@2
   265
        /**
williamr@2
   266
        * Removes certain attribute from given element. May also be used when
williamr@2
   267
        * removing namespaces from the element.
williamr@2
   268
        * @param aElement   Element from which attribute will be removed.
williamr@2
   269
        * @param aAttrName  Attribute's local name
williamr@2
   270
        * @return pointer to removed attribute, which ownership is transferred to
williamr@2
   271
        * caller or NULL, if no matching attribute was found from this element. 
williamr@2
   272
        */
williamr@2
   273
        IMPORT_C static CSenBaseAttribute* RemoveAttributeL(CSenElement& aElement,
williamr@2
   274
                                                            const TDesC8& aAttrName);
williamr@2
   275
williamr@2
   276
        /** 
williamr@2
   277
        * Removes attribute from this element.
williamr@2
   278
        * @param aElement       Element from which attribute will be removed.
williamr@2
   279
        * @param apAttribute    is the attribute to be removed.
williamr@2
   280
        * transferred to this element.
williamr@2
   281
        * @return pointer to removed attribute, which ownership is transferred to
williamr@2
   282
        * caller or NULL, if no matching attribute was found from this element. 
williamr@2
   283
        */
williamr@2
   284
        IMPORT_C static CSenBaseAttribute* RemoveAttributeL(CSenElement& aElement,
williamr@2
   285
                                                            CSenBaseAttribute* apAttribute);
williamr@2
   286
williamr@2
   287
        /**
williamr@2
   288
        * Adds an attribute into this element. Used also adding new namespaces
williamr@2
   289
        * into the element.
williamr@2
   290
        * @param aQName     Attribute's qualified name
williamr@2
   291
        * @param aLocalName Attribute's local name
williamr@2
   292
        * @param aValue     Attribute's value
williamr@2
   293
        * @return value of the attribute as string (TDesC&)
williamr@2
   294
        * Leave codes:  
williamr@2
   295
        *       KErrSenInvalidCharacters if aLocalName or aQName contain illegal 
williamr@2
   296
        *       characters.     
williamr@2
   297
        *       KErrSenZeroLengthDescriptor if aLocalName or aQName is zero length.
williamr@2
   298
        */
williamr@2
   299
        IMPORT_C static const TDesC8& AddAttributeL(CSenElement& aElement,
williamr@2
   300
                                                    const TDesC8& aQName,
williamr@2
   301
                                                    const TDesC8& aLocalName,
williamr@2
   302
                                                    const TDesC8& aValue);
williamr@2
   303
williamr@2
   304
        /**
williamr@2
   305
        * Adds an attribute into this element. Used also adding new namespaces
williamr@2
   306
        * into the element.
williamr@2
   307
        * @param aLocalName Attribute's local name
williamr@2
   308
        * @param aValue     Attribute's value
williamr@2
   309
        * @return value of the attribute as string (TDesC&)
williamr@2
   310
        * Leave codes:  
williamr@2
   311
        *       KErrSenInvalidCharacters if aLocalName contains illegal characters.     
williamr@2
   312
        *       KErrSenZeroLengthDescriptor if aAttrName is zero length, or
williamr@2
   313
        *       if the local name part of it is zero length.
williamr@2
   314
        */
williamr@2
   315
        IMPORT_C static const TDesC8& AddAttributeL(CSenElement& aElement,
williamr@2
   316
                                                    const TDesC8& aAttrName,
williamr@2
   317
                                                    const TDesC8& aValue);
williamr@2
   318
        /** 
williamr@2
   319
        * Adds an attribute into this element.
williamr@2
   320
        * @param apAttribute    Attribute to be added. Ownership is transferred
williamr@2
   321
        *                       to this element.
williamr@2
   322
        * @return attribute value as a string (TDesC8&)
williamr@2
   323
        */
williamr@2
   324
        IMPORT_C static const TDesC8& AddAttributeL(CSenElement& aElement,
williamr@2
   325
                                                    CSenBaseAttribute* apAttribute);
williamr@2
   326
        
williamr@2
   327
    private:
williamr@2
   328
        
williamr@2
   329
        /**
williamr@2
   330
        * C++ default constructor.
williamr@2
   331
        */
williamr@2
   332
        SenXmlUtils() { }
williamr@2
   333
williamr@2
   334
        /**
williamr@2
   335
        * Prohibit copy constructor if not deriving from CBase.
williamr@2
   336
        */
williamr@2
   337
        SenXmlUtils( const SenXmlUtils& );
williamr@2
   338
        
williamr@2
   339
        /** 
williamr@2
   340
        * Prohibit assignment operator if not deriving from CBase.
williamr@2
   341
        */
williamr@2
   342
        SenXmlUtils& operator=( const SenXmlUtils& );
williamr@2
   343
williamr@2
   344
        /**
williamr@2
   345
        * Replaces the destination with the given values
williamr@2
   346
        */
williamr@2
   347
        static TBool ReplaceAll( TPtr8 aDestination, 
williamr@2
   348
                                 const TDesC8& aFrom,
williamr@2
   349
                                 const TDesC8& aTo );        
williamr@2
   350
williamr@2
   351
		/**
williamr@2
   352
		* Finds the attribute with the given name
williamr@2
   353
		*/
williamr@2
   354
        static CSenBaseAttribute* FindAttrL( CSenElement& aElement,
williamr@2
   355
                                             const TDesC8& aName );
williamr@2
   356
    };
williamr@2
   357
williamr@2
   358
#endif // SEN_XML_UTILS_H
williamr@2
   359
williamr@2
   360
// End of File
williamr@2
   361