summaryrefslogtreecommitdiff
path: root/samples/xmlfilter
blob: ddbc5b59438a0e9842d495946f789aa6557c3d08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#!/usr/local/bin/perl -w
#
# $Revision: 1.1.1.1 $
#
# $Date: 2003-07-27 11:07:11 $

use XML::Parser;

my $Usage =<<'End_of_Usage;';
Usage is:
    xmlfilter [-h] [-nl] [{-+}root] [{-+}el=elname] [{-+}el:elnamepat]
              [{-+}att:attname] [{-+}att:attname:attvalpat] xmlfile

Prints on standard output the result of filtering the given xmlfile
for elements according to the switches. A '-' option will drop the
element from the output; a '+' will keep it. The output should also
be a well-formed XML document.

    -h		Print this message

    -nl         Emit a newline prior to every start tag.

    [-+]root	Drop (or keep) the root element. Defaults to keep.
		If the root element were named "foo", then -root
		would be equivalent to -el=foo. Note that even if
		you're dropping the root element, it's start and
		end tag are kept in order that the output remains
		a well-formed XML document.

    [-+]el=elname
		Drop (or keep) elements of type elname.

    [-+]el:elnamepat
		Drop (or keep) element whose type name matches elnamepat.

    [-+]att:attname
		Drop (or keep) elements which have an attribute = attname.

    [-+]att:attname:attvalpat
		Drop (or keep) elements which have an attribute = attname
		and for which the attribute value matches attvalpat.
End_of_Usage;

my $pass = 1;
my $do_newline = 0;

my $attcheck = 0;

my %drop_el;
my @drop_elpat;

my %keep_el;
my @keep_elpat;

my %drop_att;
my %keep_att;

my $always_true = sub {1;};
my $root_element = '';

my $in_cdata = 0;

# Process options

while (defined($ARGV[0]) and $ARGV[0] =~ /^[-+]/)
{
    my $opt = shift;

    if ($opt eq '-root')
    {
	$pass = 0;
    }
    elsif ($opt eq '+root')
    {
	$pass = 1;
    }
    elsif ($opt eq '-h')
    {
	print $Usage;
	exit;
    }
    elsif ($opt eq '-nl')
    {
	$do_newline = 1;
    }
    elsif ($opt =~ /^([-+])el([:=])(\S*)/)
    {
	my ($disp, $kind, $pattern) = ($1, $2, $3);
	my ($hashref, $aref);

	if ($disp eq '-')
	{
	    $hashref = \%drop_el;
	    $aref    = \@drop_elpat;
	}
	else
	{
	    $hashref = \%keep_el;
	    $aref    = \@keep_elpat;
	}

	if ($kind eq '=')
	{
	    $hashref->{$pattern} = 1;
	}
	else
	{
	    push(@$aref, $pattern);
	}
    }
    elsif ($opt =~ /^([-+])att:(\w+)(?::(\S*))?/)
    {
	my ($disp, $id, $pattern) = ($1, $2, $3);
	my $ref = ($disp eq '-') ? \%drop_att : \%keep_att;

	if (defined($pattern))
	{
	    $pattern =~ s!/!\\/!g;
	    my $sub;
	    eval "\$sub = sub {\$_[0] =~ /$pattern/;};";

	    $ref->{$id} = $sub;
	}
	else
	{
	    $ref->{$id} = $always_true;
	}

	$attcheck = 1;
    }
    else
    {
	die "Unknown option: $opt\n$Usage";
    }
}

my $drop_el_pattern = join('|', @drop_elpat);
my $keep_el_pattern = join('|', @keep_elpat);

my $drop_sub;
if ($drop_el_pattern)
{
    eval "\$drop_sub = sub {\$_[0] =~ /$drop_el_pattern/;}";
}
else
{
    $drop_sub = sub {};
}

my $keep_sub;
if ($keep_el_pattern)
{
    eval "\$keep_sub = sub {\$_[0] =~ /$keep_el_pattern/;}";
}
else
{
    $keep_sub = sub {};
}

my $doc = shift;

die "No file specified\n$Usage" unless defined($doc);

my @togglestack = ();

my $p = new XML::Parser(ErrorContext => 2,
			Handlers     => {Start => \&start_handler,
					 End   => \&end_handler
					 }
			);

if ($pass) {
  $p->setHandlers(Char       => \&char_handler,
		  CdataStart => \&cdata_start,
		  CdataEnd   => \&cdata_end);
}

$p->parsefile($doc);

print "</$root_element>\n"
    unless $pass;

################
## End of main
################

sub start_handler
{
    my $xp = shift;
    my $el = shift;

    unless ($root_element)
    {
	$root_element = $el;
	print "<$el>\n"
	    unless $pass;
    }

    my ($elref, $attref, $sub);

    if ($pass)
    {
	$elref = \%drop_el;
	$attref = \%drop_att;
	$sub = $drop_sub;
    }
    else
    {
	$elref = \%keep_el;
	$attref = \%keep_att;
	$sub = $keep_sub;
    }

    if (defined($elref->{$el})
	or &$sub($el)
	or check_atts($attref, @_))
    {
	$pass = ! $pass;
	if ($pass) {
	  $xp->setHandlers(Char       => \&char_handler,
			   CdataStart => \&cdata_start,
			   CdataEnd   => \&cdata_end);
	}
	else {
	  $xp->setHandlers(Char       => 0,
			   CdataStart => 0,
			   CdataEnd   => 0);
	}
	push(@togglestack, $xp->depth);
    }

    if ($pass)
    {
	print "\n" if $do_newline;
	print "<$el";
	while (@_)
	{
	    my $id = shift;
	    my $val = shift;

	    $val = $xp->xml_escape($val, "'");
	    print " $id='$val'";
	}
	print ">";
    }
}  # End start_handler

sub end_handler
{
    my $xp = shift;
    my $el = shift;

    if ($pass)
    {
	print "</$el>";
    }

    if (@togglestack and $togglestack[-1] == $xp->depth)
    {
	$pass = ! $pass;
	if ($pass) {
	  $xp->setHandlers(Char       => \&char_handler,
			   CdataStart => \&cdata_start,
			   CdataEnd   => \&cdata_end);
	}
	else {
	  $xp->setHandlers(Char       => 0,
			   CdataStart => 0,
			   CdataEnd   => 0);
	}

	pop(@togglestack);
    }

}  # End end_handler


sub char_handler
{
    my ($xp, $text) = @_;

    if (length($text)) {

      $text = $xp->xml_escape($text, '>')
	unless $in_cdata;

      print $text;
    }
}  # End char_handler

sub cdata_start {
  my $xp = shift;

  print '<![CDATA[';
  $in_cdata = 1;
}

sub cdata_end {
  my $xp = shift;

  print ']]>';
  $in_cdata = 0;
}

sub check_atts
{
    return $attcheck unless $attcheck;

    my $ref = shift;

    while (@_)
    {
	my $id = shift;
	my $val = shift;

	if (defined($ref->{$id}))
	{
	    my $ret = &{$ref->{$id}}($val);
	    return $ret if $ret;
	}
    }

    return 0;
}  # End check_atts

# Tell Emacs that this is really a perl script
# Local Variables:
# mode:perl
# End: