| 1 | # -*- mode: Perl; -*- | ||
| 2 | package NewsClipper::Types; | ||
| 3 | |||
| 4 | # This package declares a set of built-in data types for News Clipper. It also | ||
| 5 | # defines a function MakeSubtype which can be used to create the right subtype | ||
| 6 | # relationship between existing data types and new ones. | ||
| 7 | |||
| 8 | use strict; | ||
| 9 | # For exporting of functions | ||
| 10 | use Exporter; | ||
| 11 | |||
| 12 | use vars qw( @ISA @EXPORT @EXPORT_OK $VERSION ); | ||
| 13 | |||
| 14 | @ISA = qw( Exporter ); | ||
| 15 | @EXPORT = qw( MakeSubtype Link URL Image Table Thread TypesMatch ); | ||
| 16 | @EXPORT_OK = qw( ValidateTypeSignature GetTypeSignature ConvertTypeToEnglish | ||
| 17 | NormalizeSignature); | ||
| 18 | |||
| 19 | $VERSION = 0.12; | ||
| 20 | |||
| 21 | use NewsClipper::Globals; | ||
| 22 | |||
| 23 | # ------------------------------------------------------------------------------ | ||
| 24 | |||
| 25 | # Used a lot in this package | ||
| 26 | *isa = \&UNIVERSAL::isa; | ||
| 27 | |||
| 28 | # ------------------------------------------------------------------------------ | ||
| 29 | |||
| 30 | # This routine can be used to make a type a subtype of another | ||
| 31 | |||
| 32 | sub MakeSubtype($$) | ||
| 33 | { | ||
| 34 | my $subType = shift @_; | ||
| 35 | my $baseType = shift @_; | ||
| 36 | |||
| 37 | die "You have to specify both a base type and a subtype for MakeSubtype.\n" | ||
| 38 | unless defined $subType && defined $baseType; | ||
| 39 | |||
| 40 | eval "package $subType; use vars qw(\@ISA); \@ISA = \"$baseType\";" | ||
| 41 | unless $baseType =~ /^(SCALAR|HASH|ARRAY)$/; | ||
| 42 | } | ||
| 43 | |||
| 44 | # ------------------------------------------------------------------------------ | ||
| 45 | |||
| 46 | NewsClipper::Types::MakeSubtype('Link','SCALAR'); | ||
| 47 | NewsClipper::Types::MakeSubtype('Image','SCALAR'); | ||
| 48 | NewsClipper::Types::MakeSubtype('URL','SCALAR'); | ||
| 49 | |||
| 50 | package Table; | ||
| 51 | |||
| 52 | package Thread; | ||
| 53 | |||
| 54 | # ------------------------------------------------------------------------------ | ||
| 55 | |||
| 56 | package NewsClipper::Types; | ||
| 57 | |||
| 58 | # Checks a type signature for well-formed-ness | ||
| 59 | |||
| 60 | sub ValidateTypeSignature($) | ||
| 61 | { | ||
| 62 | my $typeSignature = shift; | ||
| 63 | |||
| 64 | my $errors = ''; | ||
| 65 | |||
| 66 | # Ignore spaces | ||
| 67 | $typeSignature =~ s/ //g; | ||
| 68 | |||
| 69 | # Check matching parens | ||
| 70 | # Thanks Abigail! | ||
| 71 | # (Re: Match Parens - More Perlish? 2/2/99 comp.lang.perl.misc) | ||
| 72 | { | ||
| 73 | my $balanced = 1; | ||
| 74 | my $i; | ||
| 75 | foreach ($i = 0; $typeSignature =~ /([()])/g;) | ||
| 76 | { | ||
| 77 | $balanced = 0 if (($i += $1 eq '(' ? 1 : -1) < 0); | ||
| 78 | } | ||
| 79 | $balanced = 0 if $i; | ||
| 80 | |||
| 81 | $errors .= "Parentheses don't match.\n" unless $balanced; | ||
| 82 | } | ||
| 83 | |||
| 84 | # Check description immediately follows $, @, or % | ||
| 85 | $errors .= "Description \"$2\" can only follow a \$, \@, or \%.\n" | ||
| 86 | if $typeSignature =~ /(^|.)([^\@\%\$\&\|\(\)]+)/ && | ||
| 87 | $1 ne '@' && $1 ne '$' && $1 ne '%'; | ||
| 88 | |||
| 89 | # Check @, %, or $ doesn't follow the same | ||
| 90 | $errors .=<<" EOF" | ||
| 91 | "$2$3" can not follow "$1". You should probably put "$2$3" in parentheses. | ||
| 92 | EOF | ||
| 93 | if $typeSignature =~ /([\@\%\$])([\@\%\$])([^\@\%\$\|\(\)]*)/; | ||
| 94 | |||
| 95 | return $errors; | ||
| 96 | } | ||
| 97 | |||
| 98 | # ------------------------------------------------------------------------------ | ||
| 99 | |||
| 100 | # Makes implicit structure types explicit, and replaces () with <1< >1> to | ||
| 101 | # make matching parens apparent. | ||
| 102 | |||
| 103 | sub NormalizeSignature($) | ||
| 104 | { | ||
| 105 | my $typeSignature = shift; | ||
| 106 | |||
| 107 | # Make matching parens | ||
| 108 | { | ||
| 109 | my $i = 0; | ||
| 110 | while ($typeSignature =~ /\(([^()]*)\)/) | ||
| 111 | { | ||
| 112 | $i++; | ||
| 113 | $typeSignature = $` . "<$i<" . $1 . ">$i>" . $'; | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | # Make implicit structure names explicit | ||
| 118 | while ($typeSignature =~ /([\@\%\$])([^a-z]|$)/i) | ||
| 119 | { | ||
| 120 | my $tempString; | ||
| 121 | $tempString = "ARRAY" if $1 eq '@'; | ||
| 122 | $tempString = "HASH" if $1 eq '%'; | ||
| 123 | $tempString = "SCALAR" if $1 eq '$'; | ||
| 124 | $typeSignature = $` . $1 . $tempString . $2 . $'; | ||
| 125 | } | ||
| 126 | |||
| 127 | # Remove spaces | ||
| 128 | $typeSignature =~ s/ *([\@\$\%])/$1/g; | ||
| 129 | $typeSignature =~ s/ *\| */|/g; | ||
| 130 | |||
| 131 | return $typeSignature; | ||
| 132 | } | ||
| 133 | |||
| 134 | # ------------------------------------------------------------------------------ | ||
| 135 | |||
| 136 | # Undoes the work of NormalizeSignature, almost. (Spaces are lost.) | ||
| 137 | |||
| 138 | sub UnNormalizeSignature($) | ||
| 139 | { | ||
| 140 | my $typeSignature = shift; | ||
| 141 | |||
| 142 | # Remove matching parens | ||
| 143 | $typeSignature =~ s/<\d+</\(/g; | ||
| 144 | $typeSignature =~ s/>\d+>/\)/g; | ||
| 145 | |||
| 146 | # Make explicit structure names implicit | ||
| 147 | $typeSignature =~ s/\@ARRAY/\@/g; | ||
| 148 | $typeSignature =~ s/\%HASH/\%/g; | ||
| 149 | $typeSignature =~ s/\$SCALAR/\$/g; | ||
| 150 | |||
| 151 | return $typeSignature; | ||
| 152 | } | ||
| 153 | |||
| 154 | # ------------------------------------------------------------------------------ | ||
| 155 | |||
| 156 | # Given a reference to data, returns the type signature for it. The optional | ||
| 157 | # second argument tells the subroutine whether it should recurse deeper into | ||
| 158 | # the structure. Returns the empty string if the type signature can not be | ||
| 159 | # determined. | ||
| 160 | |||
| 161 | sub GetTypeSignature ($;$); | ||
| 162 | sub GetTypeSignature ($;$) | ||
| 163 | { | ||
| 164 | my $data = shift; | ||
| 165 | my $recurse = shift || 1; | ||
| 166 | |||
| 167 | my $signature = ''; | ||
| 168 | |||
| 169 | if (isa($data,'ARRAY')) | ||
| 170 | { | ||
| 171 | $signature .= '@'; | ||
| 172 | $signature .= ref $data unless ref $data eq 'ARRAY'; | ||
| 173 | |||
| 174 | if ($recurse) | ||
| 175 | { | ||
| 176 | my @containedTypes; | ||
| 177 | foreach my $item (@$data) | ||
| 178 | { | ||
| 179 | push @containedTypes, GetTypeSignature($item) if ref $item; | ||
| 180 | } | ||
| 181 | |||
| 182 | # Compress the types to the unique ones. | ||
| 183 | my %seen; | ||
| 184 | foreach my $type (@containedTypes) | ||
| 185 | { | ||
| 186 | $seen{$type} = 1; | ||
| 187 | } | ||
| 188 | @containedTypes = keys %seen; | ||
| 189 | |||
| 190 | local $" = '&'; | ||
| 191 | $signature .= "(@containedTypes)" if $#containedTypes != -1; | ||
| 192 | } | ||
| 193 | } | ||
| 194 | |||
| 195 | if (isa($data,'HASH')) | ||
| 196 | { | ||
| 197 | $signature .= '%'; | ||
| 198 | $signature .= ref $data unless ref $data eq 'HASH'; | ||
| 199 | |||
| 200 | if ($recurse) | ||
| 201 | { | ||
| 202 | my @containedTypes; | ||
| 203 | foreach my $item (keys %$data) | ||
| 204 | { | ||
| 205 | push @containedTypes, GetTypeSignature($data->{$item}) | ||
| 206 | if ref $data->{$item}; | ||
| 207 | } | ||
| 208 | |||
| 209 | # Compress the types to the unique ones. | ||
| 210 | my %seen; | ||
| 211 | foreach my $type (@containedTypes) | ||
| 212 | { | ||
| 213 | $seen{$type} = 1; | ||
| 214 | } | ||
| 215 | @containedTypes = keys %seen; | ||
| 216 | |||
| 217 | local $" = '&'; | ||
| 218 | $signature .= "(@containedTypes)" if $#containedTypes != -1; | ||
| 219 | } | ||
| 220 | } | ||
| 221 | |||
| 222 | if (isa($data,'SCALAR')) | ||
| 223 | { | ||
| 224 | $signature .= '$'; | ||
| 225 | $signature .= ref $data unless ref $data eq 'SCALAR'; | ||
| 226 | } | ||
| 227 | |||
| 228 | return $signature; | ||
| 229 | } | ||
| 230 | |||
| 231 | # ------------------------------------------------------------------------------ | ||
| 232 | |||
| 233 | # Converts a type signature to human-readable format. (Well, sort of.) | ||
| 234 | |||
| 235 | sub ConvertTypeToEnglish($) | ||
| 236 | { | ||
| 237 | my $typeSignature = shift; | ||
| 238 | |||
| 239 | return '<UNKNOWN>' unless defined $typeSignature; | ||
| 240 | |||
| 241 | # Change "@ARRAY" to "ARRAY", "$SCALAR" to "SCALAR", and | ||
| 242 | # "%HASH" to "HASH" | ||
| 243 | $typeSignature =~ s/\@ARRAY/'ARRAY'/g; | ||
| 244 | $typeSignature =~ s/\%HASH/'HASH'/g; | ||
| 245 | $typeSignature =~ s/\$SCALAR/'SCALAR'/g; | ||
| 246 | |||
| 247 | # Change "@thread" to "thread array", "$URL" to "URL scalar", and | ||
| 248 | # "%Slashdot" to "Slashdot hash" | ||
| 249 | $typeSignature =~ s/\@(\w+)/'$1 array'/g; | ||
| 250 | $typeSignature =~ s/\$(\w+)/'$1 scalar'/g; | ||
| 251 | $typeSignature =~ s/\%(\w+)/'$1 hash'/g; | ||
| 252 | |||
| 253 | # Change "@" to "array", "$" to "scalar", and "%" to "hash" | ||
| 254 | $typeSignature =~ s/\@/array/g; | ||
| 255 | $typeSignature =~ s/\$/scalar/g; | ||
| 256 | $typeSignature =~ s/\%/hash/g; | ||
| 257 | |||
| 258 | # Change "(" to " of (" | ||
| 259 | $typeSignature =~ s/\(/ of \(/g; | ||
| 260 | |||
| 261 | # Remove parens when they aren't needed | ||
| 262 | $typeSignature =~ s/\(([^\)\|\&]+)\)/$1/g; | ||
| 263 | |||
| 264 | # Change "|" to "or", and "&" to "and" | ||
| 265 | $typeSignature =~ s/ *\| */ or /g; | ||
| 266 | $typeSignature =~ s/ *& */ and /g; | ||
| 267 | |||
| 268 | return $typeSignature; | ||
| 269 | } | ||
| 270 | |||
| 271 | # ------------------------------------------------------------------------------ | ||
| 272 | |||
| 273 | # Determines if a given data item matches the given type signature. You have | ||
| 274 | # to normalize the type signature before calling this. | ||
| 275 | |||
| 276 | sub TypesMatch | ||
| 277 | { | ||
| 278 | my $data = shift; | ||
| 279 | my $typeSignature = shift; | ||
| 280 | |||
| 281 | $typeSignature = NormalizeSignature($typeSignature); | ||
| 282 | |||
| 283 | #print "Entering TypesMatch to compare the data structure ", | ||
| 284 | #GetTypeSignature($data)," against $typeSignature\n"; | ||
| 285 | |||
| 286 | # First check for ors | ||
| 287 | { | ||
| 288 | #print "Checking ors...\n"; | ||
| 289 | my $simplifiedSignature = $typeSignature; | ||
| 290 | |||
| 291 | while ($simplifiedSignature =~ /<(\d+)</) | ||
| 292 | { | ||
| 293 | $simplifiedSignature =~ s/<$1<.*>$1>//; | ||
| 294 | } | ||
| 295 | |||
| 296 | # Skip all this if there are no ors... | ||
| 297 | if ($simplifiedSignature =~ /\|/) | ||
| 298 | { | ||
| 299 | my @orStructures = split /\|/, $simplifiedSignature; | ||
| 300 | |||
| 301 | my $tempSignature = $typeSignature; | ||
| 302 | |||
| 303 | foreach my $orStructure (@orStructures) | ||
| 304 | { | ||
| 305 | #print "Extracting $orStructure from signature $tempSignature\n"; | ||
| 306 | my $orSignature; | ||
| 307 | $orStructure =~ s/([\$\%\@])/\\$1/g; | ||
| 308 | if ($tempSignature =~ /^$orStructure<(\d+)</) | ||
| 309 | { | ||
| 310 | my $level = $1; | ||
| 311 | ($orSignature) = $tempSignature =~ /^($orStructure<$level<.*>$level>)/; | ||
| 312 | $tempSignature =~ s/^$orStructure<$level<.*>$level>\|//; | ||
| 313 | } | ||
| 314 | else | ||
| 315 | { | ||
| 316 | ($orSignature) = $tempSignature =~ /^($orStructure)/; | ||
| 317 | $tempSignature =~ s/^$orStructure\|//; | ||
| 318 | } | ||
| 319 | #print "Extraction done. New signature: $tempSignature\n"; | ||
| 320 | |||
| 321 | #print "Checking data type ", GetTypeSignature($data), " against $orSignature ", | ||
| 322 | #"\n"; | ||
| 323 | if (TypesMatch($data,$orSignature)) | ||
| 324 | { | ||
| 325 | #print "Types ", GetTypeSignature($data)," and $typeSignature match!\n"; | ||
| 326 | return 1; | ||
| 327 | } | ||
| 328 | } | ||
| 329 | |||
| 330 | #print "Types don't match\n"; | ||
| 331 | return 0; | ||
| 332 | } | ||
| 333 | } | ||
| 334 | |||
| 335 | # Then check for ands | ||
| 336 | { | ||
| 337 | #print "Checking ands...\n"; | ||
| 338 | my $simplifiedSignature = $typeSignature; | ||
| 339 | |||
| 340 | while ($simplifiedSignature =~ /<(\d+)</) | ||
| 341 | { | ||
| 342 | $simplifiedSignature =~ s/<$1<.*>$1>//; | ||
| 343 | } | ||
| 344 | |||
| 345 | if ($simplifiedSignature =~ /\&/) | ||
| 346 | { | ||
| 347 | my @andStructures = split /\&/, $simplifiedSignature; | ||
| 348 | |||
| 349 | my $tempSignature = $typeSignature; | ||
| 350 | |||
| 351 | foreach my $andStructure (@andStructures) | ||
| 352 | { | ||
| 353 | #print "looking for $andStructure in $tempSignature\n"; | ||
| 354 | my $andSignature; | ||
| 355 | $andStructure =~ s/([\$\%\@])/\\$1/g; | ||
| 356 | if ($tempSignature =~ /^$andStructure<(\d+)</) | ||
| 357 | { | ||
| 358 | my $level = $1; | ||
| 359 | ($andSignature) = | ||
| 360 | $tempSignature =~ /^($andStructure<$level<.*>$level>)/; | ||
| 361 | $tempSignature =~ s/^$andStructure<$level<.*>$level>\&//; | ||
| 362 | } | ||
| 363 | else | ||
| 364 | { | ||
| 365 | ($andSignature) = $tempSignature =~ /^($andStructure)/; | ||
| 366 | $tempSignature =~ s/^$andStructure\&//; | ||
| 367 | } | ||
| 368 | |||
| 369 | unless (TypesMatch($data,$andSignature)) | ||
| 370 | { | ||
| 371 | #print "Types don't match\n"; | ||
| 372 | return 0; | ||
| 373 | } | ||
| 374 | } | ||
| 375 | |||
| 376 | #print "Types ", GetTypeSignature($data)," and $typeSignature match!\n"; | ||
| 377 | return 1; | ||
| 378 | } | ||
| 379 | } | ||
| 380 | |||
| 381 | # Finally deal with type signatures with no ors or ands | ||
| 382 | { | ||
| 383 | # First check that the top-level type matches | ||
| 384 | { | ||
| 385 | my ($topLevelType) = $typeSignature =~ /^.([\w\s]+)/; | ||
| 386 | #print "Checking toplevel types ", | ||
| 387 | #GetTypeSignature($data)," and $topLevelType\n"; | ||
| 388 | unless (isa($data, $topLevelType)) | ||
| 389 | { | ||
| 390 | #print "Types don't match\n"; | ||
| 391 | return 0; | ||
| 392 | } | ||
| 393 | } | ||
| 394 | |||
| 395 | #print "Toplevel types match. Checking internal structure...\n"; | ||
| 396 | |||
| 397 | # Now check that the internal structure matches | ||
| 398 | if ($typeSignature =~ /<(\d+)</) | ||
| 399 | { | ||
| 400 | # Do each array element if it's an array | ||
| 401 | if (isa($data,'ARRAY')) | ||
| 402 | { | ||
| 403 | my ($internalTypeSignature) = $typeSignature =~ /<$1<(.*)>$1>/; | ||
| 404 | |||
| 405 | foreach my $internalData (@$data) | ||
| 406 | { | ||
| 407 | unless (TypesMatch($internalData,$internalTypeSignature)) | ||
| 408 | { | ||
| 409 | #print "Types don't match\n"; | ||
| 410 | return 0; | ||
| 411 | } | ||
| 412 | } | ||
| 413 | |||
| 414 | #print "Types ", GetTypeSignature($data)," and $typeSignature match!\n"; | ||
| 415 | return 1; | ||
| 416 | } | ||
| 417 | |||
| 418 | # Do each hash value if it's a hash | ||
| 419 | if (isa($data,'HASH')) | ||
| 420 | { | ||
| 421 | my ($internalTypeSignature) = $typeSignature =~ /<$1<(.*)>$1>/; | ||
| 422 | |||
| 423 | foreach my $internalData (keys %$data) | ||
| 424 | { | ||
| 425 | unless (TypesMatch($data->{$internalData},$internalTypeSignature)) | ||
| 426 | { | ||
| 427 | #print "Types don't match\n"; | ||
| 428 | return 0; | ||
| 429 | } | ||
| 430 | } | ||
| 431 | |||
| 432 | #print "Types ", GetTypeSignature($data)," and $typeSignature match!\n"; | ||
| 433 | return 1; | ||
| 434 | } | ||
| 435 | } | ||
| 436 | |||
| 437 | # If the structure at this level matches, and all the internal structure | ||
| 438 | # matches, we win! | ||
| 439 | #print "Types ", GetTypeSignature($data)," and $typeSignature match!\n"; | ||
| 440 | return 1; | ||
| 441 | } | ||
| 442 | } | ||
| 443 | |||
| 444 | 1; |