| 1 | package PDF::FDF::Simple; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 15 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 15 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 15 | ▶ | use vars qw($VERSION $deferred_result_FDF_OPTIONS); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 15 | ▶ | use Data::Dumper; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 15 | ▶ | use Parse::RecDescent; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 15 | ▶ | use IO::File; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 15 | ▶ | use base 'Class::Accessor::Fast'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 15 | PDF::FDF::Simple->mk_accessors(qw( | |||||||||||||||||||
| 13 | skip_undefined_fields | ||||||||||||||||||||
| 14 | filename | ||||||||||||||||||||
| 15 | content | ||||||||||||||||||||
| 16 | errmsg | ||||||||||||||||||||
| 17 | parser | ||||||||||||||||||||
| 18 | attribute_file | ||||||||||||||||||||
| 19 | attribute_ufile | ||||||||||||||||||||
| 20 | attribute_id | ||||||||||||||||||||
| 21 | )); | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | 15 | $VERSION = '0.21'; | |||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | #Parse::RecDescent environment variables: enable for Debugging | ||||||||||||||||||||
| 26 | #$::RD_TRACE = 1; | ||||||||||||||||||||
| 27 | #$::RD_HINT = 1; | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | 25 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=16.3
Pod
documented
| |||||||||||||||||||||
| 30 | 25 | my $class = shift; | |||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | 25 | my $parser; | |||||||||||||||||||
| 33 | 25 | ▶ | if ($ENV{PDF_FDF_SIMPLE_IGNORE_PRECOMPILED_GRAMMAR}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 34 | # use external grammar file | ||||||||||||||||||||
| 35 | 0 | require File::ShareDir; | |||||||||||||||||||
| 36 | 0 | my $grammar_file = File::ShareDir::module_file('PDF::FDF::Simple', 'grammar'); | |||||||||||||||||||
| 37 | 0 | ▶ | open GRAMMAR_FILE, $grammar_file or die "Cannot open grammar file ".$grammar_file; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 38 | 0 | local $/; | |||||||||||||||||||
| 39 | 0 | my $grammar = <GRAMMAR_FILE>; | |||||||||||||||||||
| 40 | 0 | $parser = Parse::RecDescent->new($grammar); | |||||||||||||||||||
| 41 | } else { | ||||||||||||||||||||
| 42 | # use precompiled grammar | ||||||||||||||||||||
| 43 | 25 | require PDF::FDF::Simple::Grammar; | |||||||||||||||||||
| 44 | 25 | $parser = new PDF::FDF::Simple::Grammar; | |||||||||||||||||||
| 45 | } | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | 25 | my %DEFAULTS = ( | |||||||||||||||||||
| 48 | errmsg => '', | ||||||||||||||||||||
| 49 | skip_undefined_fields => 0, | ||||||||||||||||||||
| 50 | parser => $parser | ||||||||||||||||||||
| 51 | ); | ||||||||||||||||||||
| 52 | # accept hashes or hash refs for backwards compatibility | ||||||||||||||||||||
| 53 | 25 | ▶ | my %ARGS = ref($_[0]) =~ /HASH/ ? %{$_[0]} : @_; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 54 | 25 | my $self = Class::Accessor::new($class, { %DEFAULTS, %ARGS }); | |||||||||||||||||||
| 55 | 25 | return $self; | |||||||||||||||||||
| 56 | } | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | 5 | ▶ | sub _fdf_header { | ||||||||||||||||||
|
Subroutine
_fdf_header: called CC=5 SCAR=16.1
| |||||||||||||||||||||
| 59 | 5 | my $self = shift; | |||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 5 | my $string = "%FDF-1.2\n\n1 0 obj\n<<\n/FDF << /Fields 2 0 R"; | |||||||||||||||||||
| 62 | # /F | ||||||||||||||||||||
| 63 | 5 | ▶ | if ($self->attribute_file){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 64 | 2 | $string .= "/F (".$self->attribute_file.")"; | |||||||||||||||||||
| 65 | } | ||||||||||||||||||||
| 66 | # /UF | ||||||||||||||||||||
| 67 | 5 | ▶ | if ($self->attribute_ufile){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 68 | 2 | $string .= "/UF (".$self->attribute_ufile.")"; | |||||||||||||||||||
| 69 | } | ||||||||||||||||||||
| 70 | # /ID | ||||||||||||||||||||
| 71 | 5 | ▶ | if ($self->attribute_id){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 72 | 2 | $string .= "/ID["; | |||||||||||||||||||
| 73 | 2 | $string .= $_ foreach @{$self->attribute_id}; | |||||||||||||||||||
| 74 | 2 | $string .= "]"; | |||||||||||||||||||
| 75 | } | ||||||||||||||||||||
| 76 | 5 | $string .= ">>\n>>\nendobj\n2 0 obj\n["; | |||||||||||||||||||
| 77 | 5 | return $string; | |||||||||||||||||||
| 78 | } | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 5 | ▶ | sub _fdf_footer { | ||||||||||||||||||
|
Subroutine
_fdf_footer: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 81 | 5 | my $self = shift; | |||||||||||||||||||
| 82 | 5 | return <<__EOT__; | |||||||||||||||||||
| 83 | ] | ||||||||||||||||||||
| 84 | endobj | ||||||||||||||||||||
| 85 | trailer | ||||||||||||||||||||
| 86 | << | ||||||||||||||||||||
| 87 | /Root 1 0 R | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | >> | ||||||||||||||||||||
| 90 | %%EOF | ||||||||||||||||||||
| 91 | __EOT__ | ||||||||||||||||||||
| 92 | } | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | 52 | ▶ | sub _quote { | ||||||||||||||||||
|
Subroutine
_quote: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 95 | 52 | my $self = shift; | |||||||||||||||||||
| 96 | 52 | my $str = shift; | |||||||||||||||||||
| 97 | 52 | $str =~ s,\\,\\\\,g; | |||||||||||||||||||
| 98 | 52 | $str =~ s,\(,\\(,g; | |||||||||||||||||||
| 99 | 52 | $str =~ s,\),\\),g; | |||||||||||||||||||
| 100 | 52 | $str =~ s,\n,\\r,gs; | |||||||||||||||||||
| 101 | 52 | return $str; | |||||||||||||||||||
| 102 | } | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | 52 | ▶ | sub _fdf_field_formatstr { | ||||||||||||||||||
|
Subroutine
_fdf_field_formatstr: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 105 | 52 | my $self = shift; | |||||||||||||||||||
| 106 | 52 | return "<< /T (%s) /V (%s) >>\n" | |||||||||||||||||||
| 107 | } | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | 5 | ▶ | sub as_string { | ||||||||||||||||||
|
Subroutine
as_string: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||
| 110 | 5 | my $self = shift; | |||||||||||||||||||
| 111 | 5 | my $fdf_string = $self->_fdf_header; | |||||||||||||||||||
| 112 | 5 | foreach (sort keys %{$self->content}) { | |||||||||||||||||||
| 113 | 53 | my $val = $self->content->{$_}; | |||||||||||||||||||
| 114 | 53 | ▶ | if (not defined $val) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 115 | 2 | ▶ | next if ($self->skip_undefined_fields); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 116 | 1 | $val = ''; | |||||||||||||||||||
| 117 | } | ||||||||||||||||||||
| 118 | 52 | $fdf_string .= sprintf ($self->_fdf_field_formatstr, | |||||||||||||||||||
| 119 | $_, | ||||||||||||||||||||
| 120 | $self->_quote($val)); | ||||||||||||||||||||
| 121 | } | ||||||||||||||||||||
| 122 | 5 | $fdf_string .= $self->_fdf_footer; | |||||||||||||||||||
| 123 | 5 | return $fdf_string; | |||||||||||||||||||
| 124 | } | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | 4 | ▶ | sub save { | ||||||||||||||||||
|
Subroutine
save: called CC=3 SCAR=12.3
Pod
documented
| |||||||||||||||||||||
| 127 | 4 | my $self = shift; | |||||||||||||||||||
| 128 | 4 | ▶ | my $filename = shift || $self->filename; | ||||||||||||||||||
|
Condition33%1/3
shift() || $self->filename
MC/DC0%0/2
shift() || $self->filename
shift()
$self->filename
Truth table33%1/3
A || B
A= shift()B= $self->filename
| |||||||||||||||||||||
| 129 | 4 | ▶ | open (F, "> ".$filename) or do { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 130 | 0 | $self->errmsg ('error: open file ' . $filename); | |||||||||||||||||||
| 131 | 0 | return 0; | |||||||||||||||||||
| 132 | }; | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | 4 | print F $self->as_string; | |||||||||||||||||||
| 135 | 4 | close (F); | |||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | 4 | $self->errmsg (''); | |||||||||||||||||||
| 138 | 4 | return 1; | |||||||||||||||||||
| 139 | } | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | 16 | ▶ | sub _read_fdf { | ||||||||||||||||||
|
Subroutine
_read_fdf: called CC=2 SCAR=7.2
| |||||||||||||||||||||
| 142 | 16 | my $self = shift; | |||||||||||||||||||
| 143 | 16 | my $filecontent; | |||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | # read file to be checked | ||||||||||||||||||||
| 146 | 16 | ▶ | unless (open FH, "< ".$self->filename) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 147 | 0 | $self->errmsg ('error: could not read file ' . $self->filename); | |||||||||||||||||||
| 148 | 0 | return undef; | |||||||||||||||||||
| 149 | } else { | ||||||||||||||||||||
| 150 | 16 | local $/; | |||||||||||||||||||
| 151 | 16 | $filecontent = <FH>; | |||||||||||||||||||
| 152 | } | ||||||||||||||||||||
| 153 | 16 | close FH; | |||||||||||||||||||
| 154 | 16 | $self->errmsg (''); | |||||||||||||||||||
| 155 | 16 | return $filecontent; | |||||||||||||||||||
| 156 | } | ||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | 17 | ▶ | sub _map_parser_output { | ||||||||||||||||||
|
Subroutine
_map_parser_output: called CC=4 SCAR=13.9
| |||||||||||||||||||||
| 159 | 17 | my $self = shift; | |||||||||||||||||||
| 160 | 17 | my $output = shift; | |||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | 17 | my $fdfcontent = {}; | |||||||||||||||||||
| 163 | 17 | foreach my $obj ( @$output ) { | |||||||||||||||||||
| 164 | 21 | foreach my $contentblock ( @$obj ) { | |||||||||||||||||||
| 165 | 699 | foreach my $keys (keys %$contentblock) { | |||||||||||||||||||
| 166 | 699 | $fdfcontent->{$keys} = $contentblock->{$keys}; | |||||||||||||||||||
| 167 | } | ||||||||||||||||||||
| 168 | } | ||||||||||||||||||||
| 169 | } | ||||||||||||||||||||
| 170 | 17 | return $fdfcontent; | |||||||||||||||||||
| 171 | } | ||||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | 17 | ▶ | sub load { | ||||||||||||||||||
|
Subroutine
load: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||||||||||||
| 174 | 17 | my $self = shift; | |||||||||||||||||||
| 175 | 17 | my $filecontent = shift; | |||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | # prepare content | ||||||||||||||||||||
| 178 | 17 | ▶ | unless ($filecontent) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 179 | 16 | $filecontent = $self->_read_fdf; | |||||||||||||||||||
| 180 | 16 | ▶ | return undef unless $filecontent; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 181 | } | ||||||||||||||||||||
| 182 | |||||||||||||||||||||
| 183 | # parse | ||||||||||||||||||||
| 184 | 17 | my $output; | |||||||||||||||||||
| 185 | { | ||||||||||||||||||||
| 186 | 0 | ▶ | local $SIG{'__WARN__'} = sub { warn $_[0] unless $_[0] =~ /Deep recursion on subroutine/ }; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=10.5
Branch: 1 branch
| |||||||||||||||||||||
| 187 | 17 | $output = $self->parser->startrule ($filecontent); | |||||||||||||||||||
| 188 | } | ||||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | # take over parser results | ||||||||||||||||||||
| 191 | 17 | $self->attribute_file ($PDF::FDF::Simple::deferred_result_FDF_OPTIONS->{F}); # /F | |||||||||||||||||||
| 192 | 17 | $self->attribute_ufile ($PDF::FDF::Simple::deferred_result_FDF_OPTIONS->{UF}); # /UF | |||||||||||||||||||
| 193 | 17 | $self->attribute_id ($PDF::FDF::Simple::deferred_result_FDF_OPTIONS->{ID}); # /ID | |||||||||||||||||||
| 194 | 17 | $self->content ($self->_map_parser_output ($output)); | |||||||||||||||||||
| 195 | 17 | ▶ | $self->errmsg ("Corrupt FDF file!\n") unless $self->content; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 196 | |||||||||||||||||||||
| 197 | 17 | return $self->content; | |||||||||||||||||||
| 198 | } | ||||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | 15 | 1; | |||||||||||||||||||