Commit daae6911 authored by Andrew Ayer's avatar Andrew Ayer

Add javadoc handler

For now it only handles standalone html files, not html files embedded
in a Jar file.
parent ad9d61af
......@@ -24,6 +24,7 @@ use warnings;
use StripNondeterminism::handlers::ar;
use StripNondeterminism::handlers::gzip;
use StripNondeterminism::handlers::jar;
use StripNondeterminism::handlers::javadoc;
use StripNondeterminism::handlers::zip;
our($VERSION);
......@@ -57,6 +58,10 @@ sub get_normalizer_for_file {
if (m/\.jar$/ && _get_file_type($_) =~ m/Zip archive data/) {
return \&StripNondeterminism::handlers::jar::normalize;
}
# javadoc
if (m/\.html$/ && StripNondeterminism::handlers::javadoc::is_javadoc_file($_)) {
return \&StripNondeterminism::handlers::javadoc::normalize;
}
# zip
if (m/\.zip$/ && _get_file_type($_) =~ m/Zip archive data/) {
return \&StripNondeterminism::handlers::zip::normalize;
......@@ -69,6 +74,7 @@ sub get_normalizer_by_name {
return \&StripNondeterminism::handlers::ar::normalize if $_ eq 'ar';
return \&StripNondeterminism::handlers::gzip::normalize if $_ eq 'gzip';
return \&StripNondeterminism::handlers::jar::normalize if $_ eq 'jar';
return \&StripNondeterminism::handlers::javadoc::normalize if $_ eq 'javadoc';
return \&StripNondeterminism::handlers::zip::normalize if $_ eq 'zip';
return undef;
}
......
#
# Copyright 2014 Andrew Ayer
#
# This file is part of strip-nondeterminism.
#
# strip-nondeterminism is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# strip-nondeterminism is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with strip-nondeterminism. If not, see <http://www.gnu.org/licenses/>.
#
package StripNondeterminism::handlers::javadoc;
use strict;
use warnings;
use File::Temp qw/tempfile/;
use File::Basename;
sub is_javadoc_file {
my ($filename) = @_;
# If this is a javadoc file, '<!-- Generated by javadoc' should appear in first 1kb
my $fh;
my $str;
return open($fh, '<', $filename) && read($fh, $str, 1024) && $str =~ /\<!-- Generated by javadoc/;
}
sub normalize {
my ($filename) = @_;
open(my $fh, '<', $filename) or die "Unable to open $filename for reading: $!";
my ($out_fh, $out_filename) = tempfile(DIR => dirname($filename), UNLINK => 1);
# Strip the javadoc comment, which contains a timestamp.
# It should appear within first 10 lines.
while (defined(my $line = <$fh>) && $. <= 10) {
if ($line =~ /\<!-- Generated by javadoc .* --\>/) {
$line =~ s/\<!-- Generated by javadoc .* --\>//g;
print $out_fh $line unless $line =~ /^\s*$/; # elide lines that are now whitespace-only
# Copy through rest of file
my $bytes_read;
my $buf;
while ($bytes_read = read($fh, $buf, 4096)) {
print $out_fh $buf;
}
defined($bytes_read) or die "$filename: read failed: $!";
# Rename temporary file over the file
chmod((stat($fh))[2] & 07777, $out_filename);
rename($out_filename, $filename) or die "$filename: unable to overwrite: rename: $!";
last;
}
print $out_fh $line;
}
}
1;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment