JPFR/Taxonomy.pm untested

filter: Statementstmt 0.0% 0 / 212 Branchbran 0.0% 0 / 62 Conditioncond 0.0% 0 / 18 Subroutinesub 0.0% 0 / 23 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 326 CC 32 SCAR 32.6
CC
32
Cov
0%
select_arcs
72.0
trans_old_location
56.0
set
30.0
CRAP 26.0
1 package XBRL::JPFR::Taxonomy;
2  
3 our $VERSION = '0.01';
4  
5 use strict;
6 #use warnings;
7 use Carp;
8 use XML::LibXML;
9 use XML::LibXML::XPathContext;
10 use XML::LibXML::NodeList;
11 use XBRL::JPFR::Element;
12 use XBRL::JPFR::Label;
13 use XBRL::JPFR::Branch;
14 use Hash::Merge qw(merge);
15 use File::Basename qw(basename);
16 use Data::Dumper;
17  
18 use base qw(XBRL::Taxonomy);
19  
20 my @fields = qw(ref labelhash namespaces prohibiteds);
21 XBRL::JPFR::Taxonomy->mk_accessors(@fields);
22  
23  
24 my %default = (
25 'lang' => 'ja',
26 );
27  
28  
29 sub new() {
30 my ($class, $args) = @_;
31 $args = {} if !$args;
32 my $self = merge($args, \%default);
33 bless $self, $class;
34  
35 $$self{'namespaces'} = [];
36 if ($args->{'main_schema'}) {
37 $self->add_schema($$args{'main_schema'});
38 }
39 $$self{'main_schema'} = $$args{'main_schema'}->namespace();
40  
41 return $self;
42 }
43  
44 sub set {
45 my ($self, $key) = splice @_, 0, 2;
46 if ($key eq 'def' || $key eq 'pre' || $key eq 'cal' || $key eq 'lab' || $key eq 'ref') {
47 $$self{$key} = {} if !$$self{$key};
48 $$self{$key} = merge($$self{$key}, $_[0]);
49 }
50 elsif ($key eq 'prohibiteds') {
51 my ($type, $uri, $shorts) = @_;
52 my $proh = exists $$self{'prohibiteds'}{$type}{$uri} ?
53 $$self{'prohibiteds'}{$type}{$uri} : ($$self{'prohibiteds'}{$type}{$uri} = []);
54 push @$proh, @$shorts;
55 }
56 else {
57 $self->SUPER::set($key, @_);
58 }
59 }
60  
61 sub get_lb_files() {
62 my ($self) = @_;
63 my @out_array;
64 my $schemas = $$self{'schemas'};
65 foreach my $ns (@{$$self{'namespaces'}}) {
66 my $main_xpath = $$schemas{$ns}->xpath();
67 my $lbs = $main_xpath->findnodes("//*[local-name() = 'linkbaseRef']" );
68 for my $lb (@$lbs) {
69 my $lb_file = $lb->getAttribute('xlink:href');
70 if ($lb_file !~ /^http|^\//) {
71 my $namespace = $lb->getAttribute('namespace');
72 $namespace = $$schemas{$ns}->namespace() if !$namespace;
73 $lb_file = "$namespace/$lb_file";
74 }
75 trans_old_location(\$lb_file, $lb);
76 push(@out_array, $lb_file);
77 }
78 }
79 return \@out_array;
80 }
81  
82 sub get_other_schemas() {
83 my ($self, $ns) = @_;
84 my @out_array;
85 $ns = $$self{'main_schema'} if !$ns;
86 my $main_xpath = $$self{'schemas'}{$ns}->xpath();
87 my $other_schemas = $main_xpath->findnodes("//*[local-name() = 'import']");
88 for my $other (@$other_schemas) {
89 my $location_url = $other->getAttribute('schemaLocation');
90 if ($location_url !~ /^http|^\//) {
91 my $namespace = $other->getAttribute('namespace');
92 my $fn = basename $location_url;
93 $location_url = "$namespace/$fn";
94 }
95 trans_old_location(\$location_url, $other);
96 push(@out_array, $location_url) if $location_url;
97 }
98 return \@out_array;
99 }
100  
101 sub trans_old_location {
102 my ($loc, $node) = @_;
103 if ($$loc eq 'http://disclosure.edinet-fsa.go.jp/taxonomy/jppfs/2012/jppfs_pe/jppfs_pe_2012.xsd') {
104 $$loc = 'http://disclosure.edinet-fsa.go.jp/taxonomy/jppfs/2013-08-31/jppfs_pe_2012.xsd';
105 $node->setAttribute('xlink:href', $$loc);
106 }
107 if ($$loc eq 'http://www.w3.org/1999/xlink/xlink-2003-12-31.xsd') {
108 $$loc = 'http://www.xbrl.org/2003/xlink-2003-12-31.xsd';
109 $node->setAttribute('xlink:href', $$loc);
110 }
111 if ($$loc eq 'http://www.xbrl.org/2003/XLink/xl-2003-12-31.xsd') {
112 $$loc = 'http://www.xbrl.org/2003/xl-2003-12-31.xsd';
113 $node->setAttribute('xlink:href', $$loc);
114 }
115 if ($$loc eq 'http://www.xbrl.org/2003/linkbase/xbrl-linkbase-2003-12-31.xsd') {
116 $$loc = 'http://www.xbrl.org/2003/xbrl-linkbase-2003-12-31.xsd';
117 $node->setAttribute('xlink:href', $$loc);
118 }
119 if ($$loc =~ s/xbrl\.iasb\.org\/taxonomy\/ifrs_/xbrl.iasb.org\/taxonomy\//) {
120 $node->setAttribute('xlink:href', $$loc);
121 }
122 if ($$loc =~ s/http:\/\/info.edinet-fsa.go.jp\/ifrs\/gaap\/E.*?ifrs-/ifrs-/) {
123 $node->setAttribute('xlink:href', $$loc);
124 }
125 }
126  
127 sub has_schema {
128 my ($self, $file) = @_;
129 my $schemas = $$self{'schemas'};
130 foreach my $ns (keys %$schemas) {
131 my $s_file = $$schemas{$ns}->file();
132 return 1 if $file eq $s_file;
133 }
134 return 0;
135 }
136  
137 sub add_schema() {
138 my ($self, $schema) = @_;
139 my $ns = $schema->namespace();
140 push @{$$self{'namespaces'}}, $ns;
141 $$self{'schemas'}{$ns} = $schema;
142 my $element_nodes = $schema->xpath()->findnodes("//*[local-name() = 'element']");
143 for my $el_xml (@$element_nodes) {
144 my $e = XBRL::JPFR::Element->new($el_xml);
145 my $id = $e->id();
146 next if !$id;
147 my $id_full = "$ns/". $e->name();
148 $$self{'elements'}{$id} = $e;
149 $$schema{'elements'}{$id_full} = $e;
150 }
151 }
152  
153 # XBRL2.1: 3.5.3.9.7.4 and 3.5.3.9.7.5
154 sub select_arcs() {
155 my ($self) = @_;
156 foreach my $type ('pre', 'def', 'cal', 'lab', 'ref') {
157 my $all_arcs = $self->$type();
158 next unless $all_arcs;
159 foreach my $uri (keys %$all_arcs) {
160 my $arcs = $$all_arcs{$uri};
161 my (%pu, @dels);
162 for (my $i = 0 ; $i < @$arcs ; $i++) {
163 my $arc = $$arcs[$i];
164 my ($from, $to) = @$arc{'from_short', 'to_short'};
165 my ($arcrole, $pref) = @$arc{'arcrole', 'prefLabel'};
166 my ($ord, $pri, $use) = @$arc{'order', 'priority', 'use'};
167 $ord = sprintf "%g", $ord; # to delete trailing zero(s)
168 if (exists $pu{$from}{$to}{$arcrole}{$pref}{$ord}) {
169 my $pv = $pu{$from}{$to}{$arcrole}{$pref}{$ord};
170 if ($$pv{'pri'} > $pri) {
171 push @dels, $i;
172 next;
173 }
174 elsif ($$pv{'pri'} < $pri) {
175 push @dels, @{$$pv{'idx'}};
176 $$pv{'pri'} = $pri;
177 $$pv{'use'} = $use;
178 $$pv{'idx'} = [$i];
179 }
180 else {
181 $$pv{'use'} = $$pv{'use'} eq 'prohibited' || $use eq 'prohibited' ?
182 'prohibited' : 'optional';
183 $$pv{'pri'} = $pri;
184 push @{$$pv{'idx'}}, $i;
185 }
186 }
187 else {
188 $pu{$from}{$to}{$arcrole}{$pref}{$ord}{'pri'} = $pri;
189 $pu{$from}{$to}{$arcrole}{$pref}{$ord}{'use'} = $use;
190 $pu{$from}{$to}{$arcrole}{$pref}{$ord}{'idx'} = [$i];
191 }
192 }
193 my ($proh, $over) = prohibited_idxs(\%pu, $type);
194 if (@$proh) {
195 my @to_shorts = map {$$arcs[$_]->to_short()} @$proh;
196 $self->prohibiteds($type, $uri, \@to_shorts);
197 }
198 push @dels, @$proh, @$over;
199 my @deleteds = delete @$arcs[@dels] if @dels;
200 }
201 }
202 }
203  
204 sub prohibited_idxs {
205 my ($pu, $type) = @_;
206 my @dels;
207 my (@proh, @over);
208 foreach my $from (keys %$pu) {
209 my $pv = $$pu{$from};
210 foreach my $to (keys %$pv) {
211 my $pw = $$pv{$to};
212 foreach my $role (keys %$pw) {
213 my $px = $$pw{$role};
214 foreach my $pref (keys %$px) {
215 my $py = $$px{$pref};
216 foreach my $ord (keys %$py) {
217 if ($$py{$ord}{'use'} eq 'prohibited') {
218 push @proh, @{$$py{$ord}{'idx'}};
219 }
220 else {
221 my $pz = $$py{$ord}{'idx'};
222 if (@$pz > 1 && $type ne 'lab') {
223 push @over, @$pz[1..$#$pz];
224 }
225 }
226 }
227 }
228 }
229 }
230 }
231 return (\@proh, \@over);
232 }
233  
234 sub set_labels() {
235 my ($self) = @_;
236 my $all_arcs = $self->lab();
237 my $hash = exists $$self{'labelhash'} ? $$self{'labelhash'} : ($$self{'labelhash'} = {});
238 foreach my $rolelink (keys %$all_arcs) {
239 my $arcs = $$all_arcs{$rolelink};
240 foreach my $arc (@$arcs) {
241 next unless $arc;
242 my ($lang, $rolelabel, $text, $from_short) = @$arc{'lang', 'role', 'text', 'from_short'};
243 my $xbrllabel = XBRL::JPFR::Label->new();
244 $xbrllabel->name($$arc{'from_full'});
245 $xbrllabel->role($rolelabel);
246 $xbrllabel->lang($lang);
247 $xbrllabel->value($text);
248 $xbrllabel->id($$arc{'id'});
249 $xbrllabel->label($$arc{'to_name'});
250 $$hash{$lang}{$from_short}{$rolelink}{$rolelabel} = $text;
251 push @{$$self{'labels'}}, $xbrllabel;
252 }
253 }
254 }
255  
256  
257 sub discard_prohibiteds {
258 my ($self, $trees, $type, $uri) = @_;
259 my @ret;
260 foreach my $tree (@$trees) {
261 my $id_short = $tree->id_short();
262 if (grep {/^${id_short}$/} @{$$self{'prohibiteds'}{$type}{$uri}}) {
263 next;
264 }
265 push @ret, $tree;
266 }
267 return @ret;
268 }
269  
270  
271  
272 =head1 XBRL::JPFR::Taxonomy
273  
274 XBRL::JPFR::Taxonomy - OO Module for Parsing XBRL::JPFR Taxonomies
275  
276 =head1 SYNOPSIS
277  
278 use XBRL::JPFR::Taxonomy;
279  
280 my $taxonomy = XBRL::JPFR::Taxonomy->new( {main_schema => $schema} );
281  
282 =head1 DESCRIPTION
283  
284 This module is part of the XBRL::JPFR modules group and is intended for use with XBRL::JPFR.
285  
286 =over 4
287  
288 =item new
289  
290 my $taxonomy = XBRL::JPFR::Taxonomy->new( { main_schema => <schema object here> })
291  
292 =back
293  
294 =head1 AUTHOR
295  
296 Mark Gannon <mark@truenorth.nu>
297  
298 =head1 MODIFIER
299  
300 Tetsuya Yamamoto <yonjouhan@gmail.com>
301  
302 =head1 SEE ALSO
303  
304 Modules: XBRL XBRL::JPFR XBRL::Taxonomy
305  
306 =head1 COPYRIGHT AND LICENSE
307  
308 Copyright (C) 2012 by Mark Gannon
309  
310 Copyright (C) 2015 by Tetsuya Yamamoto
311  
312 This library is free software; you can redistribute it and/or modify
313 it under the same terms as Perl itself, either Perl version 5.10 or,
314 at your option, any later version of Perl 5 you may have available.
315  
316  
317 =cut
318  
319  
320 1;
321