Edinburgh Speech Tools  2.4-release
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends Pages
xml_example.cc
1  /************************************************************************/
2  /* */
3  /* Centre for Speech Technology Research */
4  /* University of Edinburgh, UK */
5  /* Copyright (c) 1996,1997 */
6  /* All Rights Reserved. */
7  /* */
8  /* Permission is hereby granted, free of charge, to use and distribute */
9  /* this software and its documentation without restriction, including */
10  /* without limitation the rights to use, copy, modify, merge, publish, */
11  /* distribute, sublicense, and/or sell copies of this work, and to */
12  /* permit persons to whom this work is furnished to do so, subject to */
13  /* the following conditions: */
14  /* 1. The code must retain the above copyright notice, this list of */
15  /* conditions and the following disclaimer. */
16  /* 2. Any modifications must be clearly marked as such. */
17  /* 3. Original authors' names are not deleted. */
18  /* 4. The authors' names are not used to endorse or promote products */
19  /* derived from this software without specific prior written */
20  /* permission. */
21  /* */
22  /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */
23  /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
24  /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
25  /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */
26  /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
27  /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
28  /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
29  /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
30  /* THIS SOFTWARE. */
31  /* */
32  /*************************************************************************/
33  /* */
34  /* Author: Richard Caley (rjc@cstr.ed.ac.uk) */
35  /* -------------------------------------------------------------------- */
36  /* Simple XML processing example. */
37  /* */
38  /*************************************************************************/
39 
40 #include <cstdlib>
41 #include <fstream>
42 #include <iostream>
43 #include "rxp/XML_Parser.h"
44 
45 #if defined(DATAC)
46 # define __STRINGIZE(X) #X
47 # define DATA __STRINGIZE(DATAC)
48 #endif
49 
50 /**@name XML_Parser:example
51  *
52  * Simple XML processing example.
53  *
54  * @see XML_Parser
55  */
56 //@{
57 
58 /** Any data needed during processing has to be passed around,
59  * here we just keep track of the nesting depth.
60  */
61 struct Parse_State
62  {
63  int depth;
64  };
65 
66 /** Define a subset of XML_Parser_Class which which has callbacks for
67  * what we want to do. In a real application we might not need
68  * to define them all.
69  */
70 class My_Parser_Class : public XML_Parser_Class
71 {
72 protected:
73  virtual void document_open(XML_Parser_Class &c,
74  XML_Parser &p,
75  void *data);
76  virtual void document_close(XML_Parser_Class &c,
77  XML_Parser &p,
78  void *data);
79 
80  virtual void element_open(XML_Parser_Class &c,
81  XML_Parser &p,
82  void *data,
83  const char *name,
84  XML_Attribute_List &attributes);
85  virtual void element(XML_Parser_Class &c,
86  XML_Parser &p,
87  void *data,
88  const char *name,
89  XML_Attribute_List &attributes);
90  virtual void element_close(XML_Parser_Class &c,
91  XML_Parser &p,
92  void *data,
93  const char *name);
94 
95  virtual void pcdata(XML_Parser_Class &c,
96  XML_Parser &p,
97  void *data,
98  const char *chars);
99  virtual void cdata(XML_Parser_Class &c,
100  XML_Parser &p,
101  void *data,
102  const char *chars);
103 
104  virtual void processing(XML_Parser_Class &c,
105  XML_Parser &p,
106  void *data,
107  const char *instruction);
108  virtual void error(XML_Parser_Class &c,
109  XML_Parser &p,
110  void *data);
111 };
112 
113 
114 int main(void)
115 {
116  My_Parser_Class pclass;
117  Parse_State state;
118 
119  /** Rewriting rules for public and system IDs can be
120  * used to locate local copies etc.
121  */
122 
123  pclass.register_id("//EST//Test/\\(.*\\)",
124  DATA "/\\1.dtd");
125 
126  /** An individual parser runs over a single source.
127  */
128  XML_Parser *parser = pclass.make_parser(DATA "/eg.xml",
129  &state);
130  /** Run the parser.
131  */
132  parser->go();
133  exit(0);
134 }
135 
136 /** Now we define the callbacks.
137  */
138 
139 void My_Parser_Class::document_open(XML_Parser_Class &c,
140  XML_Parser &p,
141  void *data)
142 {
143  (void)c; (void)p;
144  Parse_State *state = (Parse_State *)data;
145 
146  state->depth=1;
147 
148  printf("%*s document %d\n", state->depth*4, ">", state->depth);
149 }
150 
151 void My_Parser_Class::document_close(XML_Parser_Class &c,
152  XML_Parser &p,
153  void *data)
154 {
155  (void)c; (void)p;
156  Parse_State *state = (Parse_State *)data;
157 
158  printf("%*s <document %d\n", state->depth*4, ">", state->depth);
159 }
160 
161 
162 void My_Parser_Class::element_open(XML_Parser_Class &c,
163  XML_Parser &p,
164  void *data,
165  const char *name,
166  XML_Attribute_List &attributes)
167 {
168  (void)c; (void)p; (void)attributes;
169  Parse_State *state = (Parse_State *)data;
170 
171  state->depth++;
172 
173  printf("%*s %s %d\n", state->depth*4, ">", name, state->depth);
174 }
175 
176 
177 void My_Parser_Class::element(XML_Parser_Class &c,
178  XML_Parser &p,
179  void *data,
180  const char *name,
181  XML_Attribute_List &attributes)
182 {
183  (void)c; (void)p; (void)attributes;
184  Parse_State *state = (Parse_State *)data;
185 
186  printf("%*s %s %d\n", state->depth*4, ":", name, state->depth);
187 }
188 
189 
190 void My_Parser_Class::element_close(XML_Parser_Class &c,
191  XML_Parser &p,
192  void *data,
193  const char *name)
194 {
195  (void)c; (void)p;
196  Parse_State *state = (Parse_State *)data;
197 
198  printf("%*s %s %d\n", state->depth*4, "<", name, state->depth);
199  state->depth--;
200 }
201 
202 
203 void My_Parser_Class::pcdata(XML_Parser_Class &c,
204  XML_Parser &p,
205  void *data,
206  const char *chars)
207 {
208  (void)c; (void)p;
209  Parse_State *state = (Parse_State *)data;
210 
211  printf("%*s [pcdata[%s]] %d\n", state->depth*4, "", chars, state->depth);
212 }
213 
214 
215 void My_Parser_Class::cdata(XML_Parser_Class &c,
216  XML_Parser &p,
217  void *data,
218  const char *chars)
219 {
220  (void)c; (void)p;
221  Parse_State *state = (Parse_State *)data;
222 
223  printf("%*s [cdata[%s]] %d\n", state->depth*4, "", chars, state->depth);
224 }
225 
226 
227 void My_Parser_Class::processing(XML_Parser_Class &c,
228  XML_Parser &p,
229  void *data,
230  const char *instruction)
231 {
232  (void)c; (void)p;
233  Parse_State *state = (Parse_State *)data;
234 
235  printf("%*s [proc[%s]] %d\n", state->depth*4, "", instruction, state->depth);
236 }
237 
238 
240  XML_Parser &p,
241  void *data)
242 {
243  (void)c; (void)p;
244  Parse_State *state = (Parse_State *)data;
245 
246  printf("%*s [error[%s]] %d\n", state->depth*4, "", get_error(p), state->depth);
247 }
248 
249 //@}