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

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

Moved xml i/o handling to the module for simplicity of use

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