Edinburgh Speech Tools  2.1-release
xml_example.cc
Go to the documentation of this file.
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 using namespace std;
46 
47 #if defined(DATAC)
48 # define __STRINGIZE(X) #X
49 # define DATA __STRINGIZE(DATAC)
50 #endif
51 
52 /**@name XML_Parser:example
53  *
54  * Simple XML processing example.
55  *
56  * @see XML_Parser
57  */
58 //@{
59 
60 /** Any data needed during processing has to be passed around,
61  * here we just keep track of the nesting depth.
62  */
63 struct Parse_State
64  {
65  int depth;
66  };
67 
68 /** Define a subset of XML_Parser_Class which which has callbacks for
69  * what we want to do. In a real application we might not need
70  * to define them all.
71  */
72 class My_Parser_Class : public XML_Parser_Class
73 {
74 protected:
75  virtual void document_open(XML_Parser_Class &c,
76  XML_Parser &p,
77  void *data);
78  virtual void document_close(XML_Parser_Class &c,
79  XML_Parser &p,
80  void *data);
81 
82  virtual void element_open(XML_Parser_Class &c,
83  XML_Parser &p,
84  void *data,
85  const char *name,
86  XML_Attribute_List &attributes);
87  virtual void element(XML_Parser_Class &c,
88  XML_Parser &p,
89  void *data,
90  const char *name,
91  XML_Attribute_List &attributes);
92  virtual void element_close(XML_Parser_Class &c,
93  XML_Parser &p,
94  void *data,
95  const char *name);
96 
97  virtual void pcdata(XML_Parser_Class &c,
98  XML_Parser &p,
99  void *data,
100  const char *chars);
101  virtual void cdata(XML_Parser_Class &c,
102  XML_Parser &p,
103  void *data,
104  const char *chars);
105 
106  virtual void processing(XML_Parser_Class &c,
107  XML_Parser &p,
108  void *data,
109  const char *instruction);
110  virtual void error(XML_Parser_Class &c,
111  XML_Parser &p,
112  void *data);
113 };
114 
115 
116 int main(void)
117 {
118  My_Parser_Class pclass;
119  Parse_State state;
120 
121  /** Rewriting rules for public and system IDs can be
122  * used to locate local copies etc.
123  */
124 
125  pclass.register_id("//EST//Test/\\(.*\\)",
126  DATA "/\\1.dtd");
127 
128  /** An individual parser runs over a single source.
129  */
130  XML_Parser *parser = pclass.make_parser(DATA "/eg.xml",
131  &state);
132  /** Run the parser.
133  */
134  parser->go();
135  exit(0);
136 }
137 
138 /** Now we define the callbacks.
139  */
140 
141 void My_Parser_Class::document_open(XML_Parser_Class &c,
142  XML_Parser &p,
143  void *data)
144 {
145  (void)c; (void)p;
146  Parse_State *state = (Parse_State *)data;
147 
148  state->depth=1;
149 
150  printf("%*s document %d\n", state->depth*4, ">", state->depth);
151 }
152 
153 void My_Parser_Class::document_close(XML_Parser_Class &c,
154  XML_Parser &p,
155  void *data)
156 {
157  (void)c; (void)p;
158  Parse_State *state = (Parse_State *)data;
159 
160  printf("%*s <document %d\n", state->depth*4, ">", state->depth);
161 }
162 
163 
164 void My_Parser_Class::element_open(XML_Parser_Class &c,
165  XML_Parser &p,
166  void *data,
167  const char *name,
168  XML_Attribute_List &attributes)
169 {
170  (void)c; (void)p; (void)attributes;
171  Parse_State *state = (Parse_State *)data;
172 
173  state->depth++;
174 
175  printf("%*s %s %d\n", state->depth*4, ">", name, state->depth);
176 }
177 
178 
179 void My_Parser_Class::element(XML_Parser_Class &c,
180  XML_Parser &p,
181  void *data,
182  const char *name,
183  XML_Attribute_List &attributes)
184 {
185  (void)c; (void)p; (void)attributes;
186  Parse_State *state = (Parse_State *)data;
187 
188  printf("%*s %s %d\n", state->depth*4, ":", name, state->depth);
189 }
190 
191 
192 void My_Parser_Class::element_close(XML_Parser_Class &c,
193  XML_Parser &p,
194  void *data,
195  const char *name)
196 {
197  (void)c; (void)p;
198  Parse_State *state = (Parse_State *)data;
199 
200  printf("%*s %s %d\n", state->depth*4, "<", name, state->depth);
201  state->depth--;
202 }
203 
204 
205 void My_Parser_Class::pcdata(XML_Parser_Class &c,
206  XML_Parser &p,
207  void *data,
208  const char *chars)
209 {
210  (void)c; (void)p;
211  Parse_State *state = (Parse_State *)data;
212 
213  printf("%*s [pcdata[%s]] %d\n", state->depth*4, "", chars, state->depth);
214 }
215 
216 
217 void My_Parser_Class::cdata(XML_Parser_Class &c,
218  XML_Parser &p,
219  void *data,
220  const char *chars)
221 {
222  (void)c; (void)p;
223  Parse_State *state = (Parse_State *)data;
224 
225  printf("%*s [cdata[%s]] %d\n", state->depth*4, "", chars, state->depth);
226 }
227 
228 
229 void My_Parser_Class::processing(XML_Parser_Class &c,
230  XML_Parser &p,
231  void *data,
232  const char *instruction)
233 {
234  (void)c; (void)p;
235  Parse_State *state = (Parse_State *)data;
236 
237  printf("%*s [proc[%s]] %d\n", state->depth*4, "", instruction, state->depth);
238 }
239 
240 
242  XML_Parser &p,
243  void *data)
244 {
245  (void)c; (void)p;
246  Parse_State *state = (Parse_State *)data;
247 
248  printf("%*s [error[%s]] %d\n", state->depth*4, "", get_error(p), state->depth);
249 }
250 
251 //@}
int main(void)
Definition: xml_example.cc:116
A specialised hash table for when the key is an EST_String.
Definition: EST_THash.h:304
EST_Track error(EST_Track &ref, EST_Track &test, int relax=0)
void go()
Run the parser.
Definition: XML_Parser.cc:282