diff -rubB --exclude=.svn --exclude='*.swp' XML-TreeBuilder-3.09/Changes XML-TreeBuilder-patched/Changes
old
|
new
|
|
1 | | # Time-stamp: "2004-06-10 20:28:41 ADT" |
| 1 | 2009-16-03 Jeff Fearn <jfearn@redhat.com> |
2 | 2 | |
| 3 | Release 3.09.x |
| 4 | |
| 5 | Added NoExpand option to allow entities to be left untouched in xml. |
| 6 | Added ErrorContext option to allow better reporting of error locations. |
| 7 | Expanded tests to test these options. |
3 | 8 | |
4 | 9 | 2004-06-10 Sean M. Burke <sburke@cpan.org> |
5 | 10 | |
diff -rubB --exclude=.svn --exclude='*.swp' XML-TreeBuilder-3.09/lib/XML/TreeBuilder.pm XML-TreeBuilder-patched/lib/XML/TreeBuilder.pm
old
|
new
|
|
5 | 7 | use strict; |
6 | 8 | use XML::Element (); |
7 | 9 | use XML::Parser (); |
| 10 | use Carp; |
8 | 11 | use vars qw(@ISA $VERSION); |
9 | 12 | |
10 | 13 | $VERSION = '3.09'; |
… |
… |
|
12 | 15 | |
13 | 16 | #========================================================================== |
14 | 17 | sub new { |
15 | | my $class = ref($_[0]) || $_[0]; |
16 | | # that's the only parameter it knows |
| 18 | my ( $this, $arg ) = @_; |
| 19 | my $class = ref($this) || $this; |
| 20 | |
| 21 | my $NoExpand = ( delete $arg->{'NoExpand'} || undef ); |
| 22 | my $ErrorContext = ( delete $arg->{'ErrorContext'} || undef ); |
| 23 | |
| 24 | if ( %{$arg} ) { |
| 25 | croak "unknown args: " . join( ", ", keys %{$arg} ); |
| 26 | } |
17 | 27 | |
18 | 28 | my $self = XML::Element->new('NIL'); |
19 | 29 | bless $self, $class; # and rebless |
… |
… |
|
21 | 31 | $self->{'_store_comments'} = 0; |
22 | 32 | $self->{'_store_pis'} = 0; |
23 | 33 | $self->{'_store_declarations'} = 0; |
| 34 | $self->{'NoExpand'} = $NoExpand if ($NoExpand); |
| 35 | $self->{'ErrorContext'} = $ErrorContext if ($ErrorContext); |
24 | 36 | |
25 | 37 | my @stack; |
| 38 | |
26 | 39 | # Compare the simplicity of this to the sheer nastiness of HTML::TreeBuilder! |
27 | 40 | |
28 | | $self->{'_xml_parser'} = XML::Parser->new( 'Handlers' => { |
| 41 | $self->{'_xml_parser'} = XML::Parser->new( |
| 42 | 'Handlers' => { |
| 43 | 'Default' => sub { |
| 44 | if ( ( $self->{'NoExpand'} ) && ( $_[1] =~ /&.*\;/ ) ) { |
| 45 | $stack[-1]->push_content( $_[1] ); |
| 46 | } |
| 47 | return; |
| 48 | }, |
29 | 49 | 'Start' => sub { |
30 | 50 | shift; |
31 | | if(@stack) { |
| 51 | if (@stack) { |
32 | 52 | push @stack, $self->{'_element_class'}->new(@_); |
33 | 53 | $stack[-2]->push_content( $stack[-1] ); |
34 | | } else { |
| 54 | } |
| 55 | else { |
35 | 56 | $self->tag(shift); |
36 | | while(@_) { $self->attr(splice(@_,0,2)) }; |
| 57 | while (@_) { $self->attr( splice( @_, 0, 2 ) ) } |
37 | 58 | push @stack, $self; |
38 | 59 | } |
39 | 60 | }, |
40 | 61 | |
41 | 62 | 'End' => sub { pop @stack; return }, |
42 | 63 | |
43 | | 'Char' => sub { $stack[-1]->push_content($_[1]) }, |
| 64 | 'Char' => sub { $stack[-1]->push_content( $_[1] ) }, |
44 | 65 | |
45 | 66 | 'Comment' => sub { |
46 | 67 | return unless $self->{'_store_comments'}; |
47 | | ( |
48 | | @stack ? $stack[-1] : $self |
49 | | )->push_content( |
50 | | $self->{'_element_class'}->new('~comment', 'text' => $_[1]) |
51 | | ); |
| 68 | ( @stack ? $stack[-1] : $self ) |
| 69 | ->push_content( $self->{'_element_class'} |
| 70 | ->new( '~comment', 'text' => $_[1] ) ); |
52 | 71 | return; |
53 | 72 | }, |
54 | 73 | |
55 | 74 | 'Proc' => sub { |
56 | 75 | return unless $self->{'_store_pis'}; |
57 | | ( |
58 | | @stack ? $stack[-1] : $self |
59 | | )->push_content( |
60 | | $self->{'_element_class'}->new('~pi', 'text' => "$_[1] $_[2]") |
61 | | ); |
| 76 | ( @stack ? $stack[-1] : $self ) |
| 77 | ->push_content( $self->{'_element_class'} |
| 78 | ->new( '~pi', 'text' => "$_[1] $_[2]" ) ); |
62 | 79 | return; |
63 | 80 | }, |
64 | 81 | |
| 82 | 'Final' => sub { |
| 83 | $self->root()->traverse( |
| 84 | sub { |
| 85 | my ( $node, $start ) = @_; |
| 86 | if ( ref $node ) { # it's an element |
| 87 | $node->attr( 'NoExpand', undef ); |
| 88 | $node->attr( 'ErrorContext', undef ); |
| 89 | } |
| 90 | } |
| 91 | ); |
| 92 | }, |
| 93 | |
65 | 94 | # And now, declarations: |
66 | 95 | |
67 | 96 | 'Attlist' => sub { |
68 | 97 | return unless $self->{'_store_declarations'}; |
69 | 98 | shift; |
70 | | ( |
71 | | @stack ? $stack[-1] : $self |
72 | | )->push_content( |
73 | | $self->{'_element_class'}->new('~declaration', |
74 | | 'text' => join ' ', 'ATTLIST', @_ |
| 99 | ( @stack ? $stack[-1] : $self )->push_content( |
| 100 | $self->{'_element_class'}->new( |
| 101 | '~declaration', |
| 102 | 'text' => join ' ', |
| 103 | 'ATTLIST', @_ |
75 | 104 | ) |
76 | 105 | ); |
77 | 106 | return; |
… |
… |
|
80 | 109 | 'Element' => sub { |
81 | 110 | return unless $self->{'_store_declarations'}; |
82 | 111 | shift; |
83 | | ( |
84 | | @stack ? $stack[-1] : $self |
85 | | )->push_content( |
86 | | $self->{'_element_class'}->new('~declaration', |
87 | | 'text' => join ' ', 'ELEMENT', @_ |
| 112 | ( @stack ? $stack[-1] : $self )->push_content( |
| 113 | $self->{'_element_class'}->new( |
| 114 | '~declaration', |
| 115 | 'text' => join ' ', |
| 116 | 'ELEMENT', @_ |
88 | 117 | ) |
89 | 118 | ); |
90 | 119 | return; |
… |
… |
|
93 | 122 | 'Doctype' => sub { |
94 | 123 | return unless $self->{'_store_declarations'}; |
95 | 124 | shift; |
96 | | ( |
97 | | @stack ? $stack[-1] : $self |
98 | | )->push_content( |
99 | | $self->{'_element_class'}->new('~declaration', |
100 | | 'text' => join ' ', 'DOCTYPE', @_ |
| 125 | ( @stack ? $stack[-1] : $self )->push_content( |
| 126 | $self->{'_element_class'}->new( |
| 127 | '~declaration', |
| 128 | 'text' => join ' ', |
| 129 | 'DOCTYPE', @_ |
101 | 130 | ) |
102 | 131 | ); |
103 | 132 | return; |
104 | 133 | }, |
105 | 134 | |
106 | | }); |
| 135 | 'Entity' => sub { |
| 136 | return unless $self->{'_store_declarations'}; |
| 137 | shift; |
| 138 | ( @stack ? $stack[-1] : $self )->push_content( |
| 139 | $self->{'_element_class'}->new( |
| 140 | '~declaration', |
| 141 | 'text' => join ' ', |
| 142 | 'ENTITY', @_ |
| 143 | ) |
| 144 | ); |
| 145 | return; |
| 146 | }, |
| 147 | }, |
| 148 | 'NoExpand' => $self->{'NoExpand'}, |
| 149 | 'ErrorContext' => $self->{'ErrorContext'} |
| 150 | ); |
107 | 151 | |
108 | 152 | return $self; |
109 | 153 | } |
… |
… |
|
110 | 155 | #========================================================================== |
111 | 156 | sub _elem # universal accessor... |
112 | 157 | { |
113 | | my($self, $elem, $val) = @_; |
| 158 | my ( $self, $elem, $val ) = @_; |
114 | 159 | my $old = $self->{$elem}; |
115 | 160 | $self->{$elem} = $val if defined $val; |
116 | 161 | return $old; |
117 | 162 | } |
118 | 163 | |
119 | | sub store_comments { shift->_elem('_store_comments', @_); } |
120 | | sub store_declarations { shift->_elem('_store_declarations', @_); } |
121 | | sub store_pis { shift->_elem('_store_pis', @_); } |
| 164 | sub store_comments { shift->_elem( '_store_comments', @_ ); } |
| 165 | sub store_declarations { shift->_elem( '_store_declarations', @_ ); } |
| 166 | sub store_pis { shift->_elem( '_store_pis', @_ ); } |
122 | 167 | |
123 | 168 | #========================================================================== |
124 | 169 | |
diff -rubB --exclude=.svn --exclude='*.swp' XML-TreeBuilder-3.09/t/10main.t XML-TreeBuilder-patched/t/10main.t
old
|
new
|
|
2 | 2 | # Time-stamp: "2004-06-10 20:22:53 ADT" |
3 | 3 | |
4 | 4 | use Test; |
5 | | BEGIN { plan tests => 3 } |
| 5 | BEGIN { plan tests => 4 } |
6 | 6 | |
7 | 7 | use XML::TreeBuilder; |
8 | 8 | |
… |
… |
|
29 | 29 | ] |
30 | 30 | ); |
31 | 31 | |
32 | | |
33 | | ok $x->same_as($y); |
| 32 | ok($x->same_as($y)); |
34 | 33 | |
35 | 34 | unless( $ENV{'HARNESS_ACTIVE'} ) { |
36 | 35 | $x->dump; |
… |
… |
|
43 | 44 | $x->delete; |
44 | 45 | $y->delete; |
45 | 46 | |
| 47 | $x = XML::TreeBuilder->new({ 'NoExpand' => "1", 'ErrorContext' => "2" }); |
| 48 | $x->store_comments(1); |
| 49 | $x->store_pis(1); |
| 50 | $x->store_declarations(1); |
| 51 | $x->parse( |
| 52 | qq{<!-- myorp --><Gee><foo Id="me" xml:foo="lal">Hello World</foo>} . |
| 53 | qq{<lor/><!-- foo --></Gee><!-- glarg -->} |
| 54 | ); |
| 55 | |
| 56 | $y = XML::Element->new_from_lol( |
| 57 | ['Gee', |
| 58 | ['~comment', {'text' => ' myorp '}], |
| 59 | ['foo', {'Id'=> 'me', 'xml:foo' => 'lal'}, 'Hello World'], |
| 60 | ['lor'], |
| 61 | ['~comment', {'text' => ' foo '}], |
| 62 | ['~comment', {'text' => ' glarg '}], |
| 63 | ] |
| 64 | ); |
| 65 | |
| 66 | ok($x->same_as($y)); |
| 67 | |
46 | 68 | ok 1; |
47 | 69 | print "# Bye from ", __FILE__, "\n"; |