24
|
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 <xml/cxml/nw_encoder_stringtable.h>
|
|
20 |
#include <xml/cxml/nw_wbxml_parse.h>
|
|
21 |
#include <xml/cxml/nw_dom_attribute.h>
|
|
22 |
#include <xml/cxml/nw_dom_element.h>
|
|
23 |
#include <xml/cxml/nw_dom_text.h>
|
|
24 |
#include <xml/cxml/nw_dom_document.h>
|
|
25 |
#include "cxml_internal.h"
|
|
26 |
|
|
27 |
/* ------------------------------------------------------------------------- *
|
|
28 |
private methods
|
|
29 |
* ------------------------------------------------------------------------- */
|
|
30 |
|
|
31 |
/* ------------------------------------------------------------------------- */
|
|
32 |
static
|
|
33 |
NW_Status_t
|
|
34 |
NW_Encoder_StringItem_initialize(NW_Encoder_StringItem_t *strItem,
|
|
35 |
NW_String_t *str)
|
|
36 |
{
|
|
37 |
NW_Status_t status = NW_STAT_SUCCESS;
|
|
38 |
|
|
39 |
if (strItem == NULL){
|
|
40 |
return NW_STAT_BAD_INPUT_PARAM;
|
|
41 |
}
|
|
42 |
|
|
43 |
if(str == NULL)
|
|
44 |
{
|
|
45 |
return NW_STAT_BAD_INPUT_PARAM;
|
|
46 |
}
|
|
47 |
|
|
48 |
|
|
49 |
strItem->data.storage = CXML_Mem_Malloc(str->length + 1);
|
|
50 |
|
|
51 |
if(strItem->data.storage == NULL)
|
|
52 |
{
|
|
53 |
return NW_STAT_OUT_OF_MEMORY;
|
|
54 |
}
|
|
55 |
|
|
56 |
NW_Mem_memcpy(strItem->data.storage,str->storage,str->length);
|
|
57 |
|
|
58 |
strItem->data.storage[str->length] = '\0';
|
|
59 |
|
|
60 |
if (status == NW_STAT_SUCCESS)
|
|
61 |
{
|
|
62 |
strItem->frequency = 1;
|
|
63 |
strItem->data.length = str->length;
|
|
64 |
}
|
|
65 |
else
|
|
66 |
{
|
|
67 |
status = NW_STAT_OUT_OF_MEMORY;
|
|
68 |
}
|
|
69 |
|
|
70 |
return status;
|
|
71 |
}
|
|
72 |
|
|
73 |
/*
|
|
74 |
* Finds string contained in an item (AttrValItem or Text Item)
|
|
75 |
* An item can be string, extension, entity, opaque, or token
|
|
76 |
* Only extension and string may contain strings
|
|
77 |
* Returns NW_STAT_BAD_INPUT_PARAM
|
|
78 |
* NW_STAT_SUCCESS
|
|
79 |
*/
|
|
80 |
/* ------------------------------------------------------------------------- */
|
|
81 |
static
|
|
82 |
NW_Status_t
|
|
83 |
NW_Encoder_StringTable_getItemString(NW_Encoder_StringTable_t *strTable,
|
|
84 |
NW_DOM_AttrVal_t *item)
|
|
85 |
{
|
|
86 |
NW_Uint32 ignoreVal;
|
|
87 |
|
|
88 |
if ((strTable == NULL) || (item == NULL)){
|
|
89 |
return NW_STAT_BAD_INPUT_PARAM;
|
|
90 |
}
|
|
91 |
|
|
92 |
switch(item->type) {
|
|
93 |
case NW_DOM_ATTR_VAL_STRING:
|
|
94 |
return NW_Encoder_StringTable_append(strTable, &item->component.string,
|
|
95 |
NW_FALSE, &ignoreVal);
|
|
96 |
|
|
97 |
case NW_DOM_ATTR_VAL_EXTENSION:
|
|
98 |
{
|
|
99 |
NW_Uint8 t; /* 8-bit token */
|
|
100 |
|
|
101 |
t = (NW_Uint8)(item->component.ext.token);
|
|
102 |
if ((item->component.ext.type == NW_TINYDOM_EXTENSION_TYPE_NORMAL)
|
|
103 |
&& ((t == NW_WBXML_EXT_T_0)
|
|
104 |
|| (t == NW_WBXML_EXT_T_1)
|
|
105 |
|| (t == NW_WBXML_EXT_T_2))) {
|
|
106 |
return NW_Encoder_StringTable_append(strTable,
|
|
107 |
&(item->component.ext.value.string),
|
|
108 |
NW_FALSE, &ignoreVal);
|
|
109 |
}
|
|
110 |
break;
|
|
111 |
}
|
|
112 |
default:
|
|
113 |
break;
|
|
114 |
}
|
|
115 |
return NW_STAT_SUCCESS;
|
|
116 |
}
|
|
117 |
|
|
118 |
/**
|
|
119 |
* Finds strings in an attribute - iterates over attribute values
|
|
120 |
* Returns NW_STAT_BAD_INPUT_PARAM
|
|
121 |
* NW_STAT_SUCCESS
|
|
122 |
*/
|
|
123 |
/* ------------------------------------------------------------------------- */
|
|
124 |
static
|
|
125 |
NW_Status_t
|
|
126 |
NW_Encoder_StringTable_getAttributeStrings(NW_Encoder_StringTable_t *strTable,
|
|
127 |
NW_DOM_AttributeHandle_t *attrHandle)
|
|
128 |
{
|
|
129 |
NW_Status_t status;
|
|
130 |
NW_DOM_AttrVal_t attrVal;
|
|
131 |
NW_Uint16 token;
|
|
132 |
NW_Uint16 fqToken;
|
|
133 |
|
|
134 |
if ((strTable == NULL) || (attrHandle == NULL)){
|
|
135 |
return NW_STAT_BAD_INPUT_PARAM;
|
|
136 |
}
|
|
137 |
|
|
138 |
fqToken = NW_DOM_AttributeHandle_getToken(attrHandle);
|
|
139 |
NW_ASSERT(fqToken);
|
|
140 |
token = (NW_Uint16)(fqToken & NW_WBXML_MASK_TOKEN);
|
|
141 |
if (token == NW_WBXML_LITERAL)
|
|
142 |
{
|
|
143 |
NW_Uint32 ignoreVal;
|
|
144 |
NW_String_t attributeName;
|
|
145 |
|
|
146 |
status = NW_DOM_AttributeHandle_getName(attrHandle, &attributeName);
|
|
147 |
if (status != NW_STAT_SUCCESS){
|
|
148 |
return status;
|
|
149 |
}
|
|
150 |
status = NW_Encoder_StringTable_append(strTable, &attributeName, NW_TRUE,
|
|
151 |
&ignoreVal);
|
|
152 |
}
|
|
153 |
|
|
154 |
while (NW_DOM_AttributeHandle_getNextVal(attrHandle, &attrVal) == NW_STAT_WBXML_ITERATE_MORE)
|
|
155 |
{
|
|
156 |
status = NW_Encoder_StringTable_getItemString(strTable, &attrVal);
|
|
157 |
if (status != NW_STAT_SUCCESS){
|
|
158 |
return status;
|
|
159 |
}
|
|
160 |
}
|
|
161 |
|
|
162 |
return NW_STAT_SUCCESS;
|
|
163 |
}
|
|
164 |
|
|
165 |
/**
|
|
166 |
* Finds strings in an Element Node
|
|
167 |
* Returns NW_STAT_BAD_INPUT_PARAM
|
|
168 |
* NW_STAT_SUCCESS
|
|
169 |
*/
|
|
170 |
/* ------------------------------------------------------------------------- */
|
|
171 |
static
|
|
172 |
NW_Status_t
|
|
173 |
NW_Encoder_StringTable_getElementStrings(NW_Encoder_StringTable_t *strTable,
|
|
174 |
NW_DOM_ElementNode_t *e)
|
|
175 |
{
|
|
176 |
NW_Status_t status;
|
|
177 |
NW_DOM_AttributeListIterator_t listIterator;
|
|
178 |
NW_DOM_AttributeHandle_t attrHandle;
|
|
179 |
NW_Uint32 fqToken;
|
|
180 |
NW_Uint8 token;
|
|
181 |
|
|
182 |
if ((strTable == NULL) || (e == NULL)){
|
|
183 |
return NW_STAT_BAD_INPUT_PARAM;
|
|
184 |
}
|
|
185 |
|
|
186 |
fqToken = NW_DOM_ElementNode_getTagToken(e);
|
|
187 |
if (fqToken == 0){
|
|
188 |
return NW_STAT_FAILURE;
|
|
189 |
}
|
|
190 |
|
|
191 |
token = (NW_Uint8)(fqToken & NW_WBXML_MASK_TOKEN);
|
|
192 |
|
|
193 |
/* put element in table if it is LITERAL tag */
|
|
194 |
if (token == NW_WBXML_LITERAL)
|
|
195 |
{
|
|
196 |
NW_String_t elementName;
|
|
197 |
NW_Uint32 ignoreVal;
|
|
198 |
|
|
199 |
if (NW_DOM_ElementNode_getTagName(e, &elementName) != NW_STAT_SUCCESS){
|
|
200 |
return NW_STAT_FAILURE;
|
|
201 |
}
|
|
202 |
status = NW_Encoder_StringTable_append(strTable, &elementName, NW_TRUE,
|
|
203 |
&ignoreVal);
|
|
204 |
if (status != NW_STAT_SUCCESS){
|
|
205 |
return status;
|
|
206 |
}
|
|
207 |
}
|
|
208 |
|
|
209 |
/* go through attribute strings */
|
|
210 |
if (NW_DOM_ElementNode_hasAttributes(e))
|
|
211 |
{
|
|
212 |
status = NW_DOM_ElementNode_getAttributeListIterator(e, &listIterator);
|
|
213 |
if (status != NW_STAT_SUCCESS){
|
|
214 |
return status;
|
|
215 |
}
|
|
216 |
while (NW_DOM_AttributeListIterator_getNextAttribute(&listIterator, &attrHandle) == NW_STAT_WBXML_ITERATE_MORE)
|
|
217 |
{
|
|
218 |
status = NW_Encoder_StringTable_getAttributeStrings(strTable, &attrHandle);
|
|
219 |
if (status != NW_STAT_SUCCESS){
|
|
220 |
return status;
|
|
221 |
}
|
|
222 |
}
|
|
223 |
}
|
|
224 |
return NW_STAT_SUCCESS;
|
|
225 |
}
|
|
226 |
|
|
227 |
/**
|
|
228 |
* Finds strings in a TextNode
|
|
229 |
* Returns NW_STAT_BAD_INPUT_PARAM
|
|
230 |
* NW_STAT_SUCCESS
|
|
231 |
*/
|
|
232 |
/* ------------------------------------------------------------------------- */
|
|
233 |
static
|
|
234 |
NW_Status_t
|
|
235 |
NW_Encoder_StringTable_getTextStrings(NW_Encoder_StringTable_t *strTable,
|
|
236 |
NW_DOM_TextNode_t *textNode)
|
|
237 |
{
|
|
238 |
NW_DOM_TextItemIterator_t textIter;
|
|
239 |
NW_DOM_TextItem_t item;
|
|
240 |
NW_Status_t status;
|
|
241 |
|
|
242 |
if ((strTable == NULL) || (textNode == NULL))
|
|
243 |
{
|
|
244 |
return NW_STAT_BAD_INPUT_PARAM;
|
|
245 |
}
|
|
246 |
|
|
247 |
(void) NW_DOM_TextNode_getTextItemIterator(textNode, &textIter);
|
|
248 |
while (NW_DOM_TextItemIterator_getNextTextItem(&textIter, &item) == NW_STAT_WBXML_ITERATE_MORE)
|
|
249 |
{
|
|
250 |
status = NW_Encoder_StringTable_getItemString(strTable, &item);
|
|
251 |
if (status != NW_STAT_SUCCESS){
|
|
252 |
return status;
|
|
253 |
}
|
|
254 |
}
|
|
255 |
return NW_STAT_SUCCESS;
|
|
256 |
}
|
|
257 |
|
|
258 |
/**
|
|
259 |
* Finds strings in a Node (Recursive in nature)
|
|
260 |
* So it finds strings in entire tree
|
|
261 |
* Returns NW_STAT_BAD_INPUT_PARAM
|
|
262 |
* NW_STAT_SUCCESS
|
|
263 |
*/
|
|
264 |
/* ------------------------------------------------------------------------- */
|
|
265 |
static
|
|
266 |
NW_Status_t
|
|
267 |
NW_Encoder_StringTable_getNodeStrings(NW_Encoder_StringTable_t *strTable,
|
|
268 |
NW_DOM_Node_t *node,
|
|
269 |
NW_WBXML_Dictionary_t *dictionary)
|
|
270 |
{
|
|
271 |
NW_Uint32 type;
|
|
272 |
|
|
273 |
type = NW_DOM_Node_getNodeType(node);
|
|
274 |
|
|
275 |
if ((strTable == NULL) || (node == NULL)){
|
|
276 |
return NW_STAT_BAD_INPUT_PARAM;
|
|
277 |
}
|
|
278 |
|
|
279 |
switch (type)
|
|
280 |
{
|
|
281 |
case NW_DOM_ELEMENT_NODE:
|
|
282 |
{
|
|
283 |
NW_Encoder_StringTable_getElementStrings(strTable, node);
|
|
284 |
if (NW_DOM_Node_getFirstChild(node))
|
|
285 |
{
|
|
286 |
NW_Encoder_StringTable_getNodeStrings(strTable, NW_DOM_Node_getFirstChild(node), dictionary);
|
|
287 |
}
|
|
288 |
|
|
289 |
if (NW_DOM_Node_getNextSibling(node))
|
|
290 |
{
|
|
291 |
NW_Encoder_StringTable_getNodeStrings(strTable, NW_DOM_Node_getNextSibling(node), dictionary);
|
|
292 |
}
|
|
293 |
}
|
|
294 |
break;
|
|
295 |
|
|
296 |
case NW_DOM_TEXT_NODE:
|
|
297 |
NW_Encoder_StringTable_getTextStrings(strTable, node);
|
|
298 |
|
|
299 |
if (NW_DOM_Node_getNextSibling(node))
|
|
300 |
{
|
|
301 |
NW_Encoder_StringTable_getNodeStrings (strTable, NW_DOM_Node_getNextSibling(node), dictionary);
|
|
302 |
}
|
|
303 |
|
|
304 |
break;
|
|
305 |
|
|
306 |
case NW_DOM_PROCESSING_INSTRUCTION_NODE:
|
|
307 |
/* PiPrint (node, encoding); */
|
|
308 |
|
|
309 |
if (NW_DOM_Node_getNextSibling(node))
|
|
310 |
{
|
|
311 |
NW_Encoder_StringTable_getNodeStrings (strTable, NW_DOM_Node_getNextSibling(node), dictionary);
|
|
312 |
}
|
|
313 |
|
|
314 |
break;
|
|
315 |
|
|
316 |
default:
|
|
317 |
if (NW_DOM_Node_getNextSibling(node))
|
|
318 |
{
|
|
319 |
NW_Encoder_StringTable_getNodeStrings (strTable, NW_DOM_Node_getNextSibling(node), dictionary);
|
|
320 |
}
|
|
321 |
}
|
|
322 |
return NW_STAT_SUCCESS;
|
|
323 |
}
|
|
324 |
|
|
325 |
/**
|
|
326 |
* Finds all the string in document
|
|
327 |
* Returns NW_STAT_BAD_INPUT_PARAM
|
|
328 |
* NW_STAT_OUT_OF_MEMORY
|
|
329 |
* NW_STAT_SUCCESS
|
|
330 |
*/
|
|
331 |
/* ------------------------------------------------------------------------- */
|
|
332 |
static
|
|
333 |
NW_Status_t
|
|
334 |
NW_Encoder_StringTable_findDocStrings(NW_Encoder_StringTable_t *strTable,
|
|
335 |
NW_DOM_DocumentNode_t *doc,
|
|
336 |
NW_WBXML_Dictionary_t *dictionary)
|
|
337 |
{
|
|
338 |
NW_String_t docType;
|
|
339 |
NW_DOM_ElementNode_t *elem;
|
|
340 |
NW_Status_t status;
|
|
341 |
|
|
342 |
if ((strTable == NULL) || (doc == NULL)){
|
|
343 |
return NW_STAT_BAD_INPUT_PARAM;
|
|
344 |
}
|
|
345 |
|
|
346 |
if (NW_DOM_DocumentNode_getPublicIdAsNumber(doc) == 0)
|
|
347 |
{
|
|
348 |
NW_Uint32 ignoreVal;
|
|
349 |
status = NW_DOM_DocumentNode_getPublicId(doc, &docType);
|
|
350 |
if (status != NW_STAT_SUCCESS){
|
|
351 |
return status;
|
|
352 |
}
|
|
353 |
status = NW_Encoder_StringTable_append(strTable, &docType, NW_TRUE,
|
|
354 |
&ignoreVal);
|
|
355 |
if (status != NW_STAT_SUCCESS){
|
|
356 |
return status;
|
|
357 |
}
|
|
358 |
}
|
|
359 |
elem = NW_DOM_DocumentNode_getDocumentElement(doc);
|
|
360 |
status = NW_Encoder_StringTable_getNodeStrings(strTable, elem, dictionary);
|
|
361 |
return status;
|
|
362 |
}
|
|
363 |
|
|
364 |
/* ------------------------------------------------------------------------- *
|
|
365 |
public methods
|
|
366 |
* ------------------------------------------------------------------------- */
|
|
367 |
|
|
368 |
/*
|
|
369 |
* Creates a new String Table
|
|
370 |
*/
|
|
371 |
/* ------------------------------------------------------------------------- */
|
|
372 |
EXPORT_C
|
|
373 |
NW_Encoder_StringTable_t *
|
|
374 |
NW_Encoder_StringTable_new()
|
|
375 |
{
|
|
376 |
NW_Encoder_StringTable_t* strTable;
|
|
377 |
|
|
378 |
strTable = (NW_Encoder_StringTable_t*)
|
|
379 |
NW_Mem_Malloc(sizeof(NW_Encoder_StringTable_t));
|
|
380 |
if (strTable != NULL){
|
|
381 |
strTable->vector = CXML_Vector_Construct(sizeof(NW_Encoder_StringItem_t), 4);
|
|
382 |
if(strTable->vector == NULL)
|
|
383 |
{
|
|
384 |
NW_Mem_Free(strTable);
|
|
385 |
return NULL;
|
|
386 |
}
|
|
387 |
}
|
|
388 |
|
|
389 |
return strTable;
|
|
390 |
}
|
|
391 |
|
|
392 |
/*
|
|
393 |
* Deletes a String Table
|
|
394 |
*/
|
|
395 |
/* ------------------------------------------------------------------------- */
|
|
396 |
|
|
397 |
EXPORT_C void
|
|
398 |
NW_Encoder_StringTable_delete(NW_Encoder_StringTable_t* strTable)
|
|
399 |
{
|
|
400 |
CXML_Vector_t* vectorObj = NULL;
|
|
401 |
NW_Encoder_StringItem_t* strItem = NULL;
|
|
402 |
CXML_Vector_Metric_t index;
|
|
403 |
|
|
404 |
if (strTable == NULL)
|
|
405 |
{
|
|
406 |
return;
|
|
407 |
}
|
|
408 |
|
|
409 |
if(strTable->vector)
|
|
410 |
{
|
|
411 |
vectorObj = strTable->vector;
|
|
412 |
}
|
|
413 |
|
|
414 |
//Free the data memory.
|
|
415 |
|
|
416 |
|
|
417 |
for (index = 0; index < vectorObj->size; index++)
|
|
418 |
{
|
|
419 |
strItem = (NW_Encoder_StringItem_t*) CXML_Vector_AddressAt(vectorObj,index);
|
|
420 |
if(strItem)
|
|
421 |
{
|
|
422 |
if((strItem->data).storage)
|
|
423 |
{
|
|
424 |
NW_Mem_Free ( (strItem->data).storage);
|
|
425 |
}
|
|
426 |
}
|
|
427 |
}/*end for() */
|
|
428 |
|
|
429 |
CXML_Vector_Destruct(strTable->vector);
|
|
430 |
NW_Mem_Free(strTable);
|
|
431 |
}
|
|
432 |
|
|
433 |
/* returns number of elements in the string table */
|
|
434 |
/* ------------------------------------------------------------------------- */
|
|
435 |
EXPORT_C CXML_Vector_Metric_t
|
|
436 |
NW_Encoder_StringTable_GetSize(NW_Encoder_StringTable_t *strTable)
|
|
437 |
{
|
|
438 |
return (NW_Uint32)strTable->vector->size;
|
|
439 |
}
|
|
440 |
|
|
441 |
/**
|
|
442 |
* Appends new found string in the document to the list. If the string
|
|
443 |
* was already present in the list, then it increases its frequency,
|
|
444 |
* otherwise it creates a new StringItem in the list
|
|
445 |
*
|
|
446 |
* MODIFIED index n= of the string into the table
|
|
447 |
*
|
|
448 |
* Returns NW_STAT_BAD_INPUT_PARAM
|
|
449 |
* NW_STAT_STR_TBL_OVERFLOW
|
|
450 |
* NW_STAT_OUT_OF_MEMORY
|
|
451 |
* NW_STAT_SUCCESS
|
|
452 |
*/
|
|
453 |
/* ------------------------------------------------------------------------- */
|
|
454 |
NW_Status_t
|
|
455 |
NW_Encoder_StringTable_append(NW_Encoder_StringTable_t *strTable,
|
|
456 |
NW_String_t *string,
|
|
457 |
NW_Bool literalOrDocType,
|
|
458 |
NW_Uint32 *extensionByteOffset)
|
|
459 |
{
|
|
460 |
NW_Encoder_StringItem_t* strItem;
|
|
461 |
NW_Encoder_StringItem_t newStrItem;
|
|
462 |
NW_Uint32 ignoreVal;
|
|
463 |
NW_Status_t status;
|
|
464 |
|
|
465 |
if ((strTable == NULL) || (string == NULL)){
|
|
466 |
return NW_STAT_BAD_INPUT_PARAM;
|
|
467 |
}
|
|
468 |
|
|
469 |
newStrItem.data.storage = NULL;
|
|
470 |
|
|
471 |
/* Also, there can be malformed document. cXML parser wants to limits
|
|
472 |
* non DTD elements or attributes. So check current number of elements
|
|
473 |
* in the parser. This is for security reasons.
|
|
474 |
*/
|
|
475 |
|
|
476 |
if((NW_Uint32)strTable->vector->size > CXML_MAX_STRTBL_ITEMS)
|
|
477 |
{
|
|
478 |
return NW_STAT_STR_TBL_OVERFLOW;
|
|
479 |
}
|
|
480 |
|
|
481 |
/* Find if string is already there in the string table. By design
|
|
482 |
if string isn't found, then *extensionByteOffset has value equal to
|
|
483 |
the offset of the new item to add which is just the count of all
|
|
484 |
bytes already in the string table. */
|
|
485 |
strItem = NW_Encoder_StringTable_find(strTable, string, &ignoreVal,
|
|
486 |
extensionByteOffset);
|
|
487 |
if (strItem != NULL) {
|
|
488 |
strItem->frequency++;
|
|
489 |
return NW_STAT_SUCCESS;
|
|
490 |
}
|
|
491 |
/* string not found, create a new entry. We need a duplicating the
|
|
492 |
* strings.
|
|
493 |
*/
|
|
494 |
|
|
495 |
status = NW_Encoder_StringItem_initialize(&newStrItem, string);
|
|
496 |
|
|
497 |
|
|
498 |
if (status != NW_STAT_SUCCESS){
|
|
499 |
return status;
|
|
500 |
}
|
|
501 |
if (literalOrDocType){
|
|
502 |
newStrItem.frequency = 2;
|
|
503 |
}
|
|
504 |
CXML_Vector_InsertAt(strTable->vector, &newStrItem,
|
|
505 |
CXML_Vector_AtEnd, NULL);
|
|
506 |
|
|
507 |
|
|
508 |
return NW_STAT_SUCCESS;
|
|
509 |
}
|
|
510 |
|
|
511 |
/*
|
|
512 |
* Finds a string in the list and returns the corresponding
|
|
513 |
* StringItem, otherwise returns NULL
|
|
514 |
*/
|
|
515 |
/* ------------------------------------------------------------------------- */
|
|
516 |
NW_Encoder_StringItem_t*
|
|
517 |
NW_Encoder_StringTable_find(NW_Encoder_StringTable_t *strTable,
|
|
518 |
NW_String_t *string,
|
|
519 |
NW_Uint32 *strIndex,
|
|
520 |
NW_Uint32 *byteOffset)
|
|
521 |
{
|
|
522 |
CXML_Vector_Metric_t index;
|
|
523 |
|
|
524 |
if ((strTable == NULL) || (string == NULL)){
|
|
525 |
return NULL;
|
|
526 |
}
|
|
527 |
*strIndex = 0;
|
|
528 |
*byteOffset = 0;
|
|
529 |
for (index = 0;
|
|
530 |
index < strTable->vector->size;
|
|
531 |
index++)
|
|
532 |
{
|
|
533 |
NW_Encoder_StringItem_t *item;
|
|
534 |
|
|
535 |
item = (NW_Encoder_StringItem_t*)
|
|
536 |
CXML_Vector_ElementAt(strTable->vector, index);
|
|
537 |
if (NW_String_equals(string, &item->data))
|
|
538 |
{
|
|
539 |
*strIndex = index;
|
|
540 |
return item;
|
|
541 |
}
|
|
542 |
*byteOffset = *byteOffset + NW_String_getByteCount(&item->data);
|
|
543 |
}
|
|
544 |
return NULL;
|
|
545 |
}
|
|
546 |
|
|
547 |
/**
|
|
548 |
* Finds the string at a given index
|
|
549 |
*/
|
|
550 |
/* ------------------------------------------------------------------------- */
|
|
551 |
NW_Status_t
|
|
552 |
NW_Encoder_StringTable_get(NW_Encoder_StringTable_t *strTable,
|
|
553 |
NW_Uint32 index,
|
|
554 |
NW_String_t* string)
|
|
555 |
{
|
|
556 |
NW_Encoder_StringItem_t *strItem;
|
|
557 |
|
|
558 |
strItem = (NW_Encoder_StringItem_t*)
|
|
559 |
CXML_Vector_ElementAt(strTable->vector,
|
|
560 |
(CXML_Vector_Metric_t)index);
|
|
561 |
|
|
562 |
if (strItem){
|
|
563 |
string->storage = strItem->data.storage;
|
|
564 |
string->length = strItem->data.length;
|
|
565 |
return NW_STAT_SUCCESS;
|
|
566 |
}
|
|
567 |
return NW_STAT_FAILURE;
|
|
568 |
}
|
|
569 |
|
|
570 |
/**
|
|
571 |
* Finds item at a given index
|
|
572 |
*/
|
|
573 |
/* ------------------------------------------------------------------------- */
|
|
574 |
NW_Encoder_StringItem_t*
|
|
575 |
NW_Encoder_StringTable_getItemAt(NW_Encoder_StringTable_t *strTable,
|
|
576 |
NW_Uint32 index)
|
|
577 |
{
|
|
578 |
return (NW_Encoder_StringItem_t*)
|
|
579 |
CXML_Vector_ElementAt(strTable->vector,
|
|
580 |
(CXML_Vector_Metric_t)index);
|
|
581 |
}
|
|
582 |
|
|
583 |
/**
|
|
584 |
* Removes a string item
|
|
585 |
*/
|
|
586 |
/* ------------------------------------------------------------------------- */
|
|
587 |
NW_Status_t
|
|
588 |
NW_Encoder_StringTable_removeAt(NW_Encoder_StringTable_t *strTable,
|
|
589 |
CXML_Vector_Metric_t index)
|
|
590 |
{
|
|
591 |
return CXML_Vector_RemoveAt(strTable->vector, (CXML_Vector_Metric_t)index);
|
|
592 |
}
|
|
593 |
|
|
594 |
/*
|
|
595 |
* Returns the total number of bytes used for storing all the
|
|
596 |
* strings in the list
|
|
597 |
*/
|
|
598 |
/* ------------------------------------------------------------------------- */
|
|
599 |
EXPORT_C NW_Uint32
|
|
600 |
NW_Encoder_StringTable_getTotalBytes(NW_Encoder_StringTable_t *strTable)
|
|
601 |
{
|
|
602 |
NW_Uint32 byteLength;
|
|
603 |
CXML_Vector_Metric_t index;
|
|
604 |
|
|
605 |
byteLength = 0;
|
|
606 |
|
|
607 |
for (index = 0;
|
|
608 |
index < strTable->vector->size;
|
|
609 |
index++)
|
|
610 |
{
|
|
611 |
NW_Encoder_StringItem_t *strItem;
|
|
612 |
|
|
613 |
strItem = (NW_Encoder_StringItem_t*)
|
|
614 |
CXML_Vector_ElementAt(strTable->vector, index);
|
|
615 |
byteLength += strItem->data.length;
|
|
616 |
}
|
|
617 |
|
|
618 |
return byteLength;
|
|
619 |
}
|
|
620 |
|
|
621 |
/* ------------------------------------------------------------------------- */
|
|
622 |
EXPORT_C NW_Status_t
|
|
623 |
NW_Encoder_StringTable_createFromDOM(NW_Encoder_StringTable_t *strTable,
|
|
624 |
NW_DOM_DocumentNode_t *doc,
|
|
625 |
NW_WBXML_Dictionary_t *dictionary)
|
|
626 |
{
|
|
627 |
|
|
628 |
/* make a table of strings in the document */
|
|
629 |
NW_Encoder_StringTable_findDocStrings(strTable, doc, dictionary);
|
|
630 |
|
|
631 |
/* The string table will be freed once at the end. So, don't do
|
|
632 |
* this here. This is causing the memory leak otherwise.
|
|
633 |
*/
|
|
634 |
return NW_STAT_SUCCESS;
|
|
635 |
}
|
|
636 |
|
|
637 |
|
|
638 |
/* callback implementations */
|
|
639 |
/* ------------------------------------------------------------------------- */
|
|
640 |
EXPORT_C NW_Status_t
|
|
641 |
NW_Encoder_StringTable_getStringTableOffset(void* pStringTableObject,
|
|
642 |
NW_Uint32 byteCount,
|
|
643 |
const NW_Uint8* pString,
|
|
644 |
NW_Bool* pFound,
|
|
645 |
NW_Uint32* pTableOffset)
|
|
646 |
{
|
|
647 |
NW_Encoder_StringItem_t *strItem;
|
|
648 |
NW_Uint32 strIndex;
|
|
649 |
NW_String_t string;
|
|
650 |
|
|
651 |
string.storage = (NW_Byte*)pString;
|
|
652 |
string.length = byteCount;
|
|
653 |
|
|
654 |
strItem = NW_Encoder_StringTable_find((NW_Encoder_StringTable_t*) pStringTableObject,
|
|
655 |
&string, &strIndex, pTableOffset);
|
|
656 |
|
|
657 |
if (strItem == NULL)
|
|
658 |
*pFound = NW_FALSE;
|
|
659 |
else
|
|
660 |
*pFound = NW_TRUE;
|
|
661 |
|
|
662 |
return NW_STAT_SUCCESS;
|
|
663 |
}
|
|
664 |
|
|
665 |
/* ------------------------------------------------------------------------- */
|
|
666 |
EXPORT_C NW_Status_t
|
|
667 |
NW_Encoder_StringTable_addToStringTable(void* pStringTableObject,
|
|
668 |
NW_Uint32 byteCount,
|
|
669 |
const NW_Uint8* pString,
|
|
670 |
NW_Uint32* pTableOffset)
|
|
671 |
{
|
|
672 |
NW_String_t string;
|
|
673 |
|
|
674 |
string.storage = (NW_Byte*)pString;
|
|
675 |
string.length = byteCount;
|
|
676 |
*pTableOffset = 0;
|
|
677 |
return NW_Encoder_StringTable_append((NW_Encoder_StringTable_t*) pStringTableObject,
|
|
678 |
&string, NW_TRUE, pTableOffset);
|
|
679 |
}
|
|
680 |
|
|
681 |
/* ------------------------------------------------------------------------- */
|
|
682 |
EXPORT_C NW_Status_t
|
|
683 |
NW_Encoder_StringTable_StringTableIterateInit(void* pStringTableObject,
|
|
684 |
void* pStringTableIterator)
|
|
685 |
{
|
|
686 |
NW_Encoder_StringTableIterator_t *strIterator;
|
|
687 |
|
|
688 |
strIterator = (NW_Encoder_StringTableIterator_t*)pStringTableIterator;
|
|
689 |
strIterator->strTable = (NW_Encoder_StringTable_t*)pStringTableObject;
|
|
690 |
strIterator->currentIndex = 0;
|
|
691 |
return NW_STAT_SUCCESS;
|
|
692 |
}
|
|
693 |
|
|
694 |
/* at end of string table return byteCount = 0 */
|
|
695 |
/* ------------------------------------------------------------------------- */
|
|
696 |
EXPORT_C NW_Status_t
|
|
697 |
NW_Encoder_StringTable_StringTableIterateNext(void* pStringTableIterator,
|
|
698 |
NW_Uint32* pByteCount,
|
|
699 |
NW_Uint8** ppBuf)
|
|
700 |
{
|
|
701 |
NW_Encoder_StringTableIterator_t *strIterator;
|
|
702 |
CXML_Vector_Metric_t size;
|
|
703 |
NW_String_t string;
|
|
704 |
NW_Status_t status;
|
|
705 |
|
|
706 |
strIterator = (NW_Encoder_StringTableIterator_t*)pStringTableIterator;
|
|
707 |
size = NW_Encoder_StringTable_GetSize(strIterator->strTable);
|
|
708 |
|
|
709 |
if (strIterator->currentIndex >= size){
|
|
710 |
return NW_STAT_WBXML_ITERATE_DONE;
|
|
711 |
}
|
|
712 |
status = NW_Encoder_StringTable_get(strIterator->strTable, strIterator->currentIndex, &string);
|
|
713 |
if (status != NW_STAT_SUCCESS){
|
|
714 |
return status;
|
|
715 |
}
|
|
716 |
*pByteCount = string.length;
|
|
717 |
*ppBuf = string.storage;
|
|
718 |
strIterator->currentIndex++;
|
|
719 |
return NW_STAT_WBXML_ITERATE_MORE;
|
|
720 |
}
|
|
721 |
|