JPFR/Taxonomy.pm untested

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