|
1 /* |
|
2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 #include "T_DataCElementParser.h" |
|
19 #include "T_BTUtil.h" |
|
20 |
|
21 /*@{*/ |
|
22 |
|
23 //Commands |
|
24 _LIT(KCmdNewL, "NewL"); |
|
25 _LIT(KCmdReset, "Reset"); |
|
26 _LIT(KCmdBuilder, "Builder"); |
|
27 _LIT(KCmdSetBuilder, "SetBuilder"); |
|
28 _LIT(KCmdParseElementsL, "ParseElementsL"); |
|
29 _LIT(KCmdBufferedParseL, "BufferedParseL"); |
|
30 _LIT(KCmdDestructor, "~"); |
|
31 |
|
32 //Parameters |
|
33 _LIT(KObject, "object"); |
|
34 _LIT(KExpected, "expected"); |
|
35 _LIT(KType, "type"); |
|
36 _LIT(KFileName, "filename"); |
|
37 |
|
38 /*@}*/ |
|
39 |
|
40 ////////////////////////////////////////////////////////////////////// |
|
41 // Construction/Destruction |
|
42 ////////////////////////////////////////////////////////////////////// |
|
43 |
|
44 CT_DataCElementParser* CT_DataCElementParser::NewL() |
|
45 { |
|
46 CT_DataCElementParser* ret=new (ELeave) CT_DataCElementParser(); |
|
47 CleanupStack::PushL(ret); |
|
48 ret->ConstructL(); |
|
49 CleanupStack::Pop(ret); |
|
50 return ret; |
|
51 } |
|
52 |
|
53 CT_DataCElementParser::CT_DataCElementParser() |
|
54 : iCElementParser(NULL) |
|
55 { |
|
56 } |
|
57 |
|
58 void CT_DataCElementParser::ConstructL() |
|
59 { |
|
60 User::LeaveIfError(iRFs.Connect()); |
|
61 } |
|
62 |
|
63 CT_DataCElementParser::~CT_DataCElementParser() |
|
64 { |
|
65 DestroyData(); |
|
66 iRFs.Close(); |
|
67 } |
|
68 |
|
69 void CT_DataCElementParser::SetObjectL(TAny* aAny) |
|
70 { |
|
71 DestroyData(); |
|
72 iCElementParser = static_cast<CElementParser*> (aAny); |
|
73 } |
|
74 |
|
75 void CT_DataCElementParser::DisownObjectL() |
|
76 { |
|
77 iCElementParser = NULL; |
|
78 } |
|
79 |
|
80 void CT_DataCElementParser::DestroyData() |
|
81 { |
|
82 delete iCElementParser; |
|
83 iCElementParser=NULL; |
|
84 } |
|
85 |
|
86 inline TCleanupOperation CT_DataCElementParser::CleanupOperation() |
|
87 { |
|
88 return CleanupOperation; |
|
89 } |
|
90 |
|
91 void CT_DataCElementParser::CleanupOperation(TAny* aAny) |
|
92 { |
|
93 CElementParser* parser=static_cast<CElementParser*>(aAny); |
|
94 delete parser; |
|
95 } |
|
96 |
|
97 /** |
|
98 * Process a command read from the ini file |
|
99 * |
|
100 * @param aCommand The command to process |
|
101 * @param aSection The section in the ini containing data for the command |
|
102 * @param aAsyncErrorIndex Command index for async calls to return errors to |
|
103 * |
|
104 * @return ETrue if the command is processed |
|
105 * |
|
106 * @leave System wide error |
|
107 */ |
|
108 TBool CT_DataCElementParser::DoCommandL(const TTEFFunction& aCommand, const TTEFSectionName& aSection, const TInt /*aAsyncErrorIndex*/) |
|
109 { |
|
110 TBool ret=ETrue; |
|
111 |
|
112 if ( aCommand==KCmdNewL ) |
|
113 { |
|
114 DoCmdNewL(aSection); |
|
115 } |
|
116 else if ( aCommand==KCmdReset ) |
|
117 { |
|
118 DoCmdReset(aSection); |
|
119 } |
|
120 else if ( aCommand==KCmdBuilder ) |
|
121 { |
|
122 DoCmdBuilder(aSection); |
|
123 } |
|
124 else if ( aCommand==KCmdSetBuilder ) |
|
125 { |
|
126 DoCmdSetBuilder(aSection); |
|
127 } |
|
128 else if ( aCommand==KCmdParseElementsL ) |
|
129 { |
|
130 DoCmdParseElementsL(aSection); |
|
131 } |
|
132 else if ( aCommand==KCmdBufferedParseL ) |
|
133 { |
|
134 DoCmdBufferedParseL(aSection); |
|
135 } |
|
136 else if ( aCommand==KCmdDestructor ) |
|
137 { |
|
138 DoCmdDestructor(); |
|
139 } |
|
140 else |
|
141 { |
|
142 ret=EFalse; |
|
143 } |
|
144 |
|
145 return ret; |
|
146 } |
|
147 |
|
148 |
|
149 /** |
|
150 Test NewL() |
|
151 */ |
|
152 void CT_DataCElementParser::DoCmdNewL(const TDesC& aSection) |
|
153 { |
|
154 INFO_PRINTF1(_L("CElementParser NewL Call")); |
|
155 DestroyData(); |
|
156 |
|
157 MSdpElementBuilder* elementBuilder = NULL; |
|
158 TPtrC theObject; |
|
159 TPtrC theType; |
|
160 if( GetStringFromConfig(aSection, KObject(), theObject) ) |
|
161 { |
|
162 if( GetStringFromConfig(aSection, KType(), theType) ) |
|
163 { |
|
164 elementBuilder = CT_BTUtil::ElementBuilderCastL(*this, theObject, theType); |
|
165 } |
|
166 else |
|
167 { |
|
168 ERR_PRINTF1(_L("Error: Type is not specified.")); |
|
169 SetBlockResult(EFail); |
|
170 } |
|
171 } |
|
172 |
|
173 TRAPD(err, iCElementParser = CElementParser::NewL(elementBuilder)); |
|
174 if(err != KErrNone) |
|
175 { |
|
176 ERR_PRINTF2(_L("CElementParser NewL failed with error %d"), err); |
|
177 SetError(err); |
|
178 } |
|
179 } |
|
180 |
|
181 /** |
|
182 Test Reset() |
|
183 */ |
|
184 void CT_DataCElementParser::DoCmdReset(const TDesC& aSection) |
|
185 { |
|
186 INFO_PRINTF1(_L("CElementParser Reset Call")); |
|
187 |
|
188 TPtrC theObject; |
|
189 TPtrC theType; |
|
190 if( GetStringFromConfig(aSection, KObject(), theObject) ) |
|
191 { |
|
192 if( GetStringFromConfig(aSection, KType(), theType) ) |
|
193 { |
|
194 MSdpElementBuilder* elementBuilder = CT_BTUtil::ElementBuilderCastL(*this, theObject, theType); |
|
195 iCElementParser->Reset(elementBuilder); |
|
196 } |
|
197 else |
|
198 { |
|
199 ERR_PRINTF1(_L("Error: Type is not specified.")); |
|
200 SetBlockResult(EFail); |
|
201 } |
|
202 } |
|
203 else |
|
204 { |
|
205 iCElementParser->Reset(); |
|
206 } |
|
207 } |
|
208 |
|
209 /** |
|
210 Test Builder() |
|
211 */ |
|
212 void CT_DataCElementParser::DoCmdBuilder(const TDesC& aSection) |
|
213 { |
|
214 INFO_PRINTF1(_L("CElementParser Builder Call")); |
|
215 MSdpElementBuilder* actualBuilder = iCElementParser->Builder(); |
|
216 |
|
217 TPtrC theObject; |
|
218 TPtrC theType; |
|
219 if( GetStringFromConfig(aSection, KObject(), theObject) ) |
|
220 { |
|
221 if( GetStringFromConfig(aSection, KType(), theType) ) |
|
222 { |
|
223 MSdpElementBuilder* expectedBuilder = CT_BTUtil::ElementBuilderCastL(*this, theObject, theType); |
|
224 if( actualBuilder != expectedBuilder ) |
|
225 { |
|
226 ERR_PRINTF1(_L("Builder not as expected!")); |
|
227 SetBlockResult(EFail); |
|
228 } |
|
229 } |
|
230 else |
|
231 { |
|
232 ERR_PRINTF1(_L("Error: Type is not specified.")); |
|
233 SetBlockResult(EFail); |
|
234 } |
|
235 } |
|
236 } |
|
237 |
|
238 /** |
|
239 Test SetBuilder() |
|
240 */ |
|
241 void CT_DataCElementParser::DoCmdSetBuilder(const TDesC& aSection) |
|
242 { |
|
243 INFO_PRINTF1(_L("CElementParser SetBuilder Call")); |
|
244 |
|
245 MSdpElementBuilder* elementBuilder = NULL; |
|
246 TPtrC theObject; |
|
247 TPtrC theType; |
|
248 if( GetStringFromConfig(aSection, KObject(), theObject) ) |
|
249 { |
|
250 if( GetStringFromConfig(aSection, KType(), theType) ) |
|
251 { |
|
252 elementBuilder = CT_BTUtil::ElementBuilderCastL(*this, theObject, theType); |
|
253 } |
|
254 else |
|
255 { |
|
256 ERR_PRINTF1(_L("Error: Type is not specified.")); |
|
257 SetBlockResult(EFail); |
|
258 } |
|
259 } |
|
260 |
|
261 iCElementParser->SetBuilder(elementBuilder); |
|
262 } |
|
263 |
|
264 /** |
|
265 Test ParseElementsL() |
|
266 */ |
|
267 void CT_DataCElementParser::DoCmdParseElementsL(const TDesC& aSection) |
|
268 { |
|
269 INFO_PRINTF1(_L("CElementParser ParseElementsL Call")); |
|
270 |
|
271 TPtrC fileName; |
|
272 if( GetStringFromConfig(aSection, KFileName(), fileName) ) |
|
273 { |
|
274 RFile file; |
|
275 User::LeaveIfError(file.Open(iRFs, fileName, EFileRead)); |
|
276 CleanupClosePushL(file); |
|
277 |
|
278 TInt fileSize = 0; |
|
279 file.Size(fileSize); |
|
280 HBufC8* buff = HBufC8::NewLC(fileSize); |
|
281 TPtr8 tprBuff = buff->Des(); |
|
282 file.Read(tprBuff); |
|
283 |
|
284 TInt numberOfBytes = 0; |
|
285 TRAPD(err, numberOfBytes = iCElementParser->ParseElementsL(tprBuff)); |
|
286 INFO_PRINTF2(_L("CElementParser::ParseElementsL() result: actual = %d"), numberOfBytes); |
|
287 if(err != KErrNone) |
|
288 { |
|
289 ERR_PRINTF2(_L("CElementParser ParseElementsL failed with error %d"), err); |
|
290 SetError(err); |
|
291 } |
|
292 else |
|
293 { |
|
294 TInt expected; |
|
295 if( GetIntFromConfig(aSection, KExpected(), expected) ) |
|
296 { |
|
297 if( numberOfBytes != expected ) |
|
298 { |
|
299 ERR_PRINTF1(_L("ParseElementsL not as expected!")); |
|
300 SetBlockResult(EFail); |
|
301 } |
|
302 } |
|
303 else |
|
304 { |
|
305 ERR_PRINTF2(_L("Missing expected value %S"), &KExpected()); |
|
306 SetBlockResult(EFail); |
|
307 } |
|
308 } |
|
309 CleanupStack::PopAndDestroy(2, &file); |
|
310 } |
|
311 else |
|
312 { |
|
313 ERR_PRINTF2(_L("Missing parameter %S"), &KExpected()); |
|
314 SetBlockResult(EFail); |
|
315 } |
|
316 } |
|
317 |
|
318 /** |
|
319 Test BufferedParseL() |
|
320 */ |
|
321 void CT_DataCElementParser::DoCmdBufferedParseL(const TDesC& aSection) |
|
322 { |
|
323 INFO_PRINTF1(_L("CElementParser BufferedParseL Call")); |
|
324 |
|
325 TPtrC fileName; |
|
326 if( GetStringFromConfig(aSection, KFileName(), fileName) ) |
|
327 { |
|
328 RFile file; |
|
329 User::LeaveIfError(file.Open(iRFs, fileName, EFileRead)); |
|
330 CleanupClosePushL(file); |
|
331 |
|
332 TInt fileSize = 0; |
|
333 file.Size(fileSize); |
|
334 HBufC8* buff = HBufC8::NewLC(fileSize); |
|
335 TPtr8 tprBuff = buff->Des(); |
|
336 file.Read(tprBuff); |
|
337 |
|
338 TBool boolResult = EFalse; |
|
339 TRAPD(err, boolResult = iCElementParser->BufferedParseL(tprBuff)); |
|
340 INFO_PRINTF2(_L("CElementParser::BufferedParseL() result: actual = %d"), boolResult); |
|
341 if(err != KErrNone) |
|
342 { |
|
343 ERR_PRINTF2(_L("CElementParser BufferedParseL failed with error %d"), err); |
|
344 SetError(err); |
|
345 } |
|
346 else |
|
347 { |
|
348 TBool expected = EFalse; |
|
349 if( GetBoolFromConfig(aSection, KExpected(), expected) ) |
|
350 { |
|
351 if( boolResult != expected ) |
|
352 { |
|
353 ERR_PRINTF1(_L("BufferedParseL not as expected!")); |
|
354 SetBlockResult(EFail); |
|
355 } |
|
356 } |
|
357 else |
|
358 { |
|
359 ERR_PRINTF2(_L("Missing expected value %S"), &KExpected()); |
|
360 SetBlockResult(EFail); |
|
361 } |
|
362 } |
|
363 CleanupStack::PopAndDestroy(2, &file); |
|
364 } |
|
365 else |
|
366 { |
|
367 ERR_PRINTF2(_L("Missing parameter %S"), &KExpected()); |
|
368 SetBlockResult(EFail); |
|
369 } |
|
370 } |
|
371 |
|
372 /** |
|
373 Test ~CElementParser() |
|
374 */ |
|
375 void CT_DataCElementParser::DoCmdDestructor() |
|
376 { |
|
377 INFO_PRINTF1(_L("CElementParser Destructor Call")); |
|
378 |
|
379 DestroyData(); |
|
380 } |