| 1 | # $Id: Clean.pm,v 1.6 2003/09/21 14:04:37 petr Exp $ | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | XML::Clean - Ensure, that I<(HTML)> text pass throught an XML parser. | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | use XML::Clean; | ||
| 10 | |||
| 11 | print XML::Clean::clean ("<foo bar>barfoo"); | ||
| 12 | # <foo>barfoo</foo> | ||
| 13 | |||
| 14 | print XML::Clean::clean ("<foo bar>barfoo",1.5); | ||
| 15 | # <?xml version="1.5" encoding="ISO-8859-1"?> | ||
| 16 | # <foo bar>barfoo</foo> | ||
| 17 | |||
| 18 | print XML::Clean::clean ("bar <foo bar=10> bar",1.6,){root=>"XML_ROOT",encoding=>"ISO-8859-2"} ); | ||
| 19 | # <?xml version="1.6" encoding="ISO-8859-2"?> | ||
| 20 | # <XML_ROOT | ||
| 21 | # bar <foo bar="10"> bar</foo></XML_ROOT> | ||
| 22 | |||
| 23 | =head1 DESCRIPTION | ||
| 24 | |||
| 25 | The ultimate quest of this module is to produce from non-XML text | ||
| 26 | text, that will will most probably pass throught any XML parser one | ||
| 27 | could find. | ||
| 28 | |||
| 29 | Basic cleaning is just XML tag matching (for every opening tag there | ||
| 30 | will be closing tag as well, and they will form a tree structure). | ||
| 31 | |||
| 32 | When you add some extra parameters, you will receive complete XML | ||
| 33 | text, including XML head and root element (if none were defined in | ||
| 34 | text, then some will be added). | ||
| 35 | |||
| 36 | =head1 FUNCTIONS AND METHODS | ||
| 37 | |||
| 38 | =over 4 | ||
| 39 | |||
| 40 | =item XML::Clean::clean($text, [$version, [%options] ]) | ||
| 41 | |||
| 42 | |||
| 43 | Return (almost) XML text, made from input parameter C<$text>. | ||
| 44 | |||
| 45 | When C<$version> is false, only match tags, and escapes any unmatched | ||
| 46 | tags. | ||
| 47 | |||
| 48 | When you pass C<$version> parameter, then text is checked for standard | ||
| 49 | XML head (<!XML VERSION=..>), and depending on options (force_root), some is | ||
| 50 | added / existing is modified. Also depending on options, text is checked for | ||
| 51 | root element. VERSION XML head parameter in output text is set to parameter | ||
| 52 | value you pass. | ||
| 53 | |||
| 54 | Options are: | ||
| 55 | |||
| 56 | encoding - String to be added as XML encoding attribute in XML header. Defaults | ||
| 57 | to I<ISO-8859-1>. | ||
| 58 | |||
| 59 | force_root - If true, output text will have XML root. Defaults to I<false>. | ||
| 60 | |||
| 61 | root - Output text will have that tag as root element. Defaults to | ||
| 62 | I<xml_root>. | ||
| 63 | |||
| 64 | =item clean_file $filename [$version [%options] ] | ||
| 65 | |||
| 66 | Open file called C<$filename>, reads all text from it, pass it to clean | ||
| 67 | with C<$version> and C<%options>, write output text to file called | ||
| 68 | C<$filename>. | ||
| 69 | |||
| 70 | Die on I/O error. | ||
| 71 | |||
| 72 | =back | ||
| 73 | |||
| 74 | =head1 BUGS | ||
| 75 | |||
| 76 | This module is still under development. Not all XML errors are | ||
| 77 | corrected with it. | ||
| 78 | |||
| 79 | Its otherwise too ineficient and slow:). | ||
| 80 | |||
| 81 | =head1 AUTHOR | ||
| 82 | |||
| 83 | =for html | ||
| 84 | <a href="mailto:petr@kubanek.net">petr@kubanek.net</a>. Send there any complains, comments and so on. | ||
| 85 | |||
| 86 | =head1 DISTRIBUTION | ||
| 87 | |||
| 88 | =for html | ||
| 89 | <a href="http://www.kubanek.net/xmlclean">http://www.kubanek.net/xmlclean</a> | ||
| 90 | |||
| 91 | =cut | ||
| 92 | |||
| 93 | BEGIN { | ||
| 94 | $VERSION = do { my @r = (q$Revision: 1.6 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; | ||
| 95 | } | ||
| 96 | |||
| 97 | use strict; | ||
| 98 | use warnings; | ||
| 99 | |||
| 100 | package XML::Clean; | ||
| 101 | use vars qw(@ISA @EXPORT); | ||
| 102 | require Exporter; | ||
| 103 | @ISA =qw(Exporter); | ||
| 104 | |||
| 105 | my @stack; | ||
| 106 | |||
| 107 | my %escapes = ( "<" => "<", ">" => ">", "&" => "&" ) ; | ||
| 108 | my $escapes_keys = "(" . join ( "|", keys %escapes ) . ")"; | ||
| 109 | |||
| 110 | # help routine to ensure, that xml attributes for tags are correct. | ||
| 111 | # It means, they match variable="value" style | ||
| 112 | |||
| 113 | sub clean_attr { | ||
| 114 | my $attr = shift; | ||
| 115 | return "" unless $attr; | ||
| 116 | my $ret; | ||
| 117 | $ret = ""; | ||
| 118 | # put to result only well-formed or almost-well formed values | ||
| 119 | while ($attr =~ m/((?:\w|_|-)+)\s*=\s*((?:\w|\d|_|-)+|".*?")/g) { | ||
| 120 | my $name=$1; | ||
| 121 | my $val=$2; | ||
| 122 | $val =~ s#(^["']|["']$)##g; | ||
| 123 | $ret .= ' '.$name.'="'.$val.'"'; | ||
| 124 | } | ||
| 125 | $ret = $ret."/" if ($attr =~ m#/$#); | ||
| 126 | return $ret; | ||
| 127 | } | ||
| 128 | |||
| 129 | # help routine to handle start tags. Check, if they aren't legal XML | ||
| 130 | # tag (not ending with /), then push them to @stack. | ||
| 131 | |||
| 132 | sub handle_start { | ||
| 133 | my $element = shift; | ||
| 134 | my $attr = shift; | ||
| 135 | |||
| 136 | push @stack, $element unless ($attr =~ m#/$#); | ||
| 137 | |||
| 138 | $attr = clean_attr $attr; | ||
| 139 | |||
| 140 | return "<$element$attr>"; | ||
| 141 | } | ||
| 142 | |||
| 143 | # help routine to handel end tags. pop from @stack while it doesn't | ||
| 144 | # find matching same end tag, write end tag to output, returns | ||
| 145 | |||
| 146 | sub handle_end { | ||
| 147 | # exit, if empty | ||
| 148 | return "" unless @stack; | ||
| 149 | my $element = shift; | ||
| 150 | |||
| 151 | my $end_tags = ""; | ||
| 152 | my @tmp_stack = @stack; | ||
| 153 | |||
| 154 | my $end; | ||
| 155 | |||
| 156 | do { | ||
| 157 | $end = pop @tmp_stack; | ||
| 158 | $end_tags .= "</$end>"; | ||
| 159 | |||
| 160 | } until ($end eq $element) or ($#tmp_stack == -1); | ||
| 161 | |||
| 162 | if (not(@tmp_stack) and (($#stack !=0) and ($stack[0] ne $element))) { | ||
| 163 | return 1; | ||
| 164 | } | ||
| 165 | |||
| 166 | @stack = @tmp_stack; | ||
| 167 | |||
| 168 | return $end_tags; | ||
| 169 | } | ||
| 170 | |||
| 171 | sub handle_text { | ||
| 172 | my $element = shift; | ||
| 173 | |||
| 174 | # escape our elements | ||
| 175 | $element =~ s#$escapes_keys#$escapes{$1}#exg if defined $element; | ||
| 176 | |||
| 177 | return $element; | ||
| 178 | } | ||
| 179 | |||
| 180 | sub clean { | ||
| 181 | |||
| 182 | my $text = shift; | ||
| 183 | my $version = shift; | ||
| 184 | my $options = shift; | ||
| 185 | |||
| 186 | my $root = $$options{root}; | ||
| 187 | my $encoding = $$options{encoding}; | ||
| 188 | |||
| 189 | my $output = ""; | ||
| 190 | |||
| 191 | $encoding = "ISO-8859-1" unless $encoding; | ||
| 192 | |||
| 193 | if ($version) { | ||
| 194 | # first, check for <?xml ?> tag | ||
| 195 | if ($text !~ m/^<\?xml[^<>]*\?>\s*(<!\w+[^<>]*>)?\s*<\w+[^<>]*>/s ) { | ||
| 196 | $output = "<?xml version=\"$version\" encoding=\"$encoding\"?>\n"; | ||
| 197 | $text = "<$root>\n". $text if ($root); | ||
| 198 | } | ||
| 199 | } | ||
| 200 | |||
| 201 | # if there is something in $output, it must be <?xml | ||
| 202 | # version..> string | ||
| 203 | |||
| 204 | $text =~ s/^<\?xml[^<>]*\?>\s*(<!\w+[^<>]*>)?\s*//s if defined $text; | ||
| 205 | $output = $& unless $output; | ||
| 206 | |||
| 207 | # if we are asked to produce full-correct text with root as root | ||
| 208 | # element, then do it | ||
| 209 | |||
| 210 | if (($version) and ($$options{force_root}) and | ||
| 211 | ($text !~ m/<$root[^<>]*>/s)) { | ||
| 212 | $text = "<$root>\n". $text; | ||
| 213 | } | ||
| 214 | |||
| 215 | undef @stack; | ||
| 216 | |||
| 217 | if (defined $text) { | ||
| 218 | while ($text =~ m#^(.*?)<(/?\w+.*?)>(.*)#s) { | ||
| 219 | |||
| 220 | my ($bg, $cont, $en) = ($1, $2, $3); | ||
| 221 | |||
| 222 | $output .= handle_text ($bg); | ||
| 223 | |||
| 224 | if ($cont =~ /^\w+/s) { | ||
| 225 | my ($tag, $attr); | ||
| 226 | if ($cont =~ /(\w*?)\s(.*)/s) { | ||
| 227 | ($tag, $attr) = ($1, " ".$2); | ||
| 228 | } | ||
| 229 | else { | ||
| 230 | ($tag, $attr) = ($cont, ""); | ||
| 231 | } | ||
| 232 | $output .= handle_start ($tag, $attr); | ||
| 233 | } | ||
| 234 | |||
| 235 | elsif ($cont =~ m#^/\w+#s) { | ||
| 236 | my ($tag, $attr); | ||
| 237 | if ($cont =~ /^\/(\w*?)\s(.*)/s) { | ||
| 238 | ($tag, $attr) = ($1, " ".$2); | ||
| 239 | } | ||
| 240 | else { | ||
| 241 | ($tag, $attr) = ($cont, ""); | ||
| 242 | $tag =~ s/^\///; | ||
| 243 | } | ||
| 244 | $output .= handle_end ($tag); | ||
| 245 | } | ||
| 246 | |||
| 247 | else { | ||
| 248 | $output .= handle_text ("<$cont>"); | ||
| 249 | } | ||
| 250 | |||
| 251 | $text = $en; | ||
| 252 | } | ||
| 253 | } | ||
| 254 | |||
| 255 | $output .= handle_text ($text) if defined $text; | ||
| 256 | |||
| 257 | my $x; | ||
| 258 | foreach $x (reverse @stack) { | ||
| 259 | $output .= "</$x>"; | ||
| 260 | } | ||
| 261 | |||
| 262 | return $output; | ||
| 263 | } | ||
| 264 | |||
| 265 | sub clean_file { | ||
| 266 | my $filename = shift; | ||
| 267 | my $version = shift; | ||
| 268 | my $options = shift; | ||
| 269 | |||
| 270 | $version = "1.0" unless $version; | ||
| 271 | |||
| 272 | open FILE, "<$filename" or die "Cannot open $filename for reading: $!"; | ||
| 273 | |||
| 274 | undef $/; | ||
| 275 | |||
| 276 | my $text = <FILE>; | ||
| 277 | |||
| 278 | close FILE or print "Cannot close $filename after reading from it: $!"; | ||
| 279 | |||
| 280 | $text = clean $text, $version, $options; | ||
| 281 | |||
| 282 | open FILE, ">$filename" or die "Cannot open $filename for writing: $!"; | ||
| 283 | |||
| 284 | print FILE $text; | ||
| 285 | |||
| 286 | close FILE or die "Cannot close $filename after writing to it: $!"; | ||
| 287 | } | ||
| 288 | |||
| 289 | 1; | ||
| 290 |