Nokia N97 SDK
Example Applications Guide

XeDomExample.cpp

00001 /*
00002 * XEDOMEXAMPLE.CPP
00003 * Copyright (c) 2007 Nokia Corporation.
00004 * This material, including documentation and any related computer
00005 * programs,is protected by copyright controlled by Nokia Corporation.
00006 */
00007 
00008 #include <e32base.h>
00009 #include "XeDomExample.h"
00010 
00011 _LIT(KXEDomExName, "XEDomExample");
00012 
00013 /**
00014  * EUMALOR_ENV is defined here for emulator platforms on which
00015  * testdata can be exported to C: in bld.inf file for this application.
00016  * For hardware platforms, the testdata files are assumed to be copied
00017  * to the drive on which this application is installed.
00018  */
00019  
00020 #ifdef __WINS__
00021 #define EUMALOR_ENV
00022 #elif __WINSCW__
00023 #define EUMALOR_ENV
00024 #endif
00025 
00026 /*
00027 * Implementation of CXEDomEx
00028 */
00029 
00030 
00031 CXEDomEx::CXEDomEx(const TDesC& aTestName):iTest(aTestName)
00032 {
00033         iTest.Start(KNullDesC);
00034 }
00035 
00036 EXPORT_C CXEDomEx::~CXEDomEx()
00037 {
00038     iTest.Close();
00039         iDomParser.Close();
00040         iDomImpl.Close();
00041 }
00042 
00043 EXPORT_C CXEDomEx* CXEDomEx::NewLC()
00044 {
00045         CXEDomEx* me = new(ELeave) CXEDomEx(KXEDomExName);
00046         CleanupStack::PushL(me);
00047         me->ConstructL();
00048         return me;
00049 }
00050 
00051 EXPORT_C CXEDomEx* CXEDomEx::NewL()
00052 {
00053         CXEDomEx* me = NewLC();
00054         CleanupStack::Pop(me);
00055         return me;
00056 }
00057 
00058 void CXEDomEx::GetDriveLetterL()
00059 {
00060 #ifdef EUMALOR_ENV
00061     _LIT(KCDrive, "C:");
00062     iDrive = KCDrive;
00063 #else
00064     RProcess thisProcess;
00065     TFileName myFileName = thisProcess.FileName();
00066         RFs fs;
00067         User::LeaveIfError( fs.Connect() );
00068         CleanupClosePushL(fs);
00069         
00070         TParse fp;
00071     fs.Parse(myFileName, fp);
00072     iDrive = fp.Drive();
00073     CleanupStack::PopAndDestroy(); //fs
00074 #endif
00075 }
00076 
00077 void CXEDomEx::ConstructL()
00078 {
00079         // Initialize the XmlEngine
00080         iDomImpl.OpenL();
00081         
00082         // Instantiate the parser
00083         TInt err = iDomParser.Open( iDomImpl );
00084         if(KErrNone != err)
00085             User::Leave(err);
00086         
00087         //Get the installation drive, required for testdata
00088         GetDriveLetterL();
00089         
00090         _LIT(KFormat, "\nUsing testdata from %S");
00091         iTest.Console()->ClearScreen();
00092         iTest.Console()->Printf(KFormat, &iDrive);
00093     PressAnyKey();
00094 }
00095 
00096 void CXEDomEx::PressAnyKey()
00097 {
00098     _LIT(KPressAnyKey, "\nPress any key to continue");
00099         iTest.Printf(KPressAnyKey);
00100         iTest.Getch();
00101 }
00102 
00103 HBufC8* CXEDomEx::ReadFileL(const TDesC& aFileName)
00104 {
00105         // open fs and file
00106         RFs fs;
00107         User::LeaveIfError( fs.Connect() );
00108         CleanupClosePushL(fs);
00109 
00110         RFile file;
00111         CleanupClosePushL(file);
00112         User::LeaveIfError(file.Open(fs, aFileName, EFileStream));
00113 
00114         TInt size=0;
00115         User::LeaveIfError(file.Size(size));
00116         HBufC8* buf = HBufC8::NewLC(size);
00117         TPtr8 bufPtr = buf->Des();
00118         User::LeaveIfError(file.Read(bufPtr));
00119 
00120         CleanupStack::Pop(buf);
00121         CleanupStack::PopAndDestroy(); //file
00122         CleanupStack::PopAndDestroy(); //fs
00123         return buf;
00124 }
00125 
00126 TInt CXEDomEx::GetSelection(const TDesC& aPrompt, const TDesC& aValidChoices)
00127 //      Present the user with a list of options, and get their selection
00128 {
00129     _LIT(KFormat1, "%S ");
00130     _LIT(KFormat2, "[%S] :");
00131         TKeyCode key = EKeyNull;
00132         iTest.Console()->SetPos (0, iTest.Console()->WhereY ());
00133         iTest.Console()->Printf(KFormat1, &aPrompt);
00134         iTest.Console()->Printf(KFormat2, &aValidChoices);
00135         TInt xPos = iTest.Console()->WhereX();
00136         
00137         TInt retVal = KErrNotFound;
00138         _LIT(KFormat3, "%c ");
00139         while (retVal == KErrNotFound)
00140         {
00141                 key = iTest.Getch();
00142                 iTest.Console()->Printf(KFormat3, key);
00143                 iTest.Console()->SetPos(xPos);
00144                 if(KErrNotFound != aValidChoices.Locate((TChar)key)) // Check if the choice is valid
00145                 {
00146                     retVal = key - '0';
00147                 }
00148         }
00149         
00150         _LIT(KNewLines, "\n\n");
00151         iTest.Console()->Printf(KNewLines);
00152         return retVal;
00153 }
00154 
00155 
00156 EXPORT_C void CXEDomEx::StartExampleL()
00157 {
00158         TBool done = EFalse;
00159         const TDesC *example = NULL;
00160         TInt err= KErrNone;
00161         
00162     // Repeat until user selects quit
00163         while (!done)
00164         {
00165                 iTest.Console()->ClearScreen();
00166                 _LIT(KSelectOption, " Select an option \n\n");
00167                 iTest.Printf(KSelectOption);
00168                 
00169                 _LIT(KPossibleSelectionsText, " 1) Document Parsing\n 2) Document Saving\n 3) Document Processing\n 4) Namespace Processing\n 5) Attributes Access\n 6) Elements Access\n 7) Data Containers\n 0) Quit\n");
00170                 _LIT(KPossibleSelections,"12345670");
00171                 TInt selection = GetSelection(KPossibleSelectionsText, KPossibleSelections);
00172 
00173                 // Set the example method to demonstrate for the user
00174                 _LIT(KParsing, "Document Parsing,");
00175                 _LIT(KSave, "Document Saving,");
00176                 _LIT(KDocProcess, "Document Processing,");
00177                 _LIT(KNsProcess, "Namespace Processing,");
00178                 _LIT(KAttributes, "Attributes Access,");
00179                 _LIT(KElements, "Elements Access,");
00180                 _LIT(KContainer, "Data Containers,");
00181                 _LIT(KQuit, "Example application exit...");
00182                 switch (selection)
00183                 {
00184                         case EParsing:
00185                         TRAP(err, ParseExampleL());
00186                         example = &KParsing;
00187                         break;
00188 
00189                         case ESave:
00190                         TRAP(err, SaveExampleL());
00191                         example = &KSave;
00192                         break;
00193 
00194                         case EDocProcess:
00195                         TRAP(err, DocProcessingExampleL());
00196                         example = &KDocProcess;
00197                         break;
00198 
00199                         case ENsProcess:
00200                         TRAP(err, NsProcessingExampleL());
00201                         example = &KNsProcess;
00202                         break;
00203 
00204                         case EAttributes:
00205                         TRAP(err, AttributesExampleL());
00206                         example = &KAttributes;
00207                         break;
00208 
00209                         case EElements:
00210                         TRAP(err, ElementsExampleL());
00211                         example = &KElements;
00212                         break;
00213 
00214                         case EContainer:
00215                         TRAP(err, DataContainersExampleL());
00216                         example = &KContainer;
00217                         break;
00218 
00219                         case EQuit:
00220                         done = ETrue;
00221                         iTest.Console()->Printf(KQuit);
00222                         break;
00223 
00224                         default:
00225                         ;
00226                 }
00227 
00228         if(!done)
00229         {
00230             _LIT(KPathNotFound, "Input/output path not found");
00231             _LIT(KMsg1, "Error occurred, error code:%d\n ");
00232             _LIT(KMsg2, "%S\n executed successfully.");
00233                 if(err == KErrPathNotFound)
00234                     iTest.Console()->Printf(KPathNotFound);
00235                 else
00236                 {
00237                     if(err != KErrNone)
00238                             iTest.Console()->Printf(KMsg1, err);
00239                     else
00240                             iTest.Console()->Printf(KMsg2, example);
00241                 }
00242                 }
00243 
00244                 PressAnyKey();
00245         } // while
00246 }
00247 
00248 
00249 // Called from E32Main()
00250 LOCAL_D void TestL()
00251 {
00252         // Create and start the client
00253         CXEDomEx* domEx = CXEDomEx::NewLC();
00254         domEx->StartExampleL();
00255 
00256         CleanupStack::PopAndDestroy(domEx);
00257 }
00258 
00259 
00260 GLDEF_C TInt E32Main()
00261 // Main program - run the tests within a TRAP harness, reporting any errors.
00262 {
00263         __UHEAP_MARK;
00264         CTrapCleanup* tc=CTrapCleanup::New();
00265 
00266         TRAPD(err,TestL());
00267         
00268         _LIT(KPanic, "Application failed, error: %i");
00269         if (err!=KErrNone)
00270             User::Panic(KPanic, err);
00271         
00272         delete tc;
00273         __UHEAP_MARKEND;
00274         return KErrNone;
00275 }

© Nokia 2009

Back to top