websrv_pub/xml_extensions_api/inc/SenXmlUtils.h
changeset 0 62f9d29f7211
equal deleted inserted replaced
-1:000000000000 0:62f9d29f7211
       
     1 /*
       
     2 * Copyright (c) 2002-2005 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     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".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:        This class offers a set of utility functions for XML data
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 
       
    23 
       
    24 
       
    25 
       
    26 #ifndef SEN_XML_UTILS_H
       
    27 #define SEN_XML_UTILS_H
       
    28 
       
    29 //  INCLUDES
       
    30 #include <e32base.h>
       
    31 #include <stringpool.h>
       
    32 
       
    33 
       
    34 namespace Xml
       
    35     {
       
    36     // FORWARD DECLARES
       
    37     class RAttribute;
       
    38 
       
    39     // TYPE DEFINITIONS
       
    40     typedef RArray<RAttribute> RAttributeArray;
       
    41     }
       
    42 
       
    43 using namespace Xml;
       
    44 
       
    45 class CSenElement;
       
    46 class CSenBaseAttribute;
       
    47 
       
    48 
       
    49 // CONSTANTS
       
    50 namespace
       
    51     {
       
    52     const TInt KMaxEscapedLength = 8;
       
    53 
       
    54 /*
       
    55     // Moved to SenXmlConstants.hß
       
    56 
       
    57     // Five basic entities as descriptors
       
    58     _LIT8(KSenEscapedApos,     "&apos;");
       
    59     _LIT8(KSenEscapedDblQuot,  "&quot;");
       
    60     _LIT8(KSenEscapedGt,       "&gt;");
       
    61     _LIT8(KSenEscapedLt,       "&lt;");
       
    62     _LIT8(KSenEscapedAmp,      "&amp;");
       
    63 
       
    64     // XML-escaping chars as descriptors
       
    65     _LIT8(KSenAmpersandDesC8,  "&");
       
    66     _LIT8(KSenAposDesC8,       "\'");
       
    67     _LIT8(KSenDblQuotDesC8,    "\"");
       
    68     _LIT8(KSenGtDesC8,         ">");
       
    69     _LIT8(KSenLtDesC8,         "<");
       
    70 */    
       
    71     }
       
    72     
       
    73 // CLASS DECLARATION
       
    74 
       
    75 /**
       
    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 
       
    82 *  is illegal or not.
       
    83 *  @lib SenXML.dll
       
    84 *  @since Series60 3.0
       
    85 */
       
    86 class SenXmlUtils
       
    87     {
       
    88     public: // New functions
       
    89         /**
       
    90          * Helper function to convert unicode string to UTF-8 encoded.
       
    91          * @since Series60 3.0
       
    92          * @param aUnicodeString    string to be converted.
       
    93          * @return buffer as UTF-8, caller takes ownership.
       
    94         */
       
    95         IMPORT_C static HBufC8* ToUtf8LC(const TDesC16& aUnicodeString);
       
    96 
       
    97         /**
       
    98          * Helper function to convert UTF-8 string to unicode encoded.
       
    99          * @since Series60 3.0
       
   100          * @param aUtf8String   string to be converted.
       
   101          * @return buffer as unicode, caller takes ownership.
       
   102          */
       
   103         IMPORT_C static HBufC16* ToUnicodeLC(const TDesC8& aUtf8String);
       
   104 
       
   105         /**
       
   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, 
       
   111          *          otherwise EFalse.
       
   112          */
       
   113         IMPORT_C static TBool StartsWith(const TDesC8& aDes,
       
   114                                          const TDesC8& aPrefix);
       
   115 
       
   116         /**
       
   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, 
       
   122          *          otherwise EFalse.
       
   123          */
       
   124         IMPORT_C static TBool EndsWith(const TDesC8& aDes, const TDesC8& aPostfix);
       
   125 
       
   126         /**
       
   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.
       
   133          */
       
   134         IMPORT_C static TPtrC8  AttrValue(  const RAttributeArray& aAttributes,
       
   135                                             const TDesC8& aAttrName);
       
   136 
       
   137         /**
       
   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.
       
   145          */
       
   146         IMPORT_C static HBufC8* AllocAttrValueL(const RAttributeArray& apAttrs,
       
   147                                                 const TDesC8& aAttrName);
       
   148 
       
   149         /**
       
   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.
       
   158          */
       
   159         IMPORT_C static void BuildQNameL(   const TDesC8& aPrefix, 
       
   160                                             const TDesC8& aLocalName,
       
   161                                             HBufC8*& aQName);
       
   162 
       
   163         /**
       
   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
       
   171          * in any case).
       
   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.
       
   178          */
       
   179         IMPORT_C static TBool EncodeHttpCharactersL(const TDesC8& aOriginal,
       
   180                                                     HBufC8*& aEncoded);
       
   181         /**
       
   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.
       
   187          * Leave codes:
       
   188          *      KErrSenInvalidCharacters    if contains invalid characters.
       
   189          */
       
   190         IMPORT_C static void LeaveOnXmlEscapesL(const TDesC8& aCandidate); 
       
   191  
       
   192         /**
       
   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.
       
   199          * Leave codes:
       
   200          *      KErrSenInvalidCharacters    if contains invalid characters.
       
   201          *      KErrSenZeroLengthDescriptor if aCandidate is zero length
       
   202          */
       
   203         static void LeaveOnInvalidElementNameL(const TDesC8& aCandidate);
       
   204         
       
   205         /**
       
   206          * Encodes XML-escaping characters found from aOriginal 
       
   207          * to XML Basic Entities. 
       
   208          *          Example: '&' -> '&amp;'
       
   209          * Note! Function returns a copy of aOriginal descriptor,
       
   210          * even if not a single illegal, XML-escaping character
       
   211          * was encoded.
       
   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.
       
   218          */
       
   219         IMPORT_C static HBufC8* EncodeHttpCharactersLC(const TDesC8& aOriginal);
       
   220 
       
   221         /**
       
   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 
       
   229          *                      replaced.
       
   230          * @return ETrue if any XML escaping (some Basic Entity) 
       
   231          *                  character-sequence was decoded.
       
   232          */
       
   233         IMPORT_C static TBool DecodeHttpCharactersL(const TDesC8& aOriginal,
       
   234                                                 HBufC8*& aDecoded);
       
   235         
       
   236         /**
       
   237          * Same as DecodeHttpCharactersL(), but the decoded string OR
       
   238          * exact copy of the aOriginal descriptor is returned and pushed
       
   239          * to cleanup stack.
       
   240          * @since Series60 3.0
       
   241          * @param   aOriginal   the string to be decoded.
       
   242          * @return buffer located in the cleanup stack containing the decoded 
       
   243          *          string
       
   244          */
       
   245         IMPORT_C static HBufC8* DecodeHttpCharactersLC(const TDesC8& aOriginal);
       
   246 
       
   247         /**
       
   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.
       
   253          */
       
   254         IMPORT_C static TPtrC8 NsPrefix(const TDesC8& aQName);
       
   255 
       
   256         /**
       
   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
       
   261          *      'soap:Body'.
       
   262          */
       
   263         IMPORT_C static TPtrC8 LocalName(const TDesC8& aQName);
       
   264 
       
   265         /**
       
   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. 
       
   272         */
       
   273         IMPORT_C static CSenBaseAttribute* RemoveAttributeL(CSenElement& aElement,
       
   274                                                             const TDesC8& aAttrName);
       
   275 
       
   276         /** 
       
   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. 
       
   283         */
       
   284         IMPORT_C static CSenBaseAttribute* RemoveAttributeL(CSenElement& aElement,
       
   285                                                             CSenBaseAttribute* apAttribute);
       
   286 
       
   287         /**
       
   288         * Adds an attribute into this element. Used also adding new namespaces
       
   289         * into the element.
       
   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&)
       
   294         * Leave codes:  
       
   295         *       KErrSenInvalidCharacters if aLocalName or aQName contain illegal 
       
   296         *       characters.     
       
   297         *       KErrSenZeroLengthDescriptor if aLocalName or aQName is zero length.
       
   298         */
       
   299         IMPORT_C static const TDesC8& AddAttributeL(CSenElement& aElement,
       
   300                                                     const TDesC8& aQName,
       
   301                                                     const TDesC8& aLocalName,
       
   302                                                     const TDesC8& aValue);
       
   303 
       
   304         /**
       
   305         * Adds an attribute into this element. Used also adding new namespaces
       
   306         * into the element.
       
   307         * @param aLocalName Attribute's local name
       
   308         * @param aValue     Attribute's value
       
   309         * @return value of the attribute as string (TDesC&)
       
   310         * Leave codes:  
       
   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.
       
   314         */
       
   315         IMPORT_C static const TDesC8& AddAttributeL(CSenElement& aElement,
       
   316                                                     const TDesC8& aAttrName,
       
   317                                                     const TDesC8& aValue);
       
   318         /** 
       
   319         * Adds an attribute into this element.
       
   320         * @param apAttribute    Attribute to be added. Ownership is transferred
       
   321         *                       to this element.
       
   322         * @return attribute value as a string (TDesC8&)
       
   323         */
       
   324         IMPORT_C static const TDesC8& AddAttributeL(CSenElement& aElement,
       
   325                                                     CSenBaseAttribute* apAttribute);
       
   326         
       
   327     private:
       
   328         
       
   329         /**
       
   330         * C++ default constructor.
       
   331         */
       
   332         SenXmlUtils() { }
       
   333 
       
   334         /**
       
   335         * Prohibit copy constructor if not deriving from CBase.
       
   336         */
       
   337         SenXmlUtils( const SenXmlUtils& );
       
   338         
       
   339         /** 
       
   340         * Prohibit assignment operator if not deriving from CBase.
       
   341         */
       
   342         SenXmlUtils& operator=( const SenXmlUtils& );
       
   343 
       
   344         /**
       
   345         * Replaces the destination with the given values
       
   346         */
       
   347         static TBool ReplaceAll( TPtr8 aDestination, 
       
   348                                  const TDesC8& aFrom,
       
   349                                  const TDesC8& aTo );        
       
   350 
       
   351 		/**
       
   352 		* Finds the attribute with the given name
       
   353 		*/
       
   354         static CSenBaseAttribute* FindAttrL( CSenElement& aElement,
       
   355                                              const TDesC8& aName );
       
   356     };
       
   357 
       
   358 #endif // SEN_XML_UTILS_H
       
   359 
       
   360 // End of File
       
   361