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