Edinburgh Speech Tools  2.1-release
xml_parser_main.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 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 
50 using namespace std;
51 
52 struct Parse_State
53  {
54  int depth;
55  };
56 
57 class My_Parser_Class : public XML_Parser_Class
58 {
59 protected:
60  virtual void document_open(XML_Parser_Class &c,
61  XML_Parser &p,
62  void *data);
63  virtual void document_close(XML_Parser_Class &c,
64  XML_Parser &p,
65  void *data);
66 
67  virtual void element_open(XML_Parser_Class &c,
68  XML_Parser &p,
69  void *data,
70  const char *name,
71  XML_Attribute_List &attributes);
72  virtual void element(XML_Parser_Class &c,
73  XML_Parser &p,
74  void *data,
75  const char *name,
76  XML_Attribute_List &attributes);
77  virtual void element_close(XML_Parser_Class &c,
78  XML_Parser &p,
79  void *data,
80  const char *name);
81 
82  virtual void pcdata(XML_Parser_Class &c,
83  XML_Parser &p,
84  void *data,
85  const char *chars);
86  virtual void cdata(XML_Parser_Class &c,
87  XML_Parser &p,
88  void *data,
89  const char *chars);
90 
91  virtual void processing(XML_Parser_Class &c,
92  XML_Parser &p,
93  void *data,
94  const char *instruction);
95  virtual void error(XML_Parser_Class &c,
96  XML_Parser &p,
97  void *data);
98 };
99 
100 
101 
102 int main(int argc, char *argv[])
103 {
104  My_Parser_Class pclass;
105  EST_Option al;
106  EST_StrList files;
107  EST_String filename;
108  Parse_State state;
109 
111  (argc, argv,
112  EST_String("[file] [options]\n")+
113  "Summary: parse xml and output a tree.\n"
114  "use \"-\" to make input file stdin\n"
115  "-sysdir <string> Look for unqualified system entities in this directory"
116  "-h Options help\n",
117  files, al);
118 
119  switch (files.length())
120  {
121  case 0: filename="-";
122  break;
123  case 1: filename=files.first();
124  break;
125  default:
126  EST_error("Expected only one filename");
127  break;
128  }
129 
130  if (al.present("-sysdir"))
131  pclass.register_id("^\\([^/]*\\)",
132  al.sval("-sysdir") + "/\\1");
133 
134  pclass.register_id("//CSTR//EST \\(.*\\)",
135  EST_String::cat(est_libdir, "/\\1.dtd"));
136 
137  /* An individual parser runs over a single source.
138  */
139  XML_Parser *parser = pclass.make_parser(filename,
140  &state);
141  /* Run the parser.
142  */
143  parser->go();
144  exit(0);
145 }
146 
147 static void print_attributes(XML_Attribute_List &attributes)
148 {
150 
151  for(them.begin(attributes); them ; them++)
152  printf(" %s='%s'",
153  (const char *)them->k,
154  (const char *)them->v);
155 }
156 
157 /* Now we define the callbacks.
158  */
159 
160 void My_Parser_Class::document_open(XML_Parser_Class &c,
161  XML_Parser &p,
162  void *data)
163 {
164  (void)c; (void)p;
165  (void)print_attributes;
166  Parse_State *state = (Parse_State *)data;
167 
168  state->depth=1;
169 
170  printf("%*s document %d\n", state->depth*4, ">", state->depth);
171 }
172 
173 void My_Parser_Class::document_close(XML_Parser_Class &c,
174  XML_Parser &p,
175  void *data)
176 {
177  (void)c; (void)p;
178  Parse_State *state = (Parse_State *)data;
179 
180  printf("%*s <document %d\n", state->depth*4, ">", state->depth);
181 }
182 
183 
184 void My_Parser_Class::element_open(XML_Parser_Class &c,
185  XML_Parser &p,
186  void *data,
187  const char *name,
188  XML_Attribute_List &attributes)
189 {
190  (void)c; (void)p; (void)attributes;
191  Parse_State *state = (Parse_State *)data;
192 
193  state->depth++;
194 
195  printf("%*s %s %d", state->depth*4, ">", name, state->depth);
196  print_attributes(attributes);
197  printf("\n");
198 }
199 
200 
201 void My_Parser_Class::element(XML_Parser_Class &c,
202  XML_Parser &p,
203  void *data,
204  const char *name,
205  XML_Attribute_List &attributes)
206 {
207  (void)c; (void)p; (void)attributes;
208  Parse_State *state = (Parse_State *)data;
209 
210  printf("%*s %s %d", state->depth*4, ":", name, state->depth);
211  print_attributes(attributes);
212  printf("\n");
213 }
214 
215 
216 void My_Parser_Class::element_close(XML_Parser_Class &c,
217  XML_Parser &p,
218  void *data,
219  const char *name)
220 {
221  (void)c; (void)p;
222  Parse_State *state = (Parse_State *)data;
223 
224  printf("%*s %s %d\n", state->depth*4, "<", name, state->depth);
225  state->depth--;
226 }
227 
228 
229 void My_Parser_Class::pcdata(XML_Parser_Class &c,
230  XML_Parser &p,
231  void *data,
232  const char *chars)
233 {
234  (void)c; (void)p;
235  Parse_State *state = (Parse_State *)data;
236 
237  printf("%*s [pcdata[%s]] %d\n", state->depth*4, "", chars, state->depth);
238 }
239 
240 
241 void My_Parser_Class::cdata(XML_Parser_Class &c,
242  XML_Parser &p,
243  void *data,
244  const char *chars)
245 {
246  (void)c; (void)p;
247  Parse_State *state = (Parse_State *)data;
248 
249  printf("%*s [cdata[%s]] %d\n", state->depth*4, "", chars, state->depth);
250 }
251 
252 
253 void My_Parser_Class::processing(XML_Parser_Class &c,
254  XML_Parser &p,
255  void *data,
256  const char *instruction)
257 {
258  (void)c; (void)p;
259  Parse_State *state = (Parse_State *)data;
260 
261  printf("%*s [proc[%s]] %d\n", state->depth*4, "", instruction, state->depth);
262 }
263 
264 
266  XML_Parser &p,
267  void *data)
268 {
269  (void)c; (void)p;
270  Parse_State *state = (Parse_State *)data;
271 
272  printf("%*s [error[%s]] %d\n", state->depth*4, "", get_error(p), state->depth);
273 }
274 
int main(int argc, char *argv[])
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)
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:1084
void go()
Run the parser.
Definition: XML_Parser.cc:282
const EST_String & sval(const EST_String &rkey, int m=1) const
Definition: EST_Option.cc:93
#define EST_error
Definition: EST_error.h:104
const T & first() const
return const reference to first item in list
Definition: EST_TList.h:152
int length() const
Definition: EST_UList.cc:57
void begin(const Container &over)
Set the iterator ready to run over this container.
int present(const K &rkey) const
Returns true if key is present.
Definition: EST_TKVL.cc:222
EST_String
int parse_command_line(int argc, char *argv[], const EST_String &usage, EST_StrList &files, EST_Option &al, int make_stdio=1)
Definition: cmd_line.cc:101
const char *const est_libdir
Definition: EST_cutils.c:62