| 1 | # Copyright (c) 2003, Cornell University | ||
| 2 | # See the file COPYING for the status of this software | ||
| 3 | |||
| 4 | ######################################################################## | ||
| 5 | # XML::Twig wrappers | ||
| 6 | # - To match the interface in XML.pm | ||
| 7 | ######################################################################## | ||
| 8 | |||
| 9 | package SOAP::Clean::XML; | ||
| 10 | |||
| 11 | use strict; | ||
| 12 | use warnings; | ||
| 13 | |||
| 14 | use File::Temp qw/ :POSIX /; | ||
| 15 | use XML::Twig; | ||
| 16 | |||
| 17 | sub twig_to_perl { | ||
| 18 | my ($nn) = @_; | ||
| 19 | |||
| 20 | if (ref $nn eq 'XML::Twig') { | ||
| 21 | return document(twig_to_perl($nn->root)); | ||
| 22 | } elsif ($nn->is_elt) { | ||
| 23 | my @attrs = (); | ||
| 24 | my $attributes = $nn->atts; | ||
| 25 | foreach my $a_name (keys %$attributes) { | ||
| 26 | push @attrs, attr($a_name,$$attributes{$a_name}); | ||
| 27 | } | ||
| 28 | my @children = (); | ||
| 29 | foreach my $cc ($nn->children) { | ||
| 30 | my $c = twig_to_perl($cc); | ||
| 31 | if (defined($c)) { | ||
| 32 | push @children, $c; | ||
| 33 | } | ||
| 34 | } | ||
| 35 | return element($nn->tag,@attrs,@children); | ||
| 36 | } elsif ($nn->get_type eq '#PCDATA' || $nn->get_type eq '#CDATA') { | ||
| 37 | return text($nn->text); | ||
| 38 | } else { | ||
| 39 | die; | ||
| 40 | } | ||
| 41 | } | ||
| 42 | |||
| 43 | sub perl_to_twig { | ||
| 44 | my ($n) = @_; | ||
| 45 | my $t = xml_get_type($n); | ||
| 46 | if ($t eq "&Document") { | ||
| 47 | my $nn = XML::Twig->new(pretty_print => 'indented'); | ||
| 48 | $nn->set_xml_version(xml_document_version($n)); | ||
| 49 | $nn->set_encoding(xml_document_encoding($n)); | ||
| 50 | $nn->set_root(perl_to_twig(xml_get_children($n))); | ||
| 51 | return $nn; | ||
| 52 | } elsif ($t eq "&Attr") { | ||
| 53 | die; | ||
| 54 | } elsif ($t eq "&Text") { | ||
| 55 | my $nn = XML::Twig::Elt->new('#PCDATA'); | ||
| 56 | $nn->set_text(xml_text_content($n)); | ||
| 57 | return $nn; | ||
| 58 | } else { | ||
| 59 | my %attrs = (); | ||
| 60 | foreach my $a ( xml_element_attributes($n) ) { | ||
| 61 | my ($a_name,$a_value) = (xml_attr_name($a),xml_attr_value($a)); | ||
| 62 | $attrs{$a_name} = $a_value; | ||
| 63 | } | ||
| 64 | my @children = (); | ||
| 65 | foreach my $c (xml_element_children($n)) { | ||
| 66 | push @children, perl_to_twig($c); | ||
| 67 | } | ||
| 68 | return XML::Twig::Elt->new(xml_element_name($n), | ||
| 69 | \%attrs,@children); | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | ######################################################################## | ||
| 74 | |||
| 75 | sub xml_to_fh { | ||
| 76 | my ($e,$fh) = @_; | ||
| 77 | my $ee = perl_to_twig($e); | ||
| 78 | if (ref $ee eq 'XML::Twig') { | ||
| 79 | $ee->print($fh); | ||
| 80 | } else { | ||
| 81 | $ee->print($fh,'indented'); | ||
| 82 | } | ||
| 83 | print $fh "\n"; | ||
| 84 | } | ||
| 85 | |||
| 86 | sub xml_to_string { | ||
| 87 | my ($e,$pretty) = @_; | ||
| 88 | if (!defined($pretty)) { | ||
| 89 | $pretty = 0; | ||
| 90 | } | ||
| 91 | if (!$pretty) { | ||
| 92 | my $ee = perl_to_twig($e); | ||
| 93 | return $ee->sprint(1); | ||
| 94 | } | ||
| 95 | |||
| 96 | # pretty printing. XML::Twig doesn't give a clean way to do it. | ||
| 97 | |||
| 98 | my $f = tmpnam(); | ||
| 99 | xml_to_file($e,$f); | ||
| 100 | |||
| 101 | my $result = ''; | ||
| 102 | assert(open F,"<$f"); | ||
| 103 | while (my $l = <F>) { | ||
| 104 | $result .= $l; | ||
| 105 | } | ||
| 106 | assert(close F); | ||
| 107 | assert(unlink($f)); | ||
| 108 | |||
| 109 | return $result; | ||
| 110 | } | ||
| 111 | |||
| 112 | sub xml_from_fh { | ||
| 113 | my ($fh) = @_; | ||
| 114 | my $nn = XML::Twig->new(); | ||
| 115 | $nn->parse( $fh ); | ||
| 116 | my $n = twig_to_perl($nn); | ||
| 117 | $nn = 0; | ||
| 118 | return $n; | ||
| 119 | } | ||
| 120 | |||
| 121 | sub xml_from_string { | ||
| 122 | my ($str) = @_; | ||
| 123 | my $nn = XML::Twig->new(); | ||
| 124 | $nn->parsestring( $str ); | ||
| 125 | my $n = twig_to_perl($nn); | ||
| 126 | $nn = 0; | ||
| 127 | return $n; | ||
| 128 | } | ||
| 129 | |||
| 130 | ######################################################################## | ||
| 131 | |||
| 132 | 1; |