1 /* |
|
2 * Copyright (c) 2005-2007 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: ConML parser/generator |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 // ----------------------------------------------------------------------------- |
|
20 // Includes |
|
21 // ----------------------------------------------------------------------------- |
|
22 #include <s32mem.h> |
|
23 |
|
24 #include "sconconmlhandler.h" |
|
25 #include "debug.h" |
|
26 |
|
27 // ----------------------------------------------------------------------------- |
|
28 // CreateCSConConMLHandlerL |
|
29 // Creates a new instance of CSConConMLHandler |
|
30 // ----------------------------------------------------------------------------- |
|
31 // |
|
32 EXPORT_C CSConConMLHandler* CreateCSConConMLHandlerL() |
|
33 { |
|
34 LOGGER_ENTERFN( "CreateCSConConMLHandlerL()" ); |
|
35 LOGGER_LEAVEFN( "CreateCSConConMLHandlerL()" ); |
|
36 return CSConConMLHandler::NewL(); |
|
37 } |
|
38 |
|
39 // ----------------------------------------------------------------------------- |
|
40 // CSConConMLHandler::CSConConMLHandler() |
|
41 // Default constructor |
|
42 // ----------------------------------------------------------------------------- |
|
43 // |
|
44 CSConConMLHandler::CSConConMLHandler() |
|
45 { |
|
46 } |
|
47 |
|
48 // ----------------------------------------------------------------------------- |
|
49 // CSConConMLHandler::CSConConMLHandler() |
|
50 // Default constructor |
|
51 // ----------------------------------------------------------------------------- |
|
52 // |
|
53 CSConConMLHandler::~CSConConMLHandler() |
|
54 { |
|
55 LOGGER_ENTERFN( "CSConConMLHandler::~CSConConMLHandler()" ); |
|
56 |
|
57 if ( iParseBuffer ) |
|
58 { |
|
59 LOGGER_WRITE( "CSConConMLHandler::~CSConConMLHandler() : phase 1/4 " ); |
|
60 delete iParseBuffer; |
|
61 iParseBuffer = NULL; |
|
62 } |
|
63 |
|
64 if ( iBuffer ) |
|
65 { |
|
66 LOGGER_WRITE( "CSConConMLHandler::~CSConConMLHandler() : phase 2/4 " ); |
|
67 delete iBuffer; |
|
68 iBuffer = NULL; |
|
69 } |
|
70 |
|
71 if ( iGenerator ) |
|
72 { |
|
73 LOGGER_WRITE( "CSConConMLHandler::~CSConConMLHandler() : phase 3/4 " ); |
|
74 delete iGenerator; |
|
75 iGenerator = NULL; |
|
76 } |
|
77 |
|
78 if ( iStringTable ) |
|
79 { |
|
80 LOGGER_WRITE( "CSConConMLHandler::~CSConConMLHandler() : phase 4/4 " ); |
|
81 delete iStringTable; |
|
82 iStringTable = NULL; |
|
83 } |
|
84 |
|
85 iElemStack.Close(); |
|
86 |
|
87 LOGGER_LEAVEFN( "CSConConMLHandler::~CSConConMLHandler()" ); |
|
88 } |
|
89 |
|
90 // ----------------------------------------------------------------------------- |
|
91 // CSConConMLHandler::CSConConMLHaNewL() |
|
92 // Creates a new instance of CSConConMLHandler |
|
93 // ----------------------------------------------------------------------------- |
|
94 // |
|
95 CSConConMLHandler* CSConConMLHandler::NewL() |
|
96 { |
|
97 LOGGER_ENTERFN( "CSConConMLHandler::NewL()" ); |
|
98 CSConConMLHandler* self = new ( ELeave ) CSConConMLHandler(); |
|
99 CleanupStack::PushL(self); |
|
100 self->ConstructL(); |
|
101 CleanupStack::Pop(); // self |
|
102 LOGGER_LEAVEFN( "CSConConMLHandler::NewL()" ); |
|
103 return self; |
|
104 } |
|
105 |
|
106 // ----------------------------------------------------------------------------- |
|
107 // CSConConMLHandler::ConstructL() |
|
108 // 2nd phase constructor |
|
109 // ----------------------------------------------------------------------------- |
|
110 // |
|
111 void CSConConMLHandler::ConstructL() |
|
112 { |
|
113 LOGGER_ENTERFN( "CSConConMLHandler::ConstructL()" ); |
|
114 iGenerator = CSConConMLGenerator::NewL(); |
|
115 iBuffer = CBufFlat::NewL( KSConBufferGranularity ); |
|
116 iParseBuffer = CBufFlat::NewL( KSConDefaultDocumentSize ); |
|
117 LOGGER_LEAVEFN( "CSConConMLHandler::ConstructL()" ); |
|
118 } |
|
119 |
|
120 // ----------------------------------------------------------------------------- |
|
121 // CSConConMLHandler::ParseDocumentL() |
|
122 // Handles the document parsing, document in aInput |
|
123 // ----------------------------------------------------------------------------- |
|
124 // |
|
125 TInt CSConConMLHandler::ParseDocumentL( |
|
126 CBufFlat& aInput, MWBXMLConMLCallback* aCallback ) |
|
127 { |
|
128 LOGGER_ENTERFN( "CSConConMLHandler::ParseDocument()" ); |
|
129 TInt ret ( KErrNone ); |
|
130 |
|
131 iPos = 0; |
|
132 iParseBuffer->Reset(); |
|
133 iParseBuffer->ResizeL( aInput.Size() ); |
|
134 iParseBuffer->Write( 0, aInput.Ptr(0) ); |
|
135 iHeaderParsed = EFalse; |
|
136 iGenerator->SetCallback( aCallback ); |
|
137 |
|
138 while (ret == KWBXMLParserErrorOk ) |
|
139 { |
|
140 ret = ParseL(); |
|
141 } |
|
142 |
|
143 if ( ret == KWBXMLParserErrorEof ) |
|
144 { |
|
145 ret = KErrNone; |
|
146 } |
|
147 LOGGER_WRITE_1( "CSConConMLHandler::ParseDocument()\ |
|
148 returned %d", ret ); |
|
149 return ( ret ); |
|
150 } |
|
151 |
|
152 // ----------------------------------------------------------------------------- |
|
153 // CSConConMLHandler::GenerateDocument() |
|
154 // Starts the generating of a document |
|
155 // ----------------------------------------------------------------------------- |
|
156 // |
|
157 TInt CSConConMLHandler::GenerateDocument( ConML_ConMLPtr_t aContent ) |
|
158 { |
|
159 LOGGER_ENTERFN( "CSConConMLHandler::GenerateDocument()" ); |
|
160 iGenerator->GenerateConMLDocument ( aContent ); |
|
161 LOGGER_LEAVEFN( "CSConConMLHandler::GenerateDocument()" ); |
|
162 return KErrNone; |
|
163 } |
|
164 |
|
165 // ----------------------------------------------------------------------------- |
|
166 // CSConConMLHandler::WBXMLDocument() |
|
167 // Returns the generated WBXML -document |
|
168 // ----------------------------------------------------------------------------- |
|
169 // |
|
170 TPtrC8 CSConConMLHandler::WBXMLDocument() |
|
171 { |
|
172 LOGGER_ENTERFN( "CSConConMLHandler::WBXMLDocument()" ); |
|
173 LOGGER_LEAVEFN( "CSConConMLHandler::WBXMLDocument()" ); |
|
174 return iGenerator->WBXMLDocument(); |
|
175 } |
|
176 |
|
177 // ----------------------------------------------------------------------------- |
|
178 // CSConConMLHandler::XMLDocument() |
|
179 // Returns the generated XML -document |
|
180 // ----------------------------------------------------------------------------- |
|
181 // |
|
182 TPtrC8 CSConConMLHandler::XMLDocument() |
|
183 { |
|
184 LOGGER_ENTERFN( "CSConConMLHandler::XMLDocument()" ); |
|
185 LOGGER_LEAVEFN( "CSConConMLHandler::XMLDocument()" ); |
|
186 return iGenerator->XMLDocument(); |
|
187 } |
|
188 |
|
189 // ----------------------------------------------------------------------------- |
|
190 // CSConConMLHandler::ParseL() |
|
191 // Parses the next document tag / header if not parsed yet |
|
192 // ----------------------------------------------------------------------------- |
|
193 // |
|
194 TInt CSConConMLHandler::ParseL() |
|
195 { |
|
196 LOGGER_ENTERFN( "CSConConMLHandler::ParseL()" ); |
|
197 if( !iHeaderParsed ) |
|
198 { |
|
199 TConMLParserError result(KWBXMLParserErrorOk); |
|
200 TRAPD(err, result = DoParseDocumentHeaderL()); |
|
201 if( err == KErrEof ) |
|
202 { |
|
203 LOGGER_WRITE( "CSConConMLHandler::ParseL() : KWBXMLParserErrorEofTooEarly " ); |
|
204 return KWBXMLParserErrorEofTooEarly; |
|
205 } |
|
206 return result; |
|
207 } |
|
208 LOGGER_LEAVEFN( "CSConConMLHandler::ParseL()" ); |
|
209 return DoParseDocumentBodyL(); |
|
210 } |
|
211 |
|
212 // ----------------------------------------------------------------------------- |
|
213 // CSConConMLHandler::DoParseDocumentHeaderL() |
|
214 // Parses the document header |
|
215 // ----------------------------------------------------------------------------- |
|
216 // |
|
217 TConMLParserError CSConConMLHandler::DoParseDocumentHeaderL() |
|
218 { |
|
219 LOGGER_ENTERFN( "CSConConMLParser::DoParseDocumentHeaderL()" ); |
|
220 iHeaderParsed = ETrue; |
|
221 |
|
222 // Version |
|
223 TUint8 version = ReadUint8L(); |
|
224 |
|
225 // PublicId |
|
226 TInt32 publicId = ReadMUint32L(); |
|
227 |
|
228 if( publicId == 0 ) |
|
229 { |
|
230 publicId = ReadMUint32L(); |
|
231 } |
|
232 else |
|
233 { |
|
234 publicId = -publicId; |
|
235 } |
|
236 |
|
237 // Charset |
|
238 TUint32 charSet = ReadMUint32L(); |
|
239 |
|
240 // String table |
|
241 ReadStringTableL(); |
|
242 |
|
243 // Document begins now |
|
244 if( publicId < 0 ) |
|
245 { |
|
246 iGenerator->StartDocument(version, -publicId, charSet); |
|
247 } |
|
248 else |
|
249 { |
|
250 iGenerator->StartDocument(version, |
|
251 StringTableString(publicId), charSet); |
|
252 } |
|
253 LOGGER_LEAVEFN( "CSConConMLHandler::DoParseDocumentHeaderL()" ); |
|
254 return KWBXMLParserErrorOk; |
|
255 } |
|
256 |
|
257 // ----------------------------------------------------------------------------- |
|
258 // CSConConMLHandler::DoParseDocumentBodyL() |
|
259 // Parses the next element of the document |
|
260 // ----------------------------------------------------------------------------- |
|
261 // |
|
262 TConMLParserError CSConConMLHandler::DoParseDocumentBodyL() |
|
263 { |
|
264 LOGGER_ENTERFN( "CSConConMLHandler::DoParseDocumentBodyL()" ); |
|
265 TUint8 id(0); |
|
266 TRAPD(err, id = ReadUint8L()); |
|
267 |
|
268 if( err != KErrNone ) |
|
269 { |
|
270 if( err == KErrEof ) |
|
271 { |
|
272 iGenerator->EndDocument(); |
|
273 if( iElemStack.Count() > 0 ) |
|
274 { |
|
275 LOGGER_WRITE( "CSConConMLHandler::DoParseDocumentBodyL() : KWBXMLParserErrorEofTooEarly " ); |
|
276 return KWBXMLParserErrorEofTooEarly; |
|
277 } |
|
278 return KWBXMLParserErrorEof; |
|
279 } |
|
280 User::Leave(err); |
|
281 } |
|
282 |
|
283 switch (id) |
|
284 { |
|
285 case END: |
|
286 { |
|
287 if( !iElemStack.Count() ) |
|
288 { |
|
289 LOGGER_WRITE( "CSConConMLHandler::DoParseDocumentBodyL() : KWBXMLParserErrorinvalidDocument " ); |
|
290 User::Leave( KWBXMLParserErrorinvalidDocument ); |
|
291 } |
|
292 TUint8 tag( iElemStack.operator[](iElemStack.Count() - 1 )); |
|
293 iGenerator->EndElementL( tag ); |
|
294 iElemStack.Remove(iElemStack.Count() - 1); |
|
295 } |
|
296 break; |
|
297 |
|
298 case STR_I: |
|
299 iGenerator->CharactersL(ReadStrIL()); |
|
300 break; |
|
301 |
|
302 case OPAQUE: |
|
303 iGenerator->CharactersL(ReadOpaqueL()); |
|
304 break; |
|
305 |
|
306 default: |
|
307 HandleElementL(id); |
|
308 break; |
|
309 } |
|
310 LOGGER_LEAVEFN( "CSConConMLHandler::DoParseDocumentBodyL()" ); |
|
311 return KWBXMLParserErrorOk; |
|
312 } |
|
313 |
|
314 // ----------------------------------------------------------------------------- |
|
315 // CSConConMLHandler::ReadUint8L() |
|
316 // Reads one byte from the document |
|
317 // ----------------------------------------------------------------------------- |
|
318 // |
|
319 TUint8 CSConConMLHandler::ReadUint8L() |
|
320 { |
|
321 LOGGER_ENTERFN( "CSConConMLHandler::ReadUint8L()" ); |
|
322 if ( iPos == iParseBuffer->Size()) |
|
323 { |
|
324 User::Leave ( KErrEof ); |
|
325 } |
|
326 TUint8 value; |
|
327 HBufC8* data = HBufC8::NewLC( sizeof(TUint8) ); |
|
328 TPtr8 ptr = data->Des(); |
|
329 iParseBuffer->Read(iPos, ptr, sizeof(TUint8)); |
|
330 iPos+= sizeof(TUint8); |
|
331 value = ptr[0]; |
|
332 CleanupStack::PopAndDestroy(1); //data |
|
333 LOGGER_WRITE_1( "CSConConMLHandler::ReadUint8L()\ |
|
334 : returned %d ", value ); |
|
335 return value; |
|
336 } |
|
337 |
|
338 // ----------------------------------------------------------------------------- |
|
339 // CSConConMLHandler::ReadMUint32L() |
|
340 // Reads multibyte coding from the document |
|
341 // ----------------------------------------------------------------------------- |
|
342 // |
|
343 TUint32 CSConConMLHandler::ReadMUint32L() |
|
344 { |
|
345 LOGGER_ENTERFN( "CSConConMLHandler::ReadMUint32L()" ); |
|
346 TUint32 result = 0; |
|
347 TUint8 c; |
|
348 |
|
349 do { |
|
350 c = ReadUint8L(); |
|
351 result = (result << 7) | (c & 0x7f); |
|
352 } while ( c & 0x80 ); |
|
353 |
|
354 LOGGER_WRITE_1( "CSConConMLHandler::ReadMUint32L()\ |
|
355 : returned %d ", result ); |
|
356 return result; |
|
357 } |
|
358 |
|
359 // ----------------------------------------------------------------------------- |
|
360 // CSConConMLHandler::ReadStrIL() |
|
361 // Reads string data from the document |
|
362 // ----------------------------------------------------------------------------- |
|
363 // |
|
364 TPtrC8 CSConConMLHandler::ReadStrIL() |
|
365 { |
|
366 LOGGER_ENTERFN( "CSConConMLHandler::ReadStrIL()" ); |
|
367 iBuffer->Reset(); |
|
368 RBufWriteStream bws(*iBuffer); |
|
369 TUint8 c; |
|
370 while( (c = ReadUint8L()) != 0 ) |
|
371 { |
|
372 bws.WriteUint8L(c); |
|
373 } |
|
374 bws.CommitL(); |
|
375 LOGGER_LEAVEFN( "CSConConMLParser::ReadStrIL()" ); |
|
376 return iBuffer->Ptr(0); |
|
377 } |
|
378 |
|
379 // ----------------------------------------------------------------------------- |
|
380 // CSConConMLHandler::ReadStringTableL() |
|
381 // Handles the document's string table |
|
382 // ----------------------------------------------------------------------------- |
|
383 // |
|
384 void CSConConMLHandler::ReadStringTableL() |
|
385 { |
|
386 LOGGER_ENTERFN( "CSConConMLHandler::ReadStringTableL()" ); |
|
387 delete iStringTable; |
|
388 iStringTable = NULL; |
|
389 TUint32 strTblLen = ReadMUint32L(); |
|
390 |
|
391 if( strTblLen > 0 ) |
|
392 { |
|
393 if( iPos+strTblLen > iParseBuffer->Size()) |
|
394 { |
|
395 LOGGER_WRITE( "CSConConMLHandler::ReadStringTableL() : LEAVE KErrEof " ); |
|
396 User::Leave ( KErrEof ); |
|
397 } |
|
398 iStringTable = HBufC8::NewL(strTblLen); |
|
399 TPtr8 ptr = iStringTable->Des(); |
|
400 iParseBuffer->Read(iPos, ptr, strTblLen); |
|
401 iPos+=strTblLen; |
|
402 } |
|
403 LOGGER_LEAVEFN( "CSConConMLHandler::ReadStringTableL()" ); |
|
404 } |
|
405 |
|
406 // ----------------------------------------------------------------------------- |
|
407 // CSConConMLHandler::StringTableString() |
|
408 // Returns a value from the string table according to the given index |
|
409 // ----------------------------------------------------------------------------- |
|
410 // |
|
411 TPtrC8 CSConConMLHandler::StringTableString( TUint32 aIndex ) |
|
412 { |
|
413 LOGGER_ENTERFN( "CSConConMLHandler::StringTableString()" ); |
|
414 TPtrC8 temp(iStringTable->Mid(aIndex)); |
|
415 TInt pos = temp.Find(KWBXMLNull()); |
|
416 if( pos != KErrNotFound ) |
|
417 { |
|
418 temp.Set(temp.Left(pos)); |
|
419 } |
|
420 LOGGER_LEAVEFN( "CSConConMLHandler::StringTableString()" ); |
|
421 return temp; |
|
422 } |
|
423 |
|
424 // ----------------------------------------------------------------------------- |
|
425 // CSConConMLHandler::HandleElementL() |
|
426 // Starts a new element in the document |
|
427 // ----------------------------------------------------------------------------- |
|
428 // |
|
429 void CSConConMLHandler::HandleElementL( TUint8 aId ) |
|
430 { |
|
431 LOGGER_ENTERFN( "CSConConMLHandler::HandleElementL()" ); |
|
432 TUint8 tag(TUint8(aId & 0x3f)); |
|
433 |
|
434 iGenerator->StartElementL( tag ); |
|
435 |
|
436 if( aId & 0x40 ) |
|
437 { |
|
438 iElemStack.Append( tag ); |
|
439 } |
|
440 else |
|
441 { |
|
442 iGenerator->EndElementL(tag); |
|
443 } |
|
444 LOGGER_LEAVEFN( "CSConConMLHandler::HandleElementL()" ); |
|
445 } |
|
446 |
|
447 // ----------------------------------------------------------------------------- |
|
448 // CSConConMLHandler::ReadOpaqueL() |
|
449 // Reads opaque data from the document |
|
450 // ----------------------------------------------------------------------------- |
|
451 // |
|
452 TPtrC8 CSConConMLHandler::ReadOpaqueL() |
|
453 { |
|
454 LOGGER_ENTERFN( "CSConConMLHandler::ReadOpaqueL()" ); |
|
455 iBuffer->Reset(); |
|
456 RBufWriteStream bws(*iBuffer); |
|
457 TUint32 length = ReadMUint32L(); |
|
458 if( iPos+length > iParseBuffer->Size()) |
|
459 { |
|
460 LOGGER_WRITE( "CSConConMLHandler::ReadOpaqueL() : LEAVE KErrEof " ); |
|
461 User::Leave ( KErrEof ); |
|
462 } |
|
463 HBufC8* data = HBufC8::NewLC( length ); |
|
464 TPtr8 ptr = data->Des(); |
|
465 iParseBuffer->Read( iPos, ptr, length ); |
|
466 iPos+= length; |
|
467 bws.WriteL(ptr, length); |
|
468 bws.CommitL(); |
|
469 CleanupStack::PopAndDestroy(1); // data |
|
470 |
|
471 LOGGER_LEAVEFN( "CSConConMLHandler::ReadOpaqueL()" ); |
|
472 return iBuffer->Ptr(0); |
|
473 } |
|
474 |
|