Edinburgh Speech Tools 2.4-release
xml_parser_main.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 parsing mainline. */
37 /* */
38 /*************************************************************************/
39
40
41#include <cstdlib>
42#include <fstream>
43#include <iostream>
44#include "EST_error.h"
45#include "EST_cutils.h"
46#include "EST_cmd_line.h"
47#include "EST_cmd_line_options.h"
48#include "rxp/XML_Parser.h"
49
50struct Parse_State
51 {
52 int depth;
53 };
54
55class My_Parser_Class : public XML_Parser_Class
56{
57protected:
58 virtual void document_open(XML_Parser_Class &c,
59 XML_Parser &p,
60 void *data);
61 virtual void document_close(XML_Parser_Class &c,
62 XML_Parser &p,
63 void *data);
64
65 virtual void element_open(XML_Parser_Class &c,
66 XML_Parser &p,
67 void *data,
68 const char *name,
69 XML_Attribute_List &attributes);
70 virtual void element(XML_Parser_Class &c,
71 XML_Parser &p,
72 void *data,
73 const char *name,
74 XML_Attribute_List &attributes);
75 virtual void element_close(XML_Parser_Class &c,
76 XML_Parser &p,
77 void *data,
78 const char *name);
79
80 virtual void pcdata(XML_Parser_Class &c,
81 XML_Parser &p,
82 void *data,
83 const char *chars);
84 virtual void cdata(XML_Parser_Class &c,
85 XML_Parser &p,
86 void *data,
87 const char *chars);
88
89 virtual void processing(XML_Parser_Class &c,
90 XML_Parser &p,
91 void *data,
92 const char *instruction);
93 virtual void error(XML_Parser_Class &c,
94 XML_Parser &p,
95 void *data);
96};
97
98/** @name <command>xml_parser</command>
99 @id xml_parser_manual
100 * @toc
101 */
102
103//@{
104
105
106/**@name Synopsis
107 */
108//@{
109
110//@synopsis
111
112/**
113
114 <command>xml_parser</command> is a simple program giving access to
115 the <productname>RXP</productname> &xml; parser. It parses an input
116 file and prints the results out in a simple indented format. It's
117 main use is as a development tool, to check that files intended for
118 input to an &xml; application, for instance &festival;, parse as
119 you expect.
120
121 */
122
123//@}
124
125/**@name OPTIONS
126 */
127//@{
128
129//@options
130
131//@}
132
133
134
135int main(int argc, char *argv[])
136{
137 My_Parser_Class pclass;
138 EST_Option al;
139 EST_StrList files;
140 EST_String filename;
141 Parse_State state;
142
143 parse_command_line
144 (argc, argv,
145 EST_String("[file] [options]\n")+
146 "Summary: parse xml and output a tree.\n"
147 "use \"-\" to make input file stdin\n"
148 "-sysdir <string> Look for unqualified system entities in this directory"
149 "-h Options help\n",
150 files, al);
151
152 switch (files.length())
153 {
154 case 0: filename="-";
155 break;
156 case 1: filename=files.first();
157 break;
158 default:
159 EST_error("Expected only one filename");
160 break;
161 }
162
163 if (al.present("-sysdir"))
164 pclass.register_id("^\\([^/]*\\)",
165 al.sval("-sysdir") + "/\\1");
166
167 pclass.register_id("//CSTR//EST \\(.*\\)",
168 EST_String::cat(est_datadir, "/\\1.dtd"));
169
170 /* An individual parser runs over a single source.
171 */
172 XML_Parser *parser = pclass.make_parser(filename,
173 &state);
174 /* Run the parser.
175 */
176 parser->go();
177 exit(0);
178}
179
180static void print_attributes(XML_Attribute_List &attributes)
181{
183
184 for(them.begin(attributes); them ; them++)
185 printf(" %s='%s'",
186 (const char *)them->k,
187 (const char *)them->v);
188}
189
190/* Now we define the callbacks.
191 */
192
193void My_Parser_Class::document_open(XML_Parser_Class &c,
194 XML_Parser &p,
195 void *data)
196{
197 (void)c; (void)p;
198 (void)print_attributes;
199 Parse_State *state = (Parse_State *)data;
200
201 state->depth=1;
202
203 printf("%*s document %d\n", state->depth*4, ">", state->depth);
204}
205
206void My_Parser_Class::document_close(XML_Parser_Class &c,
207 XML_Parser &p,
208 void *data)
209{
210 (void)c; (void)p;
211 Parse_State *state = (Parse_State *)data;
212
213 printf("%*s <document %d\n", state->depth*4, ">", state->depth);
214}
215
216
217void My_Parser_Class::element_open(XML_Parser_Class &c,
218 XML_Parser &p,
219 void *data,
220 const char *name,
221 XML_Attribute_List &attributes)
222{
223 (void)c; (void)p; (void)attributes;
224 Parse_State *state = (Parse_State *)data;
225
226 state->depth++;
227
228 printf("%*s %s %d", state->depth*4, ">", name, state->depth);
229 print_attributes(attributes);
230 printf("\n");
231}
232
233
234void My_Parser_Class::element(XML_Parser_Class &c,
235 XML_Parser &p,
236 void *data,
237 const char *name,
238 XML_Attribute_List &attributes)
239{
240 (void)c; (void)p; (void)attributes;
241 Parse_State *state = (Parse_State *)data;
242
243 printf("%*s %s %d", state->depth*4, ":", name, state->depth);
244 print_attributes(attributes);
245 printf("\n");
246}
247
248
249void My_Parser_Class::element_close(XML_Parser_Class &c,
250 XML_Parser &p,
251 void *data,
252 const char *name)
253{
254 (void)c; (void)p;
255 Parse_State *state = (Parse_State *)data;
256
257 printf("%*s %s %d\n", state->depth*4, "<", name, state->depth);
258 state->depth--;
259}
260
261
262void My_Parser_Class::pcdata(XML_Parser_Class &c,
263 XML_Parser &p,
264 void *data,
265 const char *chars)
266{
267 (void)c; (void)p;
268 Parse_State *state = (Parse_State *)data;
269
270 printf("%*s [pcdata[%s]] %d\n", state->depth*4, "", chars, state->depth);
271}
272
273
274void My_Parser_Class::cdata(XML_Parser_Class &c,
275 XML_Parser &p,
276 void *data,
277 const char *chars)
278{
279 (void)c; (void)p;
280 Parse_State *state = (Parse_State *)data;
281
282 printf("%*s [cdata[%s]] %d\n", state->depth*4, "", chars, state->depth);
283}
284
285
286void My_Parser_Class::processing(XML_Parser_Class &c,
287 XML_Parser &p,
288 void *data,
289 const char *instruction)
290{
291 (void)c; (void)p;
292 Parse_State *state = (Parse_State *)data;
293
294 printf("%*s [proc[%s]] %d\n", state->depth*4, "", instruction, state->depth);
295}
296
297
299 XML_Parser &p,
300 void *data)
301{
302 (void)c; (void)p;
303 Parse_State *state = (Parse_State *)data;
304
305 printf("%*s [error[%s]] %d\n", state->depth*4, "", get_error(p), state->depth);
306}
307
const EST_String & sval(const EST_String &rkey, int m=1) const
Definition: EST_Option.cc:87
static EST_String cat(const EST_String s1, const EST_String s2=Empty, const EST_String s3=Empty, const EST_String s4=Empty, const EST_String s5=Empty, const EST_String s6=Empty, const EST_String s7=Empty, const EST_String s8=Empty, const EST_String s9=Empty)
Definition: EST_String.cc:1096
void begin(const Container &over)
Set the iterator ready to run over this container.
const int present(const K &rkey) const
Returns true if key is present.
Definition: EST_TKVL.cc:222
const T & first() const
return const reference to first item in list
Definition: EST_TList.h:146
virtual void element(XML_Parser_Class &c, XML_Parser &p, void *data, const char *name, XML_Attribute_List &attributes)
Definition: XML_Parser.cc:186
virtual void error(XML_Parser_Class &c, XML_Parser &p, void *data)
Definition: XML_Parser.cc:220
virtual void processing(XML_Parser_Class &c, XML_Parser &p, void *data, const char *instruction)
Definition: XML_Parser.cc:214
virtual void document_close(XML_Parser_Class &c, XML_Parser &p, void *data)
Definition: XML_Parser.cc:174
virtual void pcdata(XML_Parser_Class &c, XML_Parser &p, void *data, const char *chars)
Definition: XML_Parser.cc:202
virtual void document_open(XML_Parser_Class &c, XML_Parser &p, void *data)
Definition: XML_Parser.cc:169
virtual void element_open(XML_Parser_Class &c, XML_Parser &p, void *data, const char *name, XML_Attribute_List &attributes)
Definition: XML_Parser.cc:179
virtual void cdata(XML_Parser_Class &c, XML_Parser &p, void *data, const char *chars)
Definition: XML_Parser.cc:208
virtual void element_close(XML_Parser_Class &c, XML_Parser &p, void *data, const char *name)
Definition: XML_Parser.cc:196
void error(XML_Parser_Class &c, XML_Parser &p, void *data, EST_String message)
Definition: XML_Parser.cc:230
void go()
Run the parser.
Definition: XML_Parser.cc:274