|
1 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 |
|
17 // INCLUDE FILES |
|
18 #include "TestContentHandler.h" |
|
19 #include "XMLOutputContentHandler.h" |
|
20 #include "xmlengtester.h" |
|
21 |
|
22 #include <e32svr.h> |
|
23 #include <domain/osextensions/StifParser.h> |
|
24 #include <domain/osextensions/Stiftestinterface.h> |
|
25 |
|
26 #include <Xml\Parser.h> |
|
27 #include <Xml\ParserFeature.h> |
|
28 #include <Xml\MatchData.h> |
|
29 |
|
30 #include <libc\string.h> |
|
31 #include <charconv.h> |
|
32 #include <UTF.H> |
|
33 |
|
34 using namespace Xml; |
|
35 |
|
36 /****************************** SAX TESTs **************************************/ |
|
37 |
|
38 // ----------------------------------------------------------------------------- |
|
39 // CLibxml2Tester::ParseFileSAXL |
|
40 // test method function. |
|
41 // (other items were commented in a header). |
|
42 // ----------------------------------------------------------------------------- |
|
43 // |
|
44 TInt CLibxml2Tester::ParseFileSAXL( CStifItemParser& aItem ) |
|
45 { |
|
46 _LIT(KSourceFileErrorName,"No source file name."); |
|
47 _LIT(KOutputFileErrorName,"No output file name."); |
|
48 _LIT(KStartParsing,"Start parsing file using SAX."); |
|
49 _LIT(KEndParsing,"Parsing end."); |
|
50 _LIT(KOutputFileError,"Output file error: %d."); |
|
51 |
|
52 TPtrC SrcFile; |
|
53 if ( aItem.GetNextString(SrcFile) != KErrNone ) |
|
54 { |
|
55 TestModuleIf().Printf( infoNum++,KTEST_NAME,KSourceFileErrorName); |
|
56 iLog->Log((TDesC)KSourceFileErrorName); |
|
57 return KErrGeneral; |
|
58 } |
|
59 |
|
60 TPtrC OutputFile; |
|
61 if ( aItem.GetNextString(OutputFile) != KErrNone ) |
|
62 { |
|
63 TestModuleIf().Printf( infoNum++,KTEST_NAME,KOutputFileErrorName); |
|
64 iLog->Log((TDesC)KOutputFileErrorName); |
|
65 return KErrGeneral; |
|
66 } |
|
67 |
|
68 TBool lib = TRUE; |
|
69 TPtrC tmp; |
|
70 if ( aItem.GetNextString(tmp) == KErrNone ) |
|
71 { |
|
72 lib = FALSE; |
|
73 } |
|
74 |
|
75 //create file sesion and files |
|
76 RFs fs; |
|
77 User::LeaveIfError(fs.Connect()); |
|
78 CleanupClosePushL(fs); |
|
79 |
|
80 |
|
81 RFile outputFile; |
|
82 TFileName outFileName; |
|
83 outFileName.Copy(OutputFile); |
|
84 |
|
85 TFileName srcFileName; |
|
86 srcFileName.Copy(SrcFile); |
|
87 |
|
88 // Open output file |
|
89 TInt err = outputFile.Replace(fs,outFileName,EFileShareAny|EFileWrite); |
|
90 |
|
91 // If output file is correct |
|
92 if (err == KErrNone) |
|
93 { |
|
94 |
|
95 // Create content handler |
|
96 CTestHandler *sax = CTestHandler::NewL(outputFile); |
|
97 |
|
98 TestModuleIf().Printf( infoNum++,KTEST_NAME,KStartParsing); |
|
99 iLog->Log((TDesC)KStartParsing); |
|
100 |
|
101 // Create parser |
|
102 // 1. Create CMatchData object |
|
103 CMatchData *matchData = CMatchData::NewL(); |
|
104 |
|
105 // 2. Set mime Type |
|
106 matchData->SetMimeTypeL(KXmlMimeType); |
|
107 |
|
108 // 3. Set Variant ID |
|
109 matchData->SetVariantL(_L8("libxml2")); |
|
110 |
|
111 CParser* parser; |
|
112 if(lib) |
|
113 { |
|
114 parser = CParser::NewL(*matchData, *sax); |
|
115 } |
|
116 else |
|
117 { |
|
118 parser = CParser::NewL(KXmlMimeType, *sax); |
|
119 } |
|
120 |
|
121 // Parse file |
|
122 TInt error; |
|
123 |
|
124 TRAP(error, Xml::ParseL(*parser,fs,srcFileName)); |
|
125 if (error!=KErrNone) |
|
126 { |
|
127 // threats exceptions throwed by "ParseL" |
|
128 } |
|
129 |
|
130 delete parser; |
|
131 delete matchData; |
|
132 delete sax; |
|
133 |
|
134 outputFile.Close(); |
|
135 } |
|
136 else |
|
137 { |
|
138 TestModuleIf().Printf( infoNum++,KTEST_NAME, KOutputFileError, err); |
|
139 iLog->Log(KOutputFileError, err); |
|
140 } |
|
141 |
|
142 CleanupStack::PopAndDestroy(&fs); |
|
143 |
|
144 TestModuleIf().Printf( infoNum++,KTEST_NAME, KEndParsing); |
|
145 iLog->Log((TDesC)KEndParsing); |
|
146 |
|
147 return KErrNone; |
|
148 |
|
149 } |
|
150 |
|
151 // ----------------------------------------------------------------------------- |
|
152 // CLibxml2Tester::ParseFileSAXNSL |
|
153 // Example test method function. |
|
154 // (other items were commented in a header). |
|
155 // ----------------------------------------------------------------------------- |
|
156 // |
|
157 TInt CLibxml2Tester::ParseFileSAXNSL( CStifItemParser& aItem ) |
|
158 { |
|
159 _LIT(KSourceFileErrorName,"No source file name."); |
|
160 _LIT(KOutputFileErrorName,"No output file name."); |
|
161 _LIT(KStartParsing,"Start parsing file using SAX."); |
|
162 _LIT(KEndParsing,"Parsing end."); |
|
163 _LIT(KSetFeature,"Setting feature name: %d."); |
|
164 _LIT(KOutputFileError,"Output file error: %d."); |
|
165 |
|
166 TPtrC SrcFile; |
|
167 if ( aItem.GetNextString(SrcFile) != KErrNone ) |
|
168 { |
|
169 TestModuleIf().Printf( infoNum++,KTEST_NAME,KSourceFileErrorName); |
|
170 iLog->Log((TDesC)KSourceFileErrorName); |
|
171 return KErrGeneral; |
|
172 } |
|
173 |
|
174 TPtrC OutputFile; |
|
175 if ( aItem.GetNextString(OutputFile) != KErrNone ) |
|
176 { |
|
177 TestModuleIf().Printf( infoNum++,KTEST_NAME,KOutputFileErrorName); |
|
178 iLog->Log((TDesC)KOutputFileErrorName); |
|
179 return KErrGeneral; |
|
180 } |
|
181 |
|
182 // create file sesion and files |
|
183 RFs fs; |
|
184 User::LeaveIfError(fs.Connect()); |
|
185 CleanupClosePushL(fs); |
|
186 |
|
187 RFile outputFile; |
|
188 TFileName outFileName; |
|
189 outFileName.Copy(OutputFile); |
|
190 |
|
191 TFileName srcFileName; |
|
192 srcFileName.Copy(SrcFile); |
|
193 |
|
194 // Open output file |
|
195 TInt err = outputFile.Replace(fs,outFileName,EFileShareAny|EFileWrite); |
|
196 |
|
197 // If output file is correct |
|
198 if (err == KErrNone) |
|
199 { |
|
200 // Create content handler |
|
201 CTestHandler *sax = CTestHandler::NewL(outputFile); |
|
202 |
|
203 // Create parser |
|
204 // 1. Create CMatchData object |
|
205 CMatchData *matchData = CMatchData::NewL(); |
|
206 |
|
207 // 2. Set mime Type |
|
208 matchData->SetMimeTypeL(KXmlMimeType); |
|
209 |
|
210 // 3. Set Variant ID |
|
211 matchData->SetVariantL(_L8("libxml2")); |
|
212 |
|
213 CParser* parser = CParser::NewL(*matchData, *sax); |
|
214 |
|
215 TestModuleIf().Printf( infoNum++,KTEST_NAME,KSetFeature,EReportNamespaceMapping); |
|
216 iLog->Log(KSetFeature,EReportNamespaceMapping); |
|
217 |
|
218 parser->EnableFeature(EReportNamespaceMapping); |
|
219 |
|
220 TestModuleIf().Printf( infoNum++,KTEST_NAME,KStartParsing); |
|
221 iLog->Log((TDesC)KStartParsing); |
|
222 |
|
223 // Parse file |
|
224 TInt error; |
|
225 TRAP(error, Xml::ParseL(*parser,fs,srcFileName)); |
|
226 if (error!=KErrNone) |
|
227 { |
|
228 // threats exceptions throwed by "ParseL" |
|
229 } |
|
230 delete matchData; |
|
231 delete parser; |
|
232 delete sax; |
|
233 |
|
234 outputFile.Close(); |
|
235 } |
|
236 else |
|
237 { |
|
238 TestModuleIf().Printf( infoNum++,KTEST_NAME,KOutputFileError, err); |
|
239 iLog->Log(KOutputFileError, err); |
|
240 } |
|
241 |
|
242 CleanupStack::PopAndDestroy(&fs); |
|
243 |
|
244 TestModuleIf().Printf( infoNum++,KTEST_NAME,KEndParsing); |
|
245 iLog->Log((TDesC)KEndParsing); |
|
246 |
|
247 return KErrNone; |
|
248 |
|
249 } |
|
250 |
|
251 // ----------------------------------------------------------------------------- |
|
252 // CLibxml2Tester::ParseFileSAXLowerCaseL |
|
253 // Example test method function. |
|
254 // (other items were commented in a header). |
|
255 // ----------------------------------------------------------------------------- |
|
256 // |
|
257 TInt CLibxml2Tester::ParseFileSAXLowerCaseL( CStifItemParser& aItem ) |
|
258 { |
|
259 _LIT(KSourceFileErrorName,"No source file name."); |
|
260 _LIT(KOutputFileErrorName,"No output file name."); |
|
261 _LIT(KStartParsing,"Start parsing file using SAX."); |
|
262 _LIT(KEndParsing,"Parsing end."); |
|
263 _LIT(KSetFeature,"Setting feature name: %d."); |
|
264 _LIT(KOutputFileError,"Output file error: %d."); |
|
265 |
|
266 TPtrC SrcFile; |
|
267 if ( aItem.GetNextString(SrcFile) != KErrNone ) |
|
268 { |
|
269 TestModuleIf().Printf( infoNum++,KTEST_NAME,KSourceFileErrorName); |
|
270 iLog->Log((TDesC)KSourceFileErrorName); |
|
271 return KErrGeneral; |
|
272 } |
|
273 |
|
274 TPtrC OutputFile; |
|
275 if ( aItem.GetNextString(OutputFile) != KErrNone ) |
|
276 { |
|
277 TestModuleIf().Printf( infoNum++,KTEST_NAME,KOutputFileErrorName); |
|
278 iLog->Log((TDesC)KOutputFileErrorName); |
|
279 return KErrGeneral; |
|
280 } |
|
281 |
|
282 // create file sesion and files |
|
283 RFs fs; |
|
284 User::LeaveIfError(fs.Connect()); |
|
285 CleanupClosePushL(fs); |
|
286 |
|
287 RFile outputFile; |
|
288 TFileName outFileName; |
|
289 outFileName.Copy(OutputFile); |
|
290 |
|
291 TFileName srcFileName; |
|
292 srcFileName.Copy(SrcFile); |
|
293 |
|
294 // Open output file |
|
295 TInt err = outputFile.Replace(fs,outFileName,EFileShareAny|EFileWrite); |
|
296 |
|
297 // If output file is correct |
|
298 if (err == KErrNone) |
|
299 { |
|
300 // Create content handler |
|
301 CTestHandler *sax = CTestHandler::NewL(outputFile); |
|
302 |
|
303 // Create parser |
|
304 // 1. Create CMatchData object |
|
305 CMatchData *matchData = CMatchData::NewL(); |
|
306 |
|
307 // 2. Set mime Type |
|
308 matchData->SetMimeTypeL(KXmlMimeType); |
|
309 |
|
310 // 3. Set Variant ID |
|
311 matchData->SetVariantL(_L8("libxml2")); |
|
312 |
|
313 CParser* parser = CParser::NewL(*matchData, *sax); |
|
314 |
|
315 |
|
316 TestModuleIf().Printf( infoNum++,KTEST_NAME,KSetFeature,EConvertTagsToLowerCase); |
|
317 iLog->Log(KSetFeature,EConvertTagsToLowerCase); |
|
318 |
|
319 parser->EnableFeature(EConvertTagsToLowerCase); |
|
320 |
|
321 TestModuleIf().Printf( infoNum++,KTEST_NAME,KStartParsing); |
|
322 iLog->Log((TDesC)KStartParsing); |
|
323 |
|
324 // Parse file |
|
325 TInt error; |
|
326 TRAP(error, Xml::ParseL(*parser,fs,srcFileName)); |
|
327 if (error!=KErrNone) |
|
328 { |
|
329 // threats exceptions throwed by "ParseL" |
|
330 } |
|
331 |
|
332 delete parser; |
|
333 delete matchData; |
|
334 delete sax; |
|
335 |
|
336 outputFile.Close(); |
|
337 } |
|
338 else |
|
339 { |
|
340 TestModuleIf().Printf( infoNum++,KTEST_NAME,KOutputFileError, err); |
|
341 iLog->Log(KOutputFileError, err); |
|
342 } |
|
343 |
|
344 CleanupStack::PopAndDestroy(&fs); |
|
345 |
|
346 TestModuleIf().Printf( infoNum++,KTEST_NAME,KEndParsing); |
|
347 iLog->Log((TDesC)KEndParsing); |
|
348 |
|
349 return KErrNone; |
|
350 |
|
351 } |
|
352 |
|
353 // ----------------------------------------------------------------------------- |
|
354 // CLibxml2Tester::ParseFileSAXExternalSubsetL |
|
355 // Example test method function. |
|
356 // (other items were commented in a header). |
|
357 // ----------------------------------------------------------------------------- |
|
358 // |
|
359 TInt CLibxml2Tester::ParseFileSAXExternalSubsetL( CStifItemParser& aItem ) |
|
360 { |
|
361 _LIT(KSourceFileErrorName,"No source file name."); |
|
362 _LIT(KOutputFileErrorName,"No output file name."); |
|
363 _LIT(KStartParsing,"Start parsing file using SAX."); |
|
364 _LIT(KEndParsing,"Parsing end."); |
|
365 _LIT(KSetFeature,"Setting feature name: %d."); |
|
366 _LIT(KOutputFileError,"Output file error: %d."); |
|
367 |
|
368 TPtrC SrcFile; |
|
369 if ( aItem.GetNextString(SrcFile) != KErrNone ) |
|
370 { |
|
371 TestModuleIf().Printf( infoNum++,KTEST_NAME,KSourceFileErrorName); |
|
372 iLog->Log((TDesC)KSourceFileErrorName); |
|
373 return KErrGeneral; |
|
374 } |
|
375 |
|
376 TPtrC OutputFile; |
|
377 if ( aItem.GetNextString(OutputFile) != KErrNone ) |
|
378 { |
|
379 TestModuleIf().Printf( infoNum++,KTEST_NAME,KOutputFileErrorName); |
|
380 iLog->Log((TDesC)KOutputFileErrorName); |
|
381 return KErrGeneral; |
|
382 } |
|
383 |
|
384 // create file sesion and files |
|
385 RFs fs; |
|
386 User::LeaveIfError(fs.Connect()); |
|
387 CleanupClosePushL(fs); |
|
388 |
|
389 RFile outputFile; |
|
390 TFileName outFileName; |
|
391 outFileName.Copy(OutputFile); |
|
392 |
|
393 TFileName srcFileName; |
|
394 srcFileName.Copy(SrcFile); |
|
395 |
|
396 // Open output file |
|
397 TInt err = outputFile.Replace(fs,outFileName,EFileShareAny|EFileWrite); |
|
398 |
|
399 // If output file is correct |
|
400 if (err == KErrNone) |
|
401 { |
|
402 // Create content handler |
|
403 CTestHandler *sax = CTestHandler::NewL(outputFile); |
|
404 |
|
405 // Create parser |
|
406 // 1. Create CMatchData object |
|
407 CMatchData *matchData = CMatchData::NewL(); |
|
408 |
|
409 // 2. Set mime Type |
|
410 matchData->SetMimeTypeL(KXmlMimeType); |
|
411 |
|
412 // 3. Set Variant ID |
|
413 matchData->SetVariantL(_L8("libxml2")); |
|
414 |
|
415 CParser* parser = CParser::NewL(*matchData, *sax); |
|
416 |
|
417 |
|
418 TestModuleIf().Printf( infoNum++,KTEST_NAME,KSetFeature,0x0100); |
|
419 iLog->Log(KSetFeature,0x0100); |
|
420 |
|
421 parser->EnableFeature(0x0100); |
|
422 |
|
423 TestModuleIf().Printf( infoNum++,KTEST_NAME,KStartParsing); |
|
424 iLog->Log((TDesC)KStartParsing); |
|
425 |
|
426 // Parse file |
|
427 TInt error; |
|
428 TRAP(error, Xml::ParseL(*parser,fs,srcFileName)); |
|
429 if (error!=KErrNone) |
|
430 { |
|
431 // threats exceptions throwed by "ParseL" |
|
432 } |
|
433 |
|
434 delete parser; |
|
435 delete matchData; |
|
436 delete sax; |
|
437 |
|
438 outputFile.Close(); |
|
439 } |
|
440 else |
|
441 { |
|
442 TestModuleIf().Printf( infoNum++,KTEST_NAME,KOutputFileError, err); |
|
443 iLog->Log(KOutputFileError, err); |
|
444 } |
|
445 |
|
446 CleanupStack::PopAndDestroy(&fs); |
|
447 |
|
448 TestModuleIf().Printf( infoNum++,KTEST_NAME,KEndParsing); |
|
449 iLog->Log((TDesC)KEndParsing); |
|
450 |
|
451 return KErrNone; |
|
452 |
|
453 } |
|
454 |
|
455 // ----------------------------------------------------------------------------- |
|
456 // CLibxml2Tester::ParseBufSAXL |
|
457 // Example test method function. |
|
458 // (other items were commented in a header). |
|
459 // ----------------------------------------------------------------------------- |
|
460 // |
|
461 TInt CLibxml2Tester::ParseBufSAXL( CStifItemParser& aItem ) |
|
462 { |
|
463 const TInt KBufferSize = 20000; |
|
464 |
|
465 _LIT(KSourceFileErrorName,"No source file name."); |
|
466 _LIT(KOutputFileErrorName,"No output file name."); |
|
467 _LIT(KStartParsing,"Start parsing file using SAX."); |
|
468 _LIT(KEndParsing,"Parsing end."); |
|
469 _LIT(KReadFileToBuf,"Reading file to buffer."); |
|
470 _LIT(KOutputFileError,"Output file error: %d."); |
|
471 _LIT(KInputFileError,"Input file error: %d."); |
|
472 |
|
473 TPtrC SrcFile; |
|
474 if ( aItem.GetNextString(SrcFile) != KErrNone ) |
|
475 { |
|
476 TestModuleIf().Printf( infoNum++,KTEST_NAME,KSourceFileErrorName); |
|
477 iLog->Log((TDesC)KSourceFileErrorName); |
|
478 return KErrGeneral; |
|
479 } |
|
480 |
|
481 TPtrC OutputFile; |
|
482 if ( aItem.GetNextString(OutputFile) != KErrNone ) |
|
483 { |
|
484 TestModuleIf().Printf( infoNum++,KTEST_NAME,KOutputFileErrorName); |
|
485 iLog->Log((TDesC)KOutputFileErrorName); |
|
486 return KErrGeneral; |
|
487 } |
|
488 |
|
489 // create file sesion and files |
|
490 RFs fs; |
|
491 User::LeaveIfError(fs.Connect()); |
|
492 CleanupClosePushL(fs); |
|
493 |
|
494 RFile outputFile; |
|
495 TFileName outFileName; |
|
496 outFileName.Copy(OutputFile); |
|
497 |
|
498 RFile srcFile; |
|
499 TFileName srcFileName; |
|
500 srcFileName.Copy(SrcFile); |
|
501 |
|
502 // Open output file |
|
503 TInt err = outputFile.Replace(fs,outFileName,EFileShareAny|EFileWrite); |
|
504 |
|
505 // If output file is correct |
|
506 if (err == KErrNone) |
|
507 { |
|
508 // Create content handler |
|
509 CTestHandler *sax = CTestHandler::NewL(outputFile); |
|
510 |
|
511 // Create parser |
|
512 // 1. Create CMatchData object |
|
513 CMatchData *matchData = CMatchData::NewL(); |
|
514 |
|
515 // 2. Set mime Type |
|
516 matchData->SetMimeTypeL(KXmlMimeType); |
|
517 |
|
518 // 3. Set Variant ID |
|
519 matchData->SetVariantL(_L8("libxml2")); |
|
520 |
|
521 CParser* parser = CParser::NewL(*matchData, *sax); |
|
522 |
|
523 // Read file to buffer |
|
524 TBuf8 <KBufferSize> file; |
|
525 |
|
526 if(srcFile.Open(fs, srcFileName, EFileStreamText|EFileRead) == KErrNone) |
|
527 { |
|
528 TestModuleIf().Printf( infoNum++,KTEST_NAME,KReadFileToBuf); |
|
529 iLog->Log((TDesC)KReadFileToBuf); |
|
530 |
|
531 srcFile.Read(file); |
|
532 srcFile.Close(); |
|
533 |
|
534 TestModuleIf().Printf( infoNum++,KTEST_NAME,KStartParsing); |
|
535 iLog->Log((TDesC)KStartParsing); |
|
536 // Parse buffer |
|
537 TInt error; |
|
538 TRAP(error, Xml::ParseL(*parser, file)); |
|
539 if (error!=KErrNone) |
|
540 { |
|
541 // threats exceptions throwed by "ParseL" |
|
542 } |
|
543 } |
|
544 else |
|
545 { |
|
546 TestModuleIf().Printf( infoNum++,KTEST_NAME, KInputFileError, err); |
|
547 iLog->Log(KInputFileError, err); |
|
548 } |
|
549 |
|
550 delete parser; |
|
551 delete matchData; |
|
552 delete sax; |
|
553 |
|
554 outputFile.Close(); |
|
555 } |
|
556 else |
|
557 { |
|
558 TestModuleIf().Printf( infoNum++,KTEST_NAME, KOutputFileError, err); |
|
559 iLog->Log(KOutputFileError, err); |
|
560 } |
|
561 |
|
562 CleanupStack::PopAndDestroy(&fs); |
|
563 |
|
564 TestModuleIf().Printf( infoNum++,KTEST_NAME,KEndParsing); |
|
565 iLog->Log((TDesC)KEndParsing); |
|
566 |
|
567 return KErrNone; |
|
568 |
|
569 } |