Parser.pm

filter: Statementstmt 95.3% 61 / 64 Branchbran 54.1% 13 / 24 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 13 / 13 Podpod 100% 4 / 4 totaltotal 83.6% 92 / 110 SCAR 27.9
CC
15
Cov
82%
open
12.4
next_object
5.0
_next
3.0
CRAP 16.2
« parse.pl ↑ Summary Object.pm »
1 package xDT::Parser;
2  
3 2 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use xDT::Record;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use xDT::RecordType;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use xDT::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =head1 NAME
12  
13 xDT::Parser - A Parser for xDT files.
14  
15 =head1 VERSION
16  
17 Version 1.06
18  
19 =cut
20  
21 2 our $VERSION = '1.06';
22  
23  
24 =head1 SYNOPSIS
25  
26 Can be used to open xdt files and strings, and to iterate over contained objects.
27  
28 use xDT::Parser;
29  
30 my $parser = xDT::Parser->new();
31 # or
32 my $parser = xDT::Parser->new(record_type_config => $config);
33 # or
34 my $parser = xDT::Parser->new(
35 record_type_config => xDT::Parser::build_config_from_xml($xml_file)
36 );
37 # or
38 my $parser = xDT::Parser->new(
39 record_type_config => JSON::Parser::read_json($json_file)
40 );
41  
42 # A record type configuration can be provided via xml file or arrayref and can be used to add
43 # metadata (like accessor string or labels) to each record type.
44  
45 $parser->open(file => $xdt_file); # read from file
46 # or
47 $parser->open(string => $xdt_string); # read from string
48  
49 while (my $object = $parser->next_object) { # iterate xdt objects
50 # ...
51 }
52  
53 $parser->close(); # close the file handle
54  
55 =head1 ATTRIBUTES
56  
57 =head2 fh
58  
59 FileHandle to the currently open file.
60  
61 =cut
62  
63 2 has 'fh' => (
64 is => 'rw',
65 isa => 'FileHandle',
66 documentation => q{The filehandle the parser will use to read xDT data.},
67 );
68  
69 =head2 record_type_config
70  
71 The C<RecordType> configurations.
72  
73 e.g.:
74  
75 [{
76 "id": "0201",
77 "length": "9",
78 "type": "num",
79 "accessor": "bsnr",
80 "labels": {
81 "en": "BSNR",
82 "de": "BSNR"
83 }
84 }]
85  
86 =cut
87  
88 2 has 'record_type_config' => (
89 is => 'rw',
90 isa => 'ArrayRef',
91 documentation => q{Contains configurations for record types.},
92 );
93  
94  
95 around BUILDARGS => sub {
96 3 ▶ my $orig = shift;
Subroutine __ANON__: called CC=2 SCAR=7.2
97 3 my $class = shift;
98  
99 3 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 0 3 3
100 0 return $class->$orig(record_type_config => $_[0]);
101 } else {
102 3 my %params = @_;
103 3 return $class->$orig(\%params);
104 }
105 2 };
106  
107 =head1 SUBROUTINES/METHODS
108  
109 =head2 open
110  
111 $parser->open(file => 'example.gdt');
112 $parser->open(string => $xdt_string);
113  
114 Open a file or string with the parser.
115 If both file and string are given, the string will be ignored.
116 More information about the file format can be found at L<http://search.cpan.org/dist/xDT-RecordType/>.
117  
118 =cut
119  
120 sub open {
121 3 ▶ my ($self, %args) = @_;
Subroutine open: called CC=7 SCAR=25.1
Pod documented
122  
123 3 my $file = $args{file};
124 3 my $string = $args{string};
125 3 my $fh;
126  
127 3 ▶ die 'Error: No file or string argument given to parse xDT.'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file or defined $string 0 3 3
Condition33%1/3
defined $file or defined $string
l!l&&r!l&&!r
030
MC/DC0%0/2
defined $file or defined $string
defined $file defined $string
Truth table33%1/3
A or B
A= defined $fileB= defined $string
ABresult
1X1
011
000
128 unless (defined $file or defined $string);
129  
130 3 ▶ if (defined $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file) { } 0 3 3
131 0 ▶ die "Error: Provided file '$file' does not exist or is not readable."
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 0 0
132 unless (-f $file);
133  
134 0 ▶ $fh = FileHandle->new($file, 'r')
Branch: 1 branch
BranchTrueFalseTotal
$fh = "FileHandle"->new($file, "r") or die "Error: Could not open file handle for '${file}'." 0 0 0
135 or die "Error: Could not open file handle for '$file'.";
136 } else {
137 3 ▶ $fh = FileHandle->new(\$string, 'r')
Branch: 1 branch
BranchTrueFalseTotal
$fh = "FileHandle"->new(\$string, "r") or die "Error: Could not open file handle for provided string." 0 3 3
138 or die 'Error: Could not open file handle for provided string.';
139 }
140  
141 3 $self->fh($fh);
142 }
143  
144 =head2 close
145  
146 Closes the parsers filehandle
147  
148 =cut
149  
150 sub close {
151 1 ▶ my $self = shift;
Subroutine close: called CC=1 SCAR=0.0
Pod documented
152  
153 1 close $self->fh;
154 }
155  
156 =head2 next_object
157  
158 Returns the next object from xDT.
159  
160 =cut
161  
162 sub next_object {
163 5 ▶ my $self = shift;
Subroutine next_object: called CC=5 SCAR=16.1
Pod documented
164 5 my @records;
165  
166 5 ▶ while (my $record = $self->_next()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $record = $self->_next) 36 5 41
167 36 push @records, $record;
168 36 ▶ last if ($record->is_object_end);
Branch: 1 branch
BranchTrueFalseTotal
if $record->is_object_end 0 36 36
169 }
170  
171 5 ▶ return undef unless (scalar @records);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @records 2 3 5
172  
173 3 my $object = xDT::Object->new();
174 3 foreach my $record (@records) {
175 36 $object->add_record($record);
176 }
177  
178 3 return $object;
179 }
180  
181 =head2 build_config_from_xml
182  
183 Extracts metadata for a given record type id from a XML config file, if a file was given.
184 Otherwise id and accessor are set to the given id and all other attributes are undef.
185  
186 XML::Simple must be installed in order to use this method.
187  
188 Format of the XML config file:
189  
190 <RecordTypes>
191 <RecordType id="theId" length="theLength" type="theType" accessor="theAccessor">
192 <label lang="en">TheEnglishLabel</label>
193 <label lang="de">TheGermanLabel</label>
194 <!-- more labels -->
195 </RecordType>
196 <!-- more record types -->
197 </RecordTypes>
198  
199 =cut
200  
201 sub build_config_from_xml {
202 1 ▶ my $file = shift;
Subroutine build_config_from_xml: called CC=2 SCAR=7.0
Pod documented
203  
204 1 ▶ return [] unless (length $file);
Branch: 1 branch
BranchTrueFalseTotal
unless length $file 0 1 1
205  
206 2 ▶ use XML::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
207 return XML::Simple->new(
208 KeyAttr => { label => 'lang' },
209 GroupTags => { labels => 'label' },
210 ContentKey => '-content',
211 1 )->XMLin($file)->{RecordType};
212 }
213  
214 sub _next {
215 41 ▶ my $self = shift;
Subroutine _next: called CC=3 SCAR=11.0
216 41 my $line;
217
218 41 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /^\s*$/ and do { do { $line = $self->fh->getline or return undef } } 0 36 36
219 41 ▶ $line = $self->fh->getline() or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$line = $self->fh->getline or return undef 5 36 41
220 } while ($line =~ /^\s*$/);
221  
222 36 my $record = xDT::Record->new($line);
223 36 $record->set_record_type(xDT::RecordType::build_from_arrayref(
224 substr($line, 3, 4),
225 $self->record_type_config,
226 ));
227  
228 36 return $record;
229 }
230  
231 =head1 AUTHOR
232  
233 Christoph Beger, C<< <christoph.beger at medizin.uni-leipzig.de> >>
234  
235 =cut
236  
237 2 __PACKAGE__->meta->make_immutable;
238  
239 2 1; # End of xDT::Parser
« parse.pl ↑ Summary Object.pm »