xml/cxmllibrary/src/tinydom/src/tiny_dom_utils.c
branchRCL_3
changeset 21 604ca70b6235
parent 20 889504eac4fb
equal deleted inserted replaced
20:889504eac4fb 21:604ca70b6235
     1 /*
       
     2 * Copyright (c) 2000 - 2001 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 the License "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 
       
    19 #include "cxml_internal.h"
       
    20 #include <xml/cxml/nw_tinydom_utils.h>
       
    21 #include <xml/cxml/nw_dom_document.h>
       
    22 
       
    23 
       
    24 /****************************************************************************
       
    25  * Name:          NW_TinyDom_ParserInitialize
       
    26  * Description:   Helper function to initialize the parser
       
    27  *                
       
    28  * Parameters:    p -- pointer to a Parser_t struct
       
    29  *                dictionaries -- pointer to the WBXML dictionaries
       
    30  *                dictionaryCount -- number of dictionaries 
       
    31  *                
       
    32  * Algorithm:     This is just a wrapper to conceal unnecessary initialization details.
       
    33  * Return value:  NW_STAT_SUCCESS, NW_STAT_FAILURE, NW_STAT_OUT_OF_MEMORY
       
    34  ***************************************************************************/
       
    35 EXPORT_C NW_Status_t NW_TinyDom_ParserInitialize(Parser_t *p, 
       
    36                                         NW_WBXML_Dictionary_t * dictionaries[],
       
    37                                         NW_Int32 dictionaryCount,
       
    38                                         NW_Uint32 default_public_id)
       
    39 {
       
    40   NW_Status_t           status;
       
    41   
       
    42   NW_ASSERT(p != NULL);
       
    43 
       
    44   (void) NW_Mem_memset(p, 0, sizeof(*p));
       
    45 
       
    46   /* Initialize the dictionary to use */
       
    47   if (dictionaries){
       
    48     status = NW_WBXML_Dictionary_add( dictionaryCount, dictionaries );
       
    49 
       
    50     if (status == NW_STAT_FAILURE) {
       
    51 
       
    52       status = NW_WBXML_Dictionary_initialize (dictionaryCount, dictionaries);
       
    53       if (status != NW_STAT_SUCCESS) {
       
    54         return status;
       
    55       }
       
    56     }
       
    57   }
       
    58 
       
    59   /* create the WBXML Parser */
       
    60   NW_WBXML_Parser_newInPlace (&(p->wbxmlParser));
       
    61   status = NW_WBXML_Document_construct(&(p->document), default_public_id);
       
    62 
       
    63   /* This ASSERT removed for "out of memory" testing using CXML Testkit component */
       
    64 //  NW_ASSERT(status == NW_STAT_SUCCESS);
       
    65   
       
    66   if (status == NW_STAT_SUCCESS){
       
    67     /* initialize the TinyDOM stuff */
       
    68     NW_TinyDom_Tree_construct (&(p->tinyDOMTree), &(p->wbxmlParser), 
       
    69                                &(p->document), &(p->writer));
       
    70     NW_TinyDom_Parser_construct (&(p->tinyParser), &(p->tinyDOMTree));
       
    71   }
       
    72   return status;
       
    73 }
       
    74 
       
    75 /****************************************************************************
       
    76  * Name:          NW_TinyDom_MakeDOMTree
       
    77  * Description:   Helper function to parse the buffer, creating a tree
       
    78  *                
       
    79  * Parameters:    p       -- pointer to a Parser_t struct
       
    80  *                buffer  -- pointer to content
       
    81  *                length  -- number of bytes in buffer
       
    82  *                root    -- pointer to document root node
       
    83  * Algorithm:     This is just a wrapper to conceal unnecessary parsing details.
       
    84  * Return value:  NW_STAT_SUCCESS, NW_STAT_FAILURE, NW_STAT_OUT_OF_MEMORY
       
    85  ***************************************************************************/
       
    86 EXPORT_C NW_Status_t
       
    87 NW_TinyDom_MakeDOMTree (Parser_t *p, 
       
    88                         NW_Byte *buffer,
       
    89                         NW_Uint32 length, 
       
    90                         NW_DOM_DocumentNode_t **root)
       
    91 {
       
    92  
       
    93   NW_Status_t status;
       
    94 
       
    95   NW_ASSERT(p != NULL);
       
    96  
       
    97   /* build the tinyDOM tree */
       
    98   status = NW_TinyDom_Parser_buildTree (&(p->tinyParser), (char *)buffer, length, NW_FALSE);
       
    99   if ((status == NW_STAT_SUCCESS) && (root != NULL)) {
       
   100     *root = p->tinyDOMTree.root_node;
       
   101   }
       
   102   return status;
       
   103 }
       
   104 
       
   105 EXPORT_C NW_Status_t
       
   106 NW_TinyDom_AppendDOMTree (Parser_t *p, 
       
   107                         NW_Byte *buffer,
       
   108                         NW_Uint32 length, 
       
   109                         NW_Uint32 cp_count,
       
   110                         NW_Int32 lastValid,
       
   111                         NW_DOM_DocumentNode_t **root)
       
   112 {
       
   113  
       
   114   NW_Status_t status;
       
   115 
       
   116   NW_ASSERT(p != NULL);
       
   117  
       
   118   /* build the tinyDOM tree */
       
   119   //p->wbxmlParser.offset = p->wbxmlParser.lastValid;
       
   120   p->wbxmlParser.offset = NW_TinyDom_getLastValid(p->tinyParser.dom_tree);
       
   121   p->tinyParser.cp_count = cp_count;
       
   122   //p->wbxmlParser.lastValid = lastValid;
       
   123   status = NW_TinyDom_Parser_incrementalBuildTree (&(p->tinyParser), (char *)buffer, length, NW_FALSE, lastValid);
       
   124   if ((status == NW_STAT_SUCCESS) && (root != NULL)) {
       
   125     *root = p->tinyDOMTree.root_node;
       
   126   }
       
   127   return status;
       
   128 }
       
   129 
       
   130 /****************************************************************************
       
   131  * Name:          NW_TinyDom_ParserDelete
       
   132  * Description:   Free the internal memory in allocated in NW_TinyDom_MakeDOMTree:
       
   133  *                
       
   134  * Parameters:    p       -- pointer to a Parser_t struct
       
   135  *                
       
   136  * Algorithm:     
       
   137  * Return value:  None
       
   138  ***************************************************************************/
       
   139 EXPORT_C void NW_TinyDom_ParserDelete(Parser_t *p)
       
   140 {
       
   141   if (p != NULL) {
       
   142     NW_WBXML_Parser_delete(&(p->wbxmlParser));
       
   143     NW_TinyDom_Tree_destruct(&(p->tinyDOMTree));
       
   144     NW_WBXML_Document_destruct(&(p->document));
       
   145     (void) NW_Mem_memset(p, 0, sizeof(*p));
       
   146   }
       
   147 }