| 1 | package XML::DTDParser; | ||||||||||||||||||||||||||||||||
| 2 | 1 | require Exporter; | |||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use FileHandle; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use File::Spec; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Cwd; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | 1 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||
| 9 | 1 | our @EXPORT = qw(ParseDTD FindDTDRoot ParseDTDFile); | |||||||||||||||||||||||||||||||
| 10 | 1 | our @EXPORT_OK = @EXPORT; | |||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | 1 | our $VERSION = '2.01'; | |||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | 1 | my $namechar = '[#\x41-\x5A\x61-\x7A\xC0-\xD6\xD8-\xF6\xF8-\xFF0-9\xB7._:-]'; | |||||||||||||||||||||||||||||||
| 15 | 1 | my $name = '[\x41-\x5A\x61-\x7A\xC0-\xD6\xD8-\xF6\xF8-\xFF_:]' . $namechar . '*'; | |||||||||||||||||||||||||||||||
| 16 | 1 | my $nameX = $name . '[.?+*]*'; | |||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | 1 | my $nmtoken = $namechar . '+'; | |||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||
| 20 | 1 | my $AttType = '(?:CDATA\b|IDREFS?\b|ID\b|ENTITY\b|ENTITIES\b|NMTOKENS?\b|\([^\)]*\)|NOTATION\s+\([^\)]*\))'; | |||||||||||||||||||||||||||||||
| 21 | 1 | my $DefaultDecl = q{(?:#REQUIRED|#IMPLIED|(:?#FIXED ?)?(?:".*?"|'.*?'))}; | |||||||||||||||||||||||||||||||
| 22 | 1 | my $AttDef = '('.$name.') ('.$AttType.')(?: ('.$DefaultDecl.'))?'; | |||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||
| 25 | 0 | ▶ | sub ParseDTDFile { | ||||||||||||||||||||||||||||||
|
Subroutine
ParseDTDFile: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||
| 26 | 0 | my $file = shift; | |||||||||||||||||||||||||||||||
| 27 | 0 | ▶ | open my $IN, "< $file" | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 28 | or die "Cannot open the $file : $!\n"; | ||||||||||||||||||||||||||||||||
| 29 | 0 | my $xml = do {local $/; <$IN>}; | |||||||||||||||||||||||||||||||
| 30 | 0 | close $IN; | |||||||||||||||||||||||||||||||
| 31 | 0 | my ($vol,$dir,$filename) = File::Spec->splitpath( $file); | |||||||||||||||||||||||||||||||
| 32 | 0 | ▶ | if ($filename eq $file) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 33 | 0 | return ParseDTD($xml); | |||||||||||||||||||||||||||||||
| 34 | } else { | ||||||||||||||||||||||||||||||||
| 35 | # in case there are any includes, they should be relative to the DTD file, not to current dir | ||||||||||||||||||||||||||||||||
| 36 | 0 | my $cwd = cwd(); | |||||||||||||||||||||||||||||||
| 37 | 0 | chdir(File::Spec->catdir($vol,$dir)); | |||||||||||||||||||||||||||||||
| 38 | 0 | my $DTD = ParseDTD($xml); | |||||||||||||||||||||||||||||||
| 39 | 0 | chdir($cwd); | |||||||||||||||||||||||||||||||
| 40 | 0 | return $DTD; | |||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||
| 44 | 1 | ▶ | sub ParseDTD { | ||||||||||||||||||||||||||||||
|
Subroutine
ParseDTD: called CC=49 SCAR=47.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 45 | 1 | my $xml = shift; | |||||||||||||||||||||||||||||||
| 46 | 1 | my (%elements, %definitions); | |||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||
| 48 | 1 | $xml =~ s/\s\s*/ /gs; | |||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||
| 50 | 1 | ▶ | while ($xml =~ s{<!ENTITY\s+(?:(%)\s*)?($name)\s+SYSTEM\s*"(.*?)"\s*>}{}io) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 51 | 0 | my ($percent, $entity, $include) = ($1,$2,$3); | |||||||||||||||||||||||||||||||
| 52 | 0 | ▶ | $percent = '&' unless $percent; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 53 | 0 | my $definition; | |||||||||||||||||||||||||||||||
| 54 | { | ||||||||||||||||||||||||||||||||
| 55 | # the $include may be a URL, use LWP::Simple to fetch it if it is. | ||||||||||||||||||||||||||||||||
| 56 | 0 | my $IN; | |||||||||||||||||||||||||||||||
| 57 | 0 | ▶ | open $IN, "<$include" or die "Cannot open include file $include : $!\n"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 58 | 0 | $definition = do {local $/; <$IN>}; | |||||||||||||||||||||||||||||||
| 59 | 0 | close $IN; | |||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||
| 61 | 0 | $definition =~ s/\s\s*/ /gs; | |||||||||||||||||||||||||||||||
| 62 | 0 | $xml =~ s{\Q$percent$entity;\E}{$definition}g; | |||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||
| 65 | 1 | my (%elementinfo, %attribinfo); | |||||||||||||||||||||||||||||||
| 66 | 1 | ▶ | while ($xml =~ s{<!--#info\s+(.*?)-->}{}s) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 67 | 8 | my $info = $1;$info =~ s/\s+$//s; | |||||||||||||||||||||||||||||||
| 68 | 8 | my %info; | |||||||||||||||||||||||||||||||
| 69 | 8 | ▶ | while ($info =~ s{^([\w-]+)\s*=\s*((?:'[^']*')+|(?:"[^"]*")+|[^\s'"]\S*)\s*}{}s) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 70 | 19 | my ($name, $value) = ($1, $2); | |||||||||||||||||||||||||||||||
| 71 | 19 | ▶ | if ($value =~ /^'/) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 72 | 18 | ($value = substr $value, 1, length($value)-2) =~ s/''/'/g; | |||||||||||||||||||||||||||||||
| 73 | } elsif ($value =~ /^"/) { | ||||||||||||||||||||||||||||||||
| 74 | 0 | ($value = substr $value, 1, length($value)-2) =~ s/""/"/g; | |||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||
| 76 | 19 | $info{$name} = $value; | |||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||
| 78 | 8 | ▶ | die "Malformed <!--#info ...--> section!\n\t<!--#info $info -->\n" | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 79 | if ($info ne ''); | ||||||||||||||||||||||||||||||||
| 80 | die "The <!--#info $info --> section doesn't contain the 'element' parameter!\n" | ||||||||||||||||||||||||||||||||
| 81 | 8 | ▶ | unless exists $info{'element'}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 82 | 8 | my $element = $info{'element'}; | |||||||||||||||||||||||||||||||
| 83 | 8 | delete $info{'element'}; | |||||||||||||||||||||||||||||||
| 84 | 8 | ▶ | if (exists $info{'attribute'}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 85 | 2 | my $attribute = $info{'attribute'}; | |||||||||||||||||||||||||||||||
| 86 | 2 | delete $info{'attribute'}; | |||||||||||||||||||||||||||||||
| 87 | 2 | $attribinfo{$element}->{$attribute} = \%info; | |||||||||||||||||||||||||||||||
| 88 | } else { | ||||||||||||||||||||||||||||||||
| 89 | 6 | $elementinfo{$element} = \%info; | |||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||
| 93 | 1 | $xml =~ s{<!--.*?-->}{}gs; | |||||||||||||||||||||||||||||||
| 94 | 1 | $xml =~ s{<\?.*?\?>}{}gs; | |||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||
| 96 | 1 | ▶ | while ($xml =~ s{<!ENTITY\s+(?:(%)\s*)?($name)\s*"(.*?)"\s*>}{}io) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 97 | 0 | my ($percent, $entity, $definition) = ($1,$2,$3); | |||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | $percent = '&' unless $percent; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 99 | 0 | $definitions{"$percent$entity"} = $definition; | |||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||
| 102 | { | ||||||||||||||||||||||||||||||||
| 103 | 1 | my $replacements = 0; | |||||||||||||||||||||||||||||||
| 104 | 0 | ▶ | 1 while $replacements++ < 1000 and $xml =~ s{([&%]$name);}{(exists $definitions{$1} ? $definitions{$1} : "$1\x01;")}geo; | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
$replacements++ < 1000 and $xml =~ s/([&%]$name);/exists $definitions{$1} ? $definitions{$1} : "$1\cA;";/ego
MC/DC0%0/2
$replacements++ < 1000 and $xml =~ s/([&%]$name);/exists $definitions{$1} ? $definitions{$1} : "$1\cA;";/ego
$replacements++ < 1000
$xml =~ s/([&%]$name);/exists $definitions{$1} ? $definitions{$1} : "$1\cA;";/ego
Truth table33%1/3
A and B
A= $replacements++ < 1000B= $xml =~ s/([&%]$name);/exists $definitions{$1} ? $definitions{$1} : "$1\cA;";/ego
| |||||||||||||||||||||||||||||||||
| 105 | 1 | ▶ | die <<'*END*' if $xml =~ m{([&%]$name);}o; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 106 | Recursive <!ENTITY ...> definitions or too many entities! Only up to 1000 entity replacements allowed. | ||||||||||||||||||||||||||||||||
| 107 | (An entity is something like &foo; or %foo;. They are defined by <!ENTITY ...> tag.) | ||||||||||||||||||||||||||||||||
| 108 | *END* | ||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||
| 110 | 1 | undef %definitions; | |||||||||||||||||||||||||||||||
| 111 | 1 | $xml =~ tr/\x01//d; | |||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||
| 113 | 1 | ▶ | while ($xml =~ s{<!ELEMENT\s+($name)\s*(\(.*?\))([?*+]?)\s*>}{}io) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 114 | 10 | my ($element, $children, $option) = ($1,$2,$3); | |||||||||||||||||||||||||||||||
| 115 | 10 | $elements{$element}->{childrenSTR} = $children . $option; | |||||||||||||||||||||||||||||||
| 116 | 10 | $children =~ s/\s//g; | |||||||||||||||||||||||||||||||
| 117 | 10 | ▶ | if ($children eq '(#PCDATA)') { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 118 | 7 | $children = '#PCDATA'; | |||||||||||||||||||||||||||||||
| 119 | ▶ | } elsif ($children =~ s/^\((#PCDATA(?:\|$name)+)\)$/$1/o and $option eq '*') { | |||||||||||||||||||||||||||||||
|
Condition33%1/3
$children =~ s/^\((#PCDATA(?:\|$name)+)\)$/$1/o and $option eq "*"
MC/DC0%0/2
$children =~ s/^\((#PCDATA(?:\|$name)+)\)$/$1/o and $option eq "*"
$children =~ s/^\((#PCDATA(?:\|$name)+)\)$/$1/o
$option eq "*"
Truth table33%1/3
A and B
A= $children =~ s/^\((#PCDATA(?:\|$name)+)\)$/$1/oB= $option eq "*"
| |||||||||||||||||||||||||||||||||
| 120 | 0 | $children =~ s/\|/*,/g; | |||||||||||||||||||||||||||||||
| 121 | 0 | $children .= '*'; | |||||||||||||||||||||||||||||||
| 122 | } else { | ||||||||||||||||||||||||||||||||
| 123 | 3 | $children = simplify_children( $children, $option); | |||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||
| 126 | 10 | ▶ | die "<!ELEMENT $element (...)> is not valid!\n" | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 127 | unless $children =~ m{^#?$nameX(?:,$nameX)*$}o; | ||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||
| 130 | 10 | $elements{$element}->{childrenARR} = []; | |||||||||||||||||||||||||||||||
| 131 | 10 | foreach my $child (split ',', $children) { | |||||||||||||||||||||||||||||||
| 132 | 17 | ▶ | $child =~ s/([?*+])$// | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$child =~ s/([?*+])$// and $option = $1
MC/DC50%1/2
$child =~ s/([?*+])$// and $option = $1
$child =~ s/([?*+])$//
$option = $1
Truth table66%2/3
A and B
A= $child =~ s/([?*+])$//B= $option = $1
| |||||||||||||||||||||||||||||||||
| 133 | and $option = $1 | ||||||||||||||||||||||||||||||||
| 134 | or $option = '!'; | ||||||||||||||||||||||||||||||||
| 135 | 17 | ▶ | if (exists $elements{$element}->{children}->{$child}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 136 | 1 | $elements{$element}->{children}->{$child} = _merge_options( $elements{$element}->{children}->{$child}, $option); | |||||||||||||||||||||||||||||||
| 137 | 1 | ▶ | $elements{$element}->{childrenX}->{$child} = _merge_counts( $elements{$element}->{childrenX}->{$child}, _char2count($option)) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 138 | unless $child eq '#PCDATA'; | ||||||||||||||||||||||||||||||||
| 139 | } else { | ||||||||||||||||||||||||||||||||
| 140 | 16 | $elements{$element}->{children}->{$child} = $option; | |||||||||||||||||||||||||||||||
| 141 | 16 | ▶ | $elements{$element}->{childrenX}->{$child} = _char2count($option) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 142 | unless $child eq '#PCDATA'; | ||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||
| 144 | 17 | ▶ | push @{$elements{$element}->{childrenARR}}, $child | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 145 | unless $child eq '#PCDATA'; | ||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||
| 147 | delete $elements{$element}->{childrenARR} | ||||||||||||||||||||||||||||||||
| 148 | 10 | ▶ | if @{$elements{$element}->{childrenARR}} == 0 | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||
| 151 | 1 | ▶ | while ($xml =~ s{<!ELEMENT\s+($name)\s*(EMPTY|ANY)\s*>}{}io) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 152 | 0 | my ($element, $param) = ($1,$2); | |||||||||||||||||||||||||||||||
| 153 | 0 | ▶ | if (uc $param eq 'ANY') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 154 | 0 | $elements{$element}->{any} = 1; | |||||||||||||||||||||||||||||||
| 155 | } else { | ||||||||||||||||||||||||||||||||
| 156 | 0 | $elements{$element} = {}; | |||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||
| 159 | #=for comment | ||||||||||||||||||||||||||||||||
| 160 | 1 | ▶ | while ($xml =~ s{<!ATTLIST(?:\s+($name)\s+(.*?))?\s*>}{}io) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 161 | 2 | my ($element, $attributes) = ($1,$2); | |||||||||||||||||||||||||||||||
| 162 | die "<!ELEMENT $element ...> referenced by an <!ATTLIST ...> not found!\n" | ||||||||||||||||||||||||||||||||
| 163 | 2 | ▶ | unless exists $elements{$element}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 164 | 2 | ▶ | while ($attributes =~ s/^\s*$AttDef//io) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 165 | 4 | my ($name,$type,$option,$default) = ($1,$2,$3); | |||||||||||||||||||||||||||||||
| 166 | 4 | ▶ | if ($option =~ /^#FIXED\s+["'](.*)["']$/i){ | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 167 | 0 | $option = '#FIXED'; | |||||||||||||||||||||||||||||||
| 168 | 0 | $default = $1; | |||||||||||||||||||||||||||||||
| 169 | } elsif ($option =~ /^["'](.*)["']$/i){ | ||||||||||||||||||||||||||||||||
| 170 | 0 | $option = ''; | |||||||||||||||||||||||||||||||
| 171 | 0 | $default = $1; | |||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||||
| 173 | 4 | $elements{$element}->{attributes}->{$name} = [$type,$option,$default,undef]; | |||||||||||||||||||||||||||||||
| 174 | 4 | ▶ | if ($type =~ /^(?:NOTATION\s*)?\(\s*(.*?)\)$/) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 175 | 0 | $elements{$element}->{attributes}->{$name}->[3] = parse_enum($1); | |||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||||
| 179 | #=cut | ||||||||||||||||||||||||||||||||
| 180 | #$xml = ''; | ||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||
| 182 | 1 | $xml =~ s/\s\s*/ /g; | |||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||
| 184 | 1 | ▶ | die "UNPARSED DATA:\n$xml\n\n" | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 185 | if $xml =~ /\S/; | ||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||
| 187 | 1 | foreach my $element (keys %elements) { | |||||||||||||||||||||||||||||||
| 188 | 10 | foreach my $child (keys %{$elements{$element}->{children}}) { | |||||||||||||||||||||||||||||||
| 189 | 16 | ▶ | if ($child eq '#PCDATA') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 190 | 7 | delete $elements{$element}->{children}->{'#PCDATA'}; | |||||||||||||||||||||||||||||||
| 191 | 7 | $elements{$element}->{content} = 1; | |||||||||||||||||||||||||||||||
| 192 | } else { | ||||||||||||||||||||||||||||||||
| 193 | die "Element $child referenced by $element was not found!\n" | ||||||||||||||||||||||||||||||||
| 194 | 9 | ▶ | unless exists $elements{$child}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 195 | 9 | ▶ | if (exists $elements{$child}->{parent}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 196 | 0 | push @{$elements{$child}->{parent}}, $element; | |||||||||||||||||||||||||||||||
| 197 | } else { | ||||||||||||||||||||||||||||||||
| 198 | 9 | $elements{$child}->{parent} = [$element]; | |||||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||||||
| 200 | 9 | $elements{$child}->{option} = $elements{$element}->{children}->{$child}; | |||||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||||||
| 203 | 10 | ▶ | if (scalar(keys %{$elements{$element}->{children}}) == 0) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 204 | 7 | delete $elements{$element}->{children}; | |||||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||||||
| 206 | 10 | ▶ | if (exists $elementinfo{$element}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 207 | 6 | foreach my $info (keys %{$elementinfo{$element}}) { | |||||||||||||||||||||||||||||||
| 208 | 7 | $elements{$element}->{$info} = $elementinfo{$element}->{$info}; | |||||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||||||
| 211 | 10 | ▶ | if (exists $attribinfo{$element}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 212 | 2 | foreach my $attribute (keys %{$attribinfo{$element}}) { | |||||||||||||||||||||||||||||||
| 213 | 2 | $elements{$element}->{'attributes'}->{$attribute}->[4] = $attribinfo{$element}->{$attribute}; | |||||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||
| 218 | 1 | return \%elements; | |||||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||
| 221 | 3 | ▶ | sub flatten_children { | ||||||||||||||||||||||||||||||
|
Subroutine
flatten_children: called CC=3 SCAR=14.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 222 | 3 | my ( $children, $option ) = @_; | |||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||
| 224 | 3 | ▶ | if ($children =~ /\|/) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 225 | 0 | $children =~ s{[|,]}{?,}g; | |||||||||||||||||||||||||||||||
| 226 | 0 | $children .= '?' | |||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||
| 229 | 3 | ▶ | if ($option) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 230 | 0 | $children =~ s/,/$option,/g; | |||||||||||||||||||||||||||||||
| 231 | 0 | $children .= $option; | |||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||
| 234 | 3 | return $children; | |||||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||
| 237 | 3 | ▶ | sub simplify_children { | ||||||||||||||||||||||||||||||
|
Subroutine
simplify_children: called CC=4 SCAR=14.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 238 | 3 | my ( $children, $option ) = @_; | |||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||
| 240 | 3 | ▶ | 1 while $children =~ s{\(($nameX(?:[,|]$nameX)*)\)([?*+]*)}{flatten_children($1, $2)}geo; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||
| 242 | 3 | ▶ | if ($option) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 243 | 0 | $children =~ s/,/$option,/g; | |||||||||||||||||||||||||||||||
| 244 | 0 | $children .= $option; | |||||||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||
| 247 | 3 | foreach ($children) { | |||||||||||||||||||||||||||||||
| 248 | 3 | s{\?\?}{?}g; | |||||||||||||||||||||||||||||||
| 249 | 3 | s{\?\+}{*}g; | |||||||||||||||||||||||||||||||
| 250 | 3 | s{\?\*}{*}g; | |||||||||||||||||||||||||||||||
| 251 | 3 | s{\+\?}{*}g; | |||||||||||||||||||||||||||||||
| 252 | 3 | s{\+\+}{+}g; | |||||||||||||||||||||||||||||||
| 253 | 3 | s{\+\*}{*}g; | |||||||||||||||||||||||||||||||
| 254 | 3 | s{\*\?}{*}g; | |||||||||||||||||||||||||||||||
| 255 | 3 | s{\*\+}{*}g; | |||||||||||||||||||||||||||||||
| 256 | 3 | s{\*\*}{*}g; | |||||||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||
| 259 | 3 | return $children; | |||||||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||
| 262 | 0 | ▶ | sub parse_enum { | ||||||||||||||||||||||||||||||
|
Subroutine
parse_enum: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 263 | 0 | my $enum = shift; | |||||||||||||||||||||||||||||||
| 264 | 0 | $enum =~ tr/\x20\x09\x0D\x0A//d; # get rid of whitespace | |||||||||||||||||||||||||||||||
| 265 | 0 | return [split /\|/, $enum]; | |||||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||
| 268 | 1 | my %merge_options = ( | |||||||||||||||||||||||||||||||
| 269 | '!!' => '+', | ||||||||||||||||||||||||||||||||
| 270 | '!*' => '+' , | ||||||||||||||||||||||||||||||||
| 271 | '!+' => '+', | ||||||||||||||||||||||||||||||||
| 272 | '!?' => '+', | ||||||||||||||||||||||||||||||||
| 273 | '**' => '*', | ||||||||||||||||||||||||||||||||
| 274 | '*+' => '+', | ||||||||||||||||||||||||||||||||
| 275 | '*?' => '*', | ||||||||||||||||||||||||||||||||
| 276 | '++' => '+', | ||||||||||||||||||||||||||||||||
| 277 | '+?' => '+', | ||||||||||||||||||||||||||||||||
| 278 | '??' => '?', | ||||||||||||||||||||||||||||||||
| 279 | ); | ||||||||||||||||||||||||||||||||
| 280 | 1 | ▶ | sub _merge_options { | ||||||||||||||||||||||||||||||
|
Subroutine
_merge_options: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 281 | 1 | my ($o1, $o2) = sort @_; | |||||||||||||||||||||||||||||||
| 282 | 1 | return $merge_options{$o1.$o2}; | |||||||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||
| 285 | 1 | my %char2count = ( | |||||||||||||||||||||||||||||||
| 286 | '!' => '1', | ||||||||||||||||||||||||||||||||
| 287 | '?' => '0..1', | ||||||||||||||||||||||||||||||||
| 288 | '+' => '1..', | ||||||||||||||||||||||||||||||||
| 289 | '*' => '0..', | ||||||||||||||||||||||||||||||||
| 290 | ); | ||||||||||||||||||||||||||||||||
| 291 | 10 | ▶ | sub _char2count{ | ||||||||||||||||||||||||||||||
|
Subroutine
_char2count: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 292 | 10 | return $char2count{$_[0]} | |||||||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||
| 295 | 1 | ▶ | sub _merge_counts { | ||||||||||||||||||||||||||||||
|
Subroutine
_merge_counts: called CC=13 SCAR=46.0
| |||||||||||||||||||||||||||||||||
| 296 | 1 | my ($c1, $c2) = @_; | |||||||||||||||||||||||||||||||
| 297 | 1 | ▶ | if ($c1 =~ /^\d+$/) { | ||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||
| 298 | 1 | ▶ | if ($c2 =~ /^\d+$/) { | ||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||
| 299 | 0 | return $c1+$c2 | |||||||||||||||||||||||||||||||
| 300 | } elsif ($c2 =~ /^(\d+)..(\d+)$/) { | ||||||||||||||||||||||||||||||||
| 301 | 0 | return ($c1+$1) . ".." . ($c1+$2); | |||||||||||||||||||||||||||||||
| 302 | } elsif ($c2 =~ /^(\d+)..$/) { | ||||||||||||||||||||||||||||||||
| 303 | 1 | return ($c1+$1) . ".."; | |||||||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||||||
| 305 | } elsif ($c1 =~ /^(\d+)..(\d+)$/) { | ||||||||||||||||||||||||||||||||
| 306 | 0 | my ($c1l,$c1u) = ($1,$2); | |||||||||||||||||||||||||||||||
| 307 | 0 | ▶ | if ($c2 =~ /^\d+$/) { | ||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||
| 308 | 0 | return ($c1l+$c2) . ".." . ($c1u+$c2); | |||||||||||||||||||||||||||||||
| 309 | } elsif ($c2 =~ /^(\d+)..(\d+)$/) { | ||||||||||||||||||||||||||||||||
| 310 | 0 | return ($c1l+$1) . ".." . ($c1u+$2); | |||||||||||||||||||||||||||||||
| 311 | } elsif ($c2 =~ /^(\d+)..$/) { | ||||||||||||||||||||||||||||||||
| 312 | 0 | return ($c1l+$1) . ".."; | |||||||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||||||
| 314 | } elsif ($c1 =~ /^(\d+)..$/) { | ||||||||||||||||||||||||||||||||
| 315 | 0 | $c1=$1; | |||||||||||||||||||||||||||||||
| 316 | 0 | ▶ | if ($c2 =~ /^\d+$/) { | ||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||
| 317 | 0 | return ($c1+$c2) . ".."; | |||||||||||||||||||||||||||||||
| 318 | } elsif ($c2 =~ /^(\d+)..(\d+)$/) { | ||||||||||||||||||||||||||||||||
| 319 | 0 | return ($c1+$1) . ".."; | |||||||||||||||||||||||||||||||
| 320 | } elsif ($c2 =~ /^(\d+)..$/) { | ||||||||||||||||||||||||||||||||
| 321 | 0 | return ($c1+$1) . ".."; | |||||||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||||||
| 323 | } | ||||||||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||
| 326 | 0 | ▶ | sub FindDTDRoot { | ||||||||||||||||||||||||||||||
|
Subroutine
FindDTDRoot: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||
| 327 | 0 | my $elements = shift; | |||||||||||||||||||||||||||||||
| 328 | 0 | my @roots; | |||||||||||||||||||||||||||||||
| 329 | 0 | foreach my $element (keys %$elements) { | |||||||||||||||||||||||||||||||
| 330 | 0 | ▶ | if (!exists $elements->{$element}->{parent}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 331 | 0 | push @roots, $element; | |||||||||||||||||||||||||||||||
| 332 | 0 | $elements->{$element}->{option} = '!'; | |||||||||||||||||||||||||||||||
| 333 | } | ||||||||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||||||
| 335 | 0 | return @roots; | |||||||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||
| 338 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||
| 340 | XML::DTDParser - quick and dirty DTD parser | ||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||
| 342 | Version 2.01 | ||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||
| 344 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||
| 346 | use XML::DTDParser qw(ParseDTD ParseDTDFile); | ||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||
| 348 | $DTD = ParseDTD $DTDtext; | ||||||||||||||||||||||||||||||||
| 349 | #or | ||||||||||||||||||||||||||||||||
| 350 | $DTD = ParseDTDFile( $dtdfile) | ||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||
| 352 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||
| 354 | This module parses a DTD file and creates a data structure containing info about | ||||||||||||||||||||||||||||||||
| 355 | all tags, their allowed parameters, children, parents, optionality etc. etc. etc. | ||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||
| 357 | Since I'm too lazy to document the structure, parse a DTD you need and print | ||||||||||||||||||||||||||||||||
| 358 | the result to a file using Data::Dumper. The datastructure should be selfevident. | ||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||
| 360 | Note: The module should be able to parse just about anything, but it intentionaly looses some information. | ||||||||||||||||||||||||||||||||
| 361 | Eg. if the DTD specifies that a tag should contain either CHILD1 or CHILD2 you only get that | ||||||||||||||||||||||||||||||||
| 362 | CHILD1 and CHILD2 are optional. That is is the DTD contains | ||||||||||||||||||||||||||||||||
| 363 | <!ELEMENT FOO (BAR|BAZ)> | ||||||||||||||||||||||||||||||||
| 364 | the result will be the same is if it contained | ||||||||||||||||||||||||||||||||
| 365 | <!ELEMENT FOO (BAR?,BAZ?)> | ||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||
| 367 | You get the original unparsed parameter list as well so if you need this | ||||||||||||||||||||||||||||||||
| 368 | information you may parse it yourself. | ||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||
| 370 | Since version 1.6 this module supports my "extensions" to DTDs. | ||||||||||||||||||||||||||||||||
| 371 | If the DTD contains a comment in form | ||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||
| 373 | <!--#info element=XXX foo=bar greeting="Hello World!" person='d''Artagnan'--> | ||||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||
| 375 | and there is an element XXX in the DTD, the resulting hash for the XXX will contain | ||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||
| 377 | 'foo' => 'bar', | ||||||||||||||||||||||||||||||||
| 378 | 'person' => 'd\'Artagnan', | ||||||||||||||||||||||||||||||||
| 379 | 'greeting => 'Hello World!' | ||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||
| 381 | If the DTD contains | ||||||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||||||
| 383 | <!--#info element=XXX attribute=YYY break=no--> | ||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||
| 385 | the | ||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||
| 387 | $DTD->{XXX}->{attributes}->{YYY}->[4] | ||||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||
| 389 | will be set to | ||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||
| 391 | { break => 'no' } | ||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||
| 393 | I use this parser to import the DTD into the database so that I could map some fields | ||||||||||||||||||||||||||||||||
| 394 | to certain tags for output and I want to be able to specify the mapping inside the file: | ||||||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||||||
| 396 | <!--#info element=TagName map_to="FieldName"--> | ||||||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||||||
| 398 | =head2 EXPORT | ||||||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||||||
| 400 | By default the module exports all (both) it's functions. If you only want one, or none | ||||||||||||||||||||||||||||||||
| 401 | use | ||||||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||||||
| 403 | use XML::DTDParser qw(ParseDTD); | ||||||||||||||||||||||||||||||||
| 404 | or | ||||||||||||||||||||||||||||||||
| 405 | use XML::DTDParser qw(); | ||||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||||
| 407 | =over 4 | ||||||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||||||
| 409 | =item ParseDTD | ||||||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||||||
| 411 | $DTD = ParseDTD $DTDtext; | ||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||
| 413 | Parses the $DTDtext and creates a data structure. If the $DTDtext contains some | ||||||||||||||||||||||||||||||||
| 414 | <!ENTITY ... SYSTEM "..."> declarations those are read and parsed as needed. | ||||||||||||||||||||||||||||||||
| 415 | The paths are relative to current directory. | ||||||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||||||
| 417 | The module currently doesn't support URLs here yet. | ||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||
| 419 | =item ParseDTDFile | ||||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||||
| 421 | $DTD = ParseDTDFile $DTDfile; | ||||||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||||||
| 423 | Parses the contents of $DTDfile and creates a data structure. If the $DTDfile contains some | ||||||||||||||||||||||||||||||||
| 424 | <!ENTITY ... SYSTEM "..."> declarations those are read and parsed as needed. | ||||||||||||||||||||||||||||||||
| 425 | The paths are relative to the $DTDfile. | ||||||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||||||
| 427 | The module currently doesn't support URLs here yet. | ||||||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||||||
| 429 | =item FindDTDRoot | ||||||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||||||
| 431 | $DTD = ParseDTD $DTDtext; | ||||||||||||||||||||||||||||||||
| 432 | @roots = FindDTDRoot $DTD; | ||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||
| 434 | Returns all tags that have no parent. There could be several such tags defined by the DTD. | ||||||||||||||||||||||||||||||||
| 435 | Especialy if it used some common includes. | ||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||
| 437 | =back | ||||||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||||||
| 439 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||||||
| 441 | Jenda@Krynicky.cz | ||||||||||||||||||||||||||||||||
| 442 | http://Jenda.Krynicky.cz | ||||||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||||||
| 444 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||||||
| 446 | Copyright (c) 2002 Jan Krynicky <Jenda@Krynicky.cz>. All rights reserved. | ||||||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||||||
| 448 | This program is free software; you can redistribute it and/or | ||||||||||||||||||||||||||||||||
| 449 | modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||||||
| 451 | =cut | ||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||