| 1 | package XBRL::JPFR::Branch; | ||
| 2 | |||
| 3 | our $VERSION = '0.01'; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | use Carp; | ||
| 8 | use Clone qw(clone); | ||
| 9 | use Data::Dumper; | ||
| 10 | |||
| 11 | use base qw(XBRL::JPFR::Element); | ||
| 12 | |||
| 13 | my @fields = qw(depth order weight prefLabel label priority prefixed_name use from from_full from_short id_full id_short tos); | ||
| 14 | XBRL::JPFR::Branch->mk_accessors(@fields); | ||
| 15 | |||
| 16 | sub new() { | ||
| 17 | my ($class, $element) = @_; | ||
| 18 | my $self = clone($element); | ||
| 19 | $$self{'tos'} = []; | ||
| 20 | bless $self, $class; | ||
| 21 | |||
| 22 | return $self; | ||
| 23 | } | ||
| 24 | |||
| 25 | sub splice_undefs { | ||
| 26 | my ($tree) = @_; | ||
| 27 | my $tos = $$tree{'tos'}; | ||
| 28 | $$tree{'tos'} = [grep {defined} @$tos]; | ||
| 29 | foreach my $to (@{$$tree{'tos'}}) { | ||
| 30 | $to->splice_undefs(); | ||
| 31 | } | ||
| 32 | } | ||
| 33 | |||
| 34 | sub splice_branch_by_id_short { | ||
| 35 | my ($parent, $id_short, $prefLabel) = @_; | ||
| 36 | $prefLabel = 'http://www.xbrl.org/2003/role/label' unless $prefLabel; | ||
| 37 | my $tos = $$parent{'tos'}; | ||
| 38 | for (my $i = 0 ; $i < @$tos ; $i++) { | ||
| 39 | next if $$tos[$i]->id_short() ne $id_short; | ||
| 40 | next if $$tos[$i]->prefLabel() ne $prefLabel; | ||
| 41 | splice @$tos, $i, 1; | ||
| 42 | return; | ||
| 43 | } | ||
| 44 | } | ||
| 45 | |||
| 46 | sub delete_branch_by_id_short { | ||
| 47 | my ($parent, $id_short, $prefLabel) = @_; | ||
| 48 | $prefLabel = 'http://www.xbrl.org/2003/role/label' unless $prefLabel; | ||
| 49 | my $tos = $$parent{'tos'}; | ||
| 50 | my @dels; | ||
| 51 | for (my $i = 0 ; $i < @$tos ; $i++) { | ||
| 52 | next if !$$tos[$i]; | ||
| 53 | next if $$tos[$i]->id_short() ne $id_short; | ||
| 54 | next if $$tos[$i]->prefLabel() ne $prefLabel; | ||
| 55 | push @dels, $i; | ||
| 56 | } | ||
| 57 | delete @$tos[@dels]; | ||
| 58 | } | ||
| 59 | |||
| 60 | sub clone_branch { | ||
| 61 | my ($self) = @_; | ||
| 62 | my ($from, $tos) = delete @$self{'from', 'tos'}; | ||
| 63 | my $clone = clone($self); | ||
| 64 | @$clone{'from', 'tos'} = ($from, $tos); | ||
| 65 | @$self{'from', 'tos'} = ($from, $tos); | ||
| 66 | return $clone; | ||
| 67 | } | ||
| 68 | |||
| 69 | sub connect_to { | ||
| 70 | my ($from, $to, $arc, $from_label, $to_label) = @_; | ||
| 71 | my ($from_full, $from_short) = @$arc{'from_full', 'from_short'}; | ||
| 72 | my ($to_full, $to_short) = @$arc{'to_full', 'to_short'}; | ||
| 73 | my ($order, $pref, $weight) = @$arc{'order', 'prefLabel', 'weight'}; | ||
| 74 | my ($priority, $use) = @$arc{'priority', 'use'}; | ||
| 75 | if ($to->from()) { | ||
| 76 | $to = $to->clone_branch(); | ||
| 77 | $to->from(undef); | ||
| 78 | $to->tos([]); | ||
| 79 | } | ||
| 80 | push @{$$from{'tos'}}, $to; | ||
| 81 | $from->label($from_label); | ||
| 82 | $from->id_full($from_full); | ||
| 83 | $from->id_short($from_short); | ||
| 84 | $to->from($from); | ||
| 85 | $to->label($to_label); | ||
| 86 | $to->from_full($from_full); | ||
| 87 | $to->from_short($from_short); | ||
| 88 | $to->id_full($to_full); | ||
| 89 | $to->id_short($to_short); | ||
| 90 | $to->order($order); | ||
| 91 | $to->prefLabel($pref); | ||
| 92 | $to->weight($weight); | ||
| 93 | $to->priority($priority); | ||
| 94 | $to->use($use); | ||
| 95 | } | ||
| 96 | |||
| 97 | sub find_branch_by_name { | ||
| 98 | my ($tree, $name) = @_; | ||
| 99 | return $tree if $tree->name() eq $name; | ||
| 100 | foreach my $to (@{$$tree{'tos'}}) { | ||
| 101 | my $t = $to->find_branch_by_name($name); | ||
| 102 | return $t if $t; | ||
| 103 | } | ||
| 104 | return undef; | ||
| 105 | } | ||
| 106 | |||
| 107 | sub find_branch_by_id { | ||
| 108 | my ($tree, $id) = @_; | ||
| 109 | return $tree if $tree->id_short() eq $id; | ||
| 110 | foreach my $to (@{$$tree{'tos'}}) { | ||
| 111 | my $t = $to->find_branch_by_id($id); | ||
| 112 | return $t if $t; | ||
| 113 | } | ||
| 114 | return undef; | ||
| 115 | } | ||
| 116 | |||
| 117 | sub find_branch_by_label_and_relation { | ||
| 118 | my ($tree, $branch) = @_; | ||
| 119 | my $is_branch = $tree->label() eq $branch->label(); | ||
| 120 | $is_branch &&= $tree->depth() == $branch->depth(); | ||
| 121 | if ($branch->abstract() eq 'false') { | ||
| 122 | $is_branch &&= $tree->abstract() eq 'false'; | ||
| 123 | } | ||
| 124 | if ($tree->from() && $branch->from()) { | ||
| 125 | $is_branch &&= $tree->from()->label() eq $branch->from()->label(); | ||
| 126 | } | ||
| 127 | return $tree if $is_branch; | ||
| 128 | foreach my $to (@{$$tree{'tos'}}) { | ||
| 129 | my $t = $to->find_branch_by_label_and_relation($branch); | ||
| 130 | return $t if $t; | ||
| 131 | } | ||
| 132 | return undef; | ||
| 133 | } | ||
| 134 | |||
| 135 | sub dump_branch { | ||
| 136 | my ($branch) = @_; | ||
| 137 | my $clone = $branch->clone_branch(); | ||
| 138 | delete @$clone{'from', 'tos'}; | ||
| 139 | return Dumper($clone); | ||
| 140 | } | ||
| 141 | |||
| 142 | =head1 XBRL::JPFR::Branch | ||
| 143 | |||
| 144 | XBRL::JPFR::Branch - OO Module for Encapsulating XBRL::JPFR Branchs | ||
| 145 | |||
| 146 | =head1 SYNOPSIS | ||
| 147 | |||
| 148 | use XBRL::JPFR::Branch; | ||
| 149 | |||
| 150 | my $branch = XBRL::JPFR::Branch->new(XBRL::JPFR::Element); | ||
| 151 | |||
| 152 | =head1 DESCRIPTION | ||
| 153 | |||
| 154 | This module is part of the XBRL::JPFR modules group and is intended for use with XBRL::JPFR. | ||
| 155 | |||
| 156 | =over 4 | ||
| 157 | |||
| 158 | =item new | ||
| 159 | |||
| 160 | Constructor for object takes the branch XML from the element. | ||
| 161 | |||
| 162 | =item depth | ||
| 163 | |||
| 164 | Get or set the Branch's depth. | ||
| 165 | |||
| 166 | =item order | ||
| 167 | |||
| 168 | Get or set the branch's order. | ||
| 169 | |||
| 170 | =item weight | ||
| 171 | |||
| 172 | Get or set the branch's weight. | ||
| 173 | |||
| 174 | =item label | ||
| 175 | |||
| 176 | Get or set the branch's label | ||
| 177 | |||
| 178 | =item from_short | ||
| 179 | |||
| 180 | Get or set the branch's from_short | ||
| 181 | |||
| 182 | =item from_full | ||
| 183 | |||
| 184 | Get or set the branch's from_full | ||
| 185 | |||
| 186 | =item to_short | ||
| 187 | |||
| 188 | Get or set the branch's to_short. | ||
| 189 | |||
| 190 | =item to_full | ||
| 191 | |||
| 192 | Get or set the branch's to_full. | ||
| 193 | |||
| 194 | =item id_short | ||
| 195 | |||
| 196 | Get or set the branch's id_short. | ||
| 197 | |||
| 198 | =item id_full | ||
| 199 | |||
| 200 | Get or set the branch's id_full. | ||
| 201 | |||
| 202 | =item tos | ||
| 203 | |||
| 204 | Get or set the branch's children. | ||
| 205 | |||
| 206 | =back | ||
| 207 | |||
| 208 | =head1 AUTHOR | ||
| 209 | |||
| 210 | Tetsuya Yamamoto <yonjouhan@gmail.com> | ||
| 211 | |||
| 212 | =head1 SEE ALSO | ||
| 213 | |||
| 214 | Modules: XBRL XBRL::JPFR | ||
| 215 | |||
| 216 | =head1 COPYRIGHT AND LICENSE | ||
| 217 | |||
| 218 | Copyright (C) 2015 by Tetsuya Yamamoto | ||
| 219 | |||
| 220 | This library is free software; you can redistribute it and/or modify | ||
| 221 | it under the same terms as Perl itself, either Perl version 5.10 or, | ||
| 222 | at your option, any later version of Perl 5 you may have available. | ||
| 223 | |||
| 224 | |||
| 225 | =cut | ||
| 226 | |||
| 227 | |||
| 228 | 1; |