upnpavcontroller/upnpxmlparser/src/upnpxmlstringutility.cpp
changeset 0 7f85d04be362
equal deleted inserted replaced
-1:000000000000 0:7f85d04be362
       
     1 /*
       
     2 * Copyright (c) 2008 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:      Utility for string operations in UpnpXmlParser component
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 
       
    23 // INCLUDE FILES
       
    24 #include <e32base.h>
       
    25 #include "upnpxmlstringutility.h"
       
    26 
       
    27 _LIT( KComponentLogfile, "upnpxmlstringutility.txt" );
       
    28 #include "upnplog.h"
       
    29 
       
    30 
       
    31 // CONSTANTS
       
    32 /**
       
    33  * Range of control characters taken from http:
       
    34  * //www.w3.org/International/questions/qa-controls. 
       
    35  * Remove characters that belong to "C0" range U+0000-U+001F 
       
    36  * (0-31) and also DEL (Delete) U+007F (127). C1 range U+0080-U+009F 
       
    37  * (128-159) is ignored for now because it seems to contain ok 
       
    38  * characters like € (euro sign).
       
    39  * */
       
    40 
       
    41 const TInt KC0RangeStart = 0;
       
    42 const TInt KC0RangeEnd = 31;
       
    43 const TInt KDeleteChar = 127;
       
    44 
       
    45 // All control characters start with &#
       
    46 _LIT8( KXmlControlCharStart, "&#" );
       
    47 // All xml entities end with ; because they are of for &entityname;
       
    48 _LIT8( KXmlEntityEnd, ";" );
       
    49 
       
    50 // ============================ LOCAL FUNCTIONS =============================
       
    51 
       
    52 // --------------------------------------------------------------------------
       
    53 // UpnpXmlStringUtility::RemoveXmlControlCharactersL
       
    54 //---------------------------------------------------------------------------
       
    55 EXPORT_C HBufC8* UpnpXmlStringUtility::RemoveXmlControlCharactersL(
       
    56     const TDesC8& aXmlData )
       
    57     {
       
    58     __LOG( "UpnpXmlStringUtility::RemoveXmlControlCharactersL Begin" );
       
    59     HBufC8* filtrateBuf = NULL;
       
    60     //if no data
       
    61     if ( aXmlData.Length() != 0 )
       
    62         {
       
    63         //The offset of the KXmlControlCharStart from the beginning of \
       
    64           this descriptor's data
       
    65         
       
    66         TInt controlCharStartOffset = aXmlData.Find( KXmlControlCharStart );
       
    67         // to make sure the data contain control character.
       
    68         if ( controlCharStartOffset != KErrNotFound )
       
    69             {
       
    70             filtrateBuf = aXmlData.AllocL();
       
    71             CleanupStack::PushL( filtrateBuf );    
       
    72             //The offset of the KXmlEntityEnd from the beginning of this \
       
    73               descriptor's data
       
    74             TInt controlCharEndOffset = 0;
       
    75             //The offset of the buffer who not be checked
       
    76             TInt partofBufferOffset = 0;
       
    77    
       
    78             //circle find control character.
       
    79             while( controlCharStartOffset != KErrNotFound )
       
    80                 {
       
    81                 controlCharStartOffset += partofBufferOffset;
       
    82                 partofBufferOffset = controlCharStartOffset;
       
    83                 controlCharEndOffset = ( filtrateBuf->Des().
       
    84                     Mid( controlCharStartOffset ).Find( KXmlEntityEnd ) );
       
    85                 if ( controlCharEndOffset != KErrNotFound )
       
    86                     {
       
    87                     TLex8 tempLex( filtrateBuf->Des().Mid( partofBufferOffset
       
    88                       + KXmlControlCharStart().Length() ,controlCharEndOffset
       
    89                       - KXmlControlCharStart().Length() ) );
       
    90                     //Parses the string to extract a signed 8-bit integer.
       
    91                     TInt tempNum;
       
    92                     User::LeaveIfError( tempLex.Val( tempNum ) );
       
    93                     if( ( tempNum >= KC0RangeStart && tempNum <= KC0RangeEnd )
       
    94                          || ( tempNum == KDeleteChar ) )
       
    95                         {
       
    96                         __LOG( "UpnpXmlStringUtility::\
       
    97 RemoveXmlControlCharactersL delete control character" );
       
    98                         TPtrC8 tmpBuf = filtrateBuf->Des().Mid( 
       
    99                             partofBufferOffset, controlCharEndOffset + 
       
   100                             KXmlEntityEnd().Length() );
       
   101                         __LOG8( tmpBuf);
       
   102                         
       
   103                         filtrateBuf->Des().Delete( partofBufferOffset, 
       
   104                         controlCharEndOffset + KXmlEntityEnd().Length() );
       
   105                         }
       
   106                     else 
       
   107                         {
       
   108                         __LOG1( "UpnpXmlStringUtility::\
       
   109 RemoveXmlControlCharactersL between &# and; the Number is %d",tempNum );
       
   110                         }
       
   111                     controlCharStartOffset = partofBufferOffset;
       
   112                     controlCharStartOffset = ( filtrateBuf->Des().
       
   113                                         Mid( controlCharStartOffset ).
       
   114                                         Find( KXmlControlCharStart ) );
       
   115                     }
       
   116                 else 
       
   117                     {
       
   118                     __LOG( "UpnpXmlStringUtility::RemoveXmlControlCharactersL\
       
   119  Can not find EntityEnd" );
       
   120                     User::Leave( KErrArgument );
       
   121                     }
       
   122                 }
       
   123             CleanupStack::Pop( filtrateBuf );
       
   124             }
       
   125         else
       
   126             {
       
   127             // not contains control character.
       
   128             __LOG( "UpnpXmlStringUtility::RemoveXmlControlCharactersL Can \
       
   129                    not find control character!" );
       
   130             }
       
   131         }
       
   132     else
       
   133         {
       
   134         //xml data is NULL
       
   135         __LOG( "UpnpXmlStringUtility::RemoveXmlControlCharactersL the \
       
   136                             xml data is NULL" );
       
   137         }
       
   138     __LOG( "UpnpXmlStringUtility::RemoveXmlControlCharactersL End" );
       
   139     return filtrateBuf;
       
   140     }