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 |
|
21 /***************************************************************** |
|
22 ** File: tinydom2wbxml.c |
|
23 ** |
|
24 ** Description: maps TinyDom types onto WBXMLWriter functions |
|
25 *****************************************************************/ |
|
26 |
|
27 #include "cxml_internal.h" |
|
28 #include "nw_encoder_tinydom2wbxml.h" |
|
29 #include <xml/cxml/nw_dom_text.h> |
|
30 |
|
31 /* |
|
32 Writes an AttrVal |
|
33 Return NW_STAT_BAD_INPUT_PARAM |
|
34 NW_STAT_OUT_OF_MEMORY |
|
35 NW_STAT_SUCCESS |
|
36 */ |
|
37 NW_Status_t |
|
38 NW_Encoder_writeAttrVal(NW_WBXML_Writer_t* pW, NW_DOM_AttrVal_t *val, NW_Uint32 encoding) |
|
39 { |
|
40 if ((val == NULL) || (pW == NULL)) { |
|
41 return NW_STAT_BAD_INPUT_PARAM; |
|
42 } |
|
43 |
|
44 switch (NW_DOM_AttrVal_getType(val)) { |
|
45 case NW_DOM_ATTR_VAL_STRING: |
|
46 { |
|
47 NW_String_t *string = &(val->component.string); |
|
48 return NW_WBXML_Writer_Text(pW, encoding, |
|
49 string->length, string->storage); |
|
50 } |
|
51 case NW_DOM_ATTR_VAL_EXTENSION: |
|
52 { |
|
53 NW_Uint8 t; /* 8-bit token */ |
|
54 NW_String_t str; |
|
55 NW_Status_t status; |
|
56 |
|
57 t = (NW_Uint8)(val->component.ext.token); |
|
58 if ((t == NW_WBXML_EXT_0) |
|
59 || (t == NW_WBXML_EXT_1) |
|
60 || (t == NW_WBXML_EXT_2)) { |
|
61 status |
|
62 = NW_WBXML_Writer_Extension(pW, |
|
63 (NW_Uint16)(val->component.ext.token), |
|
64 0, 0, NULL); |
|
65 } else if ((t == NW_WBXML_EXT_T_0) |
|
66 || (t == NW_WBXML_EXT_T_1) |
|
67 || (t == NW_WBXML_EXT_T_2)) { |
|
68 NW_ASSERT((val->component.ext.type |
|
69 == NW_TINYDOM_EXTENSION_TYPE_NORMAL) |
|
70 || (val->component.ext.type |
|
71 == NW_TINYDOM_EXTENSION_TYPE_EXT_T_INTEGER)); |
|
72 if (val->component.ext.type == NW_TINYDOM_EXTENSION_TYPE_NORMAL) { |
|
73 NW_Uint16 token = NW_DOM_TextItem_getExtension(val, &str); |
|
74 status |
|
75 = NW_WBXML_Writer_ExtensionUseStringTable(pW, |
|
76 token, |
|
77 str.length, |
|
78 str.storage); |
|
79 } else { |
|
80 status = NW_WBXML_Writer_Extension(pW, |
|
81 (NW_Uint16)(val->component.ext.token), |
|
82 val->component.ext.value.x, |
|
83 0, NULL); |
|
84 } |
|
85 } else if ((t == NW_WBXML_EXT_I_0) |
|
86 || (t == NW_WBXML_EXT_I_1) |
|
87 || (t == NW_WBXML_EXT_I_2)) { |
|
88 NW_Uint16 token = NW_DOM_TextItem_getExtension(val, &str); |
|
89 status = NW_WBXML_Writer_Extension(pW, token, |
|
90 0, str.length, str.storage); |
|
91 } else { |
|
92 status = NW_STAT_FAILURE; |
|
93 } |
|
94 return status; |
|
95 } |
|
96 case NW_DOM_ATTR_VAL_ENTITY: |
|
97 { |
|
98 NW_Uint32 entity = NW_DOM_TextItem_getEntity(val); |
|
99 return NW_WBXML_Writer_Entity(pW, entity); |
|
100 } |
|
101 case NW_DOM_ATTR_VAL_OPAQUE: |
|
102 { |
|
103 NW_Uint32 length = 0; |
|
104 NW_Byte* data = NW_DOM_AttrVal_getOpaque(val, &length); |
|
105 return NW_WBXML_Writer_Opaque(pW, length, data); |
|
106 } |
|
107 case NW_DOM_ATTR_VAL_TOKEN: |
|
108 { |
|
109 NW_Uint16 fqToken = NW_DOM_AttrVal_getToken(val); |
|
110 return NW_WBXML_Writer_AttributeToken(pW, fqToken); |
|
111 } |
|
112 default: |
|
113 break; |
|
114 } |
|
115 |
|
116 return NW_STAT_FAILURE; |
|
117 } |
|
118 |
|
119 /* |
|
120 Writes an attrFqToken with AttrVal |
|
121 Return NW_STAT_BAD_INPUT_PARAM |
|
122 NW_STAT_OUT_OF_MEMORY |
|
123 NW_STAT_SUCCESS |
|
124 NW_STAT_FAILURE |
|
125 */ |
|
126 NW_Status_t |
|
127 NW_Encoder_writeAttributeByToken(NW_WBXML_Writer_t* pW, |
|
128 NW_Uint16 attrFqToken, |
|
129 NW_TinyDom_AttrVal_t *val, |
|
130 NW_Uint32 encoding) |
|
131 { |
|
132 NW_Status_t s; |
|
133 NW_Uint8 token = (NW_Uint8)(attrFqToken & NW_WBXML_MASK_TOKEN); |
|
134 |
|
135 if ((token == NW_WBXML_LITERAL) || (val == NULL) || (pW == NULL)) { |
|
136 return NW_STAT_BAD_INPUT_PARAM; |
|
137 } |
|
138 |
|
139 s = NW_WBXML_Writer_AttributeToken(pW, attrFqToken); |
|
140 if (NW_STAT_IS_FAILURE(s)) { |
|
141 return s; |
|
142 } |
|
143 |
|
144 /* TODO: Talk to Shaun about it */ |
|
145 if ((NW_DOM_AttrVal_getType(val) == NW_DOM_ATTR_VAL_TOKEN) && |
|
146 (attrFqToken == val->component.value_token)) |
|
147 {} |
|
148 else{ |
|
149 s = NW_Encoder_writeAttrVal(pW, val, encoding); |
|
150 if (NW_STAT_IS_FAILURE(s)) { |
|
151 return s; |
|
152 } |
|
153 } |
|
154 |
|
155 return NW_WBXML_Writer_End(pW); |
|
156 } |
|
157 |
|
158 /* |
|
159 Writes an Attribute by name plus AttrVal |
|
160 Return NW_STAT_BAD_INPUT_PARAM |
|
161 NW_STAT_OUT_OF_MEMORY |
|
162 NW_STAT_SUCCESS |
|
163 NW_STAT_FAILURE |
|
164 */ |
|
165 NW_Status_t |
|
166 NW_Encoder_writeAttributeByName(NW_WBXML_Writer_t* pW, |
|
167 NW_String_t *attrName, |
|
168 NW_TinyDom_AttrVal_t *val, |
|
169 NW_Uint32 encoding) |
|
170 { |
|
171 NW_Status_t s; |
|
172 NW_Uint32 byteCount; |
|
173 NW_Int32 charCount; |
|
174 |
|
175 if ((pW == NULL) || (attrName == NULL) || (val == NULL)) { |
|
176 return NW_STAT_BAD_INPUT_PARAM; |
|
177 } |
|
178 |
|
179 charCount = NW_String_charBuffGetLength(attrName->storage, |
|
180 encoding, |
|
181 &byteCount); |
|
182 if (charCount < 0) { |
|
183 return NW_STAT_BAD_INPUT_PARAM; |
|
184 } |
|
185 NW_ASSERT(byteCount == attrName->length); |
|
186 s = NW_WBXML_Writer_AttributeNameString(pW, encoding, (NW_Uint32)charCount, |
|
187 byteCount, attrName->storage); |
|
188 if (NW_STAT_IS_FAILURE(s)) { |
|
189 return s; |
|
190 } |
|
191 |
|
192 s = NW_Encoder_writeAttrVal(pW, val, encoding); |
|
193 if (NW_STAT_IS_FAILURE(s)) { |
|
194 return s; |
|
195 } |
|
196 |
|
197 return NW_WBXML_Writer_End(pW); |
|
198 } |
|
199 |
|
200 /* |
|
201 Writes an Element by token |
|
202 Return NW_STAT_BAD_INPUT_PARAM |
|
203 NW_STAT_OUT_OF_MEMORY |
|
204 NW_STAT_SUCCESS |
|
205 */ |
|
206 NW_Status_t |
|
207 NW_Encoder_writeElementByToken(NW_WBXML_Writer_t* pW, |
|
208 NW_Uint16 elementToken) |
|
209 { |
|
210 NW_Status_t s; |
|
211 NW_Uint32 ignoreIndex; |
|
212 |
|
213 if (pW == NULL) { |
|
214 return NW_STAT_BAD_INPUT_PARAM; |
|
215 } |
|
216 s = NW_WBXML_Writer_TagToken(pW, elementToken, &ignoreIndex); |
|
217 if (NW_STAT_IS_FAILURE(s)) { |
|
218 return s; |
|
219 } |
|
220 |
|
221 return NW_WBXML_Writer_End(pW); |
|
222 } |
|
223 |
|
224 /* |
|
225 Writes an Element |
|
226 Return NW_STAT_BAD_INPUT_PARAM |
|
227 NW_STAT_OUT_OF_MEMORY |
|
228 NW_STAT_SUCCESS |
|
229 */ |
|
230 NW_Status_t |
|
231 NW_Encoder_writeElementByName(NW_WBXML_Writer_t* pW, |
|
232 NW_String_t *elementName, |
|
233 NW_Uint32 encoding) |
|
234 { |
|
235 NW_Status_t s; |
|
236 NW_Uint32 ignoreIndex; |
|
237 NW_Uint32 byteCount; |
|
238 NW_Int32 charCount; |
|
239 |
|
240 if ((elementName == NULL) || (pW == NULL)) { |
|
241 return NW_STAT_BAD_INPUT_PARAM; |
|
242 } |
|
243 |
|
244 charCount = NW_String_charBuffGetLength(elementName->storage, |
|
245 encoding, |
|
246 &byteCount); |
|
247 if (charCount < 0) { |
|
248 return NW_STAT_BAD_INPUT_PARAM; |
|
249 } |
|
250 NW_ASSERT(byteCount == elementName->length); |
|
251 s = NW_WBXML_Writer_TagString(pW, encoding, (NW_Uint32)charCount, byteCount, |
|
252 elementName->storage, &ignoreIndex); |
|
253 if (NW_STAT_IS_FAILURE(s)) { |
|
254 return s; |
|
255 } |
|
256 |
|
257 return NW_WBXML_Writer_End(pW); |
|
258 } |
|
259 |
|
260 /* |
|
261 Writes a TextItem |
|
262 Return NW_STAT_BAD_INPUT_PARAM |
|
263 NW_STAT_OUT_OF_MEMORY |
|
264 NW_STAT_SUCCESS |
|
265 */ |
|
266 NW_Status_t |
|
267 NW_Encoder_writeText(NW_WBXML_Writer_t* pW, |
|
268 NW_TinyDom_Text_t *text, |
|
269 NW_Uint32 encoding) |
|
270 { |
|
271 NW_Status_t s; |
|
272 s = NW_Encoder_writeAttrVal(pW, text, encoding); |
|
273 if (NW_STAT_IS_FAILURE(s)) { |
|
274 return s; |
|
275 } |
|
276 |
|
277 return NW_WBXML_Writer_End(pW); |
|
278 } |
|
279 |
|
280 |
|