Debian Codegraph @

src/.svn/text-base/_xml.pl.svn-base

raw
1 #BSD Copyright (c) 2009, Jeff Forristal (wiretrip.net)
2 #BSD All rights reserved.
3 #BSD
4 #BSD Redistribution and use in source and binary forms, with or without
5 #BSD modification, are permitted provided that the following conditions
6 #BSD are met:
7 #BSD
8 #BSD - Redistributions of source code must retain the above copyright
9 #BSD notice, this list of conditions and the following disclaimer.
10 #BSD
11 #BSD - Redistributions in binary form must reproduce the above copyright
12 #BSD notice, this list of conditions and the following disclaimer in the
13 #BSD documentation and/or other materials provided with the distribution.
14 #BSD
15 #BSD THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 #BSD "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 #BSD LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 #BSD FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 #BSD COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 #BSD INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 #BSD BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 #BSD LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 #BSD CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 #BSD LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 #BSD ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 #BSD POSSIBILITY OF SUCH DAMAGE.
27
28 ################################################################
29
30 { # static variable container:
31 $XML = undef; # current document hash ptr
32 @tag_stack = ("/\t/\t0"); # current tag stack order
33 $XML_IGNORING_XSL = 0;
34
35 sub _xml_callback {
36 my ( $TAG, $hr, $dr, $start, $len ) = @_;
37
38 # tags we don't care about
39 return if ( $TAG =~ /^(!--|\?xml|!doctype)/ );
40
41 # ignore all XSL
42 $XML_IGNORING_XSL++ if ( $TAG eq 'xsl:stylesheet' );
43 if ( $TAG eq '/xsl:stylesheet' ) {
44 $XML_IGNORING_XSL = 0;
45 return;
46 }
47 return if ($XML_IGNORING_XSL);
48
49 my $ns = undef;
50 $ns = $1 if ( $TAG =~ s/^(.+?):// );
51
52 if ( substr( $TAG, 0, 1 ) ne '/' ) {
53 my ( $n, $cur, $end ) = split( /\t/, $tag_stack[-1] );
54 my $ab = _xml_join_name( $TAG, $cur );
55 my $flag = 0;
56 if ( defined( $$hr{'/'} ) ) {
57 $flag++;
58 delete $$hr{'/'};
59 }
60
61 # indicate previous element has children
62 $tag_stack[-1] = "$n\t$cur\t0";
63
64 my $rname = _xml_add_tag( $XML, $ab, undef, %$hr, $ns );
65 my $e = $start + $len;
66 push( @tag_stack, "$TAG\t$rname\t$e" ) unless $flag;
67
68 # for some reason, we need to reset the array ptr for
69 # push and pop to work correctly; might have something to
70 # do with it being a static variable
71 foreach (@tag_stack) { }
72 }
73 else {
74 my ( $n, $path, $end ) = split( /\t/, pop(@tag_stack) );
75 if ( "/$n" ne $TAG ) {
76
77 # WHOA, bad XML? We are now going to puke.
78 %$XML = ();
79 $XML->{'ERROR'} =
80 "Closing tag <$TAG> not expected" . " (expecting </$n>)";
81
82 # short circuit parsing to the end
83 _html_find_tags_adjust( 0, 0 );
84 }
85
86 if ( $end > 0 ) { # set value
87 my $val = substr( $$dr, $end, $start - $end );
88 _xml_set_value( $XML, $path, _xml_deentify($val) );
89 }
90 }
91 return;
92 }
93
94 ################################################################
95
96 sub _xml_deentify { # /* INTERNAL */
97 my $val = shift;
98 $val =~ s/&lt;/</g;
99 $val =~ s/&gt;/>/g;
100 $val =~ s/&quot;/"/g;
101 $val =~ s/&#([0-9]{1,3});/chr($1)/eg;
102 $val =~ s/&amp;/&/g; # must be last
103 return $val;
104 }
105
106 ################################################################
107
108 =item B<xml_read_data>
109
110 Params: \$data
111
112 Return: $XML_object, undef on error
113
114 This function takes a reference to a scalar containing XML
115 markup and parses it into the %XML hash format used by the
116 other XML functions.
117
118 If a parsing error is encountered, the returned hash only has
119 one element, 'ERROR', which contains an error message.
120
121 =cut
122
123 sub xml_read_data {
124 my $p = shift;
125 return undef if ( !defined $p );
126 $p = \$p if ( !ref($p) );
127 $XML = {}; # new anonymous hash
128 @tag_stack = ("/\t/\t0"); # reset the tag stack
129 html_find_tags( $p, \&_xml_callback, 1 );
130 return $XML; # return new document
131 }
132
133 ################################################################
134
135 =item B<xml_read_file>
136
137 Params: $filename
138
139 Return: $XML_object, undef on error
140
141 xml_read_file opens the given $filename and attempts to
142 parse the XML data found within.
143
144 If a parsing error is encountered, the returned hash only has
145 one element, 'ERROR', which contains an error message.
146
147 =cut
148
149 sub xml_read_file {
150 my $filename = shift;
151 return undef if ( !defined $filename || $filename eq '' );
152 return undef if ( !-e $filename || !-f $filename );
153
154 my $data = '';
155 open( IN, "<$filename" ) or return undef;
156 binmode(IN); # Stupid Windows
157 $data .= $_ while (<IN>);
158 close(IN);
159
160 return xml_read_data( \$data );
161 }
162
163 } # end of static variable container
164
165 ################################################################
166
167 sub _xml_add_tag {
168 my ( $hr, $name, $value, %e, $ns ) = @_;
169 my $realname = $name;
170
171 if ( exists $$hr{$name} ) {
172
173 # already have a tag named that; make array
174 my @t = _xml_get_4arr( $hr, $name );
175 $t[0]++;
176 $$hr{$name} = [ $t[0], $t[1], $t[2], $t[3] ];
177 $realname .= '[' . $t[0] . ']';
178 }
179
180 # there's three different storage formats, in order
181 # to reduce the amount of anonymous structures in
182 # the main hash
183 if ( ( scalar keys %e ) > 0 || defined $ns ) { # we need a full anon array
184 my $p = undef;
185 if ( ( scalar keys %e ) > 0 ) {
186 $p = [];
187 foreach ( keys %e ) { # need to add each param
188 $_ =~ s/^.+?://; # remove namespace
189 next if ( $_ eq '' );
190 push @$p, $_;
191 $$hr{ _xml_join_name( "\@$_", $realname ) } = $e{$_};
192 }
193 }
194 $$hr{$realname} = [ 0, $p, $value, $ns ];
195 }
196 elsif ( defined $value ) { # simple value
197 $$hr{$realname} = $value;
198 }
199 else { # empty tag with no value or elements
200 $$hr{$realname} = undef;
201 }
202
203 return $realname;
204 }
205
206 ################################################################
207
208 sub _xml_get_4arr {
209 my ( $hr, $name ) = @_;
210 return undef if ( !exists $$hr{$name} );
211 return ( 0, undef, undef, undef ) if ( !defined $$hr{$name} );
212 return ( 0, undef, $$hr{$name}, undef ) if ( !ref( $$hr{$name} ) );
213 return @{ $$hr{$name} };
214 }
215
216 ################################################################
217
218 sub _xml_check {
219 my ( $hr, $name, $root ) = @_;
220 return undef if ( !defined $hr || !ref($hr) );
221 return undef if ( !defined $name || $name eq '' );
222 $name = _xml_join_name( $name, $root ) if ( defined $root );
223 return undef if ( !exists( $$hr{$name} ) );
224 return $name;
225 }
226
227 ################################################################
228
229 sub _xml_set_value {
230 my ( $hr, $name, $value, $root ) = @_;
231 return undef unless ( $name = _xml_check( $hr, $name, $root ) );
232 if ( ref( $$hr{$name} ) ) {
233 $$hr{$name}->[2] = $value;
234 }
235 else {
236 $$hr{$name} = $value;
237 }
238 return 1;
239 }
240
241 ################################################################
242
243 =item B<xml_get_multi>
244
245 Params: $XML_obj, $name [, $root]
246
247 Return: @elements, undef on error
248
249 xml_get_multi() returns an array of absolute element pathnames for
250 the given named element, which can then be iterated over to access
251 child elements/parameters of each element.
252
253 =cut
254
255 sub xml_get_multi {
256 my ( $count, $c, $hr, $name, $root ) = ( 0, 1, @_ );
257 return undef unless ( $name = _xml_check( $hr, $name, $root ) );
258 my @ret = ($name);
259 return @ret if ( !defined $$hr{$name} || !ref( $$hr{$name} ) );
260 $count = $$hr{$name}->[0];
261 return @ret if ( $count == 0 );
262 for ( $c = 1 ; $c <= $count ; $c++ ) {
263 push @ret, $name . "[$c]";
264 }
265 return @ret;
266 }
267
268 ################################################################
269
270 =item B<xml_is_multi>
271
272 Params: $XML_obj, $name [, $root]
273
274 Return: 1 if multi, 0 if not, undef on error
275
276 This function checks to see if the named element has multiple elements
277 present in the given $XML_object.
278
279 =cut
280
281 sub xml_is_multi {
282 my ( $hr, $name, $root ) = @_;
283 return undef unless ( $name = _xml_check( $hr, $name, $root ) );
284 return 0 if ( !ref( $$hr{$name} ) );
285 return $$hr{$name}->[0];
286 }
287
288 ################################################################
289
290 sub _xml_join_name {
291 my ( $name, $root ) = @_;
292 return undef if ( !defined $name );
293 $root = '/' if ( !defined $root || $root eq '' );
294 my $join = "$root/$name";
295 $join =~ s#/{2,}#/#g;
296 chop $join if ( substr( $join, -1, 1 ) eq '/' );
297 return $join;
298 }
299
300 ################################################################
301
302 =item B<xml_get_element_value>
303
304 Params: $XML_obj, $name [, $root]
305
306 Return: $value, undef on error/not found
307
308 Fetches the value of the element of name $name.
309
310 =cut
311
312 sub xml_get_element_value {
313 my ( $hr, $name, $root ) = @_;
314 return undef unless ( $name = _xml_check( $hr, $name, $root ) );
315 my @x = _xml_get_4arr( $hr, $name );
316 return $x[2];
317 }
318
319 sub xml_gev { # typing laziness shortcut
320 goto &xml_get_element_value;
321 }
322
323 ################################################################
324
325 =item B<xml_get_element_namespace>
326
327 Params: $XML_obj, $name [, $root]
328
329 Return: $value, undef on error/not found/unknown
330
331 Returns the namespace of an element, if it was present in the XML
332 document. If not known, then undef is returned.
333
334 =cut
335
336 sub xml_get_element_namespace {
337 my ( $hr, $name, $root ) = @_;
338 return undef unless ( $name = _xml_check( $hr, $name, $root ) );
339 my @x = _xml_get_4arr( $hr, $name );
340 return $x[3];
341 }
342
343 sub xml_gens { # typing laziness shortcut
344 goto &xml_get_element_namespace;
345 }
346
347 ################################################################
348
349 =item B<xml_get_element_parameters>
350
351 Params: $XML_obj, $name [, $root]
352
353 Return: @parameter_names, undef on error/not found
354
355 Returns a list of parameter names set for the named element.
356
357 =cut
358
359 sub xml_get_element_parameters {
360 my ( $hr, $name, $root ) = @_;
361 return undef unless ( $name = _xml_check( $hr, $name, $root ) );
362 return undef if ( !ref( $$hr{$name} ) );
363 my @t = _xml_get_4arr( $hr, $name );
364 return undef if ( !defined( $t[1] ) );
365 return @{ $t[1] };
366 }
367
368 sub xml_gep { # typing laziness shortcut
369 goto &xml_get_element_parameters;
370 }
371
372 ################################################################
373
374 =item B<xml_if_exist>
375
376 Params: $XML_obj, $name [, $root]
377
378 Return: 1 if found, undef if not found/error
379
380 Check to see if the named element exists.
381
382 =cut
383
384 sub xml_if_exist {
385 my ( $hr, $name, $root ) = @_;
386 return undef unless ( $name = _xml_check( $hr, $name, $root ) );
387 return 1;
388 }
389
390 ################################################################
391