Changeset 13192


Ignore:
Timestamp:
2006-10-30T14:05:28+13:00 (18 years ago)
Author:
kjdon
Message:

moved the doctype checking code into a function. implemented metadata_read cos we need to do the doctype check there as well so we don't block files unnecessarily

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl/perllib/plugins/XMLPlug.pm

    r13148 r13192  
    8787}
    8888
     89# the inheriting class must implement this method to tell whether to parse this doc type
     90sub get_doctype {
     91    my $self = shift(@_);
     92    die "The inheriting class must implement this method";
     93}
     94
     95
    8996sub apply_xslt
    9097{
     
    152159}
    153160
     161sub check_doctype {
     162    $self = shift (@_);
     163   
     164    my ($filename) = @_;
     165   
     166    if (open(XMLIN,"<$filename")) {
     167    my $doctype = $self->get_doctype();
     168    ## check whether the doctype has the same name as the root element tag
     169    while (defined (my $line = <XMLIN>)) {
     170        ## find the root element
     171        if ($line =~ /<(\w+)[\s>]/){
     172        my $root = $1;
     173        if ($root !~ $doctype){
     174            close(XMLIN);
     175            return 0;
     176        }
     177        else {
     178            close(XMLIN);
     179            return 1;
     180        }
     181        }
     182    }
     183    close(XMLIN);
     184    }
     185   
     186    return undef; # haven't found a valid line
     187   
     188}
     189
     190# because we are not just using process_exp to determine whether to process or not, we need to implement this too, so that a file can be passed down if we are not actually processing it
     191sub metadata_read {
     192    $self = shift (@_);
     193   
     194    my ($pluginfo, $base_dir, $file, $metadata, $extrametakeys, $extrametadata, $processor, $maxdocs, $gli) = @_;
     195 
     196    my $result = $self->SUPER::metadata_read($pluginfo, $base_dir, $file, $metadata, $extrametakeys, $extrametadata, $processor, $maxdocs, $gli);
     197
     198    if (defined $result) {
     199    # we think we are processing this, but check that we actually are
     200    my $filename = &util::filename_cat ($base_dir, $file) if $base_dir =~ /\w/;
     201
     202    if ($self->check_doctype($filename)) {
     203        return $result;
     204    }
     205    }
     206    return undef;
     207}
    154208
    155209sub read {
     
    162216    my ($block_status,$filename) = $self->read_block(@_);   
    163217    return $block_status if ((!defined $block_status) || ($block_status==0));
     218
     219    ## check the doctype to see whether we really want to process the file
     220    if (!$self->check_doctype($filename)) {
     221    # this file is not for us
     222    return undef;
     223    }
    164224
    165225    $file =~ s/^[\/\\]+//; # $file often begins with / so we'll tidy it up
     
    169229    $self->{'processor'} = $processor;
    170230    $self->{'metadata'} = $metadata;
    171 
    172 
    173     ##call the inheriting plugin's get_doctype method to decide whether to carry on the parsing
    174     if (open(XMLIN,"<$filename")) {
    175     my $doctype = $self->get_doctype();
    176     ## check whether the doctype have the same name as the root element tag
    177     while (defined (my $line = <XMLIN>)) {
    178         ## find the root element
    179          if ($line =~ /<(\w+)[\s>]/){
    180         my $root = $1;
    181         if ($root !~ $doctype){
    182             close(XMLIN);
    183             return undef;
    184         }
    185         else{
    186             close(XMLIN);
    187             last;
    188         }
    189         }
    190     }
    191 }
    192 
    193231
    194232    eval {
     
    302340    die "XMLPlug Cannot process XML document with DOCTYPE of $name";
    303341}
    304 
    305 # the inheriting class must implement this method to tell whether to parse this doc type
    306 sub get_doctype {
    307     my $self = shift(@_);
    308     die "The inheriting class must implement this method";
    309 }
    310 
    311342
    312343
Note: See TracChangeset for help on using the changeset viewer.