examples/AppProts/InetProtUtil/ExampleInetProtUtil.cpp

00001 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
00002 // All rights reserved.
00003 // This component and the accompanying materials are made available
00004 // under the terms of "Eclipse Public License v1.0"
00005 // which accompanies this distribution, and is available
00006 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
00007 //
00008 // Initial Contributors:
00009 // Nokia Corporation - initial contribution.
00010 //
00011 // Contributors:
00012 //
00013 // Description:
00014 // ExampleInetProtUtil is a sample code for understanding the applicability of InetProtUtil. 
00015 // It is intended as an example and introduction to the INETPROTUTIL API's.
00016 // Users should have a brief understanding of URIs and the different concepts
00017 // associated with it, like, URLs and URI components. 
00018 //
00019 
00020 
00021 
00022 
00023 // System includes
00024 #include <e32base.h>
00025 #include <e32cons.h>
00026 // Local include
00027 #include "exampleInetProtUtil.h"
00028 
00029 _LIT ( KTest, "InetProtUtil Example" );
00030 _LIT ( KInetProtUtilExamplePanic, "EXAMPLES");
00031 _LIT ( KDisplayAll, "\n%d %S %d %S " );
00032 _LIT ( KLeaveALine, "\n" );
00033 
00034 CExampleInetProtUtil::CExampleInetProtUtil()
00035         {
00036         }
00037         
00038 CExampleInetProtUtil::~CExampleInetProtUtil()
00039         {
00040         delete iConsole;        
00041         }
00042         
00043 void CExampleInetProtUtil::NewL()
00044         {
00045         CExampleInetProtUtil *self = new (ELeave) CExampleInetProtUtil;
00046         CleanupStack::PushL(self);
00047         self->ConstructL();
00048         CleanupStack::PopAndDestroy(self);
00049         }
00050 
00051 void CExampleInetProtUtil::ConstructL()
00052         {       
00053         
00054         iConsole = Console::NewL(KTest,TSize(KConsFullScreen,KConsFullScreen));
00055         iConsole->Printf ( KTest );
00056         _LIT(KTextStart, "\nPress any key to step through the example");
00057         iConsole->Printf ( KTextStart );
00058         iConsole->Getch ();
00059 
00060         //Create an URI
00061         CreateUriL();  
00062 
00063         //Modify URI components
00064         ModifyUriComponentsL();
00065         
00066         //Resolve
00067         ResolveUriL();
00068         
00069         //Parse the URI
00070         ParseUriL();
00071         
00072         //Validate URI components
00073         ValidateUriComponentsL();
00074         
00075         //Extract URI components
00076         ExtractUriComponentsL();
00077         
00078         //Retrieve Filename from a given Uri
00079         RetrieveFileNameL();
00080         
00081         //Add and Trim delimiters
00082         ModifyDelimiterL();
00083         
00084         //Text Utilities
00085         WhiteSpaceRemover();
00086         
00087         //Escape encode and decode
00088         EscapeEncodeDecodeL();  
00089         }
00090         
00091         
00092 //Create an URI
00093 void CExampleInetProtUtil::CreateUriL()
00094         {
00095         //Set the physical path of the file
00096         
00097         _LIT(KText1, "\n\n\nThe Physical location of the file is....");
00098         iConsole->Printf ( KText1 );
00099         
00100         _LIT(KFullUriName, "K:\\ws\\direct\\direct.mmp");
00101 
00102         //display it 
00103         TBuf<40> desFullUriName(KFullUriName);
00104         iConsole->Printf ( KLeaveALine );
00105         iConsole->Printf ( KFullUriName );
00106         
00107         //create the Uri for the path
00108         CUri8* uri8 = CUri8::CreateFileUriL(desFullUriName); 
00109         
00110         const TDesC8& desUriDisplay = uri8->Uri().UriDes();
00111         TBuf16<100> desCreateUri;
00112         desCreateUri.Copy (desUriDisplay);
00113         
00114         //display it 
00115         _LIT(KText2, "And its Uri is....");
00116         iConsole->Printf ( KLeaveALine );
00117         iConsole->Printf ( KText2 );
00118         
00119         iConsole->Printf ( KLeaveALine );
00120         iConsole->Printf ( desCreateUri );
00121         
00122         delete uri8;
00123         
00124         iConsole->Getch ();
00125         }
00126         
00127 //Modify URI components
00128 void CExampleInetProtUtil::ModifyUriComponentsL()
00129         {       
00130         TUriParser8 parser1;
00131         CUri8* aUri = CUri8::NewL(parser1);
00132         
00133         _LIT(KText3, "\n\n\nAdding Uri components one by one....");
00134         iConsole->Printf ( KText3 );
00135 
00136         // Adding components to the Uri
00137         
00138         //Setting and displaying SCHEME
00139         _LIT8(KScheme, "http");
00140         _LIT(KScheme1, "\nSCHEME : http");
00141         iConsole->Printf ( KScheme1 );
00142         aUri->SetComponentL(KScheme, EUriScheme);
00143         
00144         //Setting and displaying HOST
00145         _LIT8(KHost, "www.nokia.com");
00146         _LIT(KHost1, "\nHOST : www.nokia.com");
00147         iConsole->Printf ( KHost1 );
00148         aUri->SetComponentL(KHost, EUriHost);
00149         
00150         //Setting and displaying PORT
00151         _LIT8(KPort, "80");
00152         _LIT(KPort1, "\nPORT : 80");
00153         iConsole->Printf ( KPort1 );
00154         aUri->SetComponentL(KPort, EUriPort);
00155         
00156         //Setting and displaying PATH
00157         _LIT8(KPath, "/developer/techlib/turic8class.html");
00158         _LIT(KPath1, "\nPATH : /developer/techlib/turic8class.html");
00159         iConsole->Printf ( KPath1 );
00160         aUri->SetComponentL(KPath, EUriPath);
00161         
00162         //Display the constucted Uri
00163         _LIT(KText4, "\nThe fully constructed Uri....");
00164         iConsole->Printf ( KText4 );
00165         const TDesC8& desUriDisplay = aUri->Uri().UriDes();
00166         TBuf16<100> desFullUri;
00167         desFullUri.Copy (desUriDisplay);
00168         
00169         iConsole->Printf( KLeaveALine );
00170         iConsole->Printf ( desFullUri );
00171         
00172         // Removal of component from the Uri
00173         iConsole->Getch ();
00174         _LIT(KText5, "\n\nUri with the Port number removed....\n");
00175         iConsole->Printf ( KText5 );
00176         aUri->RemoveComponentL(EUriPort);
00177 
00178         //Display the modified Uri
00179         const TDesC8& desRemovedComponentDisplay =aUri->Uri().UriDes();
00180         TBuf16<100> desRemovedComponent;
00181         desRemovedComponent.Copy (desRemovedComponentDisplay);
00182         iConsole->Printf ( desRemovedComponent );
00183         
00184         delete aUri;
00185         
00186         iConsole->Getch ();
00187         }
00188         
00189         
00190 //Parse the URI
00191 void CExampleInetProtUtil::ParseUriL()
00192         {
00193         //Create a Uri
00194         _LIT(KText6, "\n\n\nParsing the Uri for....\n");
00195         iConsole->Printf ( KText6 );
00196 
00197         _LIT(KFullUriName,"K:\\ws\\direct\\direct.mmp");
00198         TBuf<40> desFullUriName(KFullUriName);
00199         iConsole->Printf ( desFullUriName );
00200         
00201         //Convert from Unicode format to UTF-8 format
00202         HBufC8* convert8 = EscapeUtils::ConvertFromUnicodeToUtf8L(desFullUriName);
00203         TUriParser8 parser;
00204 
00205         //Parse the Uri
00206         TInt errResult = parser.Parse(*convert8);
00207         
00208         if (errResult==KErrNone)
00209         {
00210                 _LIT(KTextParsing, "\nThis Uri has been parsed successfully");
00211                 iConsole->Printf ( KTextParsing );
00212         }
00213         
00214         delete convert8;
00215 
00216         iConsole->Getch();
00217         }
00218 
00219 //Validate URI components
00220 void CExampleInetProtUtil::ValidateUriComponentsL()
00221         {
00222         TUriParser8 parser1;
00223         CUri8* aUri = CUri8::NewL(parser1);
00224         
00225         _LIT(KTextf, "\n\n\nValidating the Uri....\n");
00226         iConsole->Printf ( KTextf );
00227         
00228         // Adding components to the Uri
00229         
00230         //Adding Scheme
00231         _LIT8(KScheme, "http");
00232         aUri->SetComponentL(KScheme,EUriScheme);
00233         
00234         //Adding Host
00235         _LIT8(KHost, "waterlang.org");
00236         aUri->SetComponentL(KHost,EUriHost);
00237         
00238         //Adding Port
00239         _LIT8(KPort, "90");
00240         aUri->SetComponentL(KPort,EUriPort);
00241         
00242         //Adding Path
00243         _LIT8(KPath, "/turic8class.html");
00244         aUri->SetComponentL(KPath,EUriPath);
00245         
00246         //Adding Query
00247         _LIT8(KQuery, "bar=2&x=3");
00248         aUri->SetComponentL(KQuery,EUriQuery);
00249         
00250         //Adding Fragment
00251         _LIT8(KFragment, "fragment");
00252         aUri->SetComponentL(KFragment,EUriFragment);
00253         
00254         //Display the constructed Uri
00255         const TDesC8& desUriDisplays =aUri->Uri().UriDes();
00256         TBuf16<100> desValidate;
00257         desValidate.Copy (desUriDisplays);
00258         iConsole->Printf ( desValidate );
00259         
00260         // Validate() is not supported for HTTP, but only SIP and SIPS. 
00261         //The Parse() function itself validates the components and returns the 
00262         //appropriate result.
00263         TInt res = parser1.Parse(desUriDisplays);
00264         if (res==KErrNone)
00265         {
00266                 _LIT(KText8, "\nThis Uri is VALID");
00267                 iConsole->Printf ( KText8 );
00268         }
00269         
00270         delete aUri;
00271         
00272         iConsole->Getch();
00273         }
00274 
00275 //Extract URI components
00276 void CExampleInetProtUtil::ExtractUriComponentsL()
00277         {
00278         _LIT(KTextExtract, "\n\n\nExtracting from....\n");
00279         iConsole->Printf ( KTextExtract );
00280         
00281         //Create a Uri 
00282         _LIT(KUriName, "K:\\ws\\direct\\direct.mmp");
00283         TBuf<40> desUriName(KUriName);
00284         CUri8* uriName = CUri8::CreateFileUriL(desUriName);  
00285         
00286         //Display the Uri
00287         const TDesC8& uriDisplay = uriName->Uri().UriDes();
00288         TBuf16<100> desExtract;
00289         desExtract.Copy (uriDisplay);
00290         iConsole->Printf ( desExtract );
00291         
00292         //Parse the Uri
00293         TUriParser8* uriComponent = new(ELeave) TUriParser8();
00294         uriComponent->Parse(uriDisplay);
00295         
00296         //Extract the Scheme component from this Uri
00297         const TDesC8& host = uriComponent->Extract(EUriScheme);
00298         TBuf16<100> desEx;
00299         desEx.Copy (host);
00300                 
00301         //Display the Component extracted
00302         _LIT(KTextEx, "\nThe extracted Scheme component is....\n");
00303         iConsole->Printf ( KTextEx );
00304         iConsole->Printf ( desEx );
00305         
00306         //delete fileName;
00307         delete uriComponent;
00308         delete uriName;
00309         
00310         iConsole->Getch();
00311         }
00312         
00313         
00314 //Extract URI components
00315 void CExampleInetProtUtil::RetrieveFileNameL()
00316         {
00317         _LIT(KTextRet, "\n\n\nRetrieving filename from....\n");
00318         iConsole->Printf ( KTextRet );
00319         
00320         //Create a Uri 
00321         _LIT(KUriRetName, "K:\\ws\\direct\\direct.mmp");
00322         TBuf<40> desUriRetName(KUriRetName);
00323         CUri8* uriRetName = CUri8::CreateFileUriL(desUriRetName);  
00324         
00325         //Display the Uri
00326         const TDesC8& uriDisp = uriRetName->Uri().UriDes();
00327         TBuf16<100> desRetrieve;
00328         desRetrieve.Copy (uriDisp);
00329         iConsole->Printf ( desRetrieve );
00330         
00331         //Parse the Uri
00332         TUriParser8* uriComp = new(ELeave) TUriParser8();
00333         uriComp->Parse(uriDisp);
00334         
00335         //Get or Extract the Filename from the Uri
00336         _LIT(KTextGetFilename, "\nGetting the filename....\n");
00337         iConsole->Printf ( KTextGetFilename );
00338         
00339         HBufC* fileName = uriComp->GetFileNameL();
00340         TPtr uriFileNameDisplay = fileName->Des();
00341         TBuf16<100> desFileName;
00342         desFileName.Copy (uriFileNameDisplay);
00343         iConsole->Printf ( desFileName );
00344 
00345         delete fileName;
00346         delete uriComp;
00347         delete uriRetName;
00348         
00349         iConsole->Getch();
00350         }
00351         
00352 //Modify the Uri w.r.t Delimiters       
00353 void CExampleInetProtUtil::ModifyDelimiterL()
00354         {
00355         // First set the delimiter before performing any operation
00356         _LIT(KTextDelimit1, "\n\n\nThe Delimiter is...\n");
00357         iConsole->Printf ( KTextDelimit1 );
00358         CExampleDelimiterModifier* delimiterModifyingObj = new(ELeave) CExampleDelimiterModifier;
00359         delimiterModifyingObj->SetDelimiter(TChar(';'));
00360         
00361         //Display the delimiter
00362         _LIT(KTextDelimit2, ";\n");
00363         iConsole->Printf ( KTextDelimit2 );
00364         
00365         _LIT(KTextDelimit, "to be checked in...\n");
00366         iConsole->Printf ( KTextDelimit );
00367         _LIT(KFullUriName,"K:\\ws\\direct\\direct.mmp;");
00368         TBuf<40> desFullUriName(KFullUriName);
00369         iConsole->Printf ( desFullUriName );
00370         
00371         //Then parse the Uri
00372         HBufC8* convert8 = EscapeUtils::ConvertFromUnicodeToUtf8L(desFullUriName);
00373         delimiterModifyingObj->Parse(*convert8);
00374         
00375         //Check if the delimiter is present in the front
00376         TBool checkFrontDelimiter = delimiterModifyingObj->CheckFrontDelimiter();
00377         
00378         //Check if the delimiter is present at the back
00379         TBool checkBackDelimiter = delimiterModifyingObj->CheckBackDelimiter();
00380         
00381         //Display the result
00382         if (!checkFrontDelimiter)
00383         {
00384                 _LIT(KTextDelimit3, "\nNo delimiter in the front");
00385                 iConsole->Printf ( KTextDelimit3 );
00386         }
00387         else
00388         {
00389                 _LIT(KTextDelimit4, "\nDelimiter is present in the front");
00390                 iConsole->Printf ( KTextDelimit4 );
00391         }
00392         if (!checkBackDelimiter)
00393         {
00394                 _LIT(KTextDelimit5, "\nNo delimiter at the back");
00395                 iConsole->Printf ( KTextDelimit5 );
00396         }
00397         else
00398         {
00399                 _LIT(KTextDelimit6, "\nDelimiter is present at the back");
00400                 iConsole->Printf ( KTextDelimit6 );
00401         }
00402 
00403         delete convert8;
00404         delete delimiterModifyingObj;
00405         
00406         iConsole->Getch();
00407         }
00408         
00409 //Check for white spaces in the Uri
00410 void CExampleInetProtUtil::WhiteSpaceRemover()
00411         {
00412         //Take an eg file and insert white spaces in the front and rear
00413         _LIT(KTextWhiteSpace, "\n\n\nThe Uri containing white spaces....\n");
00414         iConsole->Printf ( KTextWhiteSpace );
00415         
00416         _LIT(KFullUriPath,"     K:\\ws\\direct\\direct.mmp  ");
00417         TBuf<40> desFullUriPath(KFullUriPath);
00418         iConsole->Printf ( desFullUriPath );
00419         
00420         //Gets the Whitespaces on the right and left of the Uri
00421         TPtrC desFullUriName(KFullUriPath);
00422         
00423         //Check for white spaces in the front or on the left of the Uri
00424         TInt consumedWhiteSpacesLeft = InetProtTextUtils::RemoveWhiteSpace(desFullUriName,InetProtTextUtils::ERemoveLeft);
00425         
00426         //Check for white spaces at the back or on the right of the Uri
00427         TInt consumedWhiteSpacesRight = InetProtTextUtils::RemoveWhiteSpace(desFullUriName,InetProtTextUtils::ERemoveRight);
00428         _LIT(KTextAnd, " and ");
00429         _LIT(KTextInfo, " white spaces have been removed from the Uri");
00430         
00431         //Display the number of white spaces removed from Uri
00432         iConsole->Printf ( KDisplayAll,consumedWhiteSpacesLeft,&KTextAnd,consumedWhiteSpacesRight, &KTextInfo);
00433         
00434         iConsole->Getch();
00435         }
00436         
00437 //Encode and Decode the Uri
00438 void CExampleInetProtUtil::EscapeEncodeDecodeL()
00439         {
00440         //Take an eg file to encode it and then decode it....
00441         _LIT(KFullUriName,"K:\\ws\\direct\\direct.mmp");
00442         TBuf<40> desFullUriName(KFullUriName);
00443         
00444         //UTF-8 defines a mapping from sequences of octets to sequences of chars
00445         HBufC8* convert = EscapeUtils::ConvertFromUnicodeToUtf8L(desFullUriName);
00446 
00447         //Encode the eg Uri and display it
00448         _LIT(KTextEncode, "\n\n\nThe Encoded Uri is....\n");
00449         iConsole->Printf ( KTextEncode );
00450         HBufC16* encode = EscapeUtils::EscapeEncodeL(desFullUriName,EscapeUtils::EEscapeNormal);
00451         TPtr uriEncoded = encode->Des();
00452         TBuf16<100> desEncodedUri;
00453         desEncodedUri.Copy (uriEncoded);
00454         iConsole->Printf ( _L("%S"), &desEncodedUri );
00455 
00456         //Decode the eg Uri and display it
00457         _LIT(KTextDecode, "\nThe Decoded Uri is....\n");
00458         iConsole->Printf ( KTextDecode );
00459         
00460         HBufC16* decode = EscapeUtils::EscapeDecodeL(desFullUriName);
00461         TPtr uriDecoded = decode->Des();
00462         TBuf16<100> desDecodedUri;
00463         desDecodedUri.Copy (uriDecoded);
00464         iConsole->Printf ( _L("%S"), &desDecodedUri );
00465 
00466         delete decode;
00467         delete encode;
00468         delete convert;
00469 
00470         iConsole->Getch();
00471         iConsole->Printf ( KLeaveALine );
00472         }
00473         
00474 //Resolve the Uri w.r.t a Base and a refernce Uri
00475 void CExampleInetProtUtil::ResolveUriL()
00476         {
00477         _LIT(KTextResolve1, "\n\n\nThe Base and reference Uris are\n");
00478         iConsole->Printf ( KTextResolve1 );
00479         
00480         TUriParser8 parserResolve1;
00481         CUri8* aUriBase = CUri8::NewL(parserResolve1);
00482 
00483         //Adding Scheme
00484         _LIT8(KScheme, "http");
00485         aUriBase->SetComponentL(KScheme,EUriScheme);
00486         
00487         //Adding Host
00488         _LIT8(KHost, "nokia.com");
00489         aUriBase->SetComponentL(KHost,EUriHost);
00490         
00491         //Adding Port
00492         _LIT8(KPort, "90");
00493         aUriBase->SetComponentL(KPort,EUriPort);
00494         
00495         //Adding Path
00496         _LIT8(KPath, "/resolve.aspx");
00497         aUriBase->SetComponentL(KPath,EUriPath);
00498         
00499         const TDesC8& desBaseUri =aUriBase->Uri().UriDes();
00500         TBuf16<100> desBase;
00501         desBase.Copy (desBaseUri);
00502         iConsole->Printf ( desBase );
00503         
00504         iConsole->Printf( KLeaveALine );
00505         
00506         CUri8* aUriRef = CUri8::NewL(parserResolve1);
00507         
00508         //Adding Path
00509         _LIT8(KPath1, "/uris/base/reference/resolve.aspx");
00510         aUriRef->SetComponentL(KPath1,EUriPath);
00511         
00512         //Adding Query
00513         _LIT8(KQuery1, "bar=2&x=3");
00514         aUriRef->SetComponentL(KQuery1,EUriQuery);
00515         
00516         const TDesC8& desRefUri =aUriRef->Uri().UriDes();
00517         TBuf16<100> desRef;
00518         desRef.Copy (desRefUri);
00519         iConsole->Printf ( desRef );
00520         
00521         _LIT(KTextResolve, "\nThe Resolved Uri is....\n");
00522         iConsole->Printf ( KTextResolve );
00523         
00524         //Resolve the 2 Uri's to get a resultant uri
00525         CUri8* Uri8 = CUri8::ResolveL(aUriBase->Uri(),aUriRef->Uri());
00526         
00527         // Display the resultant Uri
00528         const TDesC8& desDisplayReslovedUri = Uri8->Uri().UriDes();
00529         TBuf16<100> desResolve;
00530         desResolve.Copy (desDisplayReslovedUri);
00531         iConsole->Printf ( desResolve );
00532         
00533         delete Uri8;
00534         delete aUriRef;
00535         delete aUriBase; 
00536         
00537         iConsole->Getch ();
00538         }
00539 
00540         
00542 //                              Main                                  //
00544 
00545 GLDEF_C TInt E32Main()
00546         {
00547         __UHEAP_MARK;
00548         CTrapCleanup* tc = CTrapCleanup::New();
00549         TRAPD(err, CExampleInetProtUtil::NewL());
00550         if (err != KErrNone)
00551                 User::Panic(KInetProtUtilExamplePanic,err);
00552         delete tc;
00553         __UHEAP_MARKEND;
00554         return KErrNone;
00555         }
00556 
00557         

Generated by  doxygen 1.6.2