omadrm/drmroapwbxmlparser/src/wbxmlroaptriggerparser.cpp
changeset 0 95b198f216e5
equal deleted inserted replaced
-1:000000000000 0:95b198f216e5
       
     1 /*
       
     2 * Copyright (c) 2007-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:  Implementation of Oma Drm 2.1 WBXML RoapTrigger to XML parser
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include <e32base.h>
       
    22 #include <f32file.h>
       
    23 #include <xml/parserfeature.h>
       
    24 
       
    25 #include "wbxmlroaptriggerparser.h"
       
    26 
       
    27 // CONSTANTS
       
    28 _LIT8( KWBXMLParserMimeType, "text/wbxml" );
       
    29 _LIT8( KSpace, " " );
       
    30 _LIT8( KLess, "<" );
       
    31 _LIT8( KMore, ">" );
       
    32 _LIT8( KEqual, "=" );
       
    33 _LIT8( KQuote, "\"" );
       
    34 _LIT8( KSlash, "/" );
       
    35 
       
    36 const TInt KBufferGran( 512 );
       
    37 
       
    38 // ============================ LOCAL FUNCTIONS ================================
       
    39 
       
    40 // -----------------------------------------------------------------------------
       
    41 // XmlEscapeL
       
    42 // -----------------------------------------------------------------------------
       
    43 //
       
    44 LOCAL_C void XmlEscapeL( CBufFlat*& aTarget, const TDesC8& aSource )
       
    45     {
       
    46     _LIT8( KEscapedQuote, "&quot;" );
       
    47     _LIT8( KEscapedApostrophe, "&apos;" );
       
    48     _LIT8( KEscapedAmpersand, "&amp;" );
       
    49     _LIT8( KEscapedLessThan, "&lt;" );
       
    50     _LIT8( KEscapedGreaterThan, "&gt;");
       
    51 
       
    52     const TInt sourceLength( aSource.Length() );
       
    53     for ( TInt i( 0 ); i < sourceLength; ++i )
       
    54         {
       
    55         switch ( aSource[i] )
       
    56             {
       
    57         case '&':
       
    58             aTarget->InsertL( aTarget->Size(), KEscapedAmpersand() );
       
    59             break;
       
    60         case '<':
       
    61             aTarget->InsertL( aTarget->Size(), KEscapedLessThan() );
       
    62             break;
       
    63         case '>':
       
    64             aTarget->InsertL( aTarget->Size(), KEscapedGreaterThan() );
       
    65             break;
       
    66         case '\"':
       
    67             aTarget->InsertL( aTarget->Size(), KEscapedQuote() );
       
    68             break;
       
    69         case '\'':
       
    70             aTarget->InsertL( aTarget->Size(), KEscapedApostrophe() );
       
    71             break;
       
    72         default:
       
    73             aTarget->InsertL( aTarget->Size(), aSource.Mid( i, 1 ) );
       
    74             break;
       
    75             }
       
    76         }
       
    77     }
       
    78 
       
    79 
       
    80 // ============================ MEMBER FUNCTIONS ===============================
       
    81 
       
    82 // -----------------------------------------------------------------------------
       
    83 // DRM::CWbxmlRoapTriggerParser::CWbxmlRoapTriggerParser
       
    84 // C++ default constructor can NOT contain any code, that
       
    85 // might leave.
       
    86 // -----------------------------------------------------------------------------
       
    87 //
       
    88 DRM::CWbxmlRoapTriggerParser::CWbxmlRoapTriggerParser()
       
    89     {
       
    90     }
       
    91 
       
    92 // -----------------------------------------------------------------------------
       
    93 // DRM::CWbxmlRoapTriggerParser::ConstructL
       
    94 // Symbian 2nd phase constructor can leave.
       
    95 // -----------------------------------------------------------------------------
       
    96 //
       
    97 void DRM::CWbxmlRoapTriggerParser::ConstructL()
       
    98     {
       
    99     iParser = Xml::CParser::NewL( KWBXMLParserMimeType, *this );
       
   100     User::LeaveIfError( iParser->EnableFeature( Xml::ERawContent ) );
       
   101     iContent = CBufFlat::NewL( KBufferGran );
       
   102     }
       
   103 
       
   104 // -----------------------------------------------------------------------------
       
   105 // DRM::CWbxmlRoapTriggerParser::NewL
       
   106 // Two-phased constructor.
       
   107 // -----------------------------------------------------------------------------
       
   108 //
       
   109 EXPORT_C DRM::CWbxmlRoapTriggerParser* DRM::CWbxmlRoapTriggerParser::NewL()
       
   110     {
       
   111     DRM::CWbxmlRoapTriggerParser* self( NewLC() );
       
   112     CleanupStack::Pop( self );
       
   113     return self;
       
   114     }
       
   115 
       
   116 // -----------------------------------------------------------------------------
       
   117 // DRM::CWbxmlRoapTriggerParser::NewLC
       
   118 // Two-phased constructor
       
   119 // -----------------------------------------------------------------------------
       
   120 //
       
   121 EXPORT_C DRM::CWbxmlRoapTriggerParser* DRM::CWbxmlRoapTriggerParser::NewLC()
       
   122     {
       
   123     DRM::CWbxmlRoapTriggerParser* self(
       
   124         new ( ELeave ) DRM::CWbxmlRoapTriggerParser );
       
   125     CleanupStack::PushL( self );
       
   126     self->ConstructL();
       
   127     return self;
       
   128     }
       
   129 
       
   130 // Destructor
       
   131 EXPORT_C DRM::CWbxmlRoapTriggerParser::~CWbxmlRoapTriggerParser()
       
   132     {
       
   133     delete iParser;
       
   134     delete iContent;
       
   135     }
       
   136 
       
   137 // -----------------------------------------------------------------------------
       
   138 // DRM::CWbxmlRoapTriggerParser::ParseL
       
   139 // -----------------------------------------------------------------------------
       
   140 //
       
   141 EXPORT_C HBufC8* DRM::CWbxmlRoapTriggerParser::ParseL(
       
   142     const TDesC8& aMessage )
       
   143     {
       
   144     TInt i( 0 );
       
   145     TInt n( 0 );
       
   146 
       
   147     iParser->ParseBeginL();
       
   148 
       
   149     while ( i < aMessage.Length() )
       
   150         {
       
   151         n = Min( aMessage.Length() - i, KBufferGran );
       
   152         iParser->ParseL( aMessage.Mid( i, n ) );
       
   153         i += n;
       
   154         }
       
   155 
       
   156     iParser->ParseEndL();
       
   157 
       
   158     return iContent->Ptr( 0 ).AllocL();
       
   159     }
       
   160 
       
   161 // -----------------------------------------------------------------------------
       
   162 // DRM::CWbxmlRoapTriggerParser::OnStartDocumentL
       
   163 // -----------------------------------------------------------------------------
       
   164 //
       
   165 void DRM::CWbxmlRoapTriggerParser::OnStartDocumentL(
       
   166     const Xml::RDocumentParameters& /*aDocParam*/,
       
   167     TInt /*aErrorCode*/)
       
   168     {
       
   169     }
       
   170 
       
   171 // -----------------------------------------------------------------------------
       
   172 // DRM::CWbxmlRoapTriggerParser::OnEndDocumentL
       
   173 // -----------------------------------------------------------------------------
       
   174 //
       
   175 void DRM::CWbxmlRoapTriggerParser::OnEndDocumentL(
       
   176     TInt /*aErrorCode*/)
       
   177     {
       
   178     }
       
   179 
       
   180 // -----------------------------------------------------------------------------
       
   181 // DRM::CWbxmlRoapTriggerParser::OnStartElementL
       
   182 // -----------------------------------------------------------------------------
       
   183 //
       
   184 void DRM::CWbxmlRoapTriggerParser::OnStartElementL(
       
   185     const Xml::RTagInfo& aElement,
       
   186     const Xml::RAttributeArray& aAttributes,
       
   187     TInt /*aErrorCode*/)
       
   188     {
       
   189     CBufFlat* escapedValueBuffer( CBufFlat::NewL( KBufferGran ) );
       
   190     CleanupStack::PushL( escapedValueBuffer );
       
   191     // <tag attribute="attribute value">
       
   192     iContent->InsertL( iContent->Size(), KLess );
       
   193     iContent->InsertL( iContent->Size(), aElement.LocalName().DesC() );
       
   194 
       
   195     for ( TInt i( 0 ); i < aAttributes.Count(); i++ )
       
   196         {
       
   197         iContent->InsertL( iContent->Size(), KSpace );
       
   198         iContent->InsertL( iContent->Size(),
       
   199                            aAttributes[i].Attribute().LocalName().DesC() );
       
   200         iContent->InsertL( iContent->Size(), KEqual );
       
   201         iContent->InsertL( iContent->Size(), KQuote );
       
   202         XmlEscapeL( escapedValueBuffer, aAttributes[i].Value().DesC() );
       
   203         iContent->InsertL( iContent->Size(), escapedValueBuffer->Ptr( 0 ) );
       
   204         iContent->InsertL( iContent->Size(), KQuote );
       
   205         escapedValueBuffer->Delete( 0, escapedValueBuffer->Size() );
       
   206         }
       
   207 
       
   208     iContent->InsertL( iContent->Size(), KMore );
       
   209     CleanupStack::PopAndDestroy( escapedValueBuffer );
       
   210     }
       
   211 
       
   212 // -----------------------------------------------------------------------------
       
   213 // DRM::CWbxmlRoapTriggerParser::OnEndElementL
       
   214 // -----------------------------------------------------------------------------
       
   215 //
       
   216 void DRM::CWbxmlRoapTriggerParser::OnEndElementL(
       
   217     const Xml::RTagInfo& aElement,
       
   218     TInt /*aErrorCode*/)
       
   219     {
       
   220     // </tag>
       
   221     iContent->InsertL( iContent->Size(), KLess );
       
   222     iContent->InsertL( iContent->Size(), KSlash );
       
   223     iContent->InsertL( iContent->Size(), aElement.LocalName().DesC() );
       
   224     iContent->InsertL( iContent->Size(), KMore );
       
   225     }
       
   226 
       
   227 // -----------------------------------------------------------------------------
       
   228 // DRM::CWbxmlRoapTriggerParser::OnContentL
       
   229 // -----------------------------------------------------------------------------
       
   230 //
       
   231 void DRM::CWbxmlRoapTriggerParser::OnContentL(
       
   232     const TDesC8& aBytes,
       
   233     TInt /*aErrorCode*/)
       
   234     {
       
   235     XmlEscapeL( iContent, aBytes );
       
   236     }
       
   237 
       
   238 // -----------------------------------------------------------------------------
       
   239 // DRM::CWbxmlRoapTriggerParser::OnStartPrefixMappingL
       
   240 // -----------------------------------------------------------------------------
       
   241 //
       
   242 void DRM::CWbxmlRoapTriggerParser::OnStartPrefixMappingL(
       
   243     const RString& /*aPrefix*/,
       
   244     const RString& /*aUri*/,
       
   245     TInt /*aErrorCode*/)
       
   246     {
       
   247     }
       
   248 
       
   249 // -----------------------------------------------------------------------------
       
   250 // DRM::CWbxmlRoapTriggerParser::OnEndPrefixMappingL
       
   251 // -----------------------------------------------------------------------------
       
   252 //
       
   253 void DRM::CWbxmlRoapTriggerParser::OnEndPrefixMappingL(
       
   254     const RString& /*aPrefix*/,
       
   255     TInt /*aErrorCode*/)
       
   256     {
       
   257     }
       
   258 
       
   259 // -----------------------------------------------------------------------------
       
   260 // DRM::CWbxmlRoapTriggerParser::OnIgnorableWhiteSpaceL
       
   261 // -----------------------------------------------------------------------------
       
   262 //
       
   263 void DRM::CWbxmlRoapTriggerParser::OnIgnorableWhiteSpaceL(
       
   264     const TDesC8& /*aBytes*/,
       
   265     TInt /*aErrorCode*/)
       
   266     {
       
   267     }
       
   268 
       
   269 // -----------------------------------------------------------------------------
       
   270 // DRM::CWbxmlRoapTriggerParser::OnSkippedEntityL
       
   271 // -----------------------------------------------------------------------------
       
   272 //
       
   273 void DRM::CWbxmlRoapTriggerParser::OnSkippedEntityL(
       
   274     const RString& /*aName*/,
       
   275     TInt /*aErrorCode*/)
       
   276     {
       
   277     }
       
   278 
       
   279 // -----------------------------------------------------------------------------
       
   280 // DRM::CWbxmlRoapTriggerParser::OnProcessingInstructionL
       
   281 // -----------------------------------------------------------------------------
       
   282 //
       
   283 void DRM::CWbxmlRoapTriggerParser::OnProcessingInstructionL(
       
   284     const TDesC8& /*aTarget*/,
       
   285     const TDesC8& /*aData*/,
       
   286     TInt /*aErrorCode*/)
       
   287     {
       
   288     }
       
   289 
       
   290 // -----------------------------------------------------------------------------
       
   291 // DRM::CWbxmlRoapTriggerParser::OnOutOfData
       
   292 // -----------------------------------------------------------------------------
       
   293 //
       
   294 void DRM::CWbxmlRoapTriggerParser::OnOutOfData()
       
   295     {
       
   296     }
       
   297 
       
   298 // -----------------------------------------------------------------------------
       
   299 // DRM::CWbxmlRoapTriggerParser::OnError
       
   300 // -----------------------------------------------------------------------------
       
   301 //
       
   302 void DRM::CWbxmlRoapTriggerParser::OnError(
       
   303     TInt /*aErrorCode*/)
       
   304     {
       
   305     }
       
   306 
       
   307 // -----------------------------------------------------------------------------
       
   308 // DRM::CWbxmlRoapTriggerParser::GetExtendedInterface
       
   309 // -----------------------------------------------------------------------------
       
   310 //
       
   311 TAny* DRM::CWbxmlRoapTriggerParser::GetExtendedInterface(
       
   312     const TInt32 /*aUid*/)
       
   313     {
       
   314     return NULL;
       
   315     }
       
   316 
       
   317 //  End of File