mtpdataproviders/mtppictbridgedp/pictbridgeengine/src/dpsxmlparser.cpp
branchRCL_3
changeset 19 0aa8cc770c8a
equal deleted inserted replaced
18:453dfc402455 19:0aa8cc770c8a
       
     1 /*
       
     2 * Copyright (c) 2006, 2007 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 parses the dps xml script. 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <e32debug.h>
       
    20 #include "dpsxmlparser.h"
       
    21 #include "dpsconst.h"
       
    22 #include "pictbridge.h"
       
    23 #include "dpsxmlstring.h"
       
    24 
       
    25 #ifdef _DEBUG
       
    26 #	define IF_DEBUG(t) {RDebug::t;}
       
    27 #   define PRINT_DES(t)\
       
    28  {TBuf<KMaxArgLen> _buf; _buf.Copy(t);RDebug::Print(_L("---%S"), &_buf);}
       
    29 #else
       
    30 #	define IF_DEBUG(t)
       
    31 #   define PRINT_DES(t)
       
    32 #endif
       
    33 
       
    34 // ---------------------------------------------------------------------------
       
    35 // 
       
    36 // ---------------------------------------------------------------------------
       
    37 //
       
    38 CDpsXmlParser* CDpsXmlParser::NewL(CDpsEngine* aEngine)
       
    39 	{
       
    40     IF_DEBUG(Print(_L("CDpsXmlParser::NewL")));
       
    41     CDpsXmlParser* self = new (ELeave) CDpsXmlParser(aEngine);
       
    42     return self;
       
    43     }
       
    44 
       
    45 // ---------------------------------------------------------------------------
       
    46 // 
       
    47 // ---------------------------------------------------------------------------
       
    48 //	
       
    49 CDpsXmlParser::~CDpsXmlParser()
       
    50     {
       
    51     IF_DEBUG(Print(_L(">>>~CDpsXmlParser")));
       
    52     iDpsArgs.Close();
       
    53     __IF_DEBUG(Print(_L("<<<~CDpsXmlParser")));
       
    54     }
       
    55 
       
    56 // ---------------------------------------------------------------------------
       
    57 // 
       
    58 // ---------------------------------------------------------------------------
       
    59 //	
       
    60 CDpsXmlParser::CDpsXmlParser(CDpsEngine* aEngine) : iEngine(aEngine)
       
    61     {
       
    62     IF_DEBUG(Print(_L(">>>CDpsXmlParser::Ctor")));
       
    63 	Reset();
       
    64     IF_DEBUG(Print(_L("<<<CDpsXmlParser::Ctor")));
       
    65 	}
       
    66 
       
    67 // ---------------------------------------------------------------------------
       
    68 // 
       
    69 // ---------------------------------------------------------------------------
       
    70 //
       
    71 void CDpsXmlParser::OnStartDocumentL(
       
    72 		const RDocumentParameters& /*aDocParam*/, TInt aErrorCode)
       
    73     {
       
    74     IF_DEBUG(Print(_L(">>>CDpsXmlParser::OnStartDocumentL")));
       
    75     if (aErrorCode != KErrNone)
       
    76         {
       
    77         IF_DEBUG(Print(_L("---, error code is %d"), aErrorCode));
       
    78         User::Leave(aErrorCode);
       
    79         }
       
    80     IF_DEBUG(Print(_L("<<<CDpsXmlParser::OnStartDocumentL")));
       
    81 	}
       
    82 
       
    83 // ---------------------------------------------------------------------------
       
    84 // 
       
    85 // ---------------------------------------------------------------------------
       
    86 //
       
    87 void CDpsXmlParser::OnEndDocumentL(TInt aErrorCode)
       
    88 	{
       
    89     IF_DEBUG(Print(_L(">>>CDpsXmlParser::OnEndDocumentL")));
       
    90     if (aErrorCode != KErrNone)
       
    91         {
       
    92         IF_DEBUG(Print(_L("---, error code is %d"), aErrorCode));
       
    93         User::Leave(aErrorCode);
       
    94         }
       
    95     
       
    96     IF_DEBUG(Print(_L("<<<CDpsXmlParser::OnEndDocumentL")));
       
    97     }
       
    98 
       
    99 // ---------------------------------------------------------------------------
       
   100 // 
       
   101 // ---------------------------------------------------------------------------
       
   102 //	
       
   103 void CDpsXmlParser::OnStartElementL(const RTagInfo& aElement, 
       
   104 									const RAttributeArray& aAttributes, 
       
   105 									TInt aErrCode)
       
   106     {
       
   107     IF_DEBUG(Print(_L(">>>CDpsXmlParser::OnStartElementL")));
       
   108     
       
   109     if (aErrCode != KErrNone)
       
   110         {
       
   111         IF_DEBUG(Print(_L("---, error code is %d"), aErrCode));
       
   112         User::Leave(aErrCode);
       
   113         }
       
   114     if (aAttributes.Count() > 1)
       
   115         {
       
   116         IF_DEBUG(Print(_L("---cannot have more than one attribute!")));
       
   117         User::Leave(KErrArgument);
       
   118         }
       
   119     	
       
   120     // Gets the name of the tag
       
   121     const TDesC8& name = aElement.LocalName().DesC();
       
   122     
       
   123     IF_DEBUG(Print(_L("---Start"))); PRINT_DES(name);
       
   124 	
       
   125     //Checks the element
       
   126     // this is the first layer <dps>
       
   127     if (!name.Compare(KDpsXml))
       
   128         {
       
   129         iAction = EDpsXmlStart;
       
   130         }
       
   131     // this is the second layer	<input|output>
       
   132     else if (!name.Compare(KDpsXmlOutput))
       
   133         {
       
   134         iAction = EDpsXmlOutput;
       
   135         }
       
   136     else if (!name.Compare(KDpsXmlInput))
       
   137         {
       
   138         // operation and event cannot be distiguised here
       
   139         iAction = EDpsXmlInput;
       
   140         }
       
   141     // this is the third layer <operation|event|result>	
       
   142     else if (iAction == EDpsXmlOutput && !name.Compare(KDpsXmlResult))
       
   143         {
       
   144         // output must be result
       
   145         iAction = EDpsXmlResult;
       
   146         }		
       
   147     else if (iAction == EDpsXmlInput &&
       
   148         (!name.Compare(iEngine->DpsParameters()->iDpsEventStrings[0]) ||
       
   149         !name.Compare(iEngine->DpsParameters()->iDpsEventStrings[1])))
       
   150         {
       
   151         if (!name.Compare(iEngine->DpsParameters()->iDpsEventStrings[0]))
       
   152             {
       
   153             iDpsEvent = EDpsEvtNotifyJobStatus;
       
   154             }
       
   155         else
       
   156             {
       
   157             iDpsEvent = EDpsEvtNotifyDeviceStatus;	
       
   158             }
       
   159         iAction = EDpsXmlEvent;	
       
   160         }
       
   161     else if (iAction == EDpsXmlInput || iAction == EDpsXmlResult)
       
   162         {
       
   163         iAction = EDpsXmlOperation;
       
   164         for (TInt i= 0; i < EDpsOpMax-1; i++) //i=0 as AppendL() method adds item on first free position in array
       
   165             {
       
   166             if (!name.Compare(iEngine->DpsParameters()->iDpsOperationStrings[i]))
       
   167                 {
       
   168                 IF_DEBUG(Print(_L("---found the operation")));
       
   169                 PRINT_DES(name);
       
   170                 iDpsOperation = (TDpsOperation)(i+1);// i+1 to by pass first operation EDpsOpEmpty in enum which is not added to array
       
   171                 break;
       
   172                 }
       
   173             }
       
   174         }
       
   175 		
       
   176     // below is the fourth layer, e.g. the argument list/element. there
       
   177     // could be many arugments, but only one element	
       
   178     else if (iAction == EDpsXmlOperation || iAction == EDpsXmlEvent)
       
   179         {
       
   180         // only element in output is capability
       
   181         if (!name.Compare(iEngine->DpsParameters()->iDpsElementStrings[0]))
       
   182             {
       
   183             IF_DEBUG(Print(_L("---the element is")));
       
   184             PRINT_DES(name);
       
   185             iElement = EDpsCapability;
       
   186             }
       
   187         else
       
   188             {
       
   189             for (TInt i = 0; i < EDpsArgMax; i++)
       
   190                 {
       
   191                 if (!name.Compare(iEngine->DpsParameters()->iDpsArgStrings[i]))
       
   192                     {
       
   193                     IF_DEBUG(Print(_L("---the argument is ")));
       
   194                     PRINT_DES(name);	
       
   195                     TDpsArg arg;
       
   196                     arg.iElement = (TDpsArgument)i;
       
   197                     arg.iContent.Copy(_L(""));
       
   198                     iDpsArgs.Append(arg);
       
   199                     break;
       
   200                     }
       
   201                 }
       
   202             }
       
   203 		
       
   204         }
       
   205     else
       
   206         {
       
   207         // something wrong
       
   208         IF_DEBUG(Print(_L("--- non-PB element! %S"), &name));
       
   209         User::Leave(KErrNotSupported);
       
   210         }
       
   211     if (aAttributes.Count() == 1)
       
   212         {
       
   213         ParseAttributesL(aAttributes, name);
       
   214         }
       
   215     IF_DEBUG(Print(_L("<<<CDpsXmlParser::OnStartElementL")));
       
   216     }
       
   217 
       
   218 // ---------------------------------------------------------------------------
       
   219 // 
       
   220 // ---------------------------------------------------------------------------
       
   221 //	     
       
   222 void CDpsXmlParser::ParseAttributesL(const RAttributeArray& aAttributes, 
       
   223                                      const TDesC8& tag)
       
   224     {
       
   225     // element
       
   226     const TDesC8& name = aAttributes[0].Attribute().LocalName().DesC();
       
   227     // only "layouts" and "paperTypes" have attributes
       
   228     if (tag.Compare(KDpsXmlPaperTypes) && tag.Compare(KDpsXmlLayouts))
       
   229         {
       
   230         // error
       
   231         IF_DEBUG(Print(_L("--- this tag have no attribute")));
       
   232         PRINT_DES(name);
       
   233         User::Leave(KErrArgument);
       
   234         }
       
   235     // the element of the attributes must be "paperSize"	
       
   236     if (name.Compare(KDpsXmlPaperSize))
       
   237         {
       
   238         // error
       
   239         IF_DEBUG(Print(_L("--- wrong attribute")))
       
   240         PRINT_DES(name);
       
   241         User::Leave(KErrArgument);
       
   242         }
       
   243     // value
       
   244     HBufC8* value = aAttributes[0].Value().DesC().AllocLC();
       
   245     TUint32 result;    
       
   246     TLex8 converter(*value);
       
   247     TInt error = converter.Val(result, EHex);
       
   248     if (error != KErrNone)
       
   249         {
       
   250         IF_DEBUG(Print(_L("--- convert error %d"), error));
       
   251         User::Leave(error);
       
   252         }
       
   253     iAttrib = result >> KShiftLength; 
       
   254     CleanupStack::PopAndDestroy(value);
       
   255     IF_DEBUG(Print(_L("--- attribte value %x"), result));
       
   256      
       
   257     }
       
   258     
       
   259 // ---------------------------------------------------------------------------
       
   260 // 
       
   261 // ---------------------------------------------------------------------------
       
   262 //	
       
   263 void CDpsXmlParser::OnEndElementL(const RTagInfo& aElement, 
       
   264 								  TInt aErrorCode)
       
   265     {
       
   266     IF_DEBUG(Print(_L(">>>CDpsXmlParser::OnEndElementL")));
       
   267     
       
   268     if (aErrorCode != KErrNone)
       
   269         {
       
   270         IF_DEBUG(Print(_L("--- error code is %d"), aErrorCode));
       
   271         User::Leave(aErrorCode);
       
   272         }
       
   273         
       
   274     // Get the name of the tag
       
   275     const TDesC8& name = aElement.LocalName().DesC();
       
   276     PRINT_DES(name);
       
   277     IF_DEBUG(Print(_L("<<<CDpsXmlParser::OnEndElementL")));	
       
   278     }
       
   279 
       
   280 // ---------------------------------------------------------------------------
       
   281 // 
       
   282 // ---------------------------------------------------------------------------
       
   283 //
       
   284 void CDpsXmlParser::OnContentL(const TDesC8& aBytes, TInt aErrorCode)
       
   285 	{
       
   286     IF_DEBUG(Print(_L(">>>CDpsXmlParser::OnContentL content is")));
       
   287     PRINT_DES(aBytes);
       
   288     
       
   289     if (aErrorCode != KErrNone)
       
   290    	    {
       
   291         IF_DEBUG(Print(_L("--- error code %d"), aErrorCode));
       
   292         User::Leave(aErrorCode);
       
   293         }	
       
   294     if (aBytes[0] >= KSOH && aBytes[0] <= KSpace)
       
   295         {
       
   296         IF_DEBUG(Print(_L("the unprintable char %d"), aBytes[0]));
       
   297         return; 
       
   298         }
       
   299     // parses the result	
       
   300     if (iAction == EDpsXmlResult)
       
   301         {
       
   302         TUint32 value;
       
   303         TLex8 converter(aBytes);
       
   304         TInt error = converter.Val(value, EHex);
       
   305         if (error != KErrNone)
       
   306             {
       
   307             IF_DEBUG(Print(_L("--- convert error %d"), error));
       
   308             User::Leave(error);
       
   309             }
       
   310         IF_DEBUG(Print(_L("--- result %x"), value));
       
   311         // we have got the result
       
   312         iDpsResult.iMajorCode = 
       
   313             static_cast<TDpsResultMajorCode>(value >> KShiftLength);
       
   314         iDpsResult.iMinorCode = 
       
   315             static_cast<TDpsResultMinorCode>(value & KDpsMinorMask);
       
   316         }
       
   317     // gets the argument	
       
   318     else if (iAction == EDpsXmlOperation || iAction == EDpsXmlEvent)
       
   319         {
       
   320         iDpsArgs[iDpsArgs.Count() - 1].iContent.Copy(aBytes);
       
   321         }
       
   322     IF_DEBUG(Print(_L("<<<CDpsXmlParser::OnContentL")));
       
   323     }
       
   324 
       
   325 // ---------------------------------------------------------------------------
       
   326 // 
       
   327 // ---------------------------------------------------------------------------
       
   328 //
       
   329 void CDpsXmlParser::OnStartPrefixMappingL(const RString& /*aPrefix*/, 
       
   330 										  const RString& /*aUri*/, 
       
   331 										  TInt aErrorCode)
       
   332     {
       
   333     IF_DEBUG(Print(_L(">>>CDpsXmlParser::OnStartPrefixMappingL")));
       
   334     if (aErrorCode != KErrNone)
       
   335         {
       
   336         IF_DEBUG(Print(_L("--- error code %d"), aErrorCode));
       
   337         User::Leave(aErrorCode);
       
   338         }
       
   339     IF_DEBUG(Print(_L("<<<CDpsXmlParser::OnStartPrefixMappingL")));	
       
   340     }
       
   341 
       
   342 // ---------------------------------------------------------------------------
       
   343 // 
       
   344 // ---------------------------------------------------------------------------
       
   345 //	
       
   346 void CDpsXmlParser::OnEndPrefixMappingL(const RString& /*aPrefix*/, 
       
   347 									    TInt aErrorCode)
       
   348     {
       
   349     IF_DEBUG(Print(_L(">>>CDpsXmlParser::OnEndPrefixMappingL")));
       
   350     if (aErrorCode != KErrNone)
       
   351         {
       
   352         IF_DEBUG(Print(_L("--- error code %d"), aErrorCode));
       
   353         User::Leave(aErrorCode);
       
   354         }
       
   355     IF_DEBUG(Print(_L("<<<CDpsXmlParser::OnEndPrefixMappingL")));	
       
   356     }
       
   357 
       
   358 // ---------------------------------------------------------------------------
       
   359 // 
       
   360 // ---------------------------------------------------------------------------
       
   361 //
       
   362 void CDpsXmlParser::OnIgnorableWhiteSpaceL(const TDesC8& /*aBytes*/, 
       
   363                                            TInt aErrorCode)
       
   364     {
       
   365     IF_DEBUG(Print(_L(">>>CDpsXmlParser::OnIgnorableWhiteSpaceL")));
       
   366     if (aErrorCode != KErrNone)
       
   367         {
       
   368         IF_DEBUG(Print(_L("---error code %d"), aErrorCode));
       
   369         User::Leave(aErrorCode);
       
   370         }
       
   371     IF_DEBUG(Print(_L("<<<CDpsXmlParser::OnIgnorableWhiteSpaceL")));	
       
   372     }
       
   373 
       
   374 // ---------------------------------------------------------------------------
       
   375 // 
       
   376 // ---------------------------------------------------------------------------
       
   377 //	
       
   378 void CDpsXmlParser::OnSkippedEntityL(const RString& /*aName*/, 
       
   379 								     TInt aErrorCode)
       
   380 	{
       
   381     IF_DEBUG(Print(_L(">>>CDpsXmlParser::OnSkippedEntityL")));
       
   382     if (aErrorCode != KErrNone)
       
   383         {
       
   384         IF_DEBUG(Print(_L("--- error code %d"), aErrorCode));
       
   385         User::Leave(aErrorCode);
       
   386         }
       
   387     IF_DEBUG(Print(_L("<<<CDpsXmlParser::OnSkippedEntityL")));	
       
   388     }
       
   389 
       
   390 // ---------------------------------------------------------------------------
       
   391 // 
       
   392 // ---------------------------------------------------------------------------
       
   393 //	
       
   394 void CDpsXmlParser::OnProcessingInstructionL(const TDesC8& /*aTarget*/, 
       
   395 											 const TDesC8& /*aData*/, 
       
   396 											 TInt aErrorCode)
       
   397     {
       
   398     IF_DEBUG(Print(_L(">>>CDpsXmlParser::OnProcessingInstructionL")));
       
   399     if (aErrorCode != KErrNone)
       
   400         {
       
   401         IF_DEBUG(Print(_L("--- error code %d"), aErrorCode));
       
   402         User::Leave(aErrorCode);
       
   403         }
       
   404     IF_DEBUG(Print(_L("<<<CDpsXmlParser::OnProcessingInstructionL")));	
       
   405     }
       
   406 
       
   407 // ---------------------------------------------------------------------------
       
   408 // 
       
   409 // ---------------------------------------------------------------------------
       
   410 //	
       
   411 void CDpsXmlParser::OnError(TInt aErrorCode)
       
   412     {
       
   413     IF_DEBUG(Print(_L(">>>CDpsXmlParser::OnError()")));
       
   414     if (aErrorCode != KErrNone)
       
   415         {
       
   416         IF_DEBUG(Print(_L("---error code %d"), aErrorCode));
       
   417         }
       
   418     IF_DEBUG(Print(_L("<<<CDpsXmlParser::OnError()")));	
       
   419     }
       
   420 
       
   421 // ---------------------------------------------------------------------------
       
   422 // 
       
   423 // ---------------------------------------------------------------------------
       
   424 //	
       
   425 
       
   426 TAny* CDpsXmlParser::GetExtendedInterface(const TInt32 aUid)
       
   427     {
       
   428     return aUid == MDpsExtensionHandler::EInterfaceUid ?
       
   429     static_cast<MDpsExtensionHandler*>(this) : 0;
       
   430     }
       
   431 
       
   432 // ---------------------------------------------------------------------------
       
   433 // 
       
   434 // ---------------------------------------------------------------------------
       
   435 //  
       
   436 void CDpsXmlParser::Reset()
       
   437     {
       
   438     iDpsArgs.Reset();
       
   439     iAction = EDpsXmlEmpty;
       
   440     iDpsEvent = EDpsEvtEmpty; 
       
   441     iDpsOperation = EDpsOpEmpty;
       
   442     iElement = EDpsEleEmpty; 
       
   443     iAttrib = 0;
       
   444     }
       
   445