| 1 | # ---------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 2 | # © Copyright 2000-2011 by Alex Peeters [alex.peeters@citap.be] | ||||||||||||||||||||||||||||
| 3 | # ---------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 4 | # 2011/mm/dd, v3.002.003, package ASNMTAP::Asnmtap::Plugins::XML Object-Oriented Perl | ||||||||||||||||||||||||||||
| 5 | # ---------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | # Class name - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||||||||||||||||||||||||||||
| 8 | package ASNMTAP::Asnmtap::Plugins::XML; | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 57 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 57 | ▶ | use warnings; # Must be used in test mode only. This reduces a little process speed | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | #use diagnostics; # Must be used in test mode only. This reduces a lot of process speed | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 57 | ▶ | use Carp qw(cluck); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 57 | ▶ | use ASNMTAP::Asnmtap qw(%ERRORS %TYPE); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 57 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 57 | ▶ | use Exporter (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 57 | @ASNMTAP::Asnmtap::Plugins::XML::ISA = qw(Exporter ASNMTAP::Asnmtap); | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 57 | %ASNMTAP::Asnmtap::Plugins::XML::EXPORT_TAGS = ( ALL => [ qw(&extract_XML) ] ); | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 57 | @ASNMTAP::Asnmtap::Plugins::XML::EXPORT_OK = ( @{ $ASNMTAP::Asnmtap::Plugins::XML::EXPORT_TAGS{ALL} } ); | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 57 | $ASNMTAP::Asnmtap::Plugins::XML::VERSION = do { my @r = (q$Revision: 3.002.003$ =~ /\d+/g); sprintf "%d."."%03d" x $#r, @r }; | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | # Utility methods - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 26 | ▶ | sub extract_XML { | ||||||||||||||||||||||||||
|
Subroutine
extract_XML: called CC=29 SCAR=36.4
Pod
documented
| |||||||||||||||||||||||||||||
| 41 | 26 | my %defaults = ( asnmtapInherited => undef, | |||||||||||||||||||||||||||
| 42 | custom => undef, | ||||||||||||||||||||||||||||
| 43 | customArguments => undef, | ||||||||||||||||||||||||||||
| 44 | resultXML => undef, | ||||||||||||||||||||||||||||
| 45 | filenameXML => undef, | ||||||||||||||||||||||||||||
| 46 | headerXML => undef, | ||||||||||||||||||||||||||||
| 47 | footerXML => undef, | ||||||||||||||||||||||||||||
| 48 | validateDTD => 0, | ||||||||||||||||||||||||||||
| 49 | filenameDTD => ''); | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 26 | my %parms = (%defaults, @_); | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 26 | my $asnmtapInherited = $parms{asnmtapInherited}; | |||||||||||||||||||||||||||
| 54 | 0 | ▶ | unless ( defined $asnmtapInherited ) { cluck ( 'ASNMTAP::Asnmtap::Plugins::XML: asnmtapInherited missing' ); exit $ERRORS{UNKNOWN} } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 26 | ▶ | my $debug = $$asnmtapInherited->getOptionsValue ( 'debug' ) || 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
$$asnmtapInherited->getOptionsValue('debug') || 0
MC/DC0%0/1
$$asnmtapInherited->getOptionsValue('debug') || 0
$$asnmtapInherited->getOptionsValue('debug')
Truth table50%1/2
A
A= $$asnmtapInherited->getOptionsValue('debug')
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 26 | my $resultXML = $parms{resultXML}; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 26 | ▶ | if ( $debug >= 2 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 0 | ▶ | print 'ASNMTAP::Asnmtap::Plugins::XML: Result XML: ', "\n", $resultXML, "\n" if (defined $resultXML); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 0 | ▶ | print 'ASNMTAP::Asnmtap::Plugins::XML: Filename XML: ', $parms{filenameXML}, "\n" if ($parms{filenameXML}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 0 | ▶ | print 'ASNMTAP::Asnmtap::Plugins::XML: Header XML: ', $parms{headerXML}, "\n" if ($parms{headerXML}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 0 | ▶ | print 'ASNMTAP::Asnmtap::Plugins::XML: Footer XML: ', $parms{footerXML}, "\n" if ($parms{footerXML}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 0 | print 'ASNMTAP::Asnmtap::Plugins::XML: Validate DTD: ', $parms{validateDTD}, "\n"; | |||||||||||||||||||||||||||
| 66 | 0 | print 'ASNMTAP::Asnmtap::Plugins::XML: Filename DTD: ', $parms{filenameDTD}, "\n"; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 26 | ▶ | if ( $parms{filenameXML} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 21 | ▶ | unless ( -s $parms{filenameXML} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 4 | $$asnmtapInherited->pluginValues ( { stateValue => $ERRORS{UNKNOWN}, error => "The XML file '". $parms{filenameXML} ."' doesn't exist" }, $TYPE{APPEND} ); | |||||||||||||||||||||||||||
| 72 | 4 | return ( $ERRORS{UNKNOWN} ); | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 17 | my $rvOpen = open ( XMLFILE, $parms{filenameXML} ); | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 17 | ▶ | if ( $rvOpen ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 17 | ▶ | while (<XMLFILE>) { $resultXML .= $_; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 17 | ▶ | print "ASNMTAP::Asnmtap::Plugins::XML: XML file\n", $resultXML if ( $debug ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 17 | close (XMLFILE); | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 22 | ▶ | unless ( defined $resultXML ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 1 | $$asnmtapInherited->pluginValues ( { stateValue => $ERRORS{UNKNOWN}, error => 'Missing resultXML and/or filenameXML' }, $TYPE{APPEND} ); | |||||||||||||||||||||||||||
| 86 | 1 | return ( $ERRORS{UNKNOWN} ); | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 21 | ▶ | unless ( defined $parms{headerXML} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 1 | $$asnmtapInherited->pluginValues ( { stateValue => $ERRORS{UNKNOWN}, error => 'Missing XML HEADER' }, $TYPE{APPEND} ); | |||||||||||||||||||||||||||
| 91 | 1 | return ( $ERRORS{UNKNOWN} ); | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 20 | ▶ | unless ( defined $parms{footerXML} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 1 | $$asnmtapInherited->pluginValues ( { stateValue => $ERRORS{UNKNOWN}, error => 'Missing XML FOOTER' }, $TYPE{APPEND} ); | |||||||||||||||||||||||||||
| 96 | 1 | return ( $ERRORS{UNKNOWN} ); | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 19 | ▶ | unless ( $parms{validateDTD} =~ /^[01]$/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 1 | $$asnmtapInherited->pluginValues ( { stateValue => $ERRORS{UNKNOWN}, error => 'Parameter validateDTD must be 0 or 1' }, $TYPE{APPEND} ); | |||||||||||||||||||||||||||
| 101 | 1 | return ( $ERRORS{UNKNOWN} ); | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 18 | my $pos = index $resultXML, $parms{headerXML}; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 18 | ▶ | if ( $pos == -1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 1 | $$asnmtapInherited->pluginValues ( { stateValue => $ERRORS{UNKNOWN}, error => 'Wrong XML HEADER' }, $TYPE{APPEND} ); | |||||||||||||||||||||||||||
| 108 | 1 | return ( $ERRORS{UNKNOWN} ); | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 17 | $resultXML = substr ( $resultXML, $pos ); | |||||||||||||||||||||||||||
| 112 | 17 | $pos = index $resultXML, $parms{footerXML}; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 17 | ▶ | if ( $pos == -1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 1 | $$asnmtapInherited->pluginValues ( { stateValue => $ERRORS{UNKNOWN}, error => 'Wrong XML FOOTER' }, $TYPE{APPEND} ); | |||||||||||||||||||||||||||
| 116 | 1 | return ( $ERRORS{UNKNOWN} ); | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 16 | my $len = length ( $parms{footerXML} ); | |||||||||||||||||||||||||||
| 120 | 16 | $resultXML = substr ($resultXML, 0, $pos + $len); | |||||||||||||||||||||||||||
| 121 | 16 | ▶ | print "\nASNMTAP::Asnmtap::Plugins::XML: <out>\n$resultXML\nASNMTAP::Asnmtap::Plugins::XML: </out>\n" if ($debug >= 2); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 16 | ▶ | if ( $parms{validateDTD} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 4 | ▶ | unless ( $parms{filenameDTD} and -s $parms{filenameDTD} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$parms{"filenameDTD"} and -s $parms{"filenameDTD"}
MC/DC50%1/2
$parms{"filenameDTD"} and -s $parms{"filenameDTD"}
$parms{"filenameDTD"}
-s $parms{"filenameDTD"}
Truth table66%2/3
A and B
A= $parms{"filenameDTD"}B= -s $parms{"filenameDTD"}
| |||||||||||||||||||||||||||||
| 125 | 1 | $$asnmtapInherited->pluginValues ( { stateValue => $ERRORS{UNKNOWN}, error => "The DTD file '". $parms{filenameDTD} ."' doesn't exist" }, $TYPE{APPEND} ); | |||||||||||||||||||||||||||
| 126 | 1 | return ( $ERRORS{UNKNOWN} ); | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 57 | ▶ | use XML::LibXML; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 130 | 3 | my $dtd = XML::LibXML::Dtd->new ( '', $parms{filenameDTD} ); | |||||||||||||||||||||||||||
| 131 | 3 | my $xml = XML::LibXML->new->parse_string ( $resultXML ); | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 3 | ▶ | unless ( $xml->is_valid ( $dtd ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 1 | ▶ | $xml->validate ( $dtd ) if ( $debug >= 2 ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 1 | $$asnmtapInherited->pluginValues ( { stateValue => $ERRORS{UNKNOWN}, error => "The XML doesn't validate" }, $TYPE{APPEND} ); | |||||||||||||||||||||||||||
| 136 | 1 | return ( $ERRORS{UNKNOWN} ); | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 57 | ▶ | use XML::Simple; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 141 | 14 | my $returnXML = XMLin( $resultXML ); | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 14 | ▶ | unless ( defined $returnXML ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 0 | $$asnmtapInherited->pluginValues ( { stateValue => $ERRORS{UNKNOWN}, error => "Error parsing XML formatted data" }, $TYPE{APPEND} ); | |||||||||||||||||||||||||||
| 145 | 0 | return ( $ERRORS{UNKNOWN} ); | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 14 | ▶ | if ( $debug >= 2 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | 57 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 150 | 0 | print "\nASNMTAP::Asnmtap::Plugins::XML: Start XML data dump\n", Dumper( $returnXML ), "\nASNMTAP::Asnmtap::Plugins::XML: End XML data dump\n"; | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 14 | my $returnCode = $ERRORS{OK}; | |||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 14 | ▶ | if ( defined $parms{custom} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 1 | ▶ | $returnCode = ( defined $parms{customArguments} ) ? $parms{custom}->($$asnmtapInherited, $returnXML, $parms{customArguments}) : $parms{custom}->($$asnmtapInherited, $returnXML); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 14 | return ( $returnCode, $returnXML ); | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 57 | 1; | |||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | __END__ | ||||||||||||||||||||||||||||