| 1 | 2 | ▶ | use utf8; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 2 | package Document::OOXML::Document; | ||||||||||||||
| 3 | 2 | ▶ | use Moose; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 2 | ▶ | use namespace::autoclean; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | |||||||||||||||
| 6 | # ABSTRACT: Base class for the different OOXML document types | ||||||||||||||
| 7 | |||||||||||||||
| 8 | 2 | ▶ | use Moose::Util::TypeConstraints qw(role_type); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 2 | ▶ | use Path::Tiny; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | |||||||||||||||
| 11 | |||||||||||||||
| 12 | 2 | has filename => ( | |||||||||||||
| 13 | is => 'ro', | ||||||||||||||
| 14 | isa => 'Str', | ||||||||||||||
| 15 | ); | ||||||||||||||
| 16 | |||||||||||||||
| 17 | 2 | has source => ( | |||||||||||||
| 18 | is => 'ro', | ||||||||||||||
| 19 | isa => 'Archive::Zip', | ||||||||||||||
| 20 | required => 1, | ||||||||||||||
| 21 | ); | ||||||||||||||
| 22 | |||||||||||||||
| 23 | 2 | has is_strict => ( | |||||||||||||
| 24 | is => 'ro', | ||||||||||||||
| 25 | isa => 'Bool', | ||||||||||||||
| 26 | required => 1, | ||||||||||||||
| 27 | ); | ||||||||||||||
| 28 | |||||||||||||||
| 29 | has rels => ( | ||||||||||||||
| 30 | is => 'ro', | ||||||||||||||
| 31 | isa => 'HashRef[Document::OOXML::Rels]', | ||||||||||||||
| 32 | 8 | ▶ | default => sub { {} }, | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 33 | 2 | ); | |||||||||||||
| 34 | |||||||||||||||
| 35 | 2 | has document_part => ( | |||||||||||||
| 36 | is => 'rw', | ||||||||||||||
| 37 | isa => role_type('Document::OOXML::Part'), | ||||||||||||||
| 38 | writer => 'set_document_part', | ||||||||||||||
| 39 | ); | ||||||||||||||
| 40 | |||||||||||||||
| 41 | has loaded_parts => ( | ||||||||||||||
| 42 | is => 'ro', | ||||||||||||||
| 43 | isa => 'HashRef[Document::OOXML::Part]', | ||||||||||||||
| 44 | 8 | ▶ | default => sub { {} }, | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 45 | 2 | ); | |||||||||||||
| 46 | |||||||||||||||
| 47 | 2 | has content_types => ( | |||||||||||||
| 48 | is => 'ro', | ||||||||||||||
| 49 | isa => 'Document::OOXML::ContentTypes', | ||||||||||||||
| 50 | required => 1, | ||||||||||||||
| 51 | ); | ||||||||||||||
| 52 | |||||||||||||||
| 53 | |||||||||||||||
| 54 | 24 | ▶ | sub get_part { | ||||||||||||
|
Subroutine
get_part: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||
| 55 | 24 | my $self = shift; | |||||||||||||
| 56 | 24 | my $part_name = shift; | |||||||||||||
| 57 | |||||||||||||||
| 58 | return $self->loaded_parts->{$part_name} | ||||||||||||||
| 59 | 24 | ▶ | if exists $self->loaded_parts->{$part_name}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 60 | |||||||||||||||
| 61 | 12 | my $content_type = $self->content_types->get_content_type_for_part($part_name); | |||||||||||||
| 62 | |||||||||||||||
| 63 | 12 | ▶ | my $part_contents = $self->source->contents($part_name) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 64 | or croak("No member named '$part_name' in document."); | ||||||||||||||
| 65 | |||||||||||||||
| 66 | 12 | my $part = Document::OOXML::PartParser->parse_part( | |||||||||||||
| 67 | content_type => $content_type, | ||||||||||||||
| 68 | contents => $part_contents, | ||||||||||||||
| 69 | part_name => $part_name, | ||||||||||||||
| 70 | is_strict => $self->is_strict, | ||||||||||||||
| 71 | ); | ||||||||||||||
| 72 | |||||||||||||||
| 73 | 12 | $part->document($self); | |||||||||||||
| 74 | 12 | $self->loaded_parts->{$part_name} = $part; | |||||||||||||
| 75 | |||||||||||||||
| 76 | 12 | return $part; | |||||||||||||
| 77 | } | ||||||||||||||
| 78 | |||||||||||||||
| 79 | |||||||||||||||
| 80 | 24 | ▶ | sub get_rels_for_part { | ||||||||||||
|
Subroutine
get_rels_for_part: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||
| 81 | 24 | my $self = shift; | |||||||||||||
| 82 | 24 | my $part_name = shift; | |||||||||||||
| 83 | |||||||||||||||
| 84 | 24 | my $path = path($part_name); | |||||||||||||
| 85 | |||||||||||||||
| 86 | 24 | my $dir = $path->parent->stringify; | |||||||||||||
| 87 | 24 | my $filename = $path->basename; | |||||||||||||
| 88 | 24 | my $rels_file = path($dir, "_rels", "${filename}.rels"); | |||||||||||||
| 89 | |||||||||||||||
| 90 | 24 | ▶ | return $self->rels->{$part_name} if exists $self->rels->{$part_name}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 91 | |||||||||||||||
| 92 | 4 | ▶ | my $rels_data = $self->source->contents($rels_file->stringify) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 93 | or return; # No relations | ||||||||||||||
| 94 | |||||||||||||||
| 95 | 4 | $self->rels->{$part_name} = Document::OOXML::Rels->new_from_xml($rels_data, $dir); | |||||||||||||
| 96 | |||||||||||||||
| 97 | 4 | return $self->rels->{$part_name}; | |||||||||||||
| 98 | } | ||||||||||||||
| 99 | |||||||||||||||
| 100 | |||||||||||||||
| 101 | 3 | ▶ | sub save_to_file { | ||||||||||||
|
Subroutine
save_to_file: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||
| 102 | 3 | my $self = shift; | |||||||||||||
| 103 | 3 | my $filename = shift; | |||||||||||||
| 104 | |||||||||||||||
| 105 | 3 | my $output = Archive::Zip->new(); | |||||||||||||
| 106 | |||||||||||||||
| 107 | 3 | for my $member ( $self->source->members() ) { | |||||||||||||
| 108 | 41 | ▶ | if ($member->fileName eq $self->document_part->part_name) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 109 | 3 | $output->addString( | |||||||||||||
| 110 | $self->document_part->to_string(), | ||||||||||||||
| 111 | $self->document_part->part_name, | ||||||||||||||
| 112 | ); | ||||||||||||||
| 113 | } elsif (exists $self->loaded_parts->{$member->fileName}) { | ||||||||||||||
| 114 | # This is how members will be marked for deletion internally: | ||||||||||||||
| 115 | 6 | ▶ | next if not defined $self->loaded_parts->{$member->fileName}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 116 | |||||||||||||||
| 117 | 6 | my $part = $self->loaded_parts->{$member->fileName}; | |||||||||||||
| 118 | 6 | $output->addString( | |||||||||||||
| 119 | $part->to_string(), | ||||||||||||||
| 120 | $part->part_name, | ||||||||||||||
| 121 | ); | ||||||||||||||
| 122 | } else { | ||||||||||||||
| 123 | # File not changed by us. Copy verbatim. | ||||||||||||||
| 124 | 32 | $output->addMember($member); | |||||||||||||
| 125 | } | ||||||||||||||
| 126 | } | ||||||||||||||
| 127 | |||||||||||||||
| 128 | 3 | $output->writeToFileNamed($filename); | |||||||||||||
| 129 | |||||||||||||||
| 130 | 3 | return; | |||||||||||||
| 131 | } | ||||||||||||||
| 132 | |||||||||||||||
| 133 | 2 | __PACKAGE__->meta->make_immutable(); | |||||||||||||
| 134 | |||||||||||||||
| 135 | __END__ | ||||||||||||||