| 1 | package OPC; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use feature 'say'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use utf8; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use File::Find; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use File::Basename; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use File::Copy; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Cwd; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Encode; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Archive::Zip qw/ :ERROR_CODES :CONSTANTS /; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use XML::LibXML; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use OPC::Root; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use OPC::Part; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | 1 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | OPC - API for low-level manipulations with packages in OPC format (ECMA-376 Part 2) | ||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | my $Package = eval{ OPC->new( '/path/to/opc/package' ) }; | ||||||||||||||||||||||||||||||||||||||||||
| 26 | if( $@ ) { | ||||||||||||||||||||||||||||||||||||||||||
| 27 | die "/path/to/opc/package is not a valid OPC package: $@"; | ||||||||||||||||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | # Get part by name | ||||||||||||||||||||||||||||||||||||||||||
| 31 | my $Part1 = $Package->Part(name => '/part/name'); | ||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | # Get root node | ||||||||||||||||||||||||||||||||||||||||||
| 34 | my $Root = $Package->Root; | ||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | # Get package related part with C<Type> 'http://my.own/custom/type' | ||||||||||||||||||||||||||||||||||||||||||
| 37 | my $Part2 = $Root->RelatedPart(type => 'http://my.own/custom/type'); | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | # Get list of parts related to some other part | ||||||||||||||||||||||||||||||||||||||||||
| 40 | my @PictureParts = $Part2>RelatedParts(type => 'http://my.own/type/for/pictures'); | ||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | See http://www.ecma-international.org/publications/standards/Ecma-376.htm | ||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | Litres.ru Team | ||||||||||||||||||||||||||||||||||||||||||
| 49 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | 1 | my $XC = XML::LibXML::XPathContext->new(); | |||||||||||||||||||||||||||||||||||||||||
| 52 | 1 | my %FB3Namespaces = ( | |||||||||||||||||||||||||||||||||||||||||
| 53 | opcr => 'http://schemas.openxmlformats.org/package/2006/relationships', | ||||||||||||||||||||||||||||||||||||||||||
| 54 | opcct => 'http://schemas.openxmlformats.org/package/2006/content-types', | ||||||||||||||||||||||||||||||||||||||||||
| 55 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 56 | 1 | $XC->registerNs( $_ => $FB3Namespaces{$_} ) for keys %FB3Namespaces; | |||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | sub new { | ||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | ▶ | my( $Class, $PackagePath ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | if( -d $PackagePath ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | # Got path to directory. That means it's unpacked OPC | ||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | return $Class->FromDir( $PackagePath ); | |||||||||||||||||||||||||||||||||||||||||
| 64 | } elsif( -f $PackagePath ) { | ||||||||||||||||||||||||||||||||||||||||||
| 65 | # Got path to file. Probably that means it's zipped OPC | ||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | return $Class->FromZip( $PackagePath ); | |||||||||||||||||||||||||||||||||||||||||
| 67 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | Carp::confess "Must specify path to zip package or directory. Path=[$PackagePath]"; | |||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | sub FromZip { | ||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | ▶ | my( $Class, $ZipFilePath ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
FromZip: not called CC=7 SCAR=37.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | my $Zip = Archive::Zip->new(); | |||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | my $ReadStatus = $Zip->read( $ZipFilePath ); | |||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | ▶ | unless( $ReadStatus == AZ_OK ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | die $ReadStatus == AZ_FORMAT_ERROR ? "$ZipFilePath is not a valid ZIP archive" : | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | "Failed to open ZIP archive $ZipFilePath"; | ||||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | # Проверка правильности метода сжатия (deflated или stored) | ||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | for my $Member ( $Zip->members ) { | |||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | ▶ | unless( grep $Member->compressionMethod == $_, COMPRESSION_STORED, | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | COMPRESSION_DEFLATED ) { | ||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | die 'Item "'. $Member->fileName .'" uses unsupported compression method. '. | |||||||||||||||||||||||||||||||||||||||||
| 89 | 'Need "stored" or "deflated"'; | ||||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | my( $ZipMemberNameByPartName, $PartNames ); | |||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | ZIP_MEMBER_NAME: for my $ZipMemberName ( $Zip->memberNames ) { | |||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | # Пытаемся получить имя части по имени элемента ZIP архива | ||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | my $PartName = do { | |||||||||||||||||||||||||||||||||||||||||
| 99 | 1 | ▶ | use bytes; # чтобы lc действовал только на ASCII | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | lc "/$ZipMemberName"; | |||||||||||||||||||||||||||||||||||||||||
| 101 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | # Если получившееся имя части не соответствует правилам, то пропускаем такой элемент | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | # Выполняем последнюю проверку на неэквивалетность ранее прочитанным именам частей | ||||||||||||||||||||||||||||||||||||||||||
| 106 | # и, если всё в порядке, запоминаем полученное имя части | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | ▶ | if( grep $PartName eq $_, @$PartNames ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | # найдены части с эквивалетным названием. Согласно [M1.12] такого быть не должно | ||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | die "There several zip items with part name '$PartName' (OPC M1.12 violation)"; | |||||||||||||||||||||||||||||||||||||||||
| 111 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | push @$PartNames, $PartName; | |||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | $ZipMemberNameByPartName->{ $PartName } = $ZipMemberName; | |||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | return bless({ | |||||||||||||||||||||||||||||||||||||||||
| 118 | _is_zip => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 119 | _physical => $Zip, | ||||||||||||||||||||||||||||||||||||||||||
| 120 | _physical_name_by_part_name => $ZipMemberNameByPartName, | ||||||||||||||||||||||||||||||||||||||||||
| 121 | _part_names => $PartNames, | ||||||||||||||||||||||||||||||||||||||||||
| 122 | }, $Class ); | ||||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | sub FromDir { | ||||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | ▶ | my( $Class, $DirPath ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
FromDir: not called CC=3 SCAR=22.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 127 | 0 | ▶ | if( !defined($DirPath) || !-d $DirPath ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $DirPath and -d $DirPath
MC/DC0%0/2
defined $DirPath and -d $DirPath
defined $DirPath
-d $DirPath
Truth table0%0/3
A and B
A= defined $DirPathB= -d $DirPath
| |||||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | Carp::confess 'Directory doesn\'nt exist'; | |||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | $DirPath = Cwd::abs_path( $DirPath ); | |||||||||||||||||||||||||||||||||||||||||
| 131 | # delete trailing slash from directory name | ||||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | $DirPath =~ s/\/$//g; | |||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | my $DirPathLength = length( $DirPath ); | |||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | my( $PhysicalNameByPartName, $PartNames ); | |||||||||||||||||||||||||||||||||||||||||
| 136 | File::Find::find( sub{ | ||||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | ▶ | if( -f ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=13.0
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | my $PartName = $File::Find::name; | |||||||||||||||||||||||||||||||||||||||||
| 139 | 0 | $PartName = substr $PartName, $DirPathLength; # delete directory name from part name | |||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | 0 | my $PartName = do { | |||||||||||||||||||||||||||||||||||||||||
| 142 | 1 | ▶ | use bytes; # for lc to be applied only to ASCII symbols | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | 0 | lc $PartName; | |||||||||||||||||||||||||||||||||||||||||
| 144 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||
| 146 | 0 | push @$PartNames, $PartName; | |||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | $PhysicalNameByPartName->{$PartName} = $File::Find::name; | |||||||||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||||||||||
| 149 | 0 | }, $DirPath ); | |||||||||||||||||||||||||||||||||||||||||
| 150 | 0 | return bless({ | |||||||||||||||||||||||||||||||||||||||||
| 151 | _is_zip => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 152 | _physical => $DirPath, | ||||||||||||||||||||||||||||||||||||||||||
| 153 | _physical_name_by_part_name => $PhysicalNameByPartName, | ||||||||||||||||||||||||||||||||||||||||||
| 154 | _part_names => $PartNames, | ||||||||||||||||||||||||||||||||||||||||||
| 155 | }, $Class ); | ||||||||||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | sub GetPhysicalContents { | ||||||||||||||||||||||||||||||||||||||||||
| 159 | 0 | ▶ | my( $self, $PhysicalName, %Param ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
GetPhysicalContents: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 160 | 0 | ▶ | my $IsBinary = exists $Param{binary} ? $Param{binary} : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | 0 | ▶ | if( $self->{_is_zip} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 163 | 0 | return $self->{_physical}->contents( $PhysicalName ); | |||||||||||||||||||||||||||||||||||||||||
| 164 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 165 | 0 | return do { | |||||||||||||||||||||||||||||||||||||||||
| 166 | 0 | ▶ | my $Layer = $IsBinary ? 'raw' : 'encoding(UTF-8)'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | 0 | open my $fh, "<:$Layer", $PhysicalName; | |||||||||||||||||||||||||||||||||||||||||
| 168 | 0 | local $/; | |||||||||||||||||||||||||||||||||||||||||
| 169 | 0 | <$fh>; | |||||||||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||
| 174 | sub SetPhysicalContents { | ||||||||||||||||||||||||||||||||||||||||||
| 175 | 0 | ▶ | my( $self, $PhysicalName, $NewContents, %Param ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
SetPhysicalContents: not called CC=11 SCAR=48.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 176 | 0 | ▶ | my $IsBinary = exists $Param{binary} ? $Param{binary} : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||
| 178 | 0 | ▶ | if( $self->{_is_zip} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 179 | 0 | $self->{_physical}->contents( $PhysicalName, $NewContents ); | |||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||
| 181 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 182 | 0 | ▶ | my $IsFileHandle = (ref($NewContents) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
ref $NewContents eq "GLOB" or UNIVERSAL::isa($NewContents, "GLOB") or UNIVERSAL::isa($NewContents, "IO::Handle")
ref $NewContents eq "GLOB" or UNIVERSAL::isa($NewContents, "GLOB")
MC/DC0%0/3
ref $NewContents eq "GLOB" or UNIVERSAL::isa($NewContents, "GLOB") or UNIVERSAL::isa($NewContents, "IO::Handle")
ref $NewContents eq "GLOB"
UNIVERSAL::isa($NewContents, "GLOB")
UNIVERSAL::isa($NewContents, "IO::Handle")
Truth table0%0/4
A or B or C
A= ref $NewContents eq "GLOB"B= UNIVERSAL::isa($NewContents, "GLOB")C= UNIVERSAL::isa($NewContents, "IO::Handle")
| |||||||||||||||||||||||||||||||||||||||||||
| 183 | ? (ref($NewContents) eq 'GLOB' | ||||||||||||||||||||||||||||||||||||||||||
| 184 | || UNIVERSAL::isa($NewContents, 'GLOB') | ||||||||||||||||||||||||||||||||||||||||||
| 185 | || UNIVERSAL::isa($NewContents, 'IO::Handle')) | ||||||||||||||||||||||||||||||||||||||||||
| 186 | : (ref(\$NewContents) eq 'GLOB')); | ||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||
| 188 | 0 | ▶ | if( $IsFileHandle ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 189 | # new contents was given as file handle (prefered way) | ||||||||||||||||||||||||||||||||||||||||||
| 190 | 0 | ▶ | File::Copy::copy $NewContents, $PhysicalName | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 191 | or die "Can't update contents of $PhysicalName: $!"; | ||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||
| 193 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 194 | # new contents was given as string | ||||||||||||||||||||||||||||||||||||||||||
| 195 | 0 | ▶ | my $Layer = $IsBinary ? 'raw' : 'encoding(UTF-8)'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 196 | 0 | ▶ | open my $fh, ">:$Layer", $PhysicalName | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 197 | or die "Can't open $PhysicalName for writing: $!"; | ||||||||||||||||||||||||||||||||||||||||||
| 198 | 0 | ▶ | print {$fh} $NewContents | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 199 | or die "Can't update contents of $PhysicalName: $!"; | ||||||||||||||||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||
| 204 | sub GetContentTypesXML { | ||||||||||||||||||||||||||||||||||||||||||
| 205 | 0 | ▶ | my( $self ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
GetContentTypesXML: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||
| 207 | my $PhysicalName = $self->{_is_zip} | ||||||||||||||||||||||||||||||||||||||||||
| 208 | ? '[Content_Types].xml' | ||||||||||||||||||||||||||||||||||||||||||
| 209 | 0 | ▶ | : $self->{_physical}.'/'.'[Content_Types].xml'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||
| 211 | 0 | return $self->GetPhysicalContents( $PhysicalName ); | |||||||||||||||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||
| 214 | sub PartContents { | ||||||||||||||||||||||||||||||||||||||||||
| 215 | 0 | ▶ | my( $self, $PartName ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
PartContents: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 216 | 0 | my $PhysicalName = $self->PhysicalNameByPartName( $PartName ); | |||||||||||||||||||||||||||||||||||||||||
| 217 | 0 | ▶ | if( $PhysicalName ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 218 | 0 | my $IsBinary = $self->IsBinary($PartName); | |||||||||||||||||||||||||||||||||||||||||
| 219 | 0 | return $self->GetPhysicalContents( $PhysicalName, binary => $IsBinary ); | |||||||||||||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||||||||||||
| 221 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||
| 224 | sub IsBinary { | ||||||||||||||||||||||||||||||||||||||||||
| 225 | 0 | ▶ | my ($self, $PartName) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
IsBinary: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 226 | 0 | my $ContentType = $self->PartContentType( $PartName ); | |||||||||||||||||||||||||||||||||||||||||
| 227 | 0 | ▶ | if( grep $ContentType eq $_, 'image/png', 'image/jpeg', 'image/gif' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 228 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||||||||||||||||
| 230 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||
| 233 | sub SetContents { | ||||||||||||||||||||||||||||||||||||||||||
| 234 | 0 | ▶ | my( $self, $PartName, $NewContents ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
SetContents: not called CC=8 SCAR=40.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 235 | 0 | my $PhysicalName = $self->PhysicalNameByPartName( $PartName ); | |||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||
| 237 | # there is no part with such a name - let's create one | ||||||||||||||||||||||||||||||||||||||||||
| 238 | 0 | ▶ | unless( $PhysicalName ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 239 | # first check part name doesn't include . or .. - here we need only full part names | ||||||||||||||||||||||||||||||||||||||||||
| 240 | 0 | ▶ | die "Can't set part contents. Name '$PartName' includes . or .." | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 241 | if $PartName =~ /(^|\/)(.|..)(\/|$)/; | ||||||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||||
| 243 | # then correct part name | ||||||||||||||||||||||||||||||||||||||||||
| 244 | 0 | $PartName = do { | |||||||||||||||||||||||||||||||||||||||||
| 245 | 1 | ▶ | use bytes; # for lc to only affect ASCII symbols | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 246 | 0 | lc $PartName; | |||||||||||||||||||||||||||||||||||||||||
| 247 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||
| 249 | # creating physical part name | ||||||||||||||||||||||||||||||||||||||||||
| 250 | 0 | ▶ | if( $self->{_is_zip} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 251 | 0 | ( $PhysicalName = $PartName ) =~ s:^\/::; # removing leading slash | |||||||||||||||||||||||||||||||||||||||||
| 252 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 253 | 0 | $PhysicalName = $self->{_physical}.$PartName; | |||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||
| 255 | # ensure related directory exists | ||||||||||||||||||||||||||||||||||||||||||
| 256 | 0 | my $DirectoryName = File::Basename::dirname( $PartName ); | |||||||||||||||||||||||||||||||||||||||||
| 257 | 0 | ▶ | unless( -d $self->{_physical}.$DirectoryName ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 258 | 0 | my $CurPath = $self->{_physical}; | |||||||||||||||||||||||||||||||||||||||||
| 259 | 0 | for (split /[\\\/]/,$DirectoryName){ | |||||||||||||||||||||||||||||||||||||||||
| 260 | 0 | $CurPath.="$_"; | |||||||||||||||||||||||||||||||||||||||||
| 261 | 0 | ▶ | unless (-d $CurPath){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 262 | 0 | ▶ | mkdir $CurPath || die "$CurPath: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$CurPath || die("${CurPath}: $!")
MC/DC0%0/1
$CurPath || die("${CurPath}: $!")
$CurPath
Truth table0%0/2
A
A= $CurPath
| |||||||||||||||||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||||||||||||||||
| 264 | 0 | $CurPath.='/'; | |||||||||||||||||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||||||||||||||||
| 268 | 0 | push @{$self->{_part_names}}, $PartName; | |||||||||||||||||||||||||||||||||||||||||
| 269 | 0 | $self->{_physical_name_by_part_name}->{$PartName} = $PhysicalName; | |||||||||||||||||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||
| 272 | 0 | my $IsBinary = $self->IsBinary($PartName); | |||||||||||||||||||||||||||||||||||||||||
| 273 | 0 | $self->SetPhysicalContents( $PhysicalName, $NewContents, binary => $IsBinary ); | |||||||||||||||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||||||
| 276 | sub PartContentType { | ||||||||||||||||||||||||||||||||||||||||||
| 277 | 0 | ▶ | my( $self, $PartName ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
PartContentType: not called CC=5 SCAR=27.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||||||
| 279 | 0 | $PartName = do { | |||||||||||||||||||||||||||||||||||||||||
| 280 | 1 | ▶ | use bytes; # A-Za-z are case insensitive | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 281 | 0 | lc $PartName; | |||||||||||||||||||||||||||||||||||||||||
| 282 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||
| 284 | 0 | ▶ | unless( exists $self->{_content_type_by_part_name} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 285 | 0 | my $CtXML = $self->GetContentTypesXML; | |||||||||||||||||||||||||||||||||||||||||
| 286 | 0 | ▶ | unless( $CtXML ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 287 | 0 | Carp::confess "Content Types part not found"; | |||||||||||||||||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||||||||||||||||
| 289 | 0 | my $CtDoc = XML::LibXML->new()->parse_string( $CtXML ); | |||||||||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||||||||
| 291 | 1 | ▶ | use bytes; # case insensitive a-zA-Z | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||||||
| 293 | my %CtByExtension = | ||||||||||||||||||||||||||||||||||||||||||
| 294 | 0 | map { lc( $_->getAttribute('Extension') ) => $_->getAttribute('ContentType') } | |||||||||||||||||||||||||||||||||||||||||
| 295 | $XC->findnodes( '/opcct:Types/opcct:Default', $CtDoc ); | ||||||||||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||||||||||
| 297 | my %CtByPartName = | ||||||||||||||||||||||||||||||||||||||||||
| 298 | 0 | map { lc( $_->getAttribute('PartName') ) => $_->getAttribute('ContentType') } | |||||||||||||||||||||||||||||||||||||||||
| 299 | $XC->findnodes( '/opcct:Types/opcct:Override', $CtDoc ); | ||||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||||
| 301 | 1 | ▶ | no bytes; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||||||
| 303 | 0 | $self->{_content_type_by_part_name} = {}; | |||||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||||||
| 305 | 0 | for my $EachPartName ( $self->PartNames ) { | |||||||||||||||||||||||||||||||||||||||||
| 306 | 0 | $EachPartName =~ /\.([^.]+)$/; | |||||||||||||||||||||||||||||||||||||||||
| 307 | 0 | my $Extension = $1; | |||||||||||||||||||||||||||||||||||||||||
| 308 | 0 | ▶ | if( defined $CtByPartName{ $EachPartName } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 309 | 0 | $self->{_content_type_by_part_name}->{$EachPartName} = $CtByPartName{ $EachPartName }; | |||||||||||||||||||||||||||||||||||||||||
| 310 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 311 | 0 | $self->{_content_type_by_part_name}->{$EachPartName} = $CtByExtension{ $Extension }; | |||||||||||||||||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||||||||
| 316 | 0 | return $self->{_content_type_by_part_name}->{$PartName}; | |||||||||||||||||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||
| 319 | sub PhysicalNameByPartName { | ||||||||||||||||||||||||||||||||||||||||||
| 320 | 0 | ▶ | my( $self, $PartName ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
PhysicalNameByPartName: not called CC=2 SCAR=11.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||||
| 322 | 0 | $PartName = do { | |||||||||||||||||||||||||||||||||||||||||
| 323 | 1 | ▶ | use bytes; # A-Za-z are case insensitive | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 324 | 0 | lc $PartName; | |||||||||||||||||||||||||||||||||||||||||
| 325 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||||||||||
| 327 | return | ||||||||||||||||||||||||||||||||||||||||||
| 328 | exists $self->{_physical_name_by_part_name}->{$PartName} | ||||||||||||||||||||||||||||||||||||||||||
| 329 | 0 | ▶ | ? $self->{_physical_name_by_part_name}->{$PartName} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 330 | : undef; | ||||||||||||||||||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||
| 333 | # DEPRECATED: alias for compatibility | ||||||||||||||||||||||||||||||||||||||||||
| 334 | 0 | ▶ | sub ZipMemberNameByPartName { return PhysicalNameByPartName( @_ ) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ZipMemberNameByPartName: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||
| 336 | sub PartNames { | ||||||||||||||||||||||||||||||||||||||||||
| 337 | 0 | ▶ | my( $self ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
PartNames: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 338 | 0 | return @{ $self->{_part_names} }; | |||||||||||||||||||||||||||||||||||||||||
| 339 | } | ||||||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||||||
| 341 | sub HasPart { | ||||||||||||||||||||||||||||||||||||||||||
| 342 | 0 | ▶ | my( $self, $PartName ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
HasPart: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||||||||
| 344 | # Check part existense by existense in physical package | ||||||||||||||||||||||||||||||||||||||||||
| 345 | 0 | return defined $self->PhysicalNameByPartName( $PartName ); | |||||||||||||||||||||||||||||||||||||||||
| 346 | } | ||||||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||||||
| 348 | # Retrieve relations from rels file (can retrieve relations by type of by id) | ||||||||||||||||||||||||||||||||||||||||||
| 349 | sub Relations { | ||||||||||||||||||||||||||||||||||||||||||
| 350 | 0 | ▶ | my( $self, $RelsPartName, %RelationParams ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Relations: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||||||||||
| 352 | # Get list of relation nodes | ||||||||||||||||||||||||||||||||||||||||||
| 353 | 0 | my $RelsDoc = XML::LibXML->load_xml( string => $self->PartContents( $RelsPartName )); | |||||||||||||||||||||||||||||||||||||||||
| 354 | 0 | my @RelationNodes = $self->RelationNodesFromDoc( $RelsDoc, %RelationParams ); | |||||||||||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||||||||||||
| 356 | 0 | my @Relations; | |||||||||||||||||||||||||||||||||||||||||
| 357 | 0 | for my $RelationNode ( @RelationNodes ) { | |||||||||||||||||||||||||||||||||||||||||
| 358 | 0 | my %Relation = map { $_ => $RelationNode->getAttribute($_) } 'Id', 'Target', 'Type'; | |||||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||||||
| 360 | # Calculate target absolute path | ||||||||||||||||||||||||||||||||||||||||||
| 361 | 0 | my( $Source ) = ( split '_rels/', $RelsPartName ); | |||||||||||||||||||||||||||||||||||||||||
| 362 | 0 | $Relation{TargetFullName} = FullPartNameFromRelative( $Relation{Target}, $Source ); | |||||||||||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||||||||||||
| 364 | 0 | push @Relations, \%Relation; | |||||||||||||||||||||||||||||||||||||||||
| 365 | } | ||||||||||||||||||||||||||||||||||||||||||
| 366 | 0 | return @Relations; | |||||||||||||||||||||||||||||||||||||||||
| 367 | } | ||||||||||||||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||||||||||||||
| 369 | sub RemoveRelations { | ||||||||||||||||||||||||||||||||||||||||||
| 370 | 0 | ▶ | my( $self, $RelsPartName, %RelationParams ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
RemoveRelations: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||||||||||||
| 372 | # Get list of relation nodes for removement | ||||||||||||||||||||||||||||||||||||||||||
| 373 | 0 | my $RelsDoc = XML::LibXML->load_xml( string => $self->PartContents( $RelsPartName )); | |||||||||||||||||||||||||||||||||||||||||
| 374 | 0 | my @RelationNodes = $self->RelationNodesFromDoc( $RelsDoc, %RelationParams ); | |||||||||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||||||||||
| 376 | # Remove if there is something to remove. Otherwise just feel fine | ||||||||||||||||||||||||||||||||||||||||||
| 377 | 0 | ▶ | if( @RelationNodes ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 378 | 0 | my( $RelsRoot ) = $XC->findnodes( '/opcr:Relationships', $RelsDoc ); | |||||||||||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||||||||||||
| 380 | 0 | for my $RelationNode ( @RelationNodes ) { | |||||||||||||||||||||||||||||||||||||||||
| 381 | 0 | $RelsRoot->removeChild( $RelationNode ); | |||||||||||||||||||||||||||||||||||||||||
| 382 | } | ||||||||||||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||||||||||||
| 384 | # update contents in file | ||||||||||||||||||||||||||||||||||||||||||
| 385 | 0 | my $RelsXML = Encode::decode_utf8( $RelsDoc->toString ); | |||||||||||||||||||||||||||||||||||||||||
| 386 | 0 | $self->SetContents( $RelsPartName, $RelsXML ); | |||||||||||||||||||||||||||||||||||||||||
| 387 | } | ||||||||||||||||||||||||||||||||||||||||||
| 388 | } | ||||||||||||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||||||||||||
| 390 | sub AddRelation { | ||||||||||||||||||||||||||||||||||||||||||
| 391 | 0 | ▶ | my( $self, $RelsPartName, %RelationParams ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
AddRelation: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||||||||||||
| 393 | 0 | my $RelsDoc = XML::LibXML->load_xml( string => $self->PartContents( $RelsPartName )); | |||||||||||||||||||||||||||||||||||||||||
| 394 | 0 | my( $RelsRoot ) = $XC->findnodes( '/opcr:Relationships', $RelsDoc ); | |||||||||||||||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||||||||||||||||
| 396 | 0 | my $NewRelationNode = $RelsDoc->createElement( 'Relationship' ); | |||||||||||||||||||||||||||||||||||||||||
| 397 | 0 | ▶ | $NewRelationNode->setAttribute( Type => $RelationParams{type} ) if $RelationParams{type}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 398 | 0 | ▶ | $NewRelationNode->setAttribute( Target => $RelationParams{target} ) if $RelationParams{target}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||||||||||||||||
| 400 | 0 | my @ExistingRelationIDs = | |||||||||||||||||||||||||||||||||||||||||
| 401 | map $_->nodeValue, | ||||||||||||||||||||||||||||||||||||||||||
| 402 | $XC->findnodes( 'opcr:Relationship/@Id', $RelsRoot ); | ||||||||||||||||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||||||||||||||||
| 404 | 0 | my $RelationID; | |||||||||||||||||||||||||||||||||||||||||
| 405 | 0 | ▶ | if( $RelationParams{id} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 406 | 0 | $RelationID = $RelationParams{id}; | |||||||||||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||||||||||||
| 408 | 0 | ▶ | if( grep $RelationID eq $_, @ExistingRelationIDs ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 409 | 0 | die "Can't add relation to $RelsPartName. Id '$RelationID' already exists" | |||||||||||||||||||||||||||||||||||||||||
| 410 | } | ||||||||||||||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||||||||||||
| 412 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||||||||||||||||
| 414 | 0 | my $i = 1; | |||||||||||||||||||||||||||||||||||||||||
| 415 | 0 | while( 1 ) { | |||||||||||||||||||||||||||||||||||||||||
| 416 | 0 | $RelationID = "rId$i"; | |||||||||||||||||||||||||||||||||||||||||
| 417 | 0 | ▶ | last unless grep $RelationID eq $_, @ExistingRelationIDs; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 418 | 0 | $i++; | |||||||||||||||||||||||||||||||||||||||||
| 419 | } | ||||||||||||||||||||||||||||||||||||||||||
| 420 | } | ||||||||||||||||||||||||||||||||||||||||||
| 421 | 0 | $NewRelationNode->setAttribute( Id => $RelationID ); | |||||||||||||||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||||||||||||||||
| 423 | 0 | $RelsRoot->appendChild( $NewRelationNode ); | |||||||||||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||||||||||||
| 425 | # update contents in file | ||||||||||||||||||||||||||||||||||||||||||
| 426 | 0 | my $RelsXML = Encode::decode_utf8( $RelsDoc->toString ); | |||||||||||||||||||||||||||||||||||||||||
| 427 | 0 | $self->SetContents( $RelsPartName, $RelsXML ); | |||||||||||||||||||||||||||||||||||||||||
| 428 | } | ||||||||||||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||||||||||||
| 430 | sub CreateRelationsID { | ||||||||||||||||||||||||||||||||||||||||||
| 431 | 0 | ▶ | my ( $self, $RelsPartName, %RelationParams ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
CreateRelationsID: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 432 | 0 | my $RelationID; | |||||||||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||||||||||
| 434 | 0 | my $RelsDoc = XML::LibXML->load_xml( string => $self->PartContents( $RelsPartName )); | |||||||||||||||||||||||||||||||||||||||||
| 435 | 0 | my ( $RelsRoot ) = $XC->findnodes( '/opcr:Relationships', $RelsDoc ); | |||||||||||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||||||||||
| 437 | 0 | my @ExistingRelationIDs = | |||||||||||||||||||||||||||||||||||||||||
| 438 | map $_->nodeValue, | ||||||||||||||||||||||||||||||||||||||||||
| 439 | $XC->findnodes( 'opcr:Relationship/@Id', $RelsRoot ); | ||||||||||||||||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||||||||||||||||
| 441 | 0 | my $i = 1; | |||||||||||||||||||||||||||||||||||||||||
| 442 | 0 | while( 1 ) { | |||||||||||||||||||||||||||||||||||||||||
| 443 | 0 | $RelationID = "rId$i"; | |||||||||||||||||||||||||||||||||||||||||
| 444 | 0 | ▶ | last unless grep $RelationID eq $_, @ExistingRelationIDs; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 445 | 0 | $i++; | |||||||||||||||||||||||||||||||||||||||||
| 446 | } | ||||||||||||||||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||||||||||||||||
| 448 | 0 | return $RelationID; | |||||||||||||||||||||||||||||||||||||||||
| 449 | } | ||||||||||||||||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||||||||||||||||
| 451 | # Supplementary method: return relations nodes with given parameters | ||||||||||||||||||||||||||||||||||||||||||
| 452 | sub RelationNodesFromDoc { | ||||||||||||||||||||||||||||||||||||||||||
| 453 | 0 | ▶ | my( $self, $RelsDoc, %RelationParams ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
RelationNodesFromDoc: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||||||||||||||||
| 455 | 0 | ▶ | if( !$RelationParams{id} && !$RelationParams{type} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$RelationParams{"id"} or $RelationParams{"type"}
MC/DC0%0/2
$RelationParams{"id"} or $RelationParams{"type"}
$RelationParams{"id"}
$RelationParams{"type"}
Truth table0%0/3
A or B
A= $RelationParams{"id"}B= $RelationParams{"type"}
| |||||||||||||||||||||||||||||||||||||||||||
| 456 | 0 | Carp::confess 'No relation parameters given'; | |||||||||||||||||||||||||||||||||||||||||
| 457 | } | ||||||||||||||||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||||||||||||||||
| 459 | 0 | my $Filter; | |||||||||||||||||||||||||||||||||||||||||
| 460 | 0 | ▶ | if( $RelationParams{type} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 461 | 0 | $Filter = '@Type="'.$RelationParams{type}.'"'; | |||||||||||||||||||||||||||||||||||||||||
| 462 | } | ||||||||||||||||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||||||||||||||||
| 464 | 0 | ▶ | if( $RelationParams{id} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 465 | 0 | $Filter = '@Id="'.$RelationParams{id}.'"'; | |||||||||||||||||||||||||||||||||||||||||
| 466 | } | ||||||||||||||||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||||||||||||||||
| 468 | 0 | return $XC->findnodes( | |||||||||||||||||||||||||||||||||||||||||
| 469 | "/opcr:Relationships/opcr:Relationship[$Filter]", | ||||||||||||||||||||||||||||||||||||||||||
| 470 | $RelsDoc ); | ||||||||||||||||||||||||||||||||||||||||||
| 471 | } | ||||||||||||||||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||||||||||||||||
| 473 | # Retrieve relations by type and return list of hashref | ||||||||||||||||||||||||||||||||||||||||||
| 474 | sub RelationsByType { | ||||||||||||||||||||||||||||||||||||||||||
| 475 | 0 | ▶ | my( $self, $RelsPartName, $RelType ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
RelationsByType: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 476 | 0 | return $self->Relations( $RelsPartName, type => $RelType ); | |||||||||||||||||||||||||||||||||||||||||
| 477 | } | ||||||||||||||||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||||||||||||||||
| 479 | # Извлекает связь заданного типа из rels части, что связь данного типа только одна, | ||||||||||||||||||||||||||||||||||||||||||
| 480 | # иначе выбрасывает фатальную ошибку | ||||||||||||||||||||||||||||||||||||||||||
| 481 | # Don't use RelationByType with $GetMulty param, use RelationsByType method instead | ||||||||||||||||||||||||||||||||||||||||||
| 482 | sub RelationByType { | ||||||||||||||||||||||||||||||||||||||||||
| 483 | 0 | ▶ | my( $self, $RelsPartName, $RelType, $GetMulty ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
RelationByType: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||||||||||||||||
| 485 | 0 | my @Relations = $self->RelationsByType( $RelsPartName, $RelType ); | |||||||||||||||||||||||||||||||||||||||||
| 486 | 0 | ▶ | if( !$GetMulty && @Relations > 1 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $GetMulty and @Relations > 1
MC/DC0%0/2
not $GetMulty and @Relations > 1
not $GetMulty
@Relations > 1
Truth table0%0/3
not(A) and B
A= not $GetMultyB= @Relations > 1
| |||||||||||||||||||||||||||||||||||||||||||
| 487 | 0 | die "Expected only one relation of type $RelType in $RelsPartName but found several"; | |||||||||||||||||||||||||||||||||||||||||
| 488 | } | ||||||||||||||||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||||||||||||||||
| 490 | 0 | ▶ | if ($GetMulty) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 491 | 0 | return @Relations; | |||||||||||||||||||||||||||||||||||||||||
| 492 | } elsif (@Relations) { | ||||||||||||||||||||||||||||||||||||||||||
| 493 | 0 | return %{$Relations[0]}; | |||||||||||||||||||||||||||||||||||||||||
| 494 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 495 | 0 | return (); | |||||||||||||||||||||||||||||||||||||||||
| 496 | } | ||||||||||||||||||||||||||||||||||||||||||
| 497 | } | ||||||||||||||||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||||||||||||||||
| 499 | # вспомогательная функция: получить полное имя части по относительной ссылке на неё | ||||||||||||||||||||||||||||||||||||||||||
| 500 | # и имени части, относительно которой ищем | ||||||||||||||||||||||||||||||||||||||||||
| 501 | sub FullPartNameFromRelative { | ||||||||||||||||||||||||||||||||||||||||||
| 502 | 0 | ▶ | my $Name = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
FullPartNameFromRelative: not called CC=6 SCAR=34.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 503 | 0 | my $Dir = shift; | |||||||||||||||||||||||||||||||||||||||||
| 504 | 0 | $Dir =~ s:/$::; # remove trailing slash | |||||||||||||||||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||||||||||||||||
| 506 | 0 | ▶ | my $FullName = ( $Name =~ m:^/: ) ? $Name : # в $Name - полный путь | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 507 | "$Dir/$Name"; # в $Name - относительная ссылка | ||||||||||||||||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||||||||||||||||
| 509 | 0 | $FullName =~ s:^/::; # remove leading slash | |||||||||||||||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||||||||||||||||
| 511 | 0 | $FullName = do{ | |||||||||||||||||||||||||||||||||||||||||
| 512 | 1 | ▶ | use bytes; # A-Za-z are case insensitive | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 513 | 0 | lc $FullName; | |||||||||||||||||||||||||||||||||||||||||
| 514 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||||||||||||||||
| 516 | # обрабатываем все . и .. в имени | ||||||||||||||||||||||||||||||||||||||||||
| 517 | 0 | my @CleanedSegments; | |||||||||||||||||||||||||||||||||||||||||
| 518 | 0 | my @OriginalSegments = split m:/:, $FullName; | |||||||||||||||||||||||||||||||||||||||||
| 519 | 0 | for my $Part ( @OriginalSegments ) { | |||||||||||||||||||||||||||||||||||||||||
| 520 | 0 | ▶ | if( $Part eq '.' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 521 | # просто пропускаем | ||||||||||||||||||||||||||||||||||||||||||
| 522 | } elsif( $Part eq '..' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 523 | 0 | ▶ | if( @CleanedSegments > 0 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 524 | 0 | pop @CleanedSegments; | |||||||||||||||||||||||||||||||||||||||||
| 525 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 526 | 0 | die "/$FullName part name is invalid, because it's pointing out of FB3 root"; | |||||||||||||||||||||||||||||||||||||||||
| 527 | } | ||||||||||||||||||||||||||||||||||||||||||
| 528 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 529 | 0 | push @CleanedSegments, $Part; | |||||||||||||||||||||||||||||||||||||||||
| 530 | } | ||||||||||||||||||||||||||||||||||||||||||
| 531 | } | ||||||||||||||||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||||||||||||||||
| 533 | 0 | return '/'.( join '/', @CleanedSegments ); | |||||||||||||||||||||||||||||||||||||||||
| 534 | } | ||||||||||||||||||||||||||||||||||||||||||
| 535 | |||||||||||||||||||||||||||||||||||||||||||
| 536 | sub Root { | ||||||||||||||||||||||||||||||||||||||||||
| 537 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Root: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||||||||||||||||
| 539 | 0 | return OPC::Root->new( package => $self ); | |||||||||||||||||||||||||||||||||||||||||
| 540 | } | ||||||||||||||||||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||||||||||||||||
| 542 | sub Part { | ||||||||||||||||||||||||||||||||||||||||||
| 543 | 0 | ▶ | my( $self, %Params ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Part: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 544 | 0 | my $PartName = $Params{name}; | |||||||||||||||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||||||||||||||||
| 546 | 0 | return OPC::Part->new( package => $self, name => $PartName ) | |||||||||||||||||||||||||||||||||||||||||
| 547 | } | ||||||||||||||||||||||||||||||||||||||||||
| 548 | |||||||||||||||||||||||||||||||||||||||||||
| 549 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||
| 550 | |||||||||||||||||||||||||||||||||||||||||||
| 551 | Copyright (C) 2018 Litres.ru | ||||||||||||||||||||||||||||||||||||||||||
| 552 | |||||||||||||||||||||||||||||||||||||||||||
| 553 | The GNU Lesser General Public License version 3.0 | ||||||||||||||||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||||||||||||||||
| 555 | OPC is free software: you can redistribute it and/or modify it | ||||||||||||||||||||||||||||||||||||||||||
| 556 | under the terms of the GNU Lesser General Public License as published by | ||||||||||||||||||||||||||||||||||||||||||
| 557 | the Free Software Foundation, either version 3.0 of the License. | ||||||||||||||||||||||||||||||||||||||||||
| 558 | |||||||||||||||||||||||||||||||||||||||||||
| 559 | OPC is distributed in the hope that it will be useful, but | ||||||||||||||||||||||||||||||||||||||||||
| 560 | WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||||||||||||||||||||||||||||||||||||||||||
| 561 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public | ||||||||||||||||||||||||||||||||||||||||||
| 562 | License for more details. | ||||||||||||||||||||||||||||||||||||||||||
| 563 | |||||||||||||||||||||||||||||||||||||||||||
| 564 | Full text of License L<http://www.gnu.org/licenses/lgpl-3.0.en.html>. | ||||||||||||||||||||||||||||||||||||||||||
| 565 | |||||||||||||||||||||||||||||||||||||||||||
| 566 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||||||||||||||||
| 568 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||