1 | #!/usr/bin/env perl
|
---|
2 |
|
---|
3 | use strict;
|
---|
4 | use warnings;
|
---|
5 | use utf8;
|
---|
6 |
|
---|
7 | use XML::Parser;
|
---|
8 | use Data::Dumper;
|
---|
9 | use POSIX 'isatty';
|
---|
10 |
|
---|
11 | # Reads the servlets partial XML file using XML::Parser,
|
---|
12 | # Then converts the obtained data structure to a more readable one
|
---|
13 | # Example conversion:
|
---|
14 | =xml
|
---|
15 | <servlet>
|
---|
16 | <servlet-name>library</servlet-name>
|
---|
17 | <description attribute="example">The standard gsdl3 library program</description>
|
---|
18 | <servlet-class>org.greenstone.gsdl3.LibraryServlet</servlet-class>
|
---|
19 | <init-param>
|
---|
20 | <param-name>library_name</param-name>
|
---|
21 | <param-value>library</param-value>
|
---|
22 | </init-param>
|
---|
23 | <init-param>
|
---|
24 | <param-name>site_name</param-name>
|
---|
25 | <param-value>localsite</param-value>
|
---|
26 | </init-param>
|
---|
27 | </servlet>
|
---|
28 | =cut
|
---|
29 | =data_structure
|
---|
30 | {
|
---|
31 | 'servlet' => {
|
---|
32 | 'servlet-class' => 'org.greenstone.gsdl3.LibraryServlet',
|
---|
33 | 'servlet-name' => 'library',
|
---|
34 | 'description' => {
|
---|
35 | '.value' => 'The standard gsdl3 library program',
|
---|
36 | '.attr' => {
|
---|
37 | 'attribute' => 'example'
|
---|
38 | }
|
---|
39 | },
|
---|
40 | 'init-param' => [
|
---|
41 | {
|
---|
42 | 'param-value' => 'library',
|
---|
43 | 'param-name' => 'library_name'
|
---|
44 | },
|
---|
45 | {
|
---|
46 | 'param-name' => 'site_name',
|
---|
47 | 'param-value' => 'localsite'
|
---|
48 | }
|
---|
49 | ]
|
---|
50 | }
|
---|
51 | }
|
---|
52 | =cut
|
---|
53 | sub read_xml {
|
---|
54 | my $FH = shift;
|
---|
55 | local $/ = undef;
|
---|
56 | my $xml = <$FH>;
|
---|
57 | # The xml needs a root element, so we wrap it in one
|
---|
58 | $xml='<__root__>' . $xml . '</__root__>';
|
---|
59 | # Parse the data using XML::Parser
|
---|
60 | my $data = new XML::Parser (Style => 'Tree')->parse ($xml);
|
---|
61 |
|
---|
62 | sub tidy {
|
---|
63 | my %hash;
|
---|
64 | # If the element has attributes, add them to the hash
|
---|
65 | my $attr = shift;
|
---|
66 | if (scalar keys %$attr > 0) {
|
---|
67 | $hash{'.attr'} = $attr;
|
---|
68 | }
|
---|
69 | # Read any child elements or text value
|
---|
70 | while (@_) {
|
---|
71 | my $element = shift;
|
---|
72 | my $value = shift;
|
---|
73 | if ($element eq 0) {
|
---|
74 | $value =~ /^\s*$/ || ($hash{'.value'} .= $value);
|
---|
75 | } else {
|
---|
76 | # If there is more than one of a single tupe of child element,
|
---|
77 | # That child element must become an array
|
---|
78 | if (defined $hash{$element}) {
|
---|
79 | unless (ref $hash{$element} eq 'ARRAY') {
|
---|
80 | $hash{$element} = [ $hash{$element} ];
|
---|
81 | }
|
---|
82 | push @{$hash{$element}}, tidy (@{$value});
|
---|
83 | } else {
|
---|
84 | $hash{$element} = tidy (@{$value});
|
---|
85 | }
|
---|
86 | }
|
---|
87 | }
|
---|
88 | # If the element only has a value, it can become a scalar
|
---|
89 | if (scalar keys %hash == 1 and defined $hash{'.value'}) {
|
---|
90 | return $hash{'.value'};
|
---|
91 | }
|
---|
92 | return \%hash;
|
---|
93 | }
|
---|
94 | return tidy (@{@$data[1]});
|
---|
95 | }
|
---|
96 |
|
---|
97 | # Writes a data structure to an XML file
|
---|
98 | sub write_xml {
|
---|
99 | my ($hash, $FH) = @_;
|
---|
100 | select $FH;
|
---|
101 | sub open_tag {
|
---|
102 | my ($indent, $tag, $attr) = @_;
|
---|
103 | print $indent, "<", $tag;
|
---|
104 | if (defined $attr) {
|
---|
105 | for my $key (sort keys %$attr) {
|
---|
106 | print " ", $key, '="', $attr->{$key}, '"';
|
---|
107 | }
|
---|
108 | }
|
---|
109 | print ">";
|
---|
110 | }
|
---|
111 | sub write_element {
|
---|
112 | my ($indent, $hash) = @_;
|
---|
113 | for my $key (sort keys %$hash) {
|
---|
114 | $key eq '.attr' && next;
|
---|
115 | my $val = $hash->{$key};
|
---|
116 | $key eq '.value' && do {
|
---|
117 | print $val, "\n";
|
---|
118 | next;
|
---|
119 | };
|
---|
120 | for (ref $val) {
|
---|
121 | /^ARRAY$/ && do {
|
---|
122 | for my $element (@$val) {
|
---|
123 | open_tag ($indent, $key, $element->{'.attr'});
|
---|
124 | for (ref $element) {
|
---|
125 | /^HASH$/ && do {
|
---|
126 | print "\n";
|
---|
127 | write_element (" $indent", $element);
|
---|
128 | print $indent, "</", $key, ">\n";
|
---|
129 | last;
|
---|
130 | };
|
---|
131 | print $element, "</", $key, ">\n";
|
---|
132 | }
|
---|
133 | }
|
---|
134 | last;
|
---|
135 | };
|
---|
136 | /^HASH$/ && do {
|
---|
137 | open_tag ($indent, $key, $val->{'.attr'});
|
---|
138 | # If the element only has a value with attributes,
|
---|
139 | # it can be formatted on one line
|
---|
140 | if (scalar keys %$val == 2 and defined $val->{'.value'}) {
|
---|
141 | print $val->{'.value'}, "</", $key, ">\n";
|
---|
142 | } else {
|
---|
143 | print "\n";
|
---|
144 | write_element (" $indent", $val);
|
---|
145 | print $indent, "</", $key, ">\n";
|
---|
146 | }
|
---|
147 | last;
|
---|
148 | };
|
---|
149 | open_tag ($indent, $key);
|
---|
150 | print $val;
|
---|
151 | print "</", $key, ">\n";
|
---|
152 | }
|
---|
153 | }
|
---|
154 | }
|
---|
155 | write_element ("", $hash);
|
---|
156 | select STDOUT;
|
---|
157 | }
|
---|
158 |
|
---|
159 | my $hash;
|
---|
160 | my %commands;
|
---|
161 | # Command structure:
|
---|
162 | # key: Case-insensitive name of the command
|
---|
163 | # val: Array of info about the command
|
---|
164 | # [0]: Description of the command
|
---|
165 | # [1]: Function that is run for the command
|
---|
166 | # [2]: Number of arguments of the command (if any)
|
---|
167 | %commands = (
|
---|
168 | help => [
|
---|
169 | 'Prints help about the available commands',
|
---|
170 | sub {
|
---|
171 | for my $cmd (sort keys %commands) {
|
---|
172 | printf "%-10s %s\n", $cmd, $commands{$cmd}[0];
|
---|
173 | }
|
---|
174 | },
|
---|
175 | ],
|
---|
176 | clear => [
|
---|
177 | 'Clears the internal state',
|
---|
178 | sub {
|
---|
179 | undef $hash;
|
---|
180 | $hash = {};
|
---|
181 | }
|
---|
182 | ],
|
---|
183 | read => [
|
---|
184 | 'Parses XML from a file into the internal state',
|
---|
185 | sub {
|
---|
186 | my $file = shift @ARGV;
|
---|
187 | my $new;
|
---|
188 | if ($file eq '-') {
|
---|
189 | if (isatty *STDIN) {
|
---|
190 | print STDERR "Reading XML from STDIN. Press ^D to end\n";
|
---|
191 | }
|
---|
192 | $new = read_xml *STDIN;
|
---|
193 | } else {
|
---|
194 | open FH, '<', $file;
|
---|
195 | $new = read_xml *FH;
|
---|
196 | close FH;
|
---|
197 | }
|
---|
198 | # Append the new data to the current data
|
---|
199 | for my $key (keys %$new) {
|
---|
200 | if ($key eq '.attr' and defined $hash->{'.attr'}) {
|
---|
201 | for my $attr ($new->{'.attr'}) {
|
---|
202 | $hash->{'.attr'}->{$attr} = $new->{'.attr'}->{$attr};
|
---|
203 | }
|
---|
204 | } elsif (defined $hash->{$key}) {
|
---|
205 | if (ref $hash->{$key} ne 'ARRAY') {
|
---|
206 | $hash->{$key} = [ $hash->{$key} ];
|
---|
207 | }
|
---|
208 | if (ref $new->{$key} eq 'ARRAY') {
|
---|
209 | for my $elem (@{$new->{$key}}) {
|
---|
210 | push @{$hash->{$key}}, $elem;
|
---|
211 | }
|
---|
212 | } else {
|
---|
213 | push @{$hash->{$key}}, $new->{$key};
|
---|
214 | }
|
---|
215 | } else {
|
---|
216 | $hash->{$key} = $new->{$key};
|
---|
217 | }
|
---|
218 | }
|
---|
219 | },
|
---|
220 | 1,
|
---|
221 | ],
|
---|
222 | write => [
|
---|
223 | 'Writes the current internal state as XML to a file',
|
---|
224 | sub {
|
---|
225 | my $file = shift @ARGV;
|
---|
226 | if ($file eq '-') {
|
---|
227 | write_xml $hash, *STDOUT;
|
---|
228 | } else {
|
---|
229 | open FH, '>', $file;
|
---|
230 | write_xml $hash, *FH;
|
---|
231 | close FH;
|
---|
232 | }
|
---|
233 | },
|
---|
234 | 1,
|
---|
235 | ],
|
---|
236 | debug => [
|
---|
237 | 'Dumps the current internal state to stdout',
|
---|
238 | sub {
|
---|
239 | print Dumper $hash;
|
---|
240 | },
|
---|
241 | ],
|
---|
242 | count => [
|
---|
243 | 'Returns the number of servlets in the current internal state',
|
---|
244 | sub {
|
---|
245 | my $count = 0;
|
---|
246 | if (defined $hash->{servlet}) {
|
---|
247 | $count = 1;
|
---|
248 | my $servlets = $hash->{servlet};
|
---|
249 | if (ref $servlets eq 'ARRAY') {
|
---|
250 | $count = scalar @{$hash->{servlet}};
|
---|
251 | }
|
---|
252 | }
|
---|
253 | print $count, "\n";
|
---|
254 | },
|
---|
255 | ],
|
---|
256 | list => [
|
---|
257 | 'Lists the servlets in the current internal state',
|
---|
258 | sub {
|
---|
259 | if (defined $hash->{servlet}) {
|
---|
260 | my $servlets = $hash->{servlet};
|
---|
261 | if (ref $servlets eq 'ARRAY') {
|
---|
262 | for my $servlet (@{$hash->{servlet}}) {
|
---|
263 | print $servlet->{'servlet-name'}, "\n";
|
---|
264 | }
|
---|
265 | } else {
|
---|
266 | print $servlets->{'servlet-name'}, "\n";
|
---|
267 | }
|
---|
268 | } else {
|
---|
269 | print STDERR "No servlets found. Was valid XML provided?\n";
|
---|
270 | }
|
---|
271 | },
|
---|
272 | ],
|
---|
273 | remove => [
|
---|
274 | 'Removes a servlet from the current internal state',
|
---|
275 | sub {
|
---|
276 | my $name = shift @ARGV;
|
---|
277 | if (defined $hash->{servlet}) {
|
---|
278 | my $servlets = $hash->{servlet};
|
---|
279 | if (ref $servlets eq 'ARRAY') {
|
---|
280 | my @array = grep { $_->{'servlet-name'} ne $name } @$servlets;
|
---|
281 | $hash->{servlet} = \@array;
|
---|
282 | } elsif ($servlets->{'servlet-name'} eq $name) {
|
---|
283 | delete $hash->{servlet};
|
---|
284 | }
|
---|
285 | }
|
---|
286 | },
|
---|
287 | 1,
|
---|
288 | ],
|
---|
289 | );
|
---|
290 |
|
---|
291 | # Check that all given commands are valid
|
---|
292 | my $argc = 0;
|
---|
293 | for my $cmd (@ARGV) {
|
---|
294 | if ($argc > 0) {
|
---|
295 | # skip arguments to a previous command
|
---|
296 | $argc --;
|
---|
297 | } elsif (defined $commands{$cmd}) {
|
---|
298 | # get the argument count of a valid command
|
---|
299 | $argc = @{$commands{$cmd}}[2];
|
---|
300 | defined $argc or ($argc = 0);
|
---|
301 | } else {
|
---|
302 | # invalid command
|
---|
303 | print STDERR "Valid commands are:\n";
|
---|
304 | @{$commands{help}}[1]->();
|
---|
305 | die "Invalid command '" . $cmd . "'\n";
|
---|
306 | }
|
---|
307 | }
|
---|
308 | $argc > 0 and die "Expected $argc more argument" . ($argc > 1 ? "s" : "") . "\n";
|
---|
309 |
|
---|
310 | # Run the commands
|
---|
311 | while (@ARGV) {
|
---|
312 | @{$commands{lc shift}}[1]->();
|
---|
313 | }
|
---|