2007년 5월 8일 화요일

1/3 두 개의 파일 비교 web service

# 모듈 작성 부분
package WebSemDiff;

use strict;
use CGI::XMLApplication;
use XML::SemanticDiff;
use XML::LibXML::SAX::Builder;
use XML::Generator::PerlData;

use vars qw( @ISA );
@ISA = qw( CGI::XMLApplication );


# 메서드 추가 부분
sub selectStylesheet {
    my ( $self, $context ) = @_;
    my $style = $context->{style} || 'default';
    my $style_path = '/www/site/stylesheets/';
    return $style_path . 'semdiff_' . $style . '.xsl';
}

# XML::LibXML::Document object의 확장

sub getDOM {
    my ( $self, $context ) = @_;
    return $context->{domtree};
}

#
sub getXSLParameter {
    my $self = shift;
    return $self->Vars;
}


# event registration and event callbacks
sub registerEvents {
    return qw( semdiff_result );
}

sub event_semdiff_result {
    my ( $self, $context ) = @_;
    my ( $file1, $file2, $error );
    my $fh1 = $self->upload('file1');
    my $fh2 = $self->upload('file2');
    $context->{style} = 'result';


    if ( defined( $fh1 ) and defined( $fh2 ) ) {
        local $/ = undef;
        $file1 = <$fh1>
        $file2 = <$fh2>;

    eval {
            $context->{domtree} = $self->compare_as_dom( $file1, $file2 );
        };

        if ( $@ ) {
            $error = $@;
        }
    }
    else {
        $error = 'You must select two XML files to compare and wait for them to finish uploading';
    }

    if ( $error ) {
        $context->{domtree} =  $self->dom_from_data( {  error => $error } );
    }
   

    unless ( defined( $context->{domtree} )) {
        my $msg = "Files are semantically identical.";
        $context->{domtree} =  $self->dom_from_data( {  message => $msg } );
    }
}



sub compare {
    my $self = shift;
    my ( $xmlstring1, $xmlstring2 ) = @_;
    my $diff = XML::SemanticDiff->new( keeplinenums => 1 );
    my @results = $diff->compare( $xmlstring1, $xmlstring2 );
    return \@results;
}

댓글 없음:

댓글 쓰기