MechanicalTurk/XMLParser.pm

filter: Statementstmt 88.8% 16 / 18 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.6% 22 / 24 CC 1 SCAR 0.5
CC
1
Cov
89%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« MechanicalTurk/RowData/SQLRowData.pm ↑ Summary MechanicalTurk.pm »
1 package Net::Amazon::MechanicalTurk::XMLParser;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Net::Amazon::MechanicalTurk::BaseObject;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Net::Amazon::MechanicalTurk::DataStructure;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 0 ▶use Net::Amazon::MechanicalTurk::ModuleUtil;
Subroutine BEGIN: called CC=1 SCAR=2.6
8 use Net::Amazon::MechanicalTurk::IOUtil;
9 use IO::File;
10  
11 our $VERSION = '1.00';
12  
13 our @ISA = qw{ Net::Amazon::MechanicalTurk::BaseObject };
14 our @XML_MODULES = qw{
15 XML::Parser
16 XML::Parser::Lite
17 };
18  
19 Net::Amazon::MechanicalTurk::XMLParser->attributes(qw{
20 parser
21 });
22  
23 sub init {
24 my $self = shift;
25 if ($#_ >= 0) {
26 $self->parser(shift);
27 }
28 else {
29 $self->parser(newParser());
30 }
31 }
32  
33 sub parseURL {
34 my ($self, $url) = @_;
35 require LWP::UserAgent;
36 my $userAgent = LWP::UserAgent->new(ssl_opts => {verify_hostname => 1});
37 # Not available on all LWP's
38 #$userAgent->default_headers->push_header("Connection" => "close");
39 my $response = $userAgent->get($url);
40 if (!$response->is_success) {
41 Carp::croak("Could not retrieve url $url - " . $response->status_line);
42 }
43 return $self->parse($response->content);
44 }
45  
46 sub parseFile {
47 my ($self, $file) = @_;
48 my $in = IO::File->new($file, "r");
49 if (!$in) {
50 Carp::croak("Could not open file $file - $!");
51 }
52 return $self->parse($in);
53 }
54  
55 sub parse {
56 my ($self, $xml) = @_;
57  
58 if (UNIVERSAL::isa($xml, "GLOB")) {
59 $xml = Net::Amazon::MechanicalTurk::IOUtil->readContents($xml);
60 }
61  
62 my $context = { root => undef, rootElement => undef, stack => [] };
63 my $parser = $self->newParser();
64 $parser->setHandlers(
65 Start => sub { $self->xmlOnStart($context, @_); },
66 End => sub { $self->xmlOnEnd($context, @_); },
67 Char => sub { $self->xmlOnChar($context, @_); }
68 );
69  
70 $parser->parse($xml);
71 my $data = Net::Amazon::MechanicalTurk::DataStructure->wrap(xmlCondenseText($context->{root}));
72  
73 return (wantarray) ? ($data, $context->{rootElement}) : $data;
74 }
75  
76 sub newParser {
77 return Net::Amazon::MechanicalTurk::ModuleUtil->requireFirst(@XML_MODULES)->new;
78 }
79  
80 sub xmlOnStart {
81 my $self = shift;
82 my $context = shift;
83 my $parser = shift;
84 my $element = shift;
85 my %attrs = @_;
86
87 my $stack = $context->{stack};
88  
89 my $node = {};
90 if ($#${stack} >= 0) {
91 my $parent = $stack->[$#{$stack}];
92 if (!exists $parent->{$element}) {
93 $parent->{$element} = [];
94 }
95 push(@{$parent->{$element}}, $node);
96 push(@{$stack}, $node);
97 }
98 else {
99 $context->{root} = $node;
100 $context->{rootElement} = $element;
101 push(@{$stack}, $node);
102 }
103  
104 if (%attrs) {
105 while (my ($name,$value) = each %attrs) {
106 $self->xmlOnStart($context, $parser, $name);
107 $self->xmlOnChar($context, $parser, $value);
108 $self->xmlOnEnd($context, $parser, $name);
109 }
110 }
111 }
112  
113 sub xmlOnChar {
114 my ($self, $context, $parser, $text) = @_;
115 my $parent = $context->{stack}[$#{$context->{stack}}];
116 if (!exists $parent->{_value}) {
117 $parent->{_value} = $text;
118 }
119 else {
120 $parent->{_value} .= $text;
121 }
122 }
123  
124 sub xmlOnEnd {
125 my ($self, $context, $parser, $element) = @_;
126 pop(@{$context->{stack}});
127 }
128  
129 sub xmlCondenseText {
130 my ($node) = @_;
131  
132 return unless defined ($node);
133  
134 while (my ($name, $array) = each(%$node)) {
135 if ($name eq "_value") {
136 if ($array =~ /^\s*$/) {
137 delete $node->{$name};
138 }
139 next;
140 }
141  
142 next unless UNIVERSAL::isa($array, "ARRAY");
143  
144 for (my $i=0; $i<=$#{$array}; $i++) {
145 my $subNode = $array->[$i];
146 if (UNIVERSAL::isa($subNode, 'HASH')) {
147 if (exists $subNode->{_value} and $subNode->{_value} =~ /^\s*$/) {
148 delete $subNode->{_value};
149 }
150 if (exists $subNode->{_value} and (scalar keys %$subNode) == 1) {
151 $array->[$i] = $subNode->{_value};
152 }
153 elsif ((scalar keys %$subNode) == 0) {
154 $array->[$i] = undef;
155 }
156 else {
157 xmlCondenseText($subNode);
158 }
159 }
160 }
161 }
162  
163 return $node;
164 }
165  
166 return 1;
« MechanicalTurk/RowData/SQLRowData.pm ↑ Summary MechanicalTurk.pm »