Changeset 29679
- Timestamp:
- 2015-01-09T14:50:58+13:00 (9 years ago)
- Location:
- main/trunk/package-kits/scripts
- Files:
-
- 4 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/package-kits/scripts/gs-servlet.pl
r29678 r29679 11 11 use Greenstone::XML::Tidy; 12 12 13 my $hash ;13 my $hash = Hash::Ordered->new; 14 14 my %commands; 15 15 # Command structure: … … 32 32 'Clears the internal state', 33 33 sub { 34 undef $hash; 35 $hash = {}; 34 $hash = Hash::Ordered->new; 36 35 } 37 36 ], … … 41 40 my $new = read_xml shift @ARGV; 42 41 # 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}; 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)); 47 48 } 48 } elsif (exists $hash->{$key}) { 49 if (ref $hash->{$key} ne 'ARRAY') { 50 $hash->{$key} = [ $hash->{$key} ]; 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); 51 54 } 52 push @{$hash->{$key}}, 53 (ref $new->{$key} eq 'ARRAY' ? @{$new->{$key}} : $new->{$key}); 55 my $new_val = $new->get ($key); 56 push @{$existing}, 57 (ref $new_val eq 'ARRAY' ? @{$new_val} : $new_val); 54 58 } else { 55 $hash-> {$key} = $new->{$key};59 $hash->set ($key => $new->get ($key)); 56 60 } 57 61 } -
main/trunk/package-kits/scripts/perllib/Greenstone/XML/Tidy.pm
r29678 r29679 6 6 use XML::Parser; 7 7 use POSIX 'isatty'; 8 use Hash::Ordered; 8 9 use base 'Exporter'; 9 10 … … 70 71 71 72 sub tidy { 72 my %hash;73 my $hash = Hash::Ordered->new; 73 74 # If the element has attributes, add them to the hash 74 75 my $attr = shift; 75 76 if (scalar keys %$attr > 0) { 76 $hash {'.attr'} = $attr;77 $hash->set ('.attr' => Hash::Ordered->new ($attr)); 77 78 } 78 79 # Read any child elements or text value … … 81 82 my $value = shift; 82 83 if ($element eq 0) { 83 $value =~ /^\s*$/ || ($hash{'.value'} .= $value); 84 unless ($value =~ /^\s*$/) { 85 my $existing = $hash->get ('.value'); 86 defined $existing and ($value .= $existing); 87 $hash->set ('.value' => $value); 88 } 84 89 } else { 85 90 # If there is more than one of a single tupe of child element, 86 91 # That child element must become an array 87 if (defined $hash{$element}) { 88 unless (ref $hash{$element} eq 'ARRAY') { 89 $hash{$element} = [ $hash{$element} ]; 92 if ($hash->exists ($element)) { 93 my $existing = $hash->get ($element); 94 unless (ref $existing eq 'ARRAY') { 95 $hash->set ($element => [ $existing ]); 90 96 } 91 push @{$hash {$element}}, tidy (@{$value});97 push @{$hash->get ($element)}, tidy (@{$value}); 92 98 } else { 93 $hash {$element} = tidy (@{$value});99 $hash->set ($element => tidy (@{$value})); 94 100 } 95 101 } 96 102 } 97 103 # If the element only has a value, it can become a scalar 98 if (scalar keys %hash == 1 and defined $hash{'.value'}) {99 return $hash {'.value'};104 if (scalar $hash->keys == 1 and $hash->exists ('.value')) { 105 return $hash->get ('.value'); 100 106 } 101 return \%hash;107 return $hash; 102 108 } 103 109 return tidy (@{@$data[1]}); … … 119 125 print $indent, "<", $tag; 120 126 if (defined $attr) { 121 for my $key ( sort keys %$attr) {122 print " ", $key, '="', $attr-> {$key}, '"';127 for my $key ($attr->keys) { 128 print " ", $key, '="', $attr->get ($key), '"'; 123 129 } 124 130 } … … 127 133 sub write_element { 128 134 my ($indent, $hash) = @_; 129 for my $key ( sort keys %$hash) {135 for my $key ($hash->keys) { 130 136 $key eq '.attr' && next; 131 my $val = $hash-> {$key};137 my $val = $hash->get ($key); 132 138 $key eq '.value' && do { 133 139 print $val, "\n"; … … 137 143 /^ARRAY$/ && do { 138 144 for my $element (@$val) { 139 open_tag ($indent, $key, $element-> {'.attr'});145 open_tag ($indent, $key, $element->get ('.attr')); 140 146 for (ref $element) { 141 /^H ASH$/ && do {147 /^Hash::Ordered$/ && do { 142 148 print "\n"; 143 149 write_element (" $indent", $element); … … 150 156 last; 151 157 }; 152 /^H ASH$/ && do {153 open_tag ($indent, $key, $val-> {'.attr'});158 /^Hash::Ordered$/ && do { 159 open_tag ($indent, $key, $val->get ('.attr')); 154 160 # If the element only has a value with attributes, 155 161 # it can be formatted on one line 156 if (scalar keys %$val == 2 and defined $val->{'.value'}) {157 print $val-> {'.value'}, "</", $key, ">\n";162 if (scalar keys %$val == 2 and $val->exists ('.value')) { 163 print $val->get ('.value'), "</", $key, ">\n"; 158 164 } else { 159 165 print "\n";
Note:
See TracChangeset
for help on using the changeset viewer.