xbrlware-ruby19 1.1.2.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,181 @@
1
+ #!/usr/bin/ruby
2
+ #
3
+ # Author:: xbrlware@bitstat.com
4
+ #
5
+ # Copyright:: 2009, 2010 bitstat (http://www.bitstat.com). All Rights Reserved.
6
+ #
7
+ # License:: Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
16
+ # implied.
17
+ # See the License for the specific language governing permissions and
18
+ # limitations under the License.
19
+ #
20
+ module Xbrlware
21
+ module Linkbase
22
+ class CalculationLinkbase < Linkbase
23
+ # Creates a CalculationLinkbase.
24
+ #
25
+ # linkbase_path::
26
+ # XBRL Calculation Linkbase source. Tries to load and parse calculationlinkbase from path.
27
+ #
28
+ # instance::
29
+ # Instance object
30
+ #
31
+ # label_linkbase::
32
+ # optional parameter, LabelLinkbase object
33
+ def initialize(linkbase_path, instance, label_linkbase=nil)
34
+ super linkbase_path
35
+ @instance=instance
36
+ @label_linkbase=label_linkbase
37
+ @cal_content_optimized=nil
38
+ end
39
+
40
+ def calculation(role=nil)
41
+ calculations=[]
42
+
43
+ if @cal_content_optimized.nil?
44
+ calc_content=@linkbase_content["calculationLink"]
45
+ @cal_content_optimized = []
46
+ calc_content.each_with_index do |cal, index|
47
+ next if cal["loc"].nil? || cal["calculationArc"].nil?
48
+ cal["loc"].map! do |e|
49
+ e["xlink:label"]="#{e['xlink:label']}_#{index}"
50
+ e
51
+ end
52
+
53
+ cal["calculationArc"].map! do |e|
54
+ e["xlink:from"]="#{e['xlink:from']}_#{index}"
55
+ e["xlink:to"]="#{e['xlink:to']}_#{index}"
56
+ e
57
+ end
58
+ selected=@cal_content_optimized.select {|cal_existing| cal_existing["xlink:role"]==cal["xlink:role"]}[0]
59
+ if selected.nil?
60
+ @cal_content_optimized << cal
61
+ else
62
+ cal["loc"].each do |current|
63
+ matched_loc=nil
64
+ selected["loc"].each do |existing|
65
+ if existing["xlink:href"]==current["xlink:href"]
66
+ matched_loc=current
67
+ cal["calculationArc"].each do |arc|
68
+ arc["xlink:from"] = existing["xlink:label"] if current["xlink:label"]==arc["xlink:from"]
69
+ arc["xlink:to"] = existing["xlink:label"] if current["xlink:label"]==arc["xlink:to"]
70
+ end
71
+ break
72
+ end
73
+ end
74
+ selected["loc"] << current if matched_loc.nil?
75
+ end
76
+ selected["calculationArc"] += cal["calculationArc"]
77
+ end
78
+ end
79
+ end
80
+
81
+ @cal_content_optimized.each do |cal|
82
+ next unless cal["xlink:role"]==role unless role.nil?
83
+
84
+ if cal["calculationArc"].nil?
85
+ calculations << Calculation.new(@instance.entity_details, cal["xlink:title"], cal["xlink:role"], @role_map[cal["xlink:role"]])
86
+ else
87
+ contexts_and_arcs=arcs(cal)
88
+ calculations << Calculation.new(@instance.entity_details, cal["xlink:title"], cal["xlink:role"], @role_map[cal["xlink:role"]], contexts_and_arcs["arcs"], contexts_and_arcs["contexts"])
89
+ end
90
+ end
91
+ return calculations[0] unless role.nil?
92
+ calculations
93
+ end
94
+
95
+ private
96
+ def fetch_label(label_name, pref_label)
97
+ pref_label="http://www.xbrl.org/2003/role/label" if pref_label.nil?
98
+ label_obj=@label_linkbase.label(label_name, pref_label)
99
+ label = label_obj.value unless label_obj.nil?
100
+ return label
101
+ end
102
+
103
+ def arcs(calc)
104
+ locators={}
105
+ calc["loc"].each do |loc|
106
+ href = loc["xlink:href"]
107
+ unless href.index("#").nil?
108
+ locators[loc["xlink:label"]]= href[href.index("#")+1, href.length]
109
+ else
110
+ locators[loc["xlink:label"]]=href
111
+ end
112
+ end
113
+
114
+ arc_map={}
115
+ contexts = Set.new()
116
+
117
+ calc["calculationArc"].each do |arc|
118
+ from_label, to_label=nil, nil
119
+ unless @label_linkbase.nil?
120
+ to_label = fetch_label(locators[arc["xlink:to"]], arc["preferredLabel"])
121
+ from_label = fetch_label(locators[arc["xlink:from"]], arc["preferredLabel"])
122
+ end
123
+
124
+ to = Calculation::CalculationArc.new(arc["xlink:to"], locators[arc["xlink:to"]], arc["xlink:arcrole"], arc["order"], arc["weight"], arc["priority"], arc["use"], to_label)
125
+ from = Calculation::CalculationArc.new(arc["xlink:from"], locators[arc["xlink:from"]], role=nil, order=nil, weight=nil, priority=nil, use=nil, label=from_label)
126
+
127
+ to_item_name = locators[arc["xlink:to"]].gsub(/.*_/, "")
128
+ from_item_name = locators[arc["xlink:from"]].gsub(/.*_/, "")
129
+
130
+ to_item_map=item_map(@instance.item(to_item_name))
131
+ to.items=to_item_map.values
132
+ contexts.merge(to_item_map.keys)
133
+
134
+ from_item_map=item_map(@instance.item(from_item_name))
135
+ from.items=from_item_map.values
136
+ contexts.merge(from_item_map.keys)
137
+
138
+ if arc_map.has_key?(from)
139
+ arc_map[from] << to
140
+ else
141
+ arc_map[from]=[to]
142
+ end
143
+ end
144
+
145
+ return {"contexts" => contexts, "arcs" => Linkbase.build_relationship(arc_map)}
146
+ end
147
+
148
+ private
149
+ def item_map(items)
150
+ item_map={}
151
+ items.each do |item|
152
+ item_map[item.context]=item
153
+ end unless items.nil?
154
+ item_map
155
+ end
156
+
157
+ public
158
+ class Calculation < Linkbase::Link
159
+
160
+ attr_reader :contexts, :entity_details
161
+
162
+ def initialize(entity_details, title, role, href=nil, arcs=nil, contexts=nil)
163
+ super("Calculation", title, role, href, arcs)
164
+ @contexts=contexts
165
+ @entity_details=entity_details
166
+ end
167
+
168
+ class CalculationArc < Linkbase::Link::Arc
169
+ attr_reader :weight, :use
170
+
171
+ def initialize(item_id, href, role=nil, order=nil, weight=nil, priority=nil, use=nil, label=nil)
172
+ super item_id, href, role, order, priority, label
173
+ @weight=weight.to_i
174
+ @use=use
175
+ end
176
+ end
177
+ end
178
+
179
+ end
180
+ end
181
+ end
@@ -0,0 +1,226 @@
1
+ #!/usr/bin/ruby
2
+ #
3
+ # Author:: xbrlware@bitstat.com
4
+ #
5
+ # Copyright:: 2009, 2010 bitstat (http://www.bitstat.com). All Rights Reserved.
6
+ #
7
+ # License:: Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
16
+ # implied.
17
+ # See the License for the specific language governing permissions and
18
+ # limitations under the License.
19
+ #
20
+ module Xbrlware
21
+ module Linkbase
22
+ class DefinitionLinkbase < Linkbase
23
+
24
+ @@constants = {"primaryItem-hypercube" => "http://xbrl.org/int/dim/arcrole/all",
25
+ "hypercube-dimension" => "http://xbrl.org/int/dim/arcrole/hypercube-dimension",
26
+ "dimension-domain" => "http://xbrl.org/int/dim/arcrole/dimension-domain",
27
+ "domain-member" => "http://xbrl.org/int/dim/arcrole/domain-member"}
28
+
29
+ # Creates a DefinitionLinkbase.
30
+ #
31
+ # linkbase_path::
32
+ # XBRL Definition Linkbase source. Tries to load and parse definition linkbase from path.
33
+ #
34
+ # label_linkbase::
35
+ # optional parameter, takes LabelLinkbase object
36
+ def initialize(linkbase_path, label_linkbase=nil)
37
+ super linkbase_path
38
+ @label_linkbase=label_linkbase
39
+ @def_content_optimized=nil
40
+ end
41
+
42
+ def definition(role=nil)
43
+
44
+ definitions=[]
45
+
46
+ if @def_content_optimized.nil?
47
+ def_content=@linkbase_content["definitionLink"]
48
+
49
+ @def_content_optimized = []
50
+ def_content.each_with_index do |_def, index|
51
+ next if _def["loc"].nil? || _def["definitionArc"].nil?
52
+ _def["loc"].map! do |e|
53
+ e["xlink:label"]="#{e['xlink:label']}_#{index}"
54
+ e
55
+ end
56
+
57
+ _def["definitionArc"].map! do |e|
58
+ e["xlink:from"]="#{e['xlink:from']}_#{index}"
59
+ e["xlink:to"]="#{e['xlink:to']}_#{index}"
60
+ e
61
+ end
62
+ selected=@def_content_optimized.select {|def_existing| def_existing["xlink:role"]==_def["xlink:role"]}[0]
63
+ if selected.nil?
64
+ @def_content_optimized << _def
65
+ else
66
+ _def["loc"].each do |current|
67
+ matched_loc=nil
68
+ selected["loc"].each do |existing|
69
+ if existing["xlink:href"]==current["xlink:href"]
70
+ matched_loc=current
71
+ _def["definitionArc"].each do |arc|
72
+ arc["xlink:from"] = existing["xlink:label"] if current["xlink:label"]==arc["xlink:from"]
73
+ arc["xlink:to"] = existing["xlink:label"] if current["xlink:label"]==arc["xlink:to"]
74
+ end
75
+ break
76
+ end
77
+ end
78
+ selected["loc"] << current if matched_loc.nil?
79
+ end
80
+ selected["definitionArc"] += _def["definitionArc"]
81
+ end
82
+ end
83
+ end
84
+
85
+ @def_content_optimized.each do |def_|
86
+
87
+ next unless def_["xlink:role"]==role unless role.nil?
88
+
89
+ if def_["definitionArc"].nil?
90
+ definitions << Definition.new(def_["xlink:title"], def_["xlink:role"], @role_map[def_["xlink:role"]])
91
+ else
92
+ arcs=arcs(def_)
93
+ definitions << Definition.new(def_["xlink:title"], def_["xlink:role"], @role_map[def_["xlink:role"]], primary_items(arcs))
94
+ end
95
+ end
96
+ return definitions[0] unless role.nil?
97
+ definitions
98
+ end
99
+
100
+ private
101
+ def primary_items(arcs)
102
+ items=Set.new
103
+ arcs.each do |arc|
104
+ arc.children.each do |child|
105
+ if child.role==@@constants["primaryItem-hypercube"]
106
+ items << arc
107
+ break
108
+ end
109
+ end if arc.has_children?
110
+ end
111
+
112
+ items.each do |primary_item|
113
+
114
+ hypercubes=[]
115
+ primary_item.children.each do |p_child|
116
+ hypercubes << p_child if p_child.role==@@constants["primaryItem-hypercube"]
117
+ end if primary_item.has_children?
118
+ primary_item.children = hypercubes
119
+ MetaUtil::introduce_instance_alias(primary_item, "hypercubes", "children")
120
+
121
+ primary_item.hypercubes.each do |hypercube|
122
+ dimensions=[]
123
+ hypercube.children.each do |h_child|
124
+ dimensions << h_child if h_child.role==@@constants["hypercube-dimension"]
125
+ end if hypercube.has_children?
126
+ hypercube.children=dimensions
127
+ MetaUtil::introduce_instance_alias(hypercube, "dimensions", "children")
128
+
129
+ hypercube.dimensions.each do |dimension|
130
+ domains=[]
131
+ dimension.children.each do |di_child|
132
+ domains << di_child if di_child.role==@@constants["dimension-domain"]
133
+ end if dimension.has_children?
134
+ dimension.children=domains
135
+ MetaUtil::introduce_instance_alias(dimension, "domains", "children")
136
+
137
+ dimension.domains.each do |domain|
138
+ members=[]
139
+ domain.children.each do |do_child|
140
+ members << do_child if do_child.role==@@constants["domain-member"]
141
+ end if domain.has_children?
142
+ MetaUtil::introduce_instance_alias(domain, "members", "children")
143
+ MetaUtil::introduce_instance_writer(domain, "members", "children")
144
+ end
145
+ end
146
+ end
147
+ end
148
+ items
149
+ end
150
+
151
+ private
152
+ def arcs(def_)
153
+
154
+ locators={}
155
+ def_["loc"].each do |loc|
156
+ href = loc["xlink:href"]
157
+ unless href.index("#").nil?
158
+ locators[loc["xlink:label"]]= href[href.index("#")+1, href.length]
159
+ else
160
+ locators[loc["xlink:label"]]=href
161
+ end
162
+ end
163
+
164
+ arc_map={}
165
+
166
+ def_["definitionArc"].each do |arc|
167
+
168
+ to_label = nil
169
+ unless @label_linkbase.nil?
170
+ to_label_obj=@label_linkbase.label(locators[arc["xlink:to"]], arc["preferredLabel"]) unless arc["preferredLabel"].nil?
171
+ to_label_obj=@label_linkbase.label(locators[arc["xlink:to"]], "http://www.xbrl.org/2003/role/label") if arc["preferredLabel"].nil?
172
+ to_label = to_label_obj.value unless to_label_obj.nil?
173
+ end
174
+
175
+ to = Definition::DefinitionArc.new(arc["xlink:to"], locators[arc["xlink:to"]], arc["xlink:arcrole"], arc["order"], arc["priority"], to_label, arc["xbrldt:contextElement"])
176
+ from = Definition::DefinitionArc.new(arc["xlink:from"], locators[arc["xlink:from"]])
177
+
178
+ if arc_map.has_key?(from)
179
+ arc_map[from] << to
180
+ else
181
+ arc_map[from]=[to]
182
+ end
183
+ end
184
+
185
+ return Linkbase.build_relationship(arc_map)
186
+ end
187
+
188
+ public
189
+ class Definition < Linkbase::Link
190
+
191
+ attr_reader :primary_items
192
+
193
+ def initialize(title, role, href=nil, primary_items=nil)
194
+ super("Definition", title, role, href, primary_items)
195
+ @primary_items=primary_items
196
+ end
197
+
198
+ def dimension_domain_map
199
+ dim_dom_map={}
200
+ @primary_items.each do |primary_item|
201
+ primary_item.hypercubes.each do |hypercube|
202
+ hypercube.dimensions.each do |dimension|
203
+ domains=[]
204
+ dimension.domains.each do |domain|
205
+ domains << domain.href.sub("_", ":")
206
+ end
207
+ dim_dom_map[dimension.href.sub("_", ":")]=domains if domains.size > 0
208
+ end
209
+ end
210
+ end unless @primary_items.nil?
211
+ dim_dom_map
212
+ end
213
+
214
+ class DefinitionArc < Linkbase::Link::Arc
215
+ attr_reader :ctx_element
216
+
217
+ def initialize(item_id, href, role=nil, order=nil, priority=nil, label=nil, ctx_element=nil)
218
+ super item_id, href, role, order, priority, label
219
+ @ctx_element=ctx_element
220
+ end
221
+ end
222
+ end
223
+
224
+ end
225
+ end
226
+ end
@@ -0,0 +1,132 @@
1
+ #!/usr/bin/ruby
2
+ #
3
+ # Author:: xbrlware@bitstat.com
4
+ #
5
+ # Copyright:: 2009, 2010 bitstat (http://www.bitstat.com). All Rights Reserved.
6
+ #
7
+ # License:: Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
16
+ # implied.
17
+ # See the License for the specific language governing permissions and
18
+ # limitations under the License.
19
+ #
20
+ module Xbrlware
21
+ module Linkbase
22
+ class LabelLinkbase < Linkbase
23
+ # Creates a LabelLinkbase.
24
+ #
25
+ # linkbase_path::
26
+ # XBRL Label Linkbase source. Tries to load and parse label linkbase from path.
27
+ def initialize(linkbase_path)
28
+ super linkbase_path
29
+ @label_map={}
30
+ parse_content
31
+ end
32
+
33
+ def label(item_name, label_role=nil, language=["en-US", "en"])
34
+ return @label_map[item_name] if label_role.nil?
35
+ return @label_map[item_name][label_role] unless @label_map[item_name].nil? if language.nil?
36
+ return @label_map[item_name][label_role][language] unless @label_map[item_name].nil? || @label_map[item_name][label_role].nil? if language.is_a?(String)
37
+ language.each do |lang|
38
+ lab = @label_map[item_name][label_role][lang] unless @label_map[item_name].nil? || @label_map[item_name][label_role].nil?
39
+ return lab unless lab.nil?
40
+ end if language.is_a?(Array)
41
+ return nil
42
+ end
43
+
44
+ def inspect
45
+ self.to_s
46
+ end
47
+
48
+ def print(verbose_flag="q")
49
+ str = StringIO.new
50
+ @label_map.each do |key, value|
51
+ str << key
52
+ if verbose_flag=="v"
53
+ value.each do |k, v|
54
+ str << "\n" << "\t"
55
+ str << " role [" << k << "]"
56
+ str << "\n" << "\t\t"
57
+ v.each do |lang, label|
58
+ str << " lang [" << lang << "] label [" << label.value << "]"
59
+ end
60
+ end
61
+ end
62
+ str << "\n"
63
+ end
64
+ puts str.string
65
+ return self.to_s
66
+ end
67
+
68
+ private
69
+ def parse_content()
70
+
71
+ label_links=@linkbase_content["labelLink"]
72
+ label_links.each do |label_content|
73
+ next if label_content["loc"].nil? || label_content["labelArc"].nil?
74
+ loc_map=locator_map(label_content["loc"])
75
+ arc_map=linkarc_map(label_content["labelArc"])
76
+
77
+ labels=label_content["label"]
78
+ labels.each do |label|
79
+ l=Label.new(label["xlink:type"], label["xlink:role"], label["xlink:label"], label["xml:lang"], label["content"], @role_map[label["xlink:role"]])
80
+ item_name=loc_map[arc_map[label["xlink:label"]]]
81
+ if @label_map[item_name].nil?
82
+ @label_map[item_name]={l.role => {l.lang => l}}
83
+ else
84
+ if @label_map[item_name][l.role].nil?
85
+ @label_map[item_name][l.role] = {l.lang => l}
86
+ else
87
+ @label_map[item_name][l.role][l.lang] = l
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
93
+
94
+ def linkarc_map(arcs)
95
+ arc_map={}
96
+ arcs.each do |arc|
97
+ arc_map[arc["xlink:to"]]=arc["xlink:from"]
98
+ end
99
+ arc_map
100
+ end
101
+
102
+ def locator_map(locators)
103
+ locator_map={}
104
+ locators.each do |loc|
105
+ href = loc["xlink:href"]
106
+ unless href.index("#").nil?
107
+ locator_map[loc["xlink:label"]]=href[href.index("#")+1, href.length]
108
+ else
109
+ locator_map[loc["xlink:label"]]=href
110
+ end
111
+ end
112
+ locator_map
113
+ end
114
+
115
+ public
116
+ class Label
117
+
118
+ attr_reader :type, :role, :href, :label, :lang, :value
119
+
120
+ def initialize(type, role, label, lang, value, href=nil)
121
+ @type=type
122
+ @role=role
123
+ @label=label
124
+ @lang=lang
125
+ @value=value
126
+ @href=href
127
+ end
128
+ end
129
+
130
+ end
131
+ end
132
+ end