|
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 /***************************************************************** |
|
20 ** File: wbxml_doc.c |
|
21 ** Description: |
|
22 *****************************************************************/ |
|
23 |
|
24 #include "cxml_internal.h" |
|
25 #include <xml/cxml/nw_wbxml_document.h> |
|
26 #include <xml/cxml/nw_string_char.h> |
|
27 #include <xml/cxml/nw_encoder_stringtable.h> |
|
28 |
|
29 |
|
30 EXPORT_C NW_Status_t |
|
31 NW_WBXML_Document_construct(NW_WBXML_Document_t * doc, NW_Uint32 default_public_id) |
|
32 { |
|
33 doc->version = 0; |
|
34 doc->publicid = 0; |
|
35 doc->doc_type = NULL; |
|
36 doc->default_public_id = default_public_id; |
|
37 doc->charset = 0; |
|
38 doc->strtbl.length = 0; |
|
39 doc->strtbl.data = 0; |
|
40 doc->body_len = 0; |
|
41 doc->strtbl_extension = NW_Encoder_StringTable_new(); |
|
42 if (doc->strtbl_extension == NULL){ |
|
43 return NW_STAT_OUT_OF_MEMORY; |
|
44 } |
|
45 return NW_STAT_SUCCESS; |
|
46 } |
|
47 |
|
48 /* There is no document construct, since initialization is done by |
|
49 * the parser |
|
50 */ |
|
51 |
|
52 EXPORT_C void |
|
53 NW_WBXML_Document_destruct (NW_WBXML_Document_t * doc) |
|
54 { |
|
55 if (doc == NULL) { |
|
56 return; |
|
57 } |
|
58 |
|
59 if (doc->doc_type) { |
|
60 NW_String_delete (doc->doc_type); |
|
61 doc->doc_type = NULL; |
|
62 } |
|
63 |
|
64 NW_Encoder_StringTable_delete(doc->strtbl_extension); |
|
65 } |
|
66 |
|
67 /* |
|
68 * Safely get a string from the string table |
|
69 * |
|
70 * RETURN NULL if an error occurs and sets parser->flags accordingly |
|
71 */ |
|
72 |
|
73 |
|
74 EXPORT_C NW_Status_t |
|
75 NW_WBXML_Document_getTableString (NW_WBXML_Document_t * doc, |
|
76 NW_Uint32 index, |
|
77 NW_String_t *string) |
|
78 { |
|
79 |
|
80 NW_ASSERT(doc != NULL); |
|
81 NW_ASSERT(string != NULL); |
|
82 |
|
83 if (index >= doc->strtbl.length){ |
|
84 /* See if there are extended table members */ |
|
85 if(NW_Encoder_StringTable_GetSize(doc->strtbl_extension) != 0) |
|
86 { |
|
87 NW_Status_t status; |
|
88 NW_Encoder_StringTableIterator_t extTblIter; |
|
89 NW_Uint32 strTblItrByteCnt = 0; |
|
90 NW_Uint32 startingExtTblByteCnt = 0; |
|
91 NW_Uint8* indexBuf; |
|
92 NW_Uint32 extensionTblInx = index - doc->strtbl.length; |
|
93 NW_Bool found = NW_FALSE; |
|
94 |
|
95 /* NW_Encoder_StringTable_get(..) requires the vector item |
|
96 * index not the absolute extension string table index (as |
|
97 * passed parameter [NW_Uint32 index]). So, itertor method |
|
98 * is used to get the string. |
|
99 * |
|
100 */ |
|
101 |
|
102 /*The following function always return success so why bother to check? */ |
|
103 |
|
104 NW_Encoder_StringTable_StringTableIterateInit(doc->strtbl_extension,&extTblIter); |
|
105 |
|
106 status = NW_Encoder_StringTable_StringTableIterateNext |
|
107 (&extTblIter,&strTblItrByteCnt,&indexBuf); |
|
108 |
|
109 |
|
110 while(status == NW_STAT_WBXML_ITERATE_MORE) |
|
111 { |
|
112 |
|
113 if(startingExtTblByteCnt == extensionTblInx) |
|
114 { |
|
115 found = NW_TRUE; |
|
116 break; |
|
117 } |
|
118 |
|
119 startingExtTblByteCnt += strTblItrByteCnt; |
|
120 |
|
121 status = NW_Encoder_StringTable_StringTableIterateNext |
|
122 (&extTblIter,&strTblItrByteCnt,&indexBuf) ; |
|
123 }//end while |
|
124 |
|
125 |
|
126 if(found == NW_TRUE) |
|
127 { |
|
128 string->storage = indexBuf; |
|
129 string->length = strTblItrByteCnt; |
|
130 return NW_STAT_SUCCESS; |
|
131 } |
|
132 } |
|
133 return NW_STAT_WBXML_ERROR_BYTECODE; |
|
134 } |
|
135 |
|
136 /* Make sure string ends within string table */ |
|
137 |
|
138 if (!NW_String_valid(doc->strtbl.data + index, doc->strtbl.length - index, |
|
139 doc->charset)) |
|
140 { |
|
141 return NW_STAT_WBXML_ERROR_BYTECODE; |
|
142 } |
|
143 |
|
144 return NW_String_initialize (string, doc->strtbl.data + index, doc->charset); |
|
145 } |
|
146 |
|
147 /* String table write method |
|
148 * |
|
149 * Writing a dom tree may require adding strings to the string table, |
|
150 * if one exists, or creating a string table if none exists. We create |
|
151 * an extended table using the encoder string table write methods if |
|
152 * this is necessary. Unfortunately, there can be some duplication of |
|
153 * strings between the two tables, because there is no efficient way to |
|
154 * look up a string by name in the real string table. Therefore, any |
|
155 * string that gets added to the table gets added to the extension |
|
156 * table. The encoder takes care of rationalizing the two tables if |
|
157 * the document gets rewritten as wbxml. |
|
158 * |
|
159 * If the string is not already in the table, this method adds it. The |
|
160 * out parameter byteOffset returns the byte offset of the string in the |
|
161 * extension table plus the size of the real string table so that |
|
162 * extension table strings always have positions higher than real table |
|
163 * strings. |
|
164 */ |
|
165 EXPORT_C NW_Status_t |
|
166 NW_WBXML_Document_putTableString(NW_WBXML_Document_t * doc, |
|
167 NW_String_t *string, |
|
168 NW_Uint32 *byteOffset) |
|
169 { |
|
170 NW_Uint32 extensionOffset; |
|
171 NW_Status_t retval = |
|
172 NW_Encoder_StringTable_append(doc->strtbl_extension, |
|
173 string, |
|
174 NW_TRUE, |
|
175 &extensionOffset); |
|
176 *byteOffset = doc->strtbl.length + extensionOffset; |
|
177 return retval; |
|
178 } |
|
179 |
|
180 NW_Uint8 |
|
181 NW_WBXML_Document_getVersion(NW_WBXML_Document_t *document) |
|
182 { |
|
183 NW_ASSERT(document != NULL); |
|
184 return document->version; |
|
185 } |
|
186 |
|
187 NW_Uint32 |
|
188 NW_WBXML_Document_getPublicID(NW_WBXML_Document_t *document) |
|
189 { |
|
190 NW_ASSERT(document != NULL); |
|
191 return document->publicid; |
|
192 } |
|
193 |
|
194 NW_String_t* |
|
195 NW_WBXML_Document_getDocType(NW_WBXML_Document_t *document) |
|
196 { |
|
197 NW_ASSERT(document != NULL); |
|
198 return document->doc_type; |
|
199 } |
|
200 |
|
201 NW_Uint32 |
|
202 NW_WBXML_Document_getEncoding(NW_WBXML_Document_t *document) |
|
203 { |
|
204 NW_ASSERT(document != NULL); |
|
205 return document->charset; |
|
206 } |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |