| 1 | # ------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 2 | # MKDoc::XML::Tokenizer | ||||||||||||||||||||||||||||
| 3 | # ------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 4 | # Author : Jean-Michel Hiver. | ||||||||||||||||||||||||||||
| 5 | # Copyright : (c) MKDoc Holdings Ltd, 2003 | ||||||||||||||||||||||||||||
| 6 | # | ||||||||||||||||||||||||||||
| 7 | # This module turns an XML string into a list of tokens and returns this list. | ||||||||||||||||||||||||||||
| 8 | # It is using Robert D. Cameron "REX: XML Shallow Parsing with Regular Expressions" | ||||||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||||||
| 10 | # This module is distributed under the same license as Perl itself. | ||||||||||||||||||||||||||||
| 11 | # ------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 12 | package MKDoc::XML::Tokenizer; | ||||||||||||||||||||||||||||
| 13 | 20 | ▶ | use MKDoc::XML::Token; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 20 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 20 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 20 | our $prev_token; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | # REX/Perl 1.0 | ||||||||||||||||||||||||||||
| 20 | # Robert D. Cameron "REX: XML Shallow Parsing with Regular Expressions", | ||||||||||||||||||||||||||||
| 21 | # Technical Report TR 1998-17, School of Computing Science, Simon Fraser | ||||||||||||||||||||||||||||
| 22 | # University, November, 1998. | ||||||||||||||||||||||||||||
| 23 | # Copyright (c) 1998, Robert D. Cameron. | ||||||||||||||||||||||||||||
| 24 | # The following code may be freely used and distributed provided that | ||||||||||||||||||||||||||||
| 25 | # this copyright and citation notice remains intact and that modifications | ||||||||||||||||||||||||||||
| 26 | # or additions are clearly identified. | ||||||||||||||||||||||||||||
| 27 | # | ||||||||||||||||||||||||||||
| 28 | # Additions: | ||||||||||||||||||||||||||||
| 29 | # ---------- | ||||||||||||||||||||||||||||
| 30 | # added 'my' and 'our' keywords in front of variables | ||||||||||||||||||||||||||||
| 31 | # I like strict mode :) | ||||||||||||||||||||||||||||
| 32 | 20 | my $TextSE = "[^<]+"; | |||||||||||||||||||||||||||
| 33 | 20 | my $UntilHyphen = "[^-]*-"; | |||||||||||||||||||||||||||
| 34 | 20 | my $Until2Hyphens = "$UntilHyphen(?:[^-]$UntilHyphen)*-"; | |||||||||||||||||||||||||||
| 35 | 20 | my $CommentCE = "$Until2Hyphens>?"; | |||||||||||||||||||||||||||
| 36 | 20 | my $UntilRSBs = "[^\\]]*](?:[^\\]]+])*]+"; | |||||||||||||||||||||||||||
| 37 | 20 | my $CDATA_CE = "$UntilRSBs(?:[^\\]>]$UntilRSBs)*>"; | |||||||||||||||||||||||||||
| 38 | 20 | my $S = "[ \\n\\t\\r]+"; | |||||||||||||||||||||||||||
| 39 | 20 | my $NameStrt = "[A-Za-z_:]|[^\\x00-\\x7F]"; | |||||||||||||||||||||||||||
| 40 | 20 | my $NameChar = "[A-Za-z0-9_:.-]|[^\\x00-\\x7F]"; | |||||||||||||||||||||||||||
| 41 | 20 | my $Name = "(?:$NameStrt)(?:$NameChar)*"; | |||||||||||||||||||||||||||
| 42 | 20 | my $QuoteSE = "\"[^\"]*\"|'[^']*'"; | |||||||||||||||||||||||||||
| 43 | 20 | my $DT_IdentSE = "$S$Name(?:$S(?:$Name|$QuoteSE))*"; | |||||||||||||||||||||||||||
| 44 | 20 | my $MarkupDeclCE = "(?:[^\\]\"'><]+|$QuoteSE)*>"; | |||||||||||||||||||||||||||
| 45 | 20 | my $S1 = "[\\n\\r\\t ]"; | |||||||||||||||||||||||||||
| 46 | 20 | my $UntilQMs = "[^?]*\\?+"; | |||||||||||||||||||||||||||
| 47 | 20 | my $PI_Tail = "\\?>|$S1$UntilQMs(?:[^>?]$UntilQMs)*>"; | |||||||||||||||||||||||||||
| 48 | 20 | my $DT_ItemSE = "<(?:!(?:--$Until2Hyphens>|[^-]$MarkupDeclCE)|\\?$Name(?:$PI_Tail))|%$Name;|$S"; | |||||||||||||||||||||||||||
| 49 | 20 | my $DocTypeCE = "$DT_IdentSE(?:$S)?(?:\\[(?:$DT_ItemSE)*](?:$S)?)?>?"; | |||||||||||||||||||||||||||
| 50 | 20 | my $DeclCE = "--(?:$CommentCE)?|\\[CDATA\\[(?:$CDATA_CE)?|DOCTYPE(?:$DocTypeCE)?"; | |||||||||||||||||||||||||||
| 51 | 20 | my $PI_CE = "$Name(?:$PI_Tail)?"; | |||||||||||||||||||||||||||
| 52 | 20 | my $EndTagCE = "$Name(?:$S)?>?"; | |||||||||||||||||||||||||||
| 53 | 20 | my $AttValSE = "\"[^<\"]*\"|'[^<']*'"; | |||||||||||||||||||||||||||
| 54 | 20 | my $ElemTagCE = "$Name(?:$S$Name(?:$S)?=(?:$S)?(?:$AttValSE))*(?:$S)?/?>?"; | |||||||||||||||||||||||||||
| 55 | 20 | my $MarkupSPE = "<(?:!(?:$DeclCE)?|\\?(?:$PI_CE)?|/(?:$EndTagCE)?|(?:$ElemTagCE)?)"; | |||||||||||||||||||||||||||
| 56 | 20 | our $XML_SPE = "$TextSE|$MarkupSPE"; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | # Rather than have this: | ||||||||||||||||||||||||||||
| 60 | # sub ShallowParse { | ||||||||||||||||||||||||||||
| 61 | # my($XML_document) = @_; | ||||||||||||||||||||||||||||
| 62 | # return $XML_document =~ /$XML_SPE/g; | ||||||||||||||||||||||||||||
| 63 | # } | ||||||||||||||||||||||||||||
| 64 | sub process_data | ||||||||||||||||||||||||||||
| 65 | 50 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
process_data: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 66 | 50 | my $class = shift; | |||||||||||||||||||||||||||
| 67 | 50 | my $xml = shift; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | # remove trailing whitespace | ||||||||||||||||||||||||||||
| 70 | 50 | $xml =~ s/^(?:\s|\r|\n)*\</\</s; | |||||||||||||||||||||||||||
| 71 | 50 | $xml =~ s/\>(?:\s|\r|\n)*$/\>/s; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 50 | local ($prev_token) = ''; | |||||||||||||||||||||||||||
| 74 | my @res = map { | ||||||||||||||||||||||||||||
| 75 | 50 | _check_001(); | |||||||||||||||||||||||||||
| 76 | 984 | _check_002(); | |||||||||||||||||||||||||||
| 77 | 979 | $prev_token = $_; | |||||||||||||||||||||||||||
| 78 | 979 | bless \$_, 'MKDoc::XML::Token'; | |||||||||||||||||||||||||||
| 79 | } $xml =~ /$XML_SPE/go; | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 44 | return \@res; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | # <p foobar> | ||||||||||||||||||||||||||||
| 86 | sub _check_002 | ||||||||||||||||||||||||||||
| 87 | 984 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_check_002: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 88 | 984 | ▶ | $prev_token =~ /^</ or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 476 | ▶ | $prev_token =~ />$/ or | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | die "cannot tokenize: $prev_token$_"; | ||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | # <!-- stuff like -- that --> | ||||||||||||||||||||||||||||
| 95 | sub _check_001 | ||||||||||||||||||||||||||||
| 96 | 985 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_check_001: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 97 | 985 | ▶ | /^<!--/ and /--$/ and die "invalid comment token: $_"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
/^<!--/ and /--$/
MC/DC100%2/2
/^<!--/ and /--$/
/^<!--/
/--$/
Truth table100%3/3
A and B
A= /^<!--/B= /--$/
| |||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | sub process_file | ||||||||||||||||||||||||||||
| 102 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
process_file: called CC=3 SCAR=11.5
Pod
documented
| |||||||||||||||||||||||||||||
| 103 | 2 | my $class = shift; | |||||||||||||||||||||||||||
| 104 | 2 | my $file = shift; | |||||||||||||||||||||||||||
| 105 | 2 | ▶ | open FP, "<$file" || do { | ||||||||||||||||||||||||||
|
Condition33%1/3
"<$file" || do {
warn "Cannot read-open $file";
return []
}
MC/DC0%0/2
"<$file" || do {
warn "Cannot read-open $file";
return []
}
"<$file"
do {
warn "Cannot read-open $file";
return []
}
Truth table33%1/3
A || B
A= "<$file"B= do {
warn "Cannot read-open $file";
return []
}
| |||||||||||||||||||||||||||||
| 106 | 0 | warn "Cannot read-open $file"; | |||||||||||||||||||||||||||
| 107 | 0 | return []; | |||||||||||||||||||||||||||
| 108 | }; | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 2 | my $data = ''; | |||||||||||||||||||||||||||
| 111 | 2 | ▶ | while (<FP>) { $data .= $_ } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 2 | close FP; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 2 | return $class->process_data ($data); | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 20 | 1; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | __END__ | ||||||||||||||||||||||||||||