source: trunk/markup_stats.cxx @ 7

Last change on this file since 7 was 7, checked in by cameron, 12 years ago

Reference extraction; checking ]]> in text; < in atts.

File size: 6.9 KB
Line 
1/*  markup_stats.c - parabix demo program
2    Copyright (c) 2007, Robert D. Cameron.
3    Licensed to the public under the Open Software License 3.0.
4    Licensed to International Characters, Inc., under the Academic
5    Free License 3.0.
6
7*/
8
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12#include <errno.h>
13#include <sys/types.h>
14#include <sys/stat.h>
15#include <unistd.h>
16
17#include "src/xmlbuffer.h"
18#include "src/xmlbuffer.c"
19
20#include "src/charsets/charset_family.h"
21#include "src/bitlex.h"
22#include "src/bitlex.c"
23#include "src/charsets/ext_ascii_8.h"
24#include "src/charsets/ext_ascii_8.c"
25#include "src/charsets/ext_ascii_16.h"
26#include "src/charsets/ext_ascii_16.c"
27
28#include "src/ilax.h"
29#include "src/engine.h"
30#include "src/engine.c"
31
32/* Global declarations for statistics. */
33
34  int comment_count = 0;
35  int comment_length = 0;
36  int CDATA_count = 0;
37  int CDATA_length = 0;
38  int PI_count = 0;
39  int PI_length = 0;
40  int empty_elem_count = 0;
41  int empty_elem_length = 0;
42  int start_tag_count = 0;
43  int start_tag_length = 0;
44  int attribute_count = 0;
45  int end_tag_count = 0;
46  int end_tag_length = 0;
47  int reference_count = 0;
48  int reference_length = 0;
49  int text_item_count = 0;
50  int text_item_length = 0;
51  int error_item_count = 0;
52  int error_item_length = 0;
53  int nesting_depth = 0;
54  int max_nesting_depth = 0;
55  int total_attribute_count = 0;
56  int total_att_name_length = 0;
57  int total_att_value_length = 0;
58  int namespace_count = 0;
59  int total_namespace_name_length = 0;
60  int total_namespace_URI_length = 0;
61
62
63/* Action routine for an XML comment in "<!--"  "-->" brackets. */
64static inline void Comment_action(int start_pos, int end_pos) {
65  comment_count +=1;
66  comment_length += end_pos - start_pos;
67}
68
69/* Action routine for a CDATA section enclosed in "<![CDATA[" and "]]>" brackets. */
70static inline void CDATA_action(int start_pos, int end_pos) {
71  CDATA_count +=1;
72  CDATA_length += end_pos - start_pos;
73}
74
75/* Action routine for an XML processing instruction enclosed in "<?" and "?>" brackets. */
76static inline void PI_action(int start_pos, int end_pos) {
77  PI_count +=1;
78  PI_length += end_pos - start_pos;
79}
80
81/* Action routine for an empty element enclosed in "<" and "/>" brackets. */
82static inline void EmptyElement_action(int start_pos, int end_pos) {
83  empty_elem_count +=1;
84  empty_elem_length += end_pos - start_pos;
85}
86
87/* Action routine for a start tag enclosed in "<" and ">" brackets. */
88static inline void StartTag_action(int start_pos, int end_pos) {
89  start_tag_count +=1;
90  start_tag_length += end_pos - start_pos;
91  nesting_depth += 1;
92  if (nesting_depth > max_nesting_depth) max_nesting_depth = nesting_depth;
93}
94
95/* Action routine for an end tag enclosed in "</" and ">" brackets. */
96static inline void EndTag_action(int start_pos, int end_pos) {
97  end_tag_count +=1;
98  end_tag_length += end_pos - start_pos;
99  nesting_depth -= 1;
100}
101
102/* Action routine for an error item */
103static inline void Error_action(int start_pos, int end_pos) {
104  error_item_count +=1;
105  error_item_length += end_pos - start_pos;
106}
107
108/* Action routine for a text item */
109static inline void Text_action(int start_pos, int end_pos) {
110  text_item_count +=1;
111  text_item_length += end_pos - start_pos;
112}
113
114static inline void Reference_action(int start_pos, int end_pos) {
115  reference_count +=1;
116  reference_length += end_pos - start_pos;
117}
118
119
120
121
122/* Three action routines for markup components are defined as follows.
123|ElementName_action| is the action routine called upon recognition of
124an element name immediately after the opening angle bracket of a start
125tag or empty element tag.  {\bf OR MAYBE THIS SHOULD BE DEFERRED UNTIL
126AFTER ATTRIBUTE PROCESSING SO THAT NAMESPACES ARE SET?}
127It is called with two parameters identifying the
128first and last character positions of the expected XML_name.
129Similarly, |PI_Target_action| is the action routine called upon recognition
130of the XML Name that occurs immediately after the opening "<?"
131delimiter of a processing instruction.
132
133 The third action routine for markup components is Attribute_Value_action,
134which takes three parameters rather than two.  {\bf OR POSSIBLY JUST
135THE QUOTE MARK ITEMS, RELYING ON THE END OF THE LAST COMPONENT PROCESSED
136TO MARK THE SPACE BEFORE THE ATT NAME.- REQUIRES ELEMENT_NAME_ACTION}
137*/
138
139/* Semantic action routines for markup components. */
140/* Action routine for an element name occurring immediately after the
141   opening "<" of a start tag or empty element tag. */
142static inline void ElementName_action(int start_pos, int end_pos) {
143}
144
145/* Action routine for a processing instruction target name occurring immediately
146   after the opening "<?" of a processing instruction. */
147static inline void PI_Target_action(int start_pos, int end_pos) {
148}
149
150/* Action routine for an individual attribute/value pair occurring in
151   a element start tag or an empty element tag. */
152static inline void AttributeValue_action(int name_start, int name_end, 
153                                  int val_start, int val_end) {
154  total_attribute_count+=1;
155  total_att_name_length += name_end - name_start;
156  total_att_value_length += val_end - val_start;
157}
158
159/* Action routine for an individual attribute/value pair occurring in
160   a element start tag or an empty element tag. */
161static inline void Namespace_action(int name_start, int name_end, 
162                             int URI_start, int URI_end) {
163  namespace_count+=1;
164  total_namespace_name_length += name_end - name_start;
165  total_namespace_URI_length += URI_end - URI_start;
166}
167
168
169static inline void FinalizeBuffer_action() {
170}
171
172
173#define print_stats(stat_string, count, total_lgth) \
174  printf("%i %s", count, stat_string);\
175  if (count == 0) printf("s.\n");\
176  else if (count == 1) printf(" of length %i.\n", total_lgth);\
177  else printf("s of avg. lgth %i.\n", total_lgth/count);
178
179
180int
181main(int argc, char * argv[]) {
182  if (argc != 2) {
183    printf("Usage: %s <filename>\n", argv[0]);
184          exit(-1);
185  }
186  char * filename = argv[1];
187
188  ParsingEngine parser = ParsingEngine(filename);
189  parser.InitLexer();
190  parser.ParseContent();
191
192  print_stats("comment", comment_count, comment_length);
193  print_stats("CDATA section", CDATA_count, CDATA_length);
194  print_stats("processing instruction", PI_count, PI_length);
195  print_stats("empty element", empty_elem_count, empty_elem_length);
196  print_stats("start tag", start_tag_count, start_tag_length);
197  printf("%i total attributes\n", attribute_count);
198  print_stats("attribute name", total_attribute_count, total_att_name_length);
199  print_stats("attribute value", total_attribute_count, total_att_value_length);
200  print_stats("namespace name", namespace_count, total_namespace_name_length);
201  print_stats("namespace URI", namespace_count, total_namespace_URI_length);
202  print_stats("end tag", end_tag_count, end_tag_length);
203  print_stats("text item", text_item_count, text_item_length);
204  print_stats("reference", reference_count, reference_length);
205  print_stats("error item", error_item_count, error_item_length);
206  printf("Maximum nesting depth = %i\n", max_nesting_depth);
207
208  return(0);
209}
Note: See TracBrowser for help on using the repository browser.