/usr/bin/stag-splitter is in libdata-stag-perl 0.14-2.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | #!/usr/bin/perl -w
use strict;
use Data::Stag qw(:all);
use Data::Stag::XMLParser;
use Getopt::Long;
my $split;
my $name;
my $dir;
my $fmt = '';
my $outfmt = '';
my $help;
GetOptions("split|s=s"=>\$split,
"name|n=s"=>\$name,
"dir|d=s"=>\$dir,
"format|parser|p=s"=>\$fmt,
"outformat|writer|w=s"=>\$outfmt,
"help|h"=>\$help,
);
if ($help) {
system("perldoc $0");
exit 0;
}
my $h = Splitter->new;
if ($dir) {
`mkdir $dir` unless -d $dir;
$h->dir($dir);
}
if (!@ARGV) {
die "no files passed!";
}
$h->split_on_element($split);
$h->name_by_element($name);
$h->fmt($outfmt || $fmt);
foreach my $file (@ARGV) {
my @pargs = (-file=>$file, -format=>$fmt, -handler=>$h);
if ($file eq '-') {
if (!$fmt) {
$fmt = 'xml';
}
@pargs = (-format=>$fmt, -fh=>\*STDIN, -handler=>$h);
}
# $split = $split || shift @ARGV;
Data::Stag->parse(@pargs);
# $p->handler($h);
# $p->parse($file);
# print stag_xml($h->tree);
}
sub usage {
return <<EOM
EOM
}
package Splitter;
use base qw(Data::Stag::BaseHandler);
use Data::Stag qw(:all);
sub dir {
my $self = shift;
$self->{_dir} = shift if @_;
return $self->{_dir};
}
sub split_on_element {
my $self = shift;
$self->{_split_on_element} = shift if @_;
return $self->{_split_on_element};
}
sub fmt {
my $self = shift;
$self->{_fmt} = shift if @_;
return $self->{_fmt};
}
sub name_by_element {
my $self = shift;
$self->{_name_by_element} = shift if @_;
return $self->{_name_by_element};
}
sub i {
my $self = shift;
$self->{_i} = shift if @_;
return $self->{_i} || 0;
}
sub end_event {
my $self = shift;
my $ev = shift;
if ($ev eq $self->split_on_element) {
my $topnode = $self->popnode;
my $name_elt = $self->name_by_element;
my $name;
if ($name_elt) {
$name = stag_get($topnode, $name_elt);
}
if (!$name) {
$self->i($self->i()+1);
$name = $ev."_".$self->i;
}
my $dir = $self->dir || '.';
my $fmt = $self->fmt;
$fmt = $fmt || 'xml';
$name = safe($name);
my $fh = FileHandle->new(">$dir/$name.$fmt") || die("can't open >$dir/$name.$fmt");
if ($fmt eq 'xml') {
my $out = stag_xml($topnode);
print $fh $out;
}
else {
stag_generate($topnode, -fh=>$fh, -fmt=>$fmt);
}
$fh->close;
return [];
}
else {
return $self->SUPER::end_event($ev, @_);
}
}
sub safe {
my $n = shift;
$n =~ s/\///g;
$n;
}
1;
__END__
=head1 NAME
stag-splitter - splits a stag file into multiple files
=head1 SYNOPSIS
stag-splitter -split person -name social_security_no file.xml
=head1 DESCRIPTION
Splits a file using a user specified parser (default xml) around a
specified split node, naming each file according to the name argument
the files will be named anonymously, unless the '-name' switch is specified; this will use the value of the specified element as the filename
eg; if we have
<top>
<a>
<b>foo</b>
<c>yah</c>
<d>
<e>xxx</e>
</d>
</a>
<a>
<b>bar</b>
<d>
<e>wibble</e>
</d>
</a>
</top>
if we run
stag-splitter -split a -name b
it will generate two files, "foo.xml" and "bar.xml"
input format can be 'xml', 'sxpr' or 'itext' - if this is left blank
the format will be guessed from the file suffix
the output format defaults to the same as the input format, but
another can be chosen.
files go in the current directory, but this can be overridden with the
'-dir' switch
=head1 USAGE
stag-splitter [-split <ELEMENT-NAME>] [-name <ELEMENT-NAME>] [-dir <DIR>] [-format <INPUT-FORMAT>] [-outformat <OUTPUT-FORMAT>] <FILENAMES>
=over
=item -p|parser FORMAT
FORMAT is one of xml, sxpr or itext, or the name of a perl module
xml assumed as default
=item -w|writer FORMAT
FORMAT is one of xml, sxpr or itext, or the name of a perl module
=item -split|s NODE
node to split on
=item -name|n NODE
field/element to use when naming files
will use surrogate IDs if this argument not specified
=item -dir|d DIR
write files to this directory
=back
=cut
|