00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <e32base.h>
00025 #include <e32cons.h>
00026
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
00061 CreateUriL();
00062
00063
00064 ModifyUriComponentsL();
00065
00066
00067 ResolveUriL();
00068
00069
00070 ParseUriL();
00071
00072
00073 ValidateUriComponentsL();
00074
00075
00076 ExtractUriComponentsL();
00077
00078
00079 RetrieveFileNameL();
00080
00081
00082 ModifyDelimiterL();
00083
00084
00085 WhiteSpaceRemover();
00086
00087
00088 EscapeEncodeDecodeL();
00089 }
00090
00091
00092
00093 void CExampleInetProtUtil::CreateUriL()
00094 {
00095
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
00103 TBuf<40> desFullUriName(KFullUriName);
00104 iConsole->Printf ( KLeaveALine );
00105 iConsole->Printf ( KFullUriName );
00106
00107
00108 CUri8* uri8 = CUri8::CreateFileUriL(desFullUriName);
00109
00110 const TDesC8& desUriDisplay = uri8->Uri().UriDes();
00111 TBuf16<100> desCreateUri;
00112 desCreateUri.Copy (desUriDisplay);
00113
00114
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
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
00137
00138
00139 _LIT8(KScheme, "http");
00140 _LIT(KScheme1, "\nSCHEME : http");
00141 iConsole->Printf ( KScheme1 );
00142 aUri->SetComponentL(KScheme, EUriScheme);
00143
00144
00145 _LIT8(KHost, "www.nokia.com");
00146 _LIT(KHost1, "\nHOST : www.nokia.com");
00147 iConsole->Printf ( KHost1 );
00148 aUri->SetComponentL(KHost, EUriHost);
00149
00150
00151 _LIT8(KPort, "80");
00152 _LIT(KPort1, "\nPORT : 80");
00153 iConsole->Printf ( KPort1 );
00154 aUri->SetComponentL(KPort, EUriPort);
00155
00156
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
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
00173 iConsole->Getch ();
00174 _LIT(KText5, "\n\nUri with the Port number removed....\n");
00175 iConsole->Printf ( KText5 );
00176 aUri->RemoveComponentL(EUriPort);
00177
00178
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
00191 void CExampleInetProtUtil::ParseUriL()
00192 {
00193
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
00202 HBufC8* convert8 = EscapeUtils::ConvertFromUnicodeToUtf8L(desFullUriName);
00203 TUriParser8 parser;
00204
00205
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
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
00229
00230
00231 _LIT8(KScheme, "http");
00232 aUri->SetComponentL(KScheme,EUriScheme);
00233
00234
00235 _LIT8(KHost, "waterlang.org");
00236 aUri->SetComponentL(KHost,EUriHost);
00237
00238
00239 _LIT8(KPort, "90");
00240 aUri->SetComponentL(KPort,EUriPort);
00241
00242
00243 _LIT8(KPath, "/turic8class.html");
00244 aUri->SetComponentL(KPath,EUriPath);
00245
00246
00247 _LIT8(KQuery, "bar=2&x=3");
00248 aUri->SetComponentL(KQuery,EUriQuery);
00249
00250
00251 _LIT8(KFragment, "fragment");
00252 aUri->SetComponentL(KFragment,EUriFragment);
00253
00254
00255 const TDesC8& desUriDisplays =aUri->Uri().UriDes();
00256 TBuf16<100> desValidate;
00257 desValidate.Copy (desUriDisplays);
00258 iConsole->Printf ( desValidate );
00259
00260
00261
00262
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
00276 void CExampleInetProtUtil::ExtractUriComponentsL()
00277 {
00278 _LIT(KTextExtract, "\n\n\nExtracting from....\n");
00279 iConsole->Printf ( KTextExtract );
00280
00281
00282 _LIT(KUriName, "K:\\ws\\direct\\direct.mmp");
00283 TBuf<40> desUriName(KUriName);
00284 CUri8* uriName = CUri8::CreateFileUriL(desUriName);
00285
00286
00287 const TDesC8& uriDisplay = uriName->Uri().UriDes();
00288 TBuf16<100> desExtract;
00289 desExtract.Copy (uriDisplay);
00290 iConsole->Printf ( desExtract );
00291
00292
00293 TUriParser8* uriComponent = new(ELeave) TUriParser8();
00294 uriComponent->Parse(uriDisplay);
00295
00296
00297 const TDesC8& host = uriComponent->Extract(EUriScheme);
00298 TBuf16<100> desEx;
00299 desEx.Copy (host);
00300
00301
00302 _LIT(KTextEx, "\nThe extracted Scheme component is....\n");
00303 iConsole->Printf ( KTextEx );
00304 iConsole->Printf ( desEx );
00305
00306
00307 delete uriComponent;
00308 delete uriName;
00309
00310 iConsole->Getch();
00311 }
00312
00313
00314
00315 void CExampleInetProtUtil::RetrieveFileNameL()
00316 {
00317 _LIT(KTextRet, "\n\n\nRetrieving filename from....\n");
00318 iConsole->Printf ( KTextRet );
00319
00320
00321 _LIT(KUriRetName, "K:\\ws\\direct\\direct.mmp");
00322 TBuf<40> desUriRetName(KUriRetName);
00323 CUri8* uriRetName = CUri8::CreateFileUriL(desUriRetName);
00324
00325
00326 const TDesC8& uriDisp = uriRetName->Uri().UriDes();
00327 TBuf16<100> desRetrieve;
00328 desRetrieve.Copy (uriDisp);
00329 iConsole->Printf ( desRetrieve );
00330
00331
00332 TUriParser8* uriComp = new(ELeave) TUriParser8();
00333 uriComp->Parse(uriDisp);
00334
00335
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
00353 void CExampleInetProtUtil::ModifyDelimiterL()
00354 {
00355
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
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
00372 HBufC8* convert8 = EscapeUtils::ConvertFromUnicodeToUtf8L(desFullUriName);
00373 delimiterModifyingObj->Parse(*convert8);
00374
00375
00376 TBool checkFrontDelimiter = delimiterModifyingObj->CheckFrontDelimiter();
00377
00378
00379 TBool checkBackDelimiter = delimiterModifyingObj->CheckBackDelimiter();
00380
00381
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
00410 void CExampleInetProtUtil::WhiteSpaceRemover()
00411 {
00412
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
00421 TPtrC desFullUriName(KFullUriPath);
00422
00423
00424 TInt consumedWhiteSpacesLeft = InetProtTextUtils::RemoveWhiteSpace(desFullUriName,InetProtTextUtils::ERemoveLeft);
00425
00426
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
00432 iConsole->Printf ( KDisplayAll,consumedWhiteSpacesLeft,&KTextAnd,consumedWhiteSpacesRight, &KTextInfo);
00433
00434 iConsole->Getch();
00435 }
00436
00437
00438 void CExampleInetProtUtil::EscapeEncodeDecodeL()
00439 {
00440
00441 _LIT(KFullUriName,"K:\\ws\\direct\\direct.mmp");
00442 TBuf<40> desFullUriName(KFullUriName);
00443
00444
00445 HBufC8* convert = EscapeUtils::ConvertFromUnicodeToUtf8L(desFullUriName);
00446
00447
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
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
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
00484 _LIT8(KScheme, "http");
00485 aUriBase->SetComponentL(KScheme,EUriScheme);
00486
00487
00488 _LIT8(KHost, "nokia.com");
00489 aUriBase->SetComponentL(KHost,EUriHost);
00490
00491
00492 _LIT8(KPort, "90");
00493 aUriBase->SetComponentL(KPort,EUriPort);
00494
00495
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
00509 _LIT8(KPath1, "/uris/base/reference/resolve.aspx");
00510 aUriRef->SetComponentL(KPath1,EUriPath);
00511
00512
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
00525 CUri8* Uri8 = CUri8::ResolveL(aUriBase->Uri(),aUriRef->Uri());
00526
00527
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
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