1 mike 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 mike 1.1 **==============================================================================
23 */
24
25 #ifndef _omiar_xml_h
26 #define _omiar_xml_h
27
28 #include <stddef.h>
29
30 /* The maximum number of nested XML elements */
31 #define XML_MAX_NESTED 64
32
33 /* The maximum number of XML namespaces */
34 #define XML_MAX_NAMESPACES 32
35
36 /* The maximum number of registered XML namespaces */
37 #define XML_MAX_REGISTERED_NAMESPACES 32
38
39 /* The maximum number of attributes in a start tag */
40 #define XML_MAX_ATTRIBUTES 32
41
42 /* Represents case where tag has no namespace */
43 mike 1.1 #define XML_NAMESPACE_NONE 0
44
45 #if defined(__cplusplus)
46 extern "C" {
47 #endif
48
49 /* Represents an XML name */
50 typedef struct _XML_Name
51 {
52 /* Pointer to name */
53 char* data;
54 /* Size of name (excluding zero-terminator) */
55 size_t size;
56 }
57 XML_Name;
58
59 /* Represents an XML namespace as registered by the client */
60 typedef struct _XML_RegisteredNameSpace
61 {
62 /* URI for this namespace */
63 const char* uri;
64 mike 1.1
65 /* Hash code for uri */
66 unsigned int uriCode;
67
68 /* Single character namespace name expected by client */
69 char id;
70 }
71 XML_RegisteredNameSpace;
72
73 /* Represents an XML namespace as encountered during parsing */
74 typedef struct _XML_NameSpace
75 {
76 /* Namespace name */
77 const char* name;
78
79 /* Hash code for name */
80 unsigned int nameCode;
81
82 /* URI for this namespace */
83 const char* uri;
84
85 mike 1.1 /* Single character namespace name expected by client */
86 char id;
87
88 /* Depth at which this definition was encountered */
89 size_t depth;
90 }
91 XML_NameSpace;
92
93 void XML_NameSpace_Dump(
94 XML_NameSpace* self);
95
96 /* Represents an XML attributes */
97 typedef struct _XML_Attr
98 {
99 const char* name;
100 const char* value;
101 }
102 XML_Attr;
103
104 /* XML element type tag */
105 typedef enum _XML_Type
106 mike 1.1 {
107 XML_NONE,
108 XML_START,
109 XML_END,
110 XML_INSTRUCTION,
111 XML_CHARS,
112 XML_COMMENT
113 }
114 XML_Type;
115
116 #if 0
117 /* Attribute info (for structure mappings) */
118 typedef struct _XML_AttrDecl
119 {
120 /* Name of this attribute */
121 const char* name;
122
123 /* 'S'=String 'L'=Long 'D'=Double */
124 char type;
125
126 /* Offset of the field in destination structure */
127 mike 1.1 size_t offset;
128
129 /* Whether this attribute is required */
130 int required;
131
132 /* Pointer to value (char*, long*, or double*) */
133 void* value;
134 }
135 XML_AttrDecl;
136 #endif
137
138 /* Represents one XML element */
139 typedef struct _XML_Elem
140 {
141 /* Type of this XML object */
142 XML_Type type;
143
144 /* Character data or tag name */
145 const char* data;
146 size_t size;
147
148 mike 1.1 /* Attributes */
149 XML_Attr attrs[XML_MAX_ATTRIBUTES];
150 size_t attrsSize;
151 }
152 XML_Elem;
153
154 const char* XML_Elem_GetAttr(
155 XML_Elem* self,
156 const char* name);
157
158 void XML_Elem_Dump(
159 const XML_Elem* self);
160
161 typedef struct _XML
162 {
163 /* Points to first text character zero-terminated text */
164 char* text;
165
166 /* Pointer to current character */
167 char* ptr;
168
169 mike 1.1 /* Line number */
170 size_t line;
171
172 /* Status: 0=Okay, 1=Done, 2=Failed */
173 int status;
174
175 /* Error message */
176 char message[256];
177
178 /* Stack of open tags (used to match closing tags) */
179 XML_Name stack[XML_MAX_NESTED];
180 size_t stackSize;
181
182 /* Current nesting level */
183 size_t nesting;
184
185 /* Stack of dummy elements generated for empty tags and PutBack calls */
186 XML_Elem elemStack[XML_MAX_NESTED];
187 size_t elemStackSize;
188
189 /* Array of namespaces */
190 mike 1.1 XML_NameSpace nameSpaces[XML_MAX_NAMESPACES];
191 size_t nameSpacesSize;
192
193 /* Index of last namespace lookup from nameSpaces[] array */
194 size_t nameSpacesCacheIndex;
195
196 /* Predefined namespaces */
197 XML_RegisteredNameSpace registeredNameSpaces[XML_MAX_NAMESPACES];
198 size_t registeredNameSpacesSize;
199
200 /* Internal parser state */
201 int state;
202
203 /* Whether XML root element has been encountered */
204 int foundRoot;
205 }
206 XML;
207
208 void XML_Init(
209 XML* self);
210
211 mike 1.1 void XML_SetText(
212 XML* self,
213 char* text);
214
215 int XML_Next(
216 XML* self,
217 XML_Elem* elem);
218
219 int XML_Expect(
220 XML* self,
221 XML_Elem* elem,
222 XML_Type type,
223 const char* name);
224
225 int XML_Skip(
226 XML* self);
227
228 int XML_RegisterNameSpace(
229 XML* self,
230 char id,
231 const char* uri);
232 mike 1.1
233 int XML_PutBack(
234 XML* self,
235 const XML_Elem* elem);
236
237 void XML_Dump(
238 XML* self);
239
240 void XML_PutError(XML* self);
241
242 void XML_Raise(XML* self, const char* format, ...);
243
244 void XML_FormatError(XML* self, char* format, size_t size);
245
246 #if defined(__cplusplus)
247 } /* extern "C" */
248 #endif
249
250 #endif /* _omiar_xml_h */
|