source: main/trunk/package-kits/linux/files/servlet.pl@ 29667

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

Added ability to write to stdout (set output file to '-')

  • Property svn:executable set to *
File size: 6.1 KB
Line 
1#!/usr/bin/env perl
2
3use strict;
4use warnings;
5use utf8;
6
7use XML::Parser;
8use Data::Dumper;
9
10# Reads the servlets partial XML file using XML::Parser,
11# Then converts the obtained data structure to a more readable one
12# Example conversion:
13=xml
14<servlet>
15 <servlet-name>library</servlet-name>
16 <description attribute="example">The standard gsdl3 library program</description>
17 <servlet-class>org.greenstone.gsdl3.LibraryServlet</servlet-class>
18 <init-param>
19 <param-name>library_name</param-name>
20 <param-value>library</param-value>
21 </init-param>
22 <init-param>
23 <param-name>site_name</param-name>
24 <param-value>localsite</param-value>
25 </init-param>
26</servlet>
27=cut
28=data_structure
29{
30 'servlet' => {
31 'servlet-class' => 'org.greenstone.gsdl3.LibraryServlet',
32 'servlet-name' => 'library',
33 'description' => {
34 '.value' => 'The standard gsdl3 library program',
35 '.attr' => {
36 'attribute' => 'example'
37 }
38 },
39 'init-param' => [
40 {
41 'param-value' => 'library',
42 'param-name' => 'library_name'
43 },
44 {
45 'param-name' => 'site_name',
46 'param-value' => 'localsite'
47 }
48 ]
49 }
50}
51=cut
52sub read_servlets {
53 my $file = shift;
54 # The xml needs a root element, so we wrap it in one
55 my $xml='<!DOCTYPE doc [<!ENTITY real_doc SYSTEM "' . $file . '">] >
56 <__root__>
57 &real_doc;
58 </__root__>
59';
60 # Parse the data using XML::Parser
61 my $data = new XML::Parser (Style => 'Tree')->parse ($xml);
62
63 sub tidy {
64 my %hash;
65 # If the element has attributes, add them to the hash
66 my $attr = shift;
67 if (scalar keys %$attr > 0) {
68 $hash{'.attr'} = $attr;
69 }
70 # Read any child elements or text value
71 while (@_) {
72 my $element = shift;
73 my $value = shift;
74 if ($element eq 0) {
75 $value =~ /^\s*$/ || ($hash{'.value'} .= $value);
76 } else {
77 # If there is more than one of a single tupe of child element,
78 # That child element must become an array
79 if (defined $hash{$element}) {
80 unless (ref $hash{$element} eq 'ARRAY') {
81 $hash{$element} = [ $hash{$element} ];
82 }
83 push @{$hash{$element}}, tidy (@{$value});
84 } else {
85 $hash{$element} = tidy (@{$value});
86 }
87 }
88 }
89 # If the element only has a value, it can become a scalar
90 if (scalar keys %hash == 1 and defined $hash{'.value'}) {
91 return $hash{'.value'};
92 }
93 return \%hash;
94 }
95 return tidy (@{@$data[1]});
96}
97
98# Writes a data structure to an XML file
99sub write_servlets {
100 my ($hash, $file) = @_;
101 my $OUT;
102 if ($file ne '-') {
103 open $OUT, '>', $file;
104 select $OUT;
105 }
106 sub open_tag {
107 my ($indent, $tag, $attr) = @_;
108 print $indent, "<", $tag;
109 if (defined $attr) {
110 for my $key (sort keys %$attr) {
111 print " ", $key, '="', $attr->{$key}, '"';
112 }
113 }
114 print ">";
115 }
116 sub write_xml {
117 my ($indent, $hash) = @_;
118 for my $key (sort keys %$hash) {
119 $key eq '.attr' && next;
120 my $val = $hash->{$key};
121 $key eq '.value' && do {
122 print $val, "\n";
123 next;
124 };
125 for (ref $val) {
126 /^ARRAY$/ && do {
127 for my $element (@$val) {
128 open_tag ($indent, $key, $element->{'.attr'});
129 for (ref $element) {
130 /^HASH$/ && do {
131 print "\n";
132 write_xml (" $indent", $element);
133 print $indent, "</", $key, ">\n";
134 last;
135 };
136 print $element, "</", $key, ">\n";
137 }
138 }
139 last;
140 };
141 /^HASH$/ && do {
142 open_tag ($indent, $key, $val->{'.attr'});
143 # If the element only has a value with attributes,
144 # it can be formatted on one line
145 if (scalar keys %$val == 2 and defined $val->{'.value'}) {
146 print $val->{'.value'}, "</", $key, ">\n";
147 } else {
148 print "\n";
149 write_xml (" $indent", $val);
150 print $indent, "</", $key, ">\n";
151 }
152 last;
153 };
154 open_tag ($indent, $key);
155 print $val;
156 print "</", $key, ">\n";
157 }
158 }
159 }
160 write_xml ("", $hash);
161 if (defined $OUT) {
162 close $OUT;
163 }
164 select STDOUT;
165}
166# The code above is for generic XML read/writing
167# The code below is specific to Greenstone's servlets.xml
168
169my $hash;
170# Hackish non-interactive CLI parser
171while (@ARGV) {
172 for (shift) {
173 /^read$/i && do {
174 $hash = read_servlets (shift);
175 last;
176 };
177 /^write$/i && do {
178 write_servlets ($hash, shift);
179 last;
180 };
181 /^debug$/i && do {
182 print Dumper $hash;
183 last;
184 };
185 /^count$/i && do {
186 my $servlets = $hash->{servlet};
187 my $count = 1;
188 if (ref $servlets eq 'ARRAY') {
189 $count = scalar @{$hash->{servlet}};
190 }
191 print "Found ", $count, " servlets\n";
192 last;
193 };
194 /^list$/i && do {
195 print "Servlets:\n";
196 my $servlets = $hash->{servlet};
197 if (ref $servlets eq 'ARRAY') {
198 for my $servlet (@{$hash->{servlet}}) {
199 print " ", $servlet->{'servlet-name'}, "\n";
200 }
201 } else {
202 print " ", $servlets->{'servlet-name'}, "\n";
203 }
204 last;
205 };
206 }
207}
Note: See TracBrowser for help on using the repository browser.