1 krisbash 1.1 /*
2 **==============================================================================
3 **
4 ** Open Management Infrastructure (OMI)
5 **
6 ** Copyright (c) Microsoft Corporation
7 **
8 ** Licensed under the Apache License, Version 2.0 (the "License"); you may not
9 ** use this file except in compliance with the License. You may obtain a copy
10 ** of the License at
11 **
12 ** http://www.apache.org/licenses/LICENSE-2.0
13 **
14 ** THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 ** KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
16 ** WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
17 ** MERCHANTABLITY OR NON-INFRINGEMENT.
18 **
19 ** See the Apache 2 License for the specific language governing permissions
20 ** and limitations under the License.
21 **
22 krisbash 1.1 **==============================================================================
23 */
24 #ifndef _oscar_xml_h
25 #define _oscar_xml_h
26
27 #include <stddef.h>
28 #include <common.h>
29
30 #if !defined(_MSC_VER)
31 # include <wchar.h>
32 # define __in
33 # define __out
34 # define __inout
35 # define __in_z
36 # define __in_z_opt
37 # define __inout_z
38 # define __out_ecount_z(size)
39 # define __deref_inout_z
40 # define __in_ecount_z(n)
41 #endif
42
43 krisbash 1.1 /* Unicode character */
44 #if defined(CONFIG_ENABLE_WCHAR)
45 typedef wchar_t Char;
46 typedef wchar_t UChar;
47 # define XML_strcmp wcscmp
48 #else
49 typedef char Char;
50 typedef unsigned char UChar;
51 # define XML_strcmp strcmp
52 #endif
53
54 /* The maximum number of nested XML elements */
55 #define XML_MAX_NESTED 64
56
57 /* The maximum number of XML namespaces */
58 #define XML_MAX_NAMESPACES 32
59
60 /* The maximum number of registered XML namespaces */
61 #define XML_MAX_REGISTERED_NAMESPACES 32
62
63 /* The maximum number of attributes in a start tag */
64 krisbash 1.1 #define XML_MAX_ATTRIBUTES 32
65
66 /* Represents case where tag has no namespace */
67 #define XML_NAMESPACE_NONE 0
68
69 #if defined(__cplusplus)
70 extern "C" {
71 #endif
72
73 /* Represents an XML name */
74 typedef struct _XML_Name
75 {
76 /* Pointer to name */
77 Char* data;
78
79 /* Size of name (excluding zero-terminator) */
80 size_t size;
81
82 /* Full namespace URI */
83 const Char* namespaceUri;
84 size_t namespaceUriSize;
85 krisbash 1.1
86 /* Nonzero if a registered namespace was used */
87 Char namespaceId;
88 }
89 XML_Name;
90
91 /* Represents an XML namespace as registered by the client */
92 typedef struct _XML_RegisteredNameSpace
93 {
94 /* URI for this namespace */
95 const Char* uri;
96
97 /* Hash code for uri */
98 unsigned int uriCode;
99
100 /* Single character namespace name expected by client */
101 Char id;
102 }
103 XML_RegisteredNameSpace;
104
105 /* Represents an XML namespace as encountered during parsing */
106 krisbash 1.1 typedef struct _XML_NameSpace
107 {
108 /* Namespace name */
109 const Char* name;
110
111 /* Hash code for name */
112 unsigned int nameCode;
113
114 /* URI for this namespace */
115 const Char* uri;
116 size_t uriSize;
117
118 /* Single character namespace name expected by client */
119 Char id;
120
121 /* Depth at which this definition was encountered */
122 size_t depth;
123 }
124 XML_NameSpace;
125
126 void XML_NameSpace_Dump(
127 krisbash 1.1 __in XML_NameSpace* self);
128
129 /* Represents an XML attributes */
130 typedef struct _XML_Attr
131 {
132 Char* name;
133 size_t nameSize;
134
135 /* Full namespace URI */
136 const Char* namespaceUri;
137 size_t namespaceUriSize;
138
139 /* Nonzero if a registered namespace was used */
140 Char namespaceId;
141
142 Char* value;
143 size_t valueSize;
144 }
145 XML_Attr;
146
147 /* XML element type tag */
148 krisbash 1.1 typedef enum _XML_Type
149 {
150 XML_NONE,
151 XML_START,
152 XML_END,
153 XML_INSTRUCTION,
154 XML_CHARS,
155 XML_COMMENT
156 }
157 XML_Type;
158
159 #if 0
160 /* Attribute info (for structure mappings) */
161 typedef struct _XML_AttrDecl
162 {
163 /* Name of this attribute */
164 const Char* name;
165
166 /* 'S'=String 'L'=Long 'D'=Double */
167 Char type;
168
169 krisbash 1.1 /* Offset of the field in destination structure */
170 size_t offset;
171
172 /* Whether this attribute is required */
173 int required;
174
175 /* Pointer to value (Char*, long*, or double*) */
176 void* value;
177 }
178 XML_AttrDecl;
179 #endif
180
181 /* Represents one XML element */
182 typedef struct _XML_Elem
183 {
184 /* Type of this XML object */
185 XML_Type type;
186
187 /* Tag or character data */
188 Char* data;
189 size_t size;
190 krisbash 1.1
191 /* Full namespace URI */
192 const Char* namespaceUri;
193 size_t namespaceUriSize;
194
195 /* Nonzero if a registered namespace was used */
196 Char namespaceId;
197
198 /* Attributes */
199 XML_Attr attrs[XML_MAX_ATTRIBUTES];
200 size_t attrsSize;
201 }
202 XML_Elem;
203
204 const Char* XML_Elem_GetAttr(
205 __inout XML_Elem* self,
206 __in_z const Char* name);
207
208 static __inline int XML_CompareName(
209 __in const XML_Name* self,
210 Char namespaceId,
211 krisbash 1.1 __in_z const Char* name)
212 {
213 if (self->namespaceId != namespaceId)
214 return self->namespaceId - namespaceId;
215 return XML_strcmp(self->data, name);
216 }
217
218 void XML_Elem_Dump(
219 __in const XML_Elem* self);
220
221 typedef struct _XML
222 {
223 /* Points to first text character zero-terminated text */
224 Char* text;
225
226 /* Pointer to current character */
227 Char* ptr;
228
229 /* Line number */
230 size_t line;
231
232 krisbash 1.1 /* Status: 0=Okay, 1=Done, 2=Failed */
233 int status;
234
235 /* Error message */
236 Char message[256];
237
238 /* Stack of open tags (used to match closing tags) */
239 XML_Name stack[XML_MAX_NESTED];
240 size_t stackSize;
241
242 /* Current nesting level */
243 size_t nesting;
244
245 /* Stack of dummy elements generated for empty tags and PutBack calls */
246 XML_Elem elemStack[XML_MAX_NESTED];
247 size_t elemStackSize;
248
249 /* Array of namespaces */
250 XML_NameSpace nameSpaces[XML_MAX_NAMESPACES];
251 size_t nameSpacesSize;
252
253 krisbash 1.1 /* Index of last namespace lookup from nameSpaces[] array */
254 size_t nameSpacesCacheIndex;
255
256 /* Predefined namespaces */
257 XML_RegisteredNameSpace registeredNameSpaces[XML_MAX_NAMESPACES];
258 size_t registeredNameSpacesSize;
259
260 /* Internal parser state */
261 int state;
262
263 /* Whether XML root element has been encountered */
264 int foundRoot;
265 }
266 XML;
267
268 void XML_Init(
269 __out XML* self);
270
271 void XML_SetText(
272 __inout XML* self,
273 __in_z Char* text);
274 krisbash 1.1
275 int XML_Next(
276 __inout XML* self,
277 __out XML_Elem* elem);
278
279 int XML_Expect(
280 __inout XML* self,
281 __out XML_Elem* elem,
282 XML_Type type,
283 Char knownNamespaceID, /* 0 means not well known */
284 __in_z_opt const Char * namespaceURI, /* NULL if expectedNamespace is not 0, otherwise real URL */
285 __in_z const Char* name);
286
287 int XML_Skip(
288 __inout XML* self);
289
290 int XML_RegisterNameSpace(
291 __inout XML* self,
292 Char id,
293 __in_z const Char* uri);
294
295 krisbash 1.1 int XML_PutBack(
296 __inout XML* self,
297 __in const XML_Elem* elem);
298
299 int XML_StripWhitespace(
300 __inout XML_Elem* elem);
301
302 void XML_Dump(
303 __in XML* self);
304
305 void XML_PutError(__inout XML* self);
306
307 void XML_Raise(
308 __inout XML* self,
309 unsigned formatStringId,
310 const Char* format,
311 ...);
312
313 void XML_FormatError(
314 __inout XML* self,
315 __out_ecount_z(size) Char* format,
316 krisbash 1.1 size_t size);
317
318 #if defined(__cplusplus)
319 } /* extern "C" */
320 #endif
321
322 #endif /* _oscar_xml_h */
|