-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtb_build_hierarchy_top_down
executable file
·82 lines (72 loc) · 2.03 KB
/
htb_build_hierarchy_top_down
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/perl
use strict;
use FindBin;
use lib $FindBin::Bin;
use HierarchyToolbox;
use Getopt::Long;
sub usage {
print STDERR<<EOD;
usage: $0 <options>
options:
--toplist=filename CSV file with top level sysno
--list490=filename CSV file with recno/uplink_recno/sortform of children
--outfile=filename name of hierarchy XML file (default: stdout)
--sortkey add <sortkey> to <data>
--help show this text and exit
EOD
exit;
}
my($toplist,$list490,$outfile,$add_sortkey,$help);
GetOptions(
"toplist=s" => \$toplist,
"list490=s" => \$list490,
"outfile=s" => \$outfile,
"sortkey" => \$add_sortkey,
"help" => \$help,
) or usage;
( $help ) and usage;
( $toplist ) or usage;
( $list490 ) or usage;
$outfile ||= '-';
my $Tree = alephxtree->new(mode => 'top-down', outfile => $outfile);
# -- create lookup-hash for all potential 490 children
my $Lookup;
open(LIST,"<$list490") or die "cannot read $list490: $!";
$_ = <LIST>; # skip header
while ( <LIST> ) {
chomp;
my($current,$uplink,$sort)=split /\t/;
$Lookup->{$uplink}->{$current} = $sort;
}
close LIST;
# -- follow the top elements and create top-down-list
open(TOP,"<$toplist") or die "cannot read $toplist: $!";
while ( <TOP> ) {
chomp;
my($sysno,$sort,$data) = split /\t/;
$sort = escape_xml($sort);
$sort = HierarchyToolbox::make_sort_string($sort);
if ( $add_sortkey ) {
$data = "<sortkey>$sort</sortkey>" .$data;
}
$Tree->add($sysno,0,$sort,$data);
add_children($sysno);
}
close TOP;
sub add_children {
my($parent)=shift;
if ( defined $Lookup->{$parent} ) {
my $ref = $Lookup->{$parent};
foreach my $child ( keys %$ref ) {
my $sort = HierarchyToolbox::make_sort_string($ref->{$child});
my $data = $ref->{$child};
if ( $add_sortkey ) {
$data = "<sortkey>$sort</sortkey>" .$data;
}
$Tree->add($child,$parent,$sort,$data);
add_children($child);
}
}
}
close TOP;
$Tree->serialize;