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 return iGenerator->WBXMLDocument(); |
|
173 } |
|
174 |
|
175 // ----------------------------------------------------------------------------- |
|
176 // CSConConMLHandler::XMLDocument() |
|
177 // Returns the generated XML -document |
|
178 // ----------------------------------------------------------------------------- |
|
179 // |
|
180 TPtrC8 CSConConMLHandler::XMLDocument() |
|
181 { |
|
182 return iGenerator->XMLDocument(); |
|
183 } |
|
184 |
|
185 // ----------------------------------------------------------------------------- |
|
186 // CSConConMLHandler::ParseL() |
|
187 // Parses the next document tag / header if not parsed yet |
|
188 // ----------------------------------------------------------------------------- |
|
189 // |
|
190 TInt CSConConMLHandler::ParseL() |
|
191 { |
|
192 LOGGER_ENTERFN( "CSConConMLHandler::ParseL()" ); |
|
193 if( !iHeaderParsed ) |
|
194 { |
|
195 TConMLParserError result(KWBXMLParserErrorOk); |
|
196 TRAPD(err, result = DoParseDocumentHeaderL()); |
|
197 if( err == KErrEof ) |
|
198 { |
|
199 LOGGER_WRITE( "CSConConMLHandler::ParseL() : KWBXMLParserErrorEofTooEarly " ); |
|
200 return KWBXMLParserErrorEofTooEarly; |
|
201 } |
|
202 return result; |
|
203 } |
|
204 LOGGER_LEAVEFN( "CSConConMLHandler::ParseL()" ); |
|
205 return DoParseDocumentBodyL(); |
|
206 } |
|
207 |
|
208 // ----------------------------------------------------------------------------- |
|
209 // CSConConMLHandler::DoParseDocumentHeaderL() |
|
210 // Parses the document header |
|
211 // ----------------------------------------------------------------------------- |
|
212 // |
|
213 TConMLParserError CSConConMLHandler::DoParseDocumentHeaderL() |
|
214 { |
|
215 LOGGER_ENTERFN( "CSConConMLParser::DoParseDocumentHeaderL()" ); |
|
216 iHeaderParsed = ETrue; |
|
217 |
|
218 // Version |
|
219 TUint8 version = ReadUint8L(); |
|
220 |
|
221 // PublicId |
|
222 TInt32 publicId = ReadMUint32L(); |
|
223 |
|
224 if( publicId == 0 ) |
|
225 { |
|
226 publicId = ReadMUint32L(); |
|
227 } |
|
228 else |
|
229 { |
|
230 publicId = -publicId; |
|
231 } |
|
232 |
|
233 // Charset |
|
234 TUint32 charSet = ReadMUint32L(); |
|
235 |
|
236 // String table |
|
237 ReadStringTableL(); |
|
238 |
|
239 // Document begins now |
|
240 if( publicId < 0 ) |
|
241 { |
|
242 iGenerator->StartDocument(version, -publicId, charSet); |
|
243 } |
|
244 else |
|
245 { |
|
246 iGenerator->StartDocument(version, |
|
247 StringTableString(publicId), charSet); |
|
248 } |
|
249 LOGGER_LEAVEFN( "CSConConMLHandler::DoParseDocumentHeaderL()" ); |
|
250 return KWBXMLParserErrorOk; |
|
251 } |
|
252 |
|
253 // ----------------------------------------------------------------------------- |
|
254 // CSConConMLHandler::DoParseDocumentBodyL() |
|
255 // Parses the next element of the document |
|
256 // ----------------------------------------------------------------------------- |
|
257 // |
|
258 TConMLParserError CSConConMLHandler::DoParseDocumentBodyL() |
|
259 { |
|
260 LOGGER_ENTERFN( "CSConConMLHandler::DoParseDocumentBodyL()" ); |
|
261 TUint8 id(0); |
|
262 TRAPD(err, id = ReadUint8L()); |
|
263 |
|
264 if( err != KErrNone ) |
|
265 { |
|
266 if( err == KErrEof ) |
|
267 { |
|
268 iGenerator->EndDocument(); |
|
269 if( iElemStack.Count() > 0 ) |
|
270 { |
|
271 LOGGER_WRITE( "CSConConMLHandler::DoParseDocumentBodyL() : KWBXMLParserErrorEofTooEarly " ); |
|
272 return KWBXMLParserErrorEofTooEarly; |
|
273 } |
|
274 return KWBXMLParserErrorEof; |
|
275 } |
|
276 User::Leave(err); |
|
277 } |
|
278 |
|
279 switch (id) |
|
280 { |
|
281 case END: |
|
282 { |
|
283 if( !iElemStack.Count() ) |
|
284 { |
|
285 LOGGER_WRITE( "CSConConMLHandler::DoParseDocumentBodyL() : KWBXMLParserErrorinvalidDocument " ); |
|
286 User::Leave( KWBXMLParserErrorinvalidDocument ); |
|
287 } |
|
288 TUint8 tag( iElemStack.operator[](iElemStack.Count() - 1 )); |
|
289 iGenerator->EndElementL( tag ); |
|
290 iElemStack.Remove(iElemStack.Count() - 1); |
|
291 } |
|
292 break; |
|
293 |
|
294 case STR_I: |
|
295 iGenerator->CharactersL(ReadStrIL()); |
|
296 break; |
|
297 |
|
298 case OPAQUE: |
|
299 iGenerator->CharactersL(ReadOpaqueL()); |
|
300 break; |
|
301 |
|
302 default: |
|
303 HandleElementL(id); |
|
304 break; |
|
305 } |
|
306 LOGGER_LEAVEFN( "CSConConMLHandler::DoParseDocumentBodyL()" ); |
|
307 return KWBXMLParserErrorOk; |
|
308 } |
|
309 |
|
310 // ----------------------------------------------------------------------------- |
|
311 // CSConConMLHandler::ReadUint8L() |
|
312 // Reads one byte from the document |
|
313 // ----------------------------------------------------------------------------- |
|
314 // |
|
315 TUint8 CSConConMLHandler::ReadUint8L() |
|
316 { |
|
317 if ( iPos == iParseBuffer->Size()) |
|
318 { |
|
319 User::Leave ( KErrEof ); |
|
320 } |
|
321 TUint8 value; |
|
322 HBufC8* data = HBufC8::NewLC( sizeof(TUint8) ); |
|
323 TPtr8 ptr = data->Des(); |
|
324 iParseBuffer->Read(iPos, ptr, sizeof(TUint8)); |
|
325 iPos+= sizeof(TUint8); |
|
326 value = ptr[0]; |
|
327 CleanupStack::PopAndDestroy(1); //data |
|
328 return value; |
|
329 } |
|
330 |
|
331 // ----------------------------------------------------------------------------- |
|
332 // CSConConMLHandler::ReadMUint32L() |
|
333 // Reads multibyte coding from the document |
|
334 // ----------------------------------------------------------------------------- |
|
335 // |
|
336 TUint32 CSConConMLHandler::ReadMUint32L() |
|
337 { |
|
338 TUint32 result = 0; |
|
339 TUint8 c; |
|
340 |
|
341 do { |
|
342 c = ReadUint8L(); |
|
343 result = (result << 7) | (c & 0x7f); |
|
344 } while ( c & 0x80 ); |
|
345 |
|
346 return result; |
|
347 } |
|
348 |
|
349 // ----------------------------------------------------------------------------- |
|
350 // CSConConMLHandler::ReadStrIL() |
|
351 // Reads string data from the document |
|
352 // ----------------------------------------------------------------------------- |
|
353 // |
|
354 TPtrC8 CSConConMLHandler::ReadStrIL() |
|
355 { |
|
356 iBuffer->Reset(); |
|
357 RBufWriteStream bws(*iBuffer); |
|
358 TUint8 c; |
|
359 while( (c = ReadUint8L()) != 0 ) |
|
360 { |
|
361 bws.WriteUint8L(c); |
|
362 } |
|
363 bws.CommitL(); |
|
364 return iBuffer->Ptr(0); |
|
365 } |
|
366 |
|
367 // ----------------------------------------------------------------------------- |
|
368 // CSConConMLHandler::ReadStringTableL() |
|
369 // Handles the document's string table |
|
370 // ----------------------------------------------------------------------------- |
|
371 // |
|
372 void CSConConMLHandler::ReadStringTableL() |
|
373 { |
|
374 delete iStringTable; |
|
375 iStringTable = NULL; |
|
376 TUint32 strTblLen = ReadMUint32L(); |
|
377 |
|
378 if( strTblLen > 0 ) |
|
379 { |
|
380 if( iPos+strTblLen > iParseBuffer->Size()) |
|
381 { |
|
382 LOGGER_WRITE( "CSConConMLHandler::ReadStringTableL() : LEAVE KErrEof " ); |
|
383 User::Leave ( KErrEof ); |
|
384 } |
|
385 iStringTable = HBufC8::NewL(strTblLen); |
|
386 TPtr8 ptr = iStringTable->Des(); |
|
387 iParseBuffer->Read(iPos, ptr, strTblLen); |
|
388 iPos+=strTblLen; |
|
389 } |
|
390 } |
|
391 |
|
392 // ----------------------------------------------------------------------------- |
|
393 // CSConConMLHandler::StringTableString() |
|
394 // Returns a value from the string table according to the given index |
|
395 // ----------------------------------------------------------------------------- |
|
396 // |
|
397 TPtrC8 CSConConMLHandler::StringTableString( TUint32 aIndex ) |
|
398 { |
|
399 TPtrC8 temp(iStringTable->Mid(aIndex)); |
|
400 TInt pos = temp.Find(KWBXMLNull()); |
|
401 if( pos != KErrNotFound ) |
|
402 { |
|
403 temp.Set(temp.Left(pos)); |
|
404 } |
|
405 return temp; |
|
406 } |
|
407 |
|
408 // ----------------------------------------------------------------------------- |
|
409 // CSConConMLHandler::HandleElementL() |
|
410 // Starts a new element in the document |
|
411 // ----------------------------------------------------------------------------- |
|
412 // |
|
413 void CSConConMLHandler::HandleElementL( TUint8 aId ) |
|
414 { |
|
415 TUint8 tag(TUint8(aId & 0x3f)); |
|
416 |
|
417 iGenerator->StartElementL( tag ); |
|
418 |
|
419 if( aId & 0x40 ) |
|
420 { |
|
421 iElemStack.Append( tag ); |
|
422 } |
|
423 else |
|
424 { |
|
425 iGenerator->EndElementL(tag); |
|
426 } |
|
427 } |
|
428 |
|
429 // ----------------------------------------------------------------------------- |
|
430 // CSConConMLHandler::ReadOpaqueL() |
|
431 // Reads opaque data from the document |
|
432 // ----------------------------------------------------------------------------- |
|
433 // |
|
434 TPtrC8 CSConConMLHandler::ReadOpaqueL() |
|
435 { |
|
436 iBuffer->Reset(); |
|
437 RBufWriteStream bws(*iBuffer); |
|
438 TUint32 length = ReadMUint32L(); |
|
439 if( iPos+length > iParseBuffer->Size()) |
|
440 { |
|
441 LOGGER_WRITE( "CSConConMLHandler::ReadOpaqueL() : LEAVE KErrEof " ); |
|
442 User::Leave ( KErrEof ); |
|
443 } |
|
444 HBufC8* data = HBufC8::NewLC( length ); |
|
445 TPtr8 ptr = data->Des(); |
|
446 iParseBuffer->Read( iPos, ptr, length ); |
|
447 iPos+= length; |
|
448 bws.WriteL(ptr, length); |
|
449 bws.CommitL(); |
|
450 CleanupStack::PopAndDestroy(1); // data |
|
451 |
|
452 return iBuffer->Ptr(0); |
|
453 } |
|
454 |
|