| 1 | # ------------------------------------------------------------------------------------- | ||||||||||
| 2 | # MKDoc::XML::TreeBuilder | ||||||||||
| 3 | # ------------------------------------------------------------------------------------- | ||||||||||
| 4 | # Author : Jean-Michel Hiver. | ||||||||||
| 5 | # Copyright : (c) MKDoc Holdings Ltd, 2003 | ||||||||||
| 6 | # | ||||||||||
| 7 | # This module turns an XML string into a tree of elements and returns the top elements. | ||||||||||
| 8 | # This assumes that the XML string is well-formed. Well. More or less :) | ||||||||||
| 9 | # | ||||||||||
| 10 | # This module is distributed under the same license as Perl itself. | ||||||||||
| 11 | # ------------------------------------------------------------------------------------- | ||||||||||
| 12 | package MKDoc::XML::TreeBuilder; | ||||||||||
| 13 | 8 | ▶ | use MKDoc::XML::Tokenizer; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 8 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 8 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | |||||||||||
| 17 | |||||||||||
| 18 | ## | ||||||||||
| 19 | # $class->process_data ($xml); | ||||||||||
| 20 | # ---------------------------- | ||||||||||
| 21 | # Parses $xml and turns it into a tree structure very similar | ||||||||||
| 22 | # to HTML::Element objects. | ||||||||||
| 23 | ## | ||||||||||
| 24 | sub process_data | ||||||||||
| 25 | 22 | ▶ | { | ||||||||
|
Subroutine
process_data: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 26 | 22 | my $class = shift; | |||||||||
| 27 | 22 | my $tokens = MKDoc::XML::Tokenizer->process_data (@_); | |||||||||
| 28 | 22 | return _process_recurse ($tokens); | |||||||||
| 29 | } | ||||||||||
| 30 | |||||||||||
| 31 | |||||||||||
| 32 | ## | ||||||||||
| 33 | # $class->process_file ($filename); | ||||||||||
| 34 | # --------------------------------- | ||||||||||
| 35 | # Parses $xml and turns it into a tree structure very similar | ||||||||||
| 36 | # to HTML::Element objects. | ||||||||||
| 37 | ## | ||||||||||
| 38 | sub process_file | ||||||||||
| 39 | 1 | ▶ | { | ||||||||
|
Subroutine
process_file: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 40 | 1 | my $class = shift; | |||||||||
| 41 | 1 | my $tokens = MKDoc::XML::Tokenizer->process_file (@_); | |||||||||
| 42 | 1 | return _process_recurse ($tokens); | |||||||||
| 43 | } | ||||||||||
| 44 | |||||||||||
| 45 | |||||||||||
| 46 | ## | ||||||||||
| 47 | # _process_recurse ($token_list); | ||||||||||
| 48 | # ------------------------------- | ||||||||||
| 49 | # Turns $token_list array ref into a tree structure. | ||||||||||
| 50 | ## | ||||||||||
| 51 | sub _process_recurse | ||||||||||
| 52 | 240 | ▶ | { | ||||||||
|
Subroutine
_process_recurse: called CC=5 SCAR=16.2
| |||||||||||
| 53 | 240 | my $tokens = shift; | |||||||||
| 54 | 240 | my @result = (); | |||||||||
| 55 | |||||||||||
| 56 | 240 | ▶ | while (@{$tokens}) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | { | ||||||||||
| 58 | # takes the first available token from the $tokens array reference | ||||||||||
| 59 | 633 | my $token = shift @{$tokens}; | |||||||||
| 60 | 633 | my $node = undef; | |||||||||
| 61 | |||||||||||
| 62 | 633 | $node = $token->leaf(); | |||||||||
| 63 | 633 | ▶ | defined $node and do { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | 416 | push @result, $node; | |||||||||
| 65 | 416 | next; | |||||||||
| 66 | }; | ||||||||||
| 67 | |||||||||||
| 68 | 217 | $node = $token->tag_open(); | |||||||||
| 69 | 217 | ▶ | defined $node and do { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | 217 | my $descendants = _descendant_tokens ($token, $tokens); | |||||||||
| 71 | 217 | $node->{_content} = _process_recurse ($descendants); | |||||||||
| 72 | 217 | push @result, $node; | |||||||||
| 73 | 217 | next; | |||||||||
| 74 | }; | ||||||||||
| 75 | |||||||||||
| 76 | 0 | my $token_as_string = $token->as_string(); | |||||||||
| 77 | 0 | die qq |parse_error: Is this XML well-formed? (unexpected closing tag "$token_as_string")|; | |||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | 240 | ▶ | return wantarray ? @result : \@result; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | } | ||||||||||
| 82 | |||||||||||
| 83 | |||||||||||
| 84 | ## | ||||||||||
| 85 | # $class->descendant_tokens ($token, $tokens); | ||||||||||
| 86 | # -------------------------------------------- | ||||||||||
| 87 | # Removes all tokens from $tokens which are descendants | ||||||||||
| 88 | # of $token - assuming that $token is an opening tag token. | ||||||||||
| 89 | # | ||||||||||
| 90 | # Returns all the tokens removed except for $token matching | ||||||||||
| 91 | # closing tag. So the closing tag is removed from $tokens | ||||||||||
| 92 | # but not returned. | ||||||||||
| 93 | ## | ||||||||||
| 94 | sub _descendant_tokens | ||||||||||
| 95 | 218 | ▶ | { | ||||||||
|
Subroutine
_descendant_tokens: called CC=7 SCAR=19.7
| |||||||||||
| 96 | 218 | my $token = shift; | |||||||||
| 97 | 218 | my $tokens = shift; | |||||||||
| 98 | 218 | my @res = (); | |||||||||
| 99 | 218 | my $balance = 1; | |||||||||
| 100 | 218 | ▶ | while (@{$tokens}) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 101 | { | ||||||||||
| 102 | 7419 | my $next_token = shift (@{$tokens}); | |||||||||
| 103 | 7419 | my $node = undef; | |||||||||
| 104 | |||||||||||
| 105 | 7419 | $node = $next_token->leaf(); | |||||||||
| 106 | 7419 | ▶ | defined $node and do { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 107 | 3701 | push @res, $next_token; | |||||||||
| 108 | 3701 | next; | |||||||||
| 109 | }; | ||||||||||
| 110 | |||||||||||
| 111 | 3718 | $node = $next_token->tag_open(); | |||||||||
| 112 | 3718 | ▶ | defined $node and do { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 113 | 1750 | $balance++; | |||||||||
| 114 | 1750 | push @res, $next_token; | |||||||||
| 115 | 1750 | next; | |||||||||
| 116 | }; | ||||||||||
| 117 | |||||||||||
| 118 | 1968 | $node = $next_token->tag_close(); | |||||||||
| 119 | 1968 | ▶ | defined $node and do { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 120 | 1968 | $balance--; | |||||||||
| 121 | 1968 | ▶ | last if ($balance == 0); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 122 | 1750 | push @res, $next_token; | |||||||||
| 123 | 1750 | next; | |||||||||
| 124 | }; | ||||||||||
| 125 | |||||||||||
| 126 | 0 | die "BUG: The program should never reach this statement."; | |||||||||
| 127 | } | ||||||||||
| 128 | |||||||||||
| 129 | 218 | ▶ | return \@res if ($balance == 0); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 130 | 0 | my $token_as_string = $token->as_string(); | |||||||||
| 131 | 0 | die qq |parse_error: Is this XML well-formed? (could not find closing tag for "$token_as_string")|; | |||||||||
| 132 | } | ||||||||||
| 133 | |||||||||||
| 134 | |||||||||||
| 135 | 8 | 1; | |||||||||
| 136 | |||||||||||
| 137 | |||||||||||
| 138 | __END__ | ||||||||||