| 1 | |||
| 2 | package XML::Structured; | ||
| 3 | |||
| 4 | use vars qw($VERSION @ISA @EXPORT); | ||
| 5 | |||
| 6 | @ISA = qw(Exporter); | ||
| 7 | @EXPORT = qw(XMLin XMLinfile XMLout); | ||
| 8 | $VERSION = '1.0'; | ||
| 9 | |||
| 10 | use XML::Parser; | ||
| 11 | |||
| 12 | use strict; | ||
| 13 | |||
| 14 | sub _workin { | ||
| 15 | my ($how, $out, $ain, @in) = @_; | ||
| 16 | my @how = @$how; | ||
| 17 | my $am = shift @how; | ||
| 18 | |||
| 19 | my %known = map {ref($_) ? (!@$_ ? () : (ref($_->[0]) ? $_->[0]->[0] : $_->[0] => $_)) : ($_=> $_)} @how; | ||
| 20 | for my $a (keys %$ain) { | ||
| 21 | die("unknown attribute: $a\n") unless $known{$a}; | ||
| 22 | if (ref($known{$a})) { | ||
| 23 | die("attribute '$a' must be element\n") if @{$known{$a}} > 1 || ref($known{$a}->[0]); | ||
| 24 | push @{$out->{$a}}, $ain->{$a}; | ||
| 25 | } else { | ||
| 26 | die("attribute '$a' must be singleton\n") if exists $out->{$a}; | ||
| 27 | $out->{$a} = $ain->{$a}; | ||
| 28 | } | ||
| 29 | } | ||
| 30 | while (@in) { | ||
| 31 | my ($e, $v) = splice(@in, 0, 2); | ||
| 32 | my $ke = $known{$e}; | ||
| 33 | if ($e eq '0') { | ||
| 34 | next if $v =~ /^\s*$/s; | ||
| 35 | die("element '$am' contains content\n") unless $known{'_content'}; | ||
| 36 | $v =~ s/\s+$/ /s; | ||
| 37 | $v =~ s/^\s+/ /s; | ||
| 38 | if (exists $out->{'_content'}) { | ||
| 39 | $out->{'_content'} =~ s/ $//s if $v =~ /^ /s; | ||
| 40 | $out->{'_content'} .= $v; | ||
| 41 | } else { | ||
| 42 | $out->{'_content'} = $v; | ||
| 43 | } | ||
| 44 | next; | ||
| 45 | } | ||
| 46 | die("unknown element: $e\n") unless $ke; | ||
| 47 | if (!ref($ke)) { | ||
| 48 | push @$v, '0', '' if @$v == 1; | ||
| 49 | die("element '$e' contains attributes\n") if %{$v->[0]}; | ||
| 50 | die("element '$e' has subelements\n") if $v->[1] ne '0'; | ||
| 51 | die("element '$e' must be singleton\n") if exists $out->{$e}; | ||
| 52 | $out->{$e} = $v->[2]; | ||
| 53 | } elsif (@$ke == 1 && !ref($ke->[0])) { | ||
| 54 | push @$v, '0', '' if @$v == 1; | ||
| 55 | die("element '$e' contains attributes\n") if %{$v->[0]}; | ||
| 56 | die("element '$e' has subelements\n") if $v->[1] ne '0'; | ||
| 57 | push @{$out->{$e}}, $v->[2]; | ||
| 58 | } else { | ||
| 59 | if (@$ke == 1) { | ||
| 60 | push @{$out->{$e}}, {}; | ||
| 61 | _workin($ke->[0], $out->{$e}->[-1], @$v); | ||
| 62 | } else { | ||
| 63 | die("element '$e' must be singleton\n") if exists $out->{$e}; | ||
| 64 | $out->{$e} = {}; | ||
| 65 | _workin($ke, $out->{$e}, @$v); | ||
| 66 | } | ||
| 67 | } | ||
| 68 | } | ||
| 69 | if (exists $out->{'_content'}) { | ||
| 70 | $out->{'_content'} =~ s/^ //s; | ||
| 71 | $out->{'_content'} =~ s/ $//s; | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | sub _escape { | ||
| 76 | my ($d) = @_; | ||
| 77 | $d =~ s/&/&/sg; | ||
| 78 | $d =~ s/</</sg; | ||
| 79 | $d =~ s/>/>/sg; | ||
| 80 | $d =~ s/"/"/sg; | ||
| 81 | return $d; | ||
| 82 | } | ||
| 83 | |||
| 84 | sub _workout { | ||
| 85 | my ($how, $d, $indent) = @_; | ||
| 86 | my @how = @$how; | ||
| 87 | my $am = _escape(shift @how); | ||
| 88 | my $ret = "$indent<$am"; | ||
| 89 | my $inelem; | ||
| 90 | my %d2 = %$d; | ||
| 91 | my $gotel; | ||
| 92 | for my $e (@how) { | ||
| 93 | if (!$inelem && !ref($e) && $e ne '_content') { | ||
| 94 | next unless exists $d2{$e}; | ||
| 95 | $ret .= _escape(" $e=").'"'._escape($d2{$e}).'"'; | ||
| 96 | delete $d2{$e}; | ||
| 97 | next; | ||
| 98 | } | ||
| 99 | $inelem = 1; | ||
| 100 | next if ref($e) && !@$e; # magic inelem marker | ||
| 101 | my $en = $e; | ||
| 102 | $en = $en->[0] if ref($en); | ||
| 103 | $en = $en->[0] if ref($en); | ||
| 104 | next unless exists $d2{$en}; | ||
| 105 | my $ee = _escape($en); | ||
| 106 | $ret .= ">\n" unless $gotel; | ||
| 107 | $gotel = 1; | ||
| 108 | if (!ref($e)) { | ||
| 109 | die("'$e' must be scalar\n") if ref($d2{$e}); | ||
| 110 | if ($e eq '_content') { | ||
| 111 | my $c = $d2{$e}; | ||
| 112 | $ret .= "$indent "._escape("$c\n"); | ||
| 113 | delete $d2{$e}; | ||
| 114 | next; | ||
| 115 | } | ||
| 116 | $ret .= "$indent <$ee>"._escape($d2{$e})."</$ee>\n"; | ||
| 117 | delete $d2{$e}; | ||
| 118 | next; | ||
| 119 | } elsif (@$e == 1 && !ref($e->[0])) { | ||
| 120 | die("'$en' must be array\n") unless UNIVERSAL::isa($d2{$en}, 'ARRAY'); | ||
| 121 | for my $se (@{$d2{$en}}) { | ||
| 122 | $ret .= "$indent <$ee>"._escape($se)."</$ee>\n"; | ||
| 123 | } | ||
| 124 | delete $d2{$en}; | ||
| 125 | } elsif (@$e == 1) { | ||
| 126 | die("'$en' must be array\n") unless UNIVERSAL::isa($d2{$en}, 'ARRAY'); | ||
| 127 | for my $se (@{$d2{$en}}) { | ||
| 128 | die("'$en' must be array of hashes\n") unless UNIVERSAL::isa($se, 'HASH'); | ||
| 129 | $ret .= _workout($e->[0], $se, "$indent "); | ||
| 130 | } | ||
| 131 | delete $d2{$en}; | ||
| 132 | } else { | ||
| 133 | die("'$en' must be hash\n") unless UNIVERSAL::isa($d2{$en}, 'HASH'); | ||
| 134 | $ret .= _workout($e, $d2{$en}, "$indent "); | ||
| 135 | delete $d2{$en}; | ||
| 136 | } | ||
| 137 | } | ||
| 138 | die("excess hash entries: ".join(', ', sort keys %d2)."\n") if %d2; | ||
| 139 | if ($gotel) { | ||
| 140 | $ret .= "$indent</$am>\n"; | ||
| 141 | } else { | ||
| 142 | $ret .= " />\n"; | ||
| 143 | } | ||
| 144 | return $ret; | ||
| 145 | } | ||
| 146 | |||
| 147 | package XML::Structured::saxparser; | ||
| 148 | |||
| 149 | sub new { | ||
| 150 | return bless []; | ||
| 151 | } | ||
| 152 | |||
| 153 | sub start_document { | ||
| 154 | my ($self) = @_; | ||
| 155 | $self->[0] = []; | ||
| 156 | } | ||
| 157 | |||
| 158 | sub start_element { | ||
| 159 | my ($self, $e) = @_; | ||
| 160 | my %as = map {$_->{'Name'} => $_->{'Value'}} values %{$e->{'Attributes'} || {}}; | ||
| 161 | push @{$self->[0]}, $e->{'Name'}, [ $self->[0], \%as ]; | ||
| 162 | $self->[0] = $self->[0]->[-1]; | ||
| 163 | } | ||
| 164 | |||
| 165 | sub end_element { | ||
| 166 | my ($self) = @_; | ||
| 167 | $self->[0] = shift @{$self->[0]}; | ||
| 168 | } | ||
| 169 | |||
| 170 | sub characters { | ||
| 171 | my ($self, $c) = @_; | ||
| 172 | |||
| 173 | my $cl = $self->[0]; | ||
| 174 | if (@$cl > 2 && $cl->[-2] eq '0') { | ||
| 175 | $cl->[-1] .= $c->{'Data'}; | ||
| 176 | } else { | ||
| 177 | push @$cl, '0' => $c->{'Data'}; | ||
| 178 | } | ||
| 179 | } | ||
| 180 | |||
| 181 | sub end_document { | ||
| 182 | my ($self) = @_; | ||
| 183 | return $self->[0]; | ||
| 184 | } | ||
| 185 | |||
| 186 | package XML::Structured; | ||
| 187 | |||
| 188 | my $xmlinparser; | ||
| 189 | |||
| 190 | sub _xmlparser { | ||
| 191 | my ($str) = @_; | ||
| 192 | my $p = new XML::Parser(Style => 'Tree'); | ||
| 193 | return $p->parse($str); | ||
| 194 | } | ||
| 195 | |||
| 196 | sub _saxparser { | ||
| 197 | my ($str) = @_; | ||
| 198 | my $handler = new XML::Structured::saxparser; | ||
| 199 | my $sp = XML::SAX::ParserFactory->parser('Handler' => $handler); | ||
| 200 | if (ref(\$str) eq 'GLOB' || UNIVERSAL::isa($str, 'IO::Handle')) { | ||
| 201 | return $sp->parse_file($str); | ||
| 202 | } | ||
| 203 | return $sp->parse_string($str); | ||
| 204 | } | ||
| 205 | |||
| 206 | sub _chooseparser { | ||
| 207 | eval { require XML::SAX; }; | ||
| 208 | my $saxok; | ||
| 209 | if (!$@) { | ||
| 210 | $saxok = 1; | ||
| 211 | my $parsers = XML::SAX->parsers(); | ||
| 212 | return \&_saxparser if $parsers && @$parsers && (@$parsers > 1 || $parsers->[0]->{'Name'} ne 'XML::SAX::PurePerl'); | ||
| 213 | } | ||
| 214 | eval { require XML::Parser; }; | ||
| 215 | return \&_xmlparser unless $@; | ||
| 216 | return \&_saxparser if $saxok; | ||
| 217 | die("XML::Structured needs either XML::SAX or XML::Parser\n"); | ||
| 218 | } | ||
| 219 | |||
| 220 | sub XMLin { | ||
| 221 | my ($dtd, $str) = @_; | ||
| 222 | $xmlinparser = _chooseparser() unless defined $xmlinparser; | ||
| 223 | my $d = $xmlinparser->($str); | ||
| 224 | my $out = {}; | ||
| 225 | die("document element must be '$dtd->[0]', was '$d->[0]'\n") if $dtd->[0] ne '' && $d->[0] ne $dtd->[0]; | ||
| 226 | _workin($dtd, $out, @{$d->[1]}); | ||
| 227 | return $out; | ||
| 228 | } | ||
| 229 | |||
| 230 | sub XMLinfile { | ||
| 231 | my ($dtd, $fn) = @_; | ||
| 232 | local *F; | ||
| 233 | open(F, '<', $fn) || die("$fn: $!\n"); | ||
| 234 | my $out = XMLin($dtd, *F); | ||
| 235 | close F; | ||
| 236 | return $out; | ||
| 237 | } | ||
| 238 | |||
| 239 | sub XMLout { | ||
| 240 | my ($dtd, $d) = @_; | ||
| 241 | return _workout($dtd, $d, ''); | ||
| 242 | } | ||
| 243 | |||
| 244 | 1; | ||
| 245 | |||
| 246 | __END__ |