line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# $Id: PYX.pm,v 1.9 2000/06/21 17:05:51 matt Exp $ |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
package XML::PYX; |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
460
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
33
|
|
6
|
1
|
|
|
1
|
|
5
|
use vars qw($VERSION); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
788
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
$VERSION = '0.07'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
$XML::PYX::Lame = 0; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub encode { |
13
|
0
|
|
|
0
|
|
|
my $text = shift; |
14
|
0
|
|
|
|
|
|
$text =~ s/\n/\\n/g; |
15
|
0
|
|
|
|
|
|
return $text; |
16
|
|
|
|
|
|
|
} |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub parse { |
19
|
0
|
|
|
0
|
|
|
my $output = shift; |
20
|
0
|
|
|
|
|
|
my $ioref; |
21
|
0
|
|
|
|
|
|
my $arg = shift @_; |
22
|
|
|
|
|
|
|
|
23
|
0
|
0
|
0
|
|
|
|
if (ref($arg) and UNIVERSAL::isa($arg, 'IO::Handler')) { |
24
|
0
|
|
|
|
|
|
$ioref = $arg; |
25
|
|
|
|
|
|
|
} else { |
26
|
0
|
|
|
|
|
|
eval { |
27
|
0
|
|
|
|
|
|
$ioref = *{$arg}{IO}; |
|
0
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
}; |
29
|
|
|
|
|
|
|
} |
30
|
0
|
0
|
|
|
|
|
if (!defined($ioref)) { |
31
|
0
|
|
|
|
|
|
die "Can't get filehandle!\n"; |
32
|
|
|
|
|
|
|
} |
33
|
0
|
|
|
|
|
|
my $xml; |
34
|
|
|
|
|
|
|
# may have already done $ioref in parse, so rewind |
35
|
0
|
|
|
|
|
|
seek($ioref,0,0); |
36
|
|
|
|
|
|
|
{ |
37
|
0
|
|
|
|
|
|
local $/; |
|
0
|
|
|
|
|
|
|
38
|
0
|
|
|
|
|
|
$xml = <$ioref>; |
39
|
|
|
|
|
|
|
} |
40
|
0
|
|
|
|
|
|
my $res; |
41
|
|
|
|
|
|
|
my @stack; |
42
|
0
|
|
|
|
|
|
while($xml =~ m/\G([^<]*)(<([\?!\/]?)([\w\-]+))?/gc) { |
43
|
0
|
|
|
|
|
|
my ($data, $type, $tag) = ($1, $3, $4); |
44
|
|
|
|
|
|
|
# warn "$data $type $tag\n"; |
45
|
0
|
0
|
|
|
|
|
if (length $data) { |
46
|
0
|
|
|
|
|
|
$res .= $output->("-" . encode($data) . "\n"); |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
0
|
0
|
0
|
|
|
|
last unless $type || $tag; |
50
|
|
|
|
|
|
|
|
51
|
0
|
0
|
|
|
|
|
if ($type eq '?') { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
52
|
0
|
0
|
|
|
|
|
if ($xml =~ m/\G\s+(.*?)\?>/gcs) { |
53
|
|
|
|
|
|
|
# processing instruction |
54
|
0
|
|
|
|
|
|
my $data = $1; |
55
|
0
|
|
|
|
|
|
$res .= $output->("?$tag " . encode($data) . "\n"); |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
else { |
58
|
0
|
|
|
|
|
|
die "Invalid psuedo XML: No end to processing instruction\n"; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
elsif ($type eq '!') { |
62
|
0
|
0
|
|
|
|
|
if ($tag eq '--') { |
63
|
|
|
|
|
|
|
# comment |
64
|
0
|
0
|
|
|
|
|
if ($xml =~ m/\G(.*?)-->/gcs) { |
65
|
|
|
|
|
|
|
# pyx doesn't support comments! |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
else { |
68
|
0
|
|
|
|
|
|
die "Invalid psuedo XML: No end to comment\n"; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
else { |
72
|
0
|
|
|
|
|
|
die "Invalid tag
|
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
elsif ($type eq '/') { |
76
|
|
|
|
|
|
|
# close element |
77
|
0
|
0
|
|
|
|
|
if ($tag eq $stack[0]) { |
78
|
0
|
|
|
|
|
|
shift @stack; |
79
|
0
|
0
|
|
|
|
|
if ($xml =~ m/\G\s*>/gc) { |
80
|
0
|
|
|
|
|
|
$res .= $output->(")$tag\n"); |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
else { |
83
|
0
|
|
|
|
|
|
die "Invalid psuedo XML: Bad close tag\n"; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
else { |
87
|
0
|
|
|
|
|
|
die "Invalid psuedo XML: Close tag mismatch\n"; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
else { |
91
|
|
|
|
|
|
|
# start element |
92
|
0
|
|
|
|
|
|
unshift @stack, $tag; |
93
|
0
|
|
|
|
|
|
$res .= $output->("($tag\n"); |
94
|
0
|
|
|
|
|
|
while($xml =~ m/\G(\s*(\w+)\s*=\s*(["'])(.*?)\3|>)/gcs) { |
95
|
0
|
0
|
|
|
|
|
last if $1 eq '>'; |
96
|
0
|
|
|
|
|
|
my ($key, $val) = ($2, $4); |
97
|
0
|
|
|
|
|
|
$res .= $output->("A$key " . encode($val) . "\n"); |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
} |
101
|
0
|
|
|
|
|
|
return $res; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
{ |
105
|
|
|
|
|
|
|
package XML::PYX::Parser; |
106
|
1
|
|
|
1
|
|
5
|
use vars qw/@ISA/; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
39
|
|
107
|
|
|
|
|
|
|
|
108
|
1
|
|
|
1
|
|
1455
|
use XML::Parser; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
@ISA = 'XML::Parser'; |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub new { |
113
|
|
|
|
|
|
|
my ($class, %args) = (@_, 'Style' => 'PYX', '_output' => sub { shift; }); |
114
|
|
|
|
|
|
|
if ($args{Validating}) { |
115
|
|
|
|
|
|
|
require XML::Checker::Parser; |
116
|
|
|
|
|
|
|
@ISA = 'XML::Checker::Parser'; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
$class->SUPER::new(%args); |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
sub parse { |
122
|
|
|
|
|
|
|
my $self = shift; |
123
|
|
|
|
|
|
|
if ($XML::PYX::Lame) { |
124
|
|
|
|
|
|
|
return XML::PYX::parse($self->{_output}, @_); |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
return $self->SUPER::parse(@_); |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
{ |
131
|
|
|
|
|
|
|
package XML::PYX::Parser::ToCSF; |
132
|
|
|
|
|
|
|
use vars qw/@ISA/; |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
use XML::Parser; |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
@ISA = 'XML::Parser'; |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
sub new { |
139
|
|
|
|
|
|
|
my ($class, %args) = (@_, 'Style' => 'PYX', '_output' => sub { print shift; undef; }); |
140
|
|
|
|
|
|
|
if ($args{Validating}) { |
141
|
|
|
|
|
|
|
require XML::Checker::Parser; |
142
|
|
|
|
|
|
|
@ISA = 'XML::Checker::Parser'; |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
$class->SUPER::new(%args); |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
sub parse { |
148
|
|
|
|
|
|
|
my $self = shift; |
149
|
|
|
|
|
|
|
if ($XML::PYX::Lame) { |
150
|
|
|
|
|
|
|
return XML::PYX::parse($self->{_output}, @_); |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
return $self->SUPER::parse(@_); |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
{ |
157
|
|
|
|
|
|
|
package XML::Parser::PYX; |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
use vars qw/$_PYX/; |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
$XML::Parser::Built_In_Styles{PYX} = 1; |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
sub Final { |
164
|
|
|
|
|
|
|
return $_PYX; |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
sub Init { |
168
|
|
|
|
|
|
|
undef $_PYX; |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
sub Char { |
172
|
|
|
|
|
|
|
my ($e, $t) = @_; |
173
|
|
|
|
|
|
|
$_PYX .= $e->{_output}->("-" . XML::PYX::encode($t) . "\n"); |
174
|
|
|
|
|
|
|
} |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
sub Start { |
177
|
|
|
|
|
|
|
my ($e, $tag, @attr) = @_; |
178
|
|
|
|
|
|
|
$_PYX .= $e->{_output}->("($tag\n"); |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
while(@attr) { |
181
|
|
|
|
|
|
|
my ($key, $val) = (shift(@attr), shift(@attr)); |
182
|
|
|
|
|
|
|
$_PYX .= $e->{_output}->("A$key " . XML::PYX::encode($val) . "\n"); |
183
|
|
|
|
|
|
|
} |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
} |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
sub End { |
188
|
|
|
|
|
|
|
my ($e, $tag) = @_; |
189
|
|
|
|
|
|
|
$_PYX .= $e->{_output}->(")$tag\n"); |
190
|
|
|
|
|
|
|
} |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
sub Proc { |
193
|
|
|
|
|
|
|
my ($e, $target, $data) = @_; |
194
|
|
|
|
|
|
|
$_PYX .= $e->{_output}->("?$target " . XML::PYX::encode($data) . "\n"); |
195
|
|
|
|
|
|
|
} |
196
|
|
|
|
|
|
|
} |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
1; |
199
|
|
|
|
|
|
|
__END__ |