Sample_XML.c
26.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
/*****************************************************************************
* Copyright Statement:
* --------------------
* This software is protected by Copyright and the information contained
* herein is confidential. The software may not be copied and the information
* contained herein may not be used or disclosed except with the written
* permission of MediaTek Inc. (C) 2005
*
* BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
* THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
* RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
* AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
* NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
* SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
* SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
* THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
* NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
* SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
*
* BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
* LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
* AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
* OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
* MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
*
* THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
* WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
* LAWS PRINCIPLES. ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
* RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
* THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
*
*****************************************************************************/
/*******************************************************************************
* Filename:
* ---------
* Sample_XML.c
*
* Project:
* --------
* MAUI
*
* Description:
* ------------
* Sample code for XML Parser
*
* Author:
* -------
* -------
*
*============================================================================
* HISTORY
* Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
*------------------------------------------------------------------------------
* removed!
*------------------------------------------------------------------------------
* Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
*============================================================================
*******************************************************************************/
#include "APPSDKFrameworkGProt.h"
#include "APPSDKNetworkGProt.h"
/****************************************************************************
*
* [Sample]
* Purpose : This sample code provides correct flow about the way of using the
* XML Parser APIs.
*
* Function : The XML Parser provides a set of APIs for applications to parse
* XML document and extract useful information from the XML document
* what they need.
*
* Notice : When parsing an XML document, an application firstly must to create
* an XML Parser object to do the parsing and register a few functions
* as element or data handlers to handle the information parsed from
* the XML document.
* XML Paeser will return corresponsive error code if error
* occurs when parsing. If it wants to query which error it is,
* application can call function "xml_get_err_string" to get it.
* The parsing will not be stopped when error occurs, xml
* parser will just ignore it, and continue to do next parsing.
*
* Branch : 08A, 08B, MAUI
*
* API Ref : xml_new_parser
* xml_close_parser
* xml_register_doctype_handler
* xml_register_element_handler
* xml_register_data_handler
* xml_configure_memory
* xml_parse
* xml_parse_buffer
* xml_get_err_string
*
*****************************************************************************/
const static kal_char mmi_sample_xml_parser_buffer[] = {"<?xml version=\"1.0\" encoding=\"UTF-8\"?><adobehelp type=\"product\"><title>Adobe Reader 8</title><files type=\"nav\"><file type=\"contents\">nav_toc.xml</file><file type=\"search\">final_search_entries.xml</file></files></adobehelp>"};
#define MMI_SAMPLE_XML_FILE_PATH_LEN (128) //XML File Path length
#define MMI_SAMPLE_XML_SAMPLE_FILE "%c:\\xml_sample.xml"
typedef struct
{
XML_PARSER_STRUCT xml_parser; /* xml parser context struct */
}mmi_sample_xml_parser_context_struct;
static mmi_sample_xml_parser_context_struct g_sample_xml_parser_cntx;
/*****************************************************************************
* FUNCTION
* mmi_sample_xml_parser_doctype_start_handler
* DESCRIPTION
* This function is for XML parser to return doctype start tag information to application
* PARAMETERS
* data [IN] Reserved
* doctypeName [IN] Document type name string
* sysid [IN] System external id list
* pubid [IN] Public external id list
* internal_subset [IN] Internal subset presence
* error [IN] Error code
* RETURNS
* void
*****************************************************************************/
static void mmi_sample_xml_parser_doctype_start_handler(
void *data,
const kal_char *doctypeName,
const kal_char **sysid,
const kal_char **pubid,
kal_int32 internal_subset,
kal_int32 error)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
kal_uint32 index = 0;
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
if (XML_NO_ERROR == error)
{
index = 0;
if (NULL != sysid)
{
while (sysid[index])
{
printf("%s\n", sysid[index]);
/*
* TODO: Add any code here...
* If it wants to deal with the data of the system external id list, application
* could add the process code here, and could put the data to buffer, to file,
* or to other structure, it depends on the special usages.
* [Example]:
* 1) strcpy(app_buf, sysid[index]);
* 2) FS_Write(app_file, (WCHAR*)sysid[index], strlen(sysid[index]), NULL);
* 3) ...
*/
index++;
}
}
index = 0;
if (NULL != pubid)
{
while (pubid[index])
{
printf("%s\n", pubid[index]);
/*
* TODO: Add any code here...
* If it wants to deal with the data of the public external id list, application
* could add the process code here, and could put the data to buffer, to file,
* or to other structure, it depends on the special usages.
* [Example]:
* 1) strcpy(app_buf, pubid[index]);
* 2) FS_Write(app_file, (WCHAR*)pubid[index], strlen(pubid[index]), NULL);
* 3) ...
*/
index++;
}
}
}
else
{
kal_char *err_str = NULL;
err_str = xml_get_err_string(&g_sample_xml_parser_cntx.xml_parser);
printf("%s\n", err_str);
/*
* TODO: Add any code here...
* If it wants to deal with the data of the error string, application could
* add the process code here, and could put the error string to buffer, or to
* file, it depends on the special usages.
* [Example]:
* 1) strcpy(app_buf, err_str);
* 2) FS_Write(app_file, (WCHAR*)err_str, strlen(err_str), NULL);
* 3) ...
*/
}
}
/*****************************************************************************
* FUNCTION
* mmi_sample_xml_parser_doctype_end_handler
* DESCRIPTION
* This function is for XML parser to indicate the end of document type to application.
* PARAMETERS
* data [IN] Reserved
* error [IN] Error code
* RETURNS
* void
*****************************************************************************/
static void mmi_sample_xml_parser_doctype_end_handler(void *data, kal_int32 error)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
if (XML_NO_ERROR != error)
{
kal_char *err_str = NULL;
err_str = xml_get_err_string(&g_sample_xml_parser_cntx.xml_parser);
printf("%s\n", err_str);
/*
* TODO: Add any code here...
* If it wants to deal with the data of the error string, application could
* add the process code here, and could put the error string to buffer, or to
* file, it depends on the special usages.
* [Example]:
* 1) strcpy(app_buf, err_str);
* 2) FS_Write(app_file, (WCHAR*)err_str, strlen(err_str), NULL);
* 3) ...
*/
}
}
/*****************************************************************************
* FUNCTION
* mmi_sample_xml_parser_elem_start_handler
* DESCRIPTION
* This function is for XML parser to return start tag information to application
* PARAMETERS
* data [IN] Reserved
* el [IN] Element name
* attr [IN] Attribute list
* error [IN] Error code
* RETURNS
* void
*****************************************************************************/
static void mmi_sample_xml_parser_elem_start_handler(void *data, const kal_char *el, const kal_char **attr, kal_int32 error)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
kal_int32 index = 0;
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
if (XML_NO_ERROR == error)
{
printf("%s\n", el);
/*
* TODO: Add any code here...
* If it wants to deal with the data of the element, application could
* add the process code here, and could put the data string to buffer, or
* to file, it depends on the special usages.
* [Example]:
* 1) strcpy(app_buf, el);
* 2) FS_Write(app_file, (WCHAR*)el, strlen(el), NULL);
* 3) ...
*/
while ((NULL != attr[index]) && (NULL != attr[index + 1]))
{
printf("%s = %s\n", attr[index], attr[index + 1]);
/*
* TODO: Add any code here...
* If it wants to deal with the data of the element and attribute list,
* application could add the process code here, and could put the data to
* buffer, to file, or to other structure, it depends on the special usages.
* [Example]:
* 1) strcpy(app_buf, attr[index]);
* 2) strcpy(app_buf, attr[index + 1]);
* 3) FS_Write(app_file, (WCHAR*)attr[index], strlen(attr[index]), NULL);
* 4) FS_Write(app_file, (WCHAR*)attr[index + 1], strlen(attr[index + 1]), NULL);
* 5) ...
*/
index += 2;
}
}
else
{
kal_char *err_str = NULL;
err_str = xml_get_err_string(&g_sample_xml_parser_cntx.xml_parser);
printf("%s\n", err_str);
/*
* TODO: Add any code here...
* If it wants to deal with the data of the error string, application could
* add the process code here, and could put the error string to buffer, or to
* file, it depends on the special usages.
* [Example]:
* 1) strcpy(app_buf, err_str);
* 2) FS_Write(app_file, (WCHAR*)err_str, strlen(err_str), NULL);
* 3) ...
*/
}
}
/*****************************************************************************
* FUNCTION
* mmi_sample_xml_parser_elem_end_handler
* DESCRIPTION
* This function is for XML parser to return end tag information to application
* PARAMETERS
* data [IN] Reserved
* el [IN] Element name
* error [IN] Error code
* RETURNS
* void
*****************************************************************************/
static void mmi_sample_xml_parser_elem_end_handler(void *data, const kal_char *el, kal_int32 error)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
if (XML_NO_ERROR == error)
{
printf("%s\n", el);
/*
* TODO: Add any code here...
* If it wants to deal with the data of the element, application could
* add the process code here, and could put the data string to buffer,
* or to file, it depends on the special usages.
* [Example]:
* 1) strcpy(app_buf, el);
* 2) FS_Write(app_file, (WCHAR*)el, strlen(el), NULL);
* 3) ...
*/
}
else
{
kal_char *err_str = NULL;
err_str = xml_get_err_string(&g_sample_xml_parser_cntx.xml_parser);
printf("%s\n", err_str);
/*
* TODO: Add any code here...
* If it wants to deal with the data of the error string, application could
* add the process code here, and could put the error string to buffer, or to
* file, it depends on the special usages.
* [Example]:
* 1) strcpy(app_buf, err_str);
* 2) FS_Write(app_file, (WCHAR*)err_str, strlen(err_str), NULL);
* 3) ...
*/
}
}
/*****************************************************************************
* FUNCTION
* mmi_sample_xml_parser_data_handler
* DESCRIPTION
* This function is for XML parser to return element content information to application
* PARAMETERS
* resv [IN] Reserved
* el [IN] Element name
* data [IN] Data string
* len [IN] Data string length
* error [IN] Error code
* RETURNS
* void
*****************************************************************************/
static void mmi_sample_xml_parser_data_handler(void *resv, const kal_char *el, const kal_char *data, kal_int32 len, kal_int32 error)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
if (XML_NO_ERROR == error)
{
printf("%s\n", data);
/*
* TODO: Add any code here...
* If it wants to deal with the real data of the xml, application could
* add the process code here, and could put the real data string to buffer,
* or to file, it depends on the special usages.
* [Example]:
* 1) strcpy(app_buf, data);
* 2) FS_Write(app_file, (WCHAR*)data, strlen(data), NULL);
* 3) ...
*/
}
else
{
kal_char *err_str = NULL;
err_str = xml_get_err_string(&g_sample_xml_parser_cntx.xml_parser);
printf("%s\n", err_str);
/*
* TODO: Add any code here...
* If it wants to deal with the data of the error string, application could
* add the process code here, and could put the error string to buffer, or to
* file, it depends on the special usages.
* [Example]:
* 1) strcpy(app_buf, err_str);
* 2) FS_Write(app_file, (WCHAR*)err_str, strlen(err_str), NULL);
* 3) ...
*/
}
}
/*****************************************************************************
* FUNCTION
* mmi_sample_xml_parser_malloc_handler
* DESCRIPTION
* XML
* PARAMETERS
* buff_size [IN] buffer size of malloc
* RETURNS
* void
*****************************************************************************/
static void *mmi_sample_xml_parser_malloc_handler(kal_int32 buff_size)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
/*
* TODO: Add any code here...
* If it has been to register the handler of the custom functions for malloc and
* free memory, application should add the mallc function here, and could use the
* app-base memory, or scree-base memory, and then use the adm functions to management
* the memory, and then use the adm malloc function to get the memory from the memory pool.
* it depends on the special applications.
* [Example]:
* 1) kal_adm_alloc(adm_id, buff_size);
* 2) ...
*/
return NULL;
}
/*****************************************************************************
* FUNCTION
* mmi_sample_xml_parser_free_handler
* DESCRIPTION
* XML
* PARAMETERS
* buff_ptr [IN] point of buffer to free
* RETURNS
* void
*****************************************************************************/
static void mmi_sample_xml_parser_free_handler(void * buff_ptr)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
/*
* TODO: Add any code here...
* If it has been to register the handler of the custom functions for malloc and
* free memory, application should add the free function here, and could use the
* app-base memory, or scree-base memory, and then use the adm functions to management
* the memory, and then use the adm free function to free the memory to the memory pool.
* it depends on the special applications.
* [Example]:
* 1) kal_adm_free(adm_id, buff_ptr);
* 2) ...
*/
}
/*****************************************************************************
* FUNCTION
* mmi_sample_xml_parser
* DESCRIPTION
* The sample of xml parser from file or buffer, the output data will be writed to file
* PARAMETERS
* void
* RETURNS
* void
*****************************************************************************/
void mmi_sample_xml_parser(void)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
kal_int32 xml_result = 0;
kal_wchar xml_file_name[MMI_SAMPLE_XML_FILE_PATH_LEN];
kal_int32 driver = 0, ret = 0;
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
/*
* [1]. Create an XML parser before parse a new XML document
* Application has to allocate memory for the XML_PARSER_STRUCT structure
* on it ownbefore calling this API to create parser.
*/
xml_result = xml_new_parser(&g_sample_xml_parser_cntx.xml_parser);
/* If the xml parser is created successful, then you can parse the file continue */
if (XML_RESULT_OK == xml_result)
{
/*
* [2]. Register the doctype, element, data and memory callback function before the parsing,
* Application has to prepare the handlers as callback functions to perform actions it wants
* according to application specific logic when the every element is encountered.
*/
/*
* Register start tag and end tag handler of document type.
* [Note]: If application doesn't want to deal with the document type, this function could not
* be called, or the second parameter start_hdlr and third parameter end_hdlr are set to NULL,
* it will be processed by the default parser procedures, and the data of doc type will be ignored.
*/
xml_register_doctype_handler(
&g_sample_xml_parser_cntx.xml_parser,
mmi_sample_xml_parser_doctype_start_handler,
mmi_sample_xml_parser_doctype_end_handler);
/*
* Register start tag and end tag handler of an element.
* [Note]: If application doesn't want to deal with the element, this function could not be
* called, or the second parameter start_hdlr and third parameter end_hdlr are set to NULL,
* it will be processed by the default parser procedures, and the data of element will be ignored.
* But usually application should deal with the element for special applications.
*/
xml_register_element_handler(
&g_sample_xml_parser_cntx.xml_parser,
mmi_sample_xml_parser_elem_start_handler,
mmi_sample_xml_parser_elem_end_handler);
/*
* Register data handler of an element.
* [Note]: If application doesn't want to deal with the data, this function could not be called,
* or the second parameter data_hdlr is set to NULL, it will be processed by the default parser
* procedures, and the data of element will be ignored. But usually application should deal with
* the data for special applications.
*/
xml_register_data_handler(
&g_sample_xml_parser_cntx.xml_parser,
mmi_sample_xml_parser_data_handler);
/*
* Configure malloc and free function pointer.
* [Note]: If application wants to use the malloc and free functions by themseles, application
* could call this function to register the handler of the custom functions, and the xml parser
* will call the custom functions to malloc and free the memory. Otherwise, if application does
* not register the function handler, xml parser will use the default procedures.
*/
xml_configure_memory(
&g_sample_xml_parser_cntx.xml_parser,
mmi_sample_xml_parser_malloc_handler,
mmi_sample_xml_parser_free_handler);
/* Get sample XML file path*/
driver = FS_GetDrive(FS_DRIVE_V_NORMAL, 1, FS_DRIVE_I_SYSTEM);
memset(xml_file_name, 0, sizeof(xml_file_name));
kal_wsprintf(xml_file_name, MMI_SAMPLE_XML_SAMPLE_FILE, driver);
/* Check the sample xml file is existent or not */
ret = FS_CheckFile(xml_file_name);
/*
* [3]. Parse an XML document.
* If the xml document is existent, APP could call xml_parse function, the input file
* will be parsed, and previously registered start, end tag handlers and data handler
* will be called once corresponding segment is encountered during the parsing.
* If the xml document is not existent, APP could call xml_parse_buffer function, the
* input buffer will be parsed, and previously registered start, end tag handlers and
* data handler will be called once corresponding segment is encountered during the
* parsing.
*/
if(FS_NO_ERROR == ret)
{
/* XML Parser start from file */
xml_result = xml_parse(&g_sample_xml_parser_cntx.xml_parser, xml_file_name);
}
else
{
/* XML Parser start from buffer */
xml_result = xml_parse_buffer(&g_sample_xml_parser_cntx.xml_parser, mmi_sample_xml_parser_buffer, strlen(mmi_sample_xml_parser_buffer));
}
/*
* [4]. Close a parser.
* APP call this function to close a parser after finish parsing and
* to reset parser data structure or release internal allocated memory.
*/
xml_close_parser(&g_sample_xml_parser_cntx.xml_parser);
}
}