homescreenpluginsrv/hspsdefinitionengine/hspsdefinitionengine/src/hspsxmlhandler.cpp
changeset 0 79c6a41cd166
equal deleted inserted replaced
-1:000000000000 0:79c6a41cd166
       
     1 /*
       
     2 * Copyright (c) 2005,2006 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:  Main class for XML parsing
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include    "hspsxmlhandler.h"
       
    22 
       
    23 #include    "hspsdomattribute.h"
       
    24 #include    "hspsdomlist.h"
       
    25 #include    "hspsdomdocument.h"
       
    26 #include    "hspsdomnode.h"
       
    27 #include    "hspsdomstringpool.h"
       
    28 
       
    29 #include    <parserfeature.h>
       
    30 #include    <matchdata.h>
       
    31 
       
    32 
       
    33 // LOCAL CONSTANTS AND MACROS
       
    34 _LIT8(KXmlMimeType,"text/xml");
       
    35 _LIT8(KVariant,"libxml2");
       
    36 
       
    37 _LIT8(KAnd,"&");
       
    38 _LIT8(KSemicolon, ";" );
       
    39 _LIT8(KRefAttr, "ref");
       
    40 
       
    41 
       
    42 // ============================ MEMBER FUNCTIONS ===============================
       
    43 
       
    44 // -----------------------------------------------------------------------------
       
    45 // ChspsXMLHandler::ChspsXMLHandler
       
    46 // C++ default constructor can NOT contain any code, that
       
    47 // might leave.
       
    48 // -----------------------------------------------------------------------------
       
    49 //
       
    50 ChspsXMLHandler::ChspsXMLHandler()    
       
    51     {
       
    52     }
       
    53 
       
    54 // -----------------------------------------------------------------------------
       
    55 // ChspsXMLHandler::ConstructL
       
    56 // Symbian 2nd phase constructor can leave.
       
    57 // -----------------------------------------------------------------------------
       
    58 //
       
    59 void ChspsXMLHandler::ConstructL()
       
    60     {
       
    61     MContentHandler* contentHandler = this;
       
    62      
       
    63     CMatchData* matchData = CMatchData::NewL();
       
    64     CleanupStack::PushL(matchData);
       
    65     matchData->SetMimeTypeL(KXmlMimeType);
       
    66     matchData->SetVariantL(KVariant);
       
    67 
       
    68     iXmlParser = Xml::CParser::NewL( *matchData, *contentHandler );
       
    69     iXmlParser->EnableFeature( EReplaceIntEntityFromExtSubsetByRef );
       
    70     iNodeStack = new (ELeave)RPointerArray<ChspsDomNode>;
       
    71     CleanupStack::PopAndDestroy(matchData);
       
    72 
       
    73     }
       
    74 
       
    75 // -----------------------------------------------------------------------------
       
    76 // ChspsXMLHandler::NewL
       
    77 // Two-phased constructor.
       
    78 // -----------------------------------------------------------------------------
       
    79 //
       
    80 ChspsXMLHandler* ChspsXMLHandler::NewL()
       
    81     {
       
    82     ChspsXMLHandler* self = new( ELeave ) ChspsXMLHandler();
       
    83     
       
    84     CleanupStack::PushL( self );
       
    85     self->ConstructL();
       
    86     CleanupStack::Pop( self );
       
    87 
       
    88     return self;
       
    89     }
       
    90 
       
    91     
       
    92 // Destructor
       
    93 ChspsXMLHandler::~ChspsXMLHandler()
       
    94     {
       
    95     delete iXmlParser;
       
    96     if ( iNodeStack )
       
    97         {
       
    98         iNodeStack->Close();
       
    99         delete iNodeStack;
       
   100         }
       
   101     }
       
   102 // -----------------------------------------------------------------------------
       
   103 // ChspsXMLHandler::ParseXmlDocL
       
   104 // Parses the given XML file, creates nodes and attributes and inserting
       
   105 // them to aDomDocument
       
   106 // (other items were commented in a header).
       
   107 // -----------------------------------------------------------------------------
       
   108 //
       
   109 void ChspsXMLHandler::ParseXmlDocL(
       
   110     RFs& aFsSession, 
       
   111     const TDesC& aFileName,
       
   112     ChspsDomDocument& aDomDocument )    
       
   113     {
       
   114     iError = KErrNone;
       
   115     iDomDocument = &aDomDocument;
       
   116     Xml::ParseL(*iXmlParser, aFsSession, aFileName );
       
   117     User::LeaveIfError( iError );
       
   118     }    
       
   119   
       
   120 // -----------------------------------------------------------------------------
       
   121 // ChspsXMLHandler::OnStartDocumentL
       
   122 // This method is a callback to indicate the start of the document
       
   123 // (other items were commented in a header).
       
   124 // -----------------------------------------------------------------------------
       
   125 //
       
   126 void ChspsXMLHandler::OnStartDocumentL(const RDocumentParameters& /*aDocParam*/, TInt /*aErrorCode*/)
       
   127     {
       
   128     }
       
   129 
       
   130 // -----------------------------------------------------------------------------
       
   131 // ChspsXMLHandler::OnEndDocumentL
       
   132 // This method is a callback to indicate the end of the document
       
   133 // (other items were commented in a header).
       
   134 // -----------------------------------------------------------------------------
       
   135 //
       
   136 void ChspsXMLHandler::OnEndDocumentL(TInt /*aErrorCode*/)
       
   137     {
       
   138     }
       
   139 // -----------------------------------------------------------------------------
       
   140 // ChspsXMLHandler::OnStartElementL
       
   141 // This method is a callback to indicate the start of an element
       
   142 // (other items were commented in a header).
       
   143 // -----------------------------------------------------------------------------
       
   144 //
       
   145  void ChspsXMLHandler::OnStartElementL(
       
   146     const RTagInfo& aElement, 
       
   147     const RAttributeArray& aAttributes, 
       
   148     TInt /*aErrorCode*/)
       
   149     {
       
   150     const TDesC8& localName = aElement.LocalName().DesC();
       
   151     const TDesC8& uri = aElement.Uri().DesC();
       
   152         
       
   153     ChspsDomNode* currentNode = iDomDocument->CreateElementNSL( localName, uri );
       
   154     CleanupStack::PushL( currentNode );
       
   155     
       
   156     // set hierarchy
       
   157     if ( iParentNode  )
       
   158         {
       
   159         iParentNode->AddChildL( currentNode );
       
   160         }
       
   161     else
       
   162         {
       
   163         iDomDocument->SetRootNode( currentNode );
       
   164         iParentNode = currentNode;  // root node
       
   165         }
       
   166     CleanupStack::Pop( currentNode );   //Now parent or document has a ownership of the new node
       
   167         
       
   168     currentNode->SetNodeId( iCurrentNodeId++ );
       
   169     // "Push" parent for this node into stack. When an end element for this node is encountered, 
       
   170     // "Pop" parent node from stack. 
       
   171     iNodeStack->AppendL( iParentNode );     
       
   172     iParentNode = currentNode;                  //Current node will be a parent for a new element
       
   173     
       
   174     
       
   175     // add attributes
       
   176     TInt attrCount( aAttributes.Count() );
       
   177     TBool refSet ( EFalse );
       
   178     for( TInt i=0; i<attrCount; i++ )
       
   179         {
       
   180         const TDesC8& attrNameDes8 = aAttributes[i].Attribute().LocalName().DesC();
       
   181         const TDesC8& attValueDes8 = aAttributes[i].Value().DesC();
       
   182         
       
   183         if ( !refSet && KRefAttr().Compare( attrNameDes8 ) == 0 )
       
   184             {
       
   185             currentNode->SetRefNode();
       
   186             refSet = ETrue;
       
   187             }
       
   188         ChspsDomStringPool& stringPool = currentNode->StringPool();
       
   189         ChspsDomAttribute* newAttr = ChspsDomAttribute::NewL( attrNameDes8, stringPool );
       
   190         CleanupStack::PushL( newAttr );
       
   191         newAttr->SetValueL( attValueDes8 );
       
   192         currentNode->AttributeList().AddItemL( newAttr );    //takes ownership
       
   193         CleanupStack::Pop( newAttr );
       
   194         }
       
   195     }
       
   196 
       
   197 // -----------------------------------------------------------------------------
       
   198 // ChspsXMLHandler::OnEndElementL
       
   199 // This method is a callback to indicate the end of an element
       
   200 // (other items were commented in a header).
       
   201 // -----------------------------------------------------------------------------
       
   202 //
       
   203 void ChspsXMLHandler::OnEndElementL(const RTagInfo& /*aElement*/, TInt /*aErrorCode*/)
       
   204     {
       
   205   //  const TDesC8& localName = aElement.LocalName().DesC();
       
   206     TInt stackCount( iNodeStack->Count() );
       
   207     if ( stackCount > 0)
       
   208         {
       
   209         iParentNode = (*iNodeStack)[ stackCount-1 ];
       
   210         iNodeStack->Remove( stackCount-1 );
       
   211         }
       
   212     }
       
   213 
       
   214 // -----------------------------------------------------------------------------
       
   215 // ChspsXMLHandler::OnContentL
       
   216 // This method is a callback that sends the content of the element.
       
   217 // (other items were commented in a header).
       
   218 // -----------------------------------------------------------------------------
       
   219 //
       
   220 void ChspsXMLHandler::OnContentL(const TDesC8& aBytes, TInt aErrorCode)
       
   221     {
       
   222 #ifdef _DEBUG    
       
   223     const TDesC8& currentNodeName = iParentNode->Name();
       
   224     TContentType type = iParentNode->ContentType();
       
   225 #endif
       
   226     if ( aErrorCode == KErrNone && iParentNode->ContentType() == EPCData )
       
   227         {
       
   228         //iParentNode holds the current node
       
   229         iParentNode->AppendPCDataL( aBytes );
       
   230         }
       
   231     }
       
   232 // -----------------------------------------------------------------------------
       
   233 // ChspsXMLHandler::OnStartPrefixMappingL(
       
   234 // This method is a notification of the beginning of the scope of a prefix-URI mapping.
       
   235 // (other items were commented in a header).
       
   236 // -----------------------------------------------------------------------------
       
   237 //
       
   238 void ChspsXMLHandler::OnStartPrefixMappingL(
       
   239     const RString& /*aPrefix*/, 
       
   240     const RString& /*aUri*/, 
       
   241     TInt /*aErrorCode*/)
       
   242     {
       
   243     }
       
   244 // -----------------------------------------------------------------------------
       
   245 // ChspsXMLHandler::OnEndPrefixMappingL
       
   246 // This method is a notification of the end of the scope of a prefix-URI mapping.
       
   247 // (other items were commented in a header).
       
   248 // -----------------------------------------------------------------------------
       
   249 //    
       
   250 void ChspsXMLHandler::OnEndPrefixMappingL(const RString& /*aPrefix*/, TInt /*aErrorCode*/)
       
   251     {
       
   252     }
       
   253 // -----------------------------------------------------------------------------
       
   254 // ChspsXMLHandler::OnIgnorableWhiteSpaceL
       
   255 // This method is a notification of ignorable whitespace in element content.
       
   256 // (other items were commented in a header).
       
   257 // -----------------------------------------------------------------------------
       
   258 //    
       
   259 void ChspsXMLHandler::OnIgnorableWhiteSpaceL(const TDesC8& /*aBytes*/, TInt /*aErrorCode*/)
       
   260     {
       
   261     }    
       
   262 // -----------------------------------------------------------------------------
       
   263 // ChspsXMLHandler::OnProcessingInstructionL
       
   264 // This method is a receive notification of a processing instruction.
       
   265 // (other items were commented in a header).
       
   266 // -----------------------------------------------------------------------------
       
   267 //    
       
   268 void ChspsXMLHandler::OnProcessingInstructionL(
       
   269     const TDesC8& /*aTarget*/, 
       
   270     const TDesC8& /*aData*/, 
       
   271 	TInt /*aErrorCode*/)
       
   272     {
       
   273     }
       
   274 // -----------------------------------------------------------------------------
       
   275 // ChspsXMLHandler::OnSkippedEntityL
       
   276 // This method is a notification of a skipped entity.
       
   277 // (other items were commented in a header).
       
   278 // -----------------------------------------------------------------------------
       
   279 //    
       
   280 void ChspsXMLHandler::OnSkippedEntityL(const RString& aName, TInt aErrorCode) 
       
   281     {
       
   282 #ifdef _DEBUG    
       
   283     const TDesC8& currentNodeName = iParentNode->Name();
       
   284     const TDesC8& entity = aName.DesC(); 
       
   285     TContentType type = iParentNode->ContentType();
       
   286 #endif
       
   287     if ( aErrorCode == KErrNone && iParentNode->ContentType() == EPCData )
       
   288         {
       
   289         //iParentNode holds the current node
       
   290         iParentNode->AppendPCDataL( KAnd );
       
   291         iParentNode->AppendPCDataL( aName.DesC() );
       
   292         iParentNode->AppendPCDataL( KSemicolon );
       
   293         }    
       
   294     }
       
   295 // -----------------------------------------------------------------------------
       
   296 // ChspsXMLHandler::OnError
       
   297 // This method indicates an error has occurred.
       
   298 // (other items were commented in a header).
       
   299 // -----------------------------------------------------------------------------
       
   300 //    
       
   301 void ChspsXMLHandler::OnError(TInt aErrorCode)
       
   302     {
       
   303     iError = aErrorCode;
       
   304     }
       
   305 // -----------------------------------------------------------------------------
       
   306 // ChspsXMLHandler::GetExtendedInterface
       
   307 // This method obtains the interface matching the specified uid.
       
   308 // (other items were commented in a header).
       
   309 // -----------------------------------------------------------------------------
       
   310 //    
       
   311 TAny* ChspsXMLHandler::GetExtendedInterface(const TInt32 /*aUid*/)
       
   312     {
       
   313     return NULL;    
       
   314     }    
       
   315 
       
   316        
       
   317 
       
   318 //  End of File