File: stag-flatten.pl

package info (click to toggle)
libdata-stag-perl 0.14-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,480 kB
  • sloc: perl: 6,394; lisp: 141; xml: 116; ansic: 55; makefile: 17; sh: 2
file content (189 lines) | stat: -rwxr-xr-x 4,132 bytes parent folder | download | duplicates (3)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/local/bin/perl -w

# POD docs at end

use strict;

use Data::Stag qw(:all);
use Getopt::Long;


my @cols = ();
my $sep = "\t";
my $parser;
my $nest;
my $errhandler;
my $errf;
GetOptions(
           "parser|format|p=s" => \$parser,
           "errhandler=s" => \$errhandler,
           "errf|e=s" => \$errf,
	   "cols|c=s@"=>\@cols,
	   "help|h"=>sub { system("perldoc $0"); exit },
           "nest|n"=>\$nest,
	  );

$errhandler =  Data::Stag->getformathandler($errhandler || 'xml');
if ($errf) {
    $errhandler->file($errf);
}
else {
    $errhandler->fh(\*STDERR);
}

my $node = shift;
my $fn = shift @ARGV;
push(@cols, @ARGV);
@cols = map {split/\,/,$_} @cols;

my $np = scalar @cols;
my %idx = map {$cols[$_]=>$_} (0..$#cols);
my @vals = map {[]} @cols;
my @level_idx = ();

sub setcol {
    my ($col, $val) = @_;
    my $i = $idx{$col};
    die $col unless defined $i;
    push(@{$vals[$i]}, $val);
    return;
}

my %catch = ();
foreach my $col (@cols) {
    $catch{$col} =
      sub {
	  my ($self, $stag) = @_;
	  setcol($col, $stag->data);
	  return;
      };
}
$catch{$node} =
  sub {
      my ($self, $stag) = @_;
      push(@$_, 'NULL') foreach grep {!@$_} @vals; # "left join" - null for non-existent
      
      if ($nest) {
          print 
            join("\t", map {'{'.join(', ', @$_).'}'} @vals), 
              "\n";
      }
      else {
          my $c = 1;
          $c *= scalar(@$_) foreach @vals;
          if ($c == 1) {
              # no combinatorial explosion
              print 
                join("\t", map {$_->[0]} @vals), 
                  "\n";
              
          }
          elsif ($c == 0) {
              print STDERR $stag->xml;
              die "assertion error @vals";
          }
          else {
              # do cartesian explosion
              #
              # we COULD do this recursively but it would be slow
              my @N = map {0} @vals;
              my $done = 0;
              while (!$done) {
                  for (my $i=0; $i<@N; $i++) {
                      print "\t" if $i;
                      print $vals[$i]->[$N[$i]];
                  }
                  print "\n";
                  my $i = $#N;
                  my $carry_the_one = 1;
                  while ($i >= 0 && $carry_the_one) {
                      $N[$i] ++;
                      if ($N[$i] >= @{$vals[$i]}) {
                          $N[$i] = 0;
                          $i--;
                      }
                      else {
                          $carry_the_one = 0;
                      }
                  }
                  $done = 1 if $i<0;
              }
          }
      }
      @vals = map {[]} @cols;      
      return;
  };

my $h = Data::Stag->makehandler(%catch);
Data::Stag->parse(-file=>$fn, -format=>$parser, -handler=>$h, -errhandler=>$errhandler);
exit 0;

__END__

=head1 NAME 

stag-flatten - turns stag data into a flat table

=head1 SYNOPSIS

  stag-flatten -c name -c person/name dept MyFile.xml

=head1 DESCRIPTION

reads in a file in a stag format, and 'flattens' it to a tab-delimited
table format. given this data:

  (company
   (dept
    (name "special-operations")
    (person
     (name "james-bond"))
    (person
     (name "fred"))))

the above command will return a two column table

  special-operations      james-bond
  special-operations      fred

If there are multiple values for the columns within the node, then the
cartesian product will be calculated



=head1 USAGE

  stag-flatten [-p PARSER] [-c COLS] [-c COLS] NODE <file>

=head1 ARGUMENTS

=over

=item -p|parser FORMAT

FORMAT is one of xml, sxpr or itext

xml assumed as default

=item -c|column COL1,COL2,COL3,..

the name of the columns/elements to write out

this can be specified either with multiple -c arguments, or with a
comma-seperated (no spaces) list of column (terminal node) names after
a single -c

=item -n|nest

if set, then the output will be a compress repeating values into the
same row; each cell in the table will be enclosed by {}, and will
contain a comma-delimited set of values

=back

=head1 SEE ALSO

L<Data::Stag>

=cut