| 1 | 2 | ▶ | use utf8; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 2 | package Document::OOXML; | ||||||||||||||||||||
| 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: Manipulation of Office Open XML files | ||||||||||||||||||||
| 7 | 2 | our $VERSION = '0.181410'; # VERSION | |||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 2 | ▶ | use Archive::Zip qw( :ERROR_CODES :CONSTANTS ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 2 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 2 | ▶ | use XML::LibXML; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 2 | ▶ | use Document::OOXML::ContentTypes; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 2 | ▶ | use Document::OOXML::Document::Wordprocessor; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 2 | ▶ | use Document::OOXML::PartParser; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | 2 | ▶ | use Document::OOXML::Rels; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | 2 | my %ROOT_PART_REL_TYPES = ( | |||||||||||||||||||
| 20 | transitionalDocument => 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument', | ||||||||||||||||||||
| 21 | strictDocument => 'http://purl.oclc.org/ooxml/officeDocument/relationships/officeDocument', | ||||||||||||||||||||
| 22 | ); | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 10 | ▶ | sub read_document { | ||||||||||||||||||
|
Subroutine
read_document: called CC=8 SCAR=20.9
Pod
documented
| |||||||||||||||||||||
| 26 | 10 | my $class = shift; | |||||||||||||||||||
| 27 | 10 | my $filename = shift; | |||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | 10 | my $zip = Archive::Zip->new(); | |||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 10 | my $zip_status = $zip->read($filename); | |||||||||||||||||||
| 32 | 10 | ▶ | croak("Cannot read: $zip_status") unless $zip_status == AZ_OK; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 9 | my $content_types = do { | |||||||||||||||||||
| 35 | 9 | ▶ | my $ct_xml = $zip->contents('[Content_Types].xml') | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 36 | or croak("No member named '/[Content_Types].xml'. Is it OOXML?"); | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | 8 | Document::OOXML::ContentTypes->new_from_xml($ct_xml); | |||||||||||||||||||
| 39 | }; | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | 8 | ▶ | my $base_rels_data = $zip->contents('_rels/.rels') | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 42 | or croak("No member named '_rels/.rels' in document. Is it OOXML?"); | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | 8 | my $rels = Document::OOXML::Rels->new_from_xml($base_rels_data, ''); | |||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | # The "old"/transitional XML uses schemas.openxmlformats.org | ||||||||||||||||||||
| 47 | # "New"/ISO standard/strict XML uses purl.oclc.org/ooxml | ||||||||||||||||||||
| 48 | my %document_part_relation = %{ | ||||||||||||||||||||
| 49 | 8 | $rels->get_part_relation_by_type($ROOT_PART_REL_TYPES{transitionalDocument}) | |||||||||||||||||||
| 50 | || $rels->get_part_relation_by_type($ROOT_PART_REL_TYPES{strictDocument}) | ||||||||||||||||||||
| 51 | 8 | ▶ | }; | ||||||||||||||||||
|
Condition66%2/3
$rels->get_part_relation_by_type($ROOT_PART_REL_TYPES{'transitionalDocument'}) || $rels->get_part_relation_by_type($ROOT_PART_REL_TYPES{'strictDocument'})
MC/DC0%0/2
$rels->get_part_relation_by_type($ROOT_PART_REL_TYPES{'transitionalDocument'}) || $rels->get_part_relation_by_type($ROOT_PART_REL_TYPES{'strictDocument'})
$rels->get_part_relation_by_type($ROOT_PART_REL_TYPES{'transitionalDocument'})
$rels->get_part_relation_by_type($ROOT_PART_REL_TYPES{'strictDocument'})
Truth table66%2/3
A || B
A= $rels->get_part_relation_by_type($ROOT_PART_REL_TYPES{'transitionalDocument'})B= $rels->get_part_relation_by_type($ROOT_PART_REL_TYPES{'strictDocument'})
| |||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 8 | my $type = $document_part_relation{type}; | |||||||||||||||||||
| 54 | 8 | my $part_name = $document_part_relation{part_name}; | |||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | 8 | my $strict; | |||||||||||||||||||
| 57 | 8 | ▶ | if ($type eq $ROOT_PART_REL_TYPES{strictDocument}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 58 | 5 | $strict = 1; | |||||||||||||||||||
| 59 | } else { | ||||||||||||||||||||
| 60 | 3 | $strict = 0; | |||||||||||||||||||
| 61 | } | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 8 | ▶ | my $part_contents = $zip->contents($part_name) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 64 | or croak("No member named '$part_name' in document. Is it OOXML?"); | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | 8 | my $doc_part = Document::OOXML::PartParser->parse_part( | |||||||||||||||||||
| 67 | content_type => $content_types->get_content_type_for_part($part_name), | ||||||||||||||||||||
| 68 | contents => $part_contents, | ||||||||||||||||||||
| 69 | part_name => $part_name, | ||||||||||||||||||||
| 70 | is_strict => $strict, | ||||||||||||||||||||
| 71 | ); | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | 8 | my $document_class; | |||||||||||||||||||
| 74 | 8 | ▶ | if ($doc_part->isa('Document::OOXML::Part::WordprocessingML')) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 75 | 8 | $document_class = 'Document::OOXML::Document::Wordprocessor'; | |||||||||||||||||||
| 76 | } | ||||||||||||||||||||
| 77 | else { | ||||||||||||||||||||
| 78 | 0 | croak("Unsupported document type"); | |||||||||||||||||||
| 79 | } | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | 8 | my $ooxml = $document_class->new( | |||||||||||||||||||
| 82 | content_types => $content_types, | ||||||||||||||||||||
| 83 | filename => $filename, | ||||||||||||||||||||
| 84 | source => $zip, | ||||||||||||||||||||
| 85 | is_strict => $strict, | ||||||||||||||||||||
| 86 | ); | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | # Parts have weak references to the document they're in, so they don't | ||||||||||||||||||||
| 89 | # create reference loops. | ||||||||||||||||||||
| 90 | # | ||||||||||||||||||||
| 91 | # They can use this reference to find or add other parts (images, | ||||||||||||||||||||
| 92 | # headers, footers, etc.) referenced by the main document. | ||||||||||||||||||||
| 93 | 8 | $doc_part->document($ooxml); | |||||||||||||||||||
| 94 | 8 | $ooxml->set_document_part($doc_part); | |||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | 8 | return $ooxml; | |||||||||||||||||||
| 97 | } | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | 2 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | __END__ | ||||||||||||||||||||