source: main/trunk/package-kits/scripts/gs-servlet.pl@ 33226

Last change on this file since 33226 was 29679, checked in by Jeremy Symon, 9 years ago

Switch to an Ordered Hash to stop the XML files from being scrambled

  • Property svn:executable set to *
File size: 5.9 KB
RevLine 
[29665]1#!/usr/bin/env perl
2
3use strict;
4use warnings;
5use utf8;
6
7use Data::Dumper;
[29668]8use POSIX 'isatty';
[29665]9
[29673]10use lib 'perllib';
[29678]11use Greenstone::XML::Tidy;
[29665]12
[29679]13my $hash = Hash::Ordered->new;
[29668]14my %commands;
15# Command structure:
16# key: Case-insensitive name of the command
17# val: Array of info about the command
18# [0]: Description of the command
19# [1]: Function that is run for the command
20# [2]: Number of arguments of the command (if any)
[29672]21# [3]: True if the command has a variable number of args
[29668]22%commands = (
23 help => [
24 'Prints help about the available commands',
25 sub {
26 for my $cmd (sort keys %commands) {
27 printf "%-10s %s\n", $cmd, $commands{$cmd}[0];
28 }
29 },
30 ],
31 clear => [
32 'Clears the internal state',
33 sub {
[29679]34 $hash = Hash::Ordered->new;
[29668]35 }
36 ],
37 read => [
[29672]38 "Parses XML from a file into the internal state\n input_file (- for STDIN)",
[29668]39 sub {
[29678]40 my $new = read_xml shift @ARGV;
[29671]41 # Append the new data to the current data
[29679]42 for my $key ($new->keys) {
43 if ($key eq '.attr' and $hash->exists ('.attr')) {
44 my $existing_attr = $hash->get ('.attr');
45 my $new_attr = $new->get ('.attr');
46 for my $attr ($new_attr->keys) {
47 $existing_attr->set ($attr => $new_attr->get ($attr));
[29671]48 }
[29679]49 } elsif ($hash->exists ($key)) {
50 my $existing = $hash->get ($key);
51 if (ref $existing ne 'ARRAY') {
52 $existing = [ $existing ];
53 $hash->set ($key => $existing);
[29671]54 }
[29679]55 my $new_val = $new->get ($key);
56 push @{$existing},
57 (ref $new_val eq 'ARRAY' ? @{$new_val} : $new_val);
[29671]58 } else {
[29679]59 $hash->set ($key => $new->get ($key));
[29671]60 }
61 }
[29668]62 },
63 1,
64 ],
65 write => [
[29672]66 "Writes the current internal state as XML to a file\n output_file (- for STDOUT)",
[29668]67 sub {
[29678]68 write_xml $hash, shift @ARGV;
[29668]69 },
70 1,
71 ],
72 debug => [
[29669]73 'Dumps the current internal state to stdout',
[29668]74 sub {
[29665]75 print Dumper $hash;
[29668]76 },
77 ],
78 count => [
[29669]79 'Returns the number of servlets in the current internal state',
[29668]80 sub {
81 my $count = 0;
[29674]82 if (exists $hash->{servlet}) {
[29668]83 $count = 1;
84 my $servlets = $hash->{servlet};
85 if (ref $servlets eq 'ARRAY') {
86 $count = scalar @{$hash->{servlet}};
87 }
[29666]88 }
[29668]89 print $count, "\n";
90 },
91 ],
92 list => [
93 'Lists the servlets in the current internal state',
94 sub {
[29674]95 if (exists $hash->{servlet}) {
[29668]96 my $servlets = $hash->{servlet};
97 if (ref $servlets eq 'ARRAY') {
98 for my $servlet (@{$hash->{servlet}}) {
99 print $servlet->{'servlet-name'}, "\n";
100 }
101 } else {
102 print $servlets->{'servlet-name'}, "\n";
[29666]103 }
104 } else {
[29668]105 print STDERR "No servlets found. Was valid XML provided?\n";
[29665]106 }
[29668]107 },
108 ],
[29669]109 remove => [
110 'Removes a servlet from the current internal state',
111 sub {
112 my $name = shift @ARGV;
[29674]113 if (exists $hash->{servlet}) {
[29669]114 my $servlets = $hash->{servlet};
115 if (ref $servlets eq 'ARRAY') {
116 my @array = grep { $_->{'servlet-name'} ne $name } @$servlets;
117 $hash->{servlet} = \@array;
118 } elsif ($servlets->{'servlet-name'} eq $name) {
119 delete $hash->{servlet};
120 }
121 }
122 },
123 1,
124 ],
[29672]125 add => [
126 "Adds a new servlet to the current internal state\n name description class [param=value param2=value...] ;",
127 sub {
128 my %servlet = (
129 'servlet-name' => shift @ARGV,
130 'description' => shift @ARGV,
131 'servlet-class'=> shift @ARGV,
132 'init-param' => [],
133 );
134 while (@ARGV) {
135 my $param = shift @ARGV;
136 $param eq ';' and last;
137 my ($key, $value) = split '=', $param, 2;
138 (defined $key and defined $value) or die "Expected params in form 'param=value'\n";
139 push @{$servlet{'init-param'}}, {
140 'param-name' => $key,
141 'param-value' => $value,
142 };
143 }
[29674]144 if (exists $hash->{servlet}) {
[29672]145 unless (ref $hash->{servlet} eq 'ARRAY') {
146 $hash->{servlet} = [ $hash->{servlet} ];
147 }
148 push @{$hash->{servlet}}, \%servlet;
149 } else {
150 $hash->{servlet} = \%servlet;
151 }
152 },
153 3,
154 1,
155 ],
[29668]156);
157
158# Check that all given commands are valid
159my $argc = 0;
[29672]160my $varargs = 0;
[29668]161for my $cmd (@ARGV) {
[29672]162 if ($varargs and $cmd eq ';') {
163 $varargs = 0;
164 } elsif ($argc > 0) {
[29668]165 # skip arguments to a previous command
166 $argc --;
[29672]167 } elsif ($varargs) {
[29674]168 } elsif (exists $commands{$cmd}) {
[29668]169 # get the argument count of a valid command
170 $argc = @{$commands{$cmd}}[2];
171 defined $argc or ($argc = 0);
[29672]172 $varargs = @{$commands{$cmd}}[3];
[29668]173 } else {
174 # invalid command
175 print STDERR "Valid commands are:\n";
176 @{$commands{help}}[1]->();
177 die "Invalid command '" . $cmd . "'\n";
[29665]178 }
179}
[29672]180$argc != 0 and die "Expected $argc more argument" . ($argc != 1 ? "s" : "") . "\n";
181$varargs and die "Unclosed vararg command. Add an argument ';' to close the varargs\n";
[29668]182
183# Run the commands
184while (@ARGV) {
185 @{$commands{lc shift}}[1]->();
186}
Note: See TracBrowser for help on using the repository browser.