examples/AppProts/InetProtUtil/ExampleInetProtUtil.cpp

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

Generated by  doxygen 1.6.2