| 1 | #!/usr/local/bin/perl -Ilib | ||
| 2 | |||
| 3 | ############################################################################ | ||
| 4 | # Copyright (c) 1998 Enno Derksen | ||
| 5 | # All rights reserved. | ||
| 6 | # This program is free software; you can redistribute it and/or modify it | ||
| 7 | # under the same terms as Perl itself. | ||
| 8 | ############################################################################ | ||
| 9 | |||
| 10 | $USAGE = <<END_USAGE; | ||
| 11 | Usage: $0 XQL-query-expr [ options ] [ file ... ] | ||
| 12 | |||
| 13 | file ...: One or more input file names. If no files are specified, | ||
| 14 | input is taken from STDIN. | ||
| 15 | -m: Modify XML file and write it out | ||
| 16 | (without -r, print to STDOUT) | ||
| 17 | -r: Replace XML file with modified file | ||
| 18 | -b ext: Backup the original file using the specified extension | ||
| 19 | -f format: Print results with printf using format | ||
| 20 | (default prints results one at a time, when not using -m) | ||
| 21 | -a count: (used with -f) feed results to printf in groups of 'count' | ||
| 22 | (normally, -f is smart enough to figure this out) | ||
| 23 | -o expr: Reorder argument group (when using -f), e.g. '1-3,0,4' | ||
| 24 | -h format: When printing results, print a header for each file | ||
| 25 | (uses printf, so use %s to print the filename) | ||
| 26 | -H format: Same as -h, but does not print a header if no results | ||
| 27 | were found in the file | ||
| 28 | -s format: Record header for each result or result set (for -h) | ||
| 29 | (uses printf, use %d to fill in record number (base is 0)) | ||
| 30 | -n: Don't print newline after each result (when not using -f or -m) | ||
| 31 | -p code: Execute (eval) the code before performing the query | ||
| 32 | -c spec: The spec string defines how Nodes are converted to strings | ||
| 33 | when printing. By default, they are printed as they where | ||
| 34 | found in the XML document. The spec string may consist | ||
| 35 | of one or more of the following substrings: | ||
| 36 | |||
| 37 | Ev Use value() for Element nodes | ||
| 38 | Et Use text() for Element nodes | ||
| 39 | Er Use rawText() for Element nodes | ||
| 40 | Etx Use text(0) for Element nodes, i.e. don't include sub Elements | ||
| 41 | Erx Use rawText(0) for Elements, i.e. don't include sub Elements | ||
| 42 | Av Use value() for Attribute nodes | ||
| 43 | At Use text() for Attribute nodes | ||
| 44 | Ar Use rawText() for Attribute nodes | ||
| 45 | Tt Use text() for text nodes (Text, CDATASection and EntityRefs) | ||
| 46 | Tr Use rawText() for text nodes | ||
| 47 | Nv Shortcut for EvAv (N is optional) | ||
| 48 | Nt Shortcut for EtAtTt (N is optional) | ||
| 49 | Ntx Shortcut for EtxAtTt (N is optional) | ||
| 50 | Nr Shortcut for ErArTr (N is optional) | ||
| 51 | Nrx Shortcut for ErxArTr (N is optional) | ||
| 52 | END_USAGE | ||
| 53 | |||
| 54 | # Leave this comment to de-confuse Emacs'; | ||
| 55 | |||
| 56 | use XML::XQL; | ||
| 57 | use XML::XQL::DOM; | ||
| 58 | use XML::XQL::DirXQL; | ||
| 59 | |||
| 60 | if (@ARGV < 1) | ||
| 61 | { | ||
| 62 | die "$USAGE\n"; | ||
| 63 | } | ||
| 64 | |||
| 65 | my $query_expr = shift @ARGV; | ||
| 66 | |||
| 67 | $opt_m = $opt_n = $opt_r = undef; # disable warnings with -w | ||
| 68 | |||
| 69 | use Getopt::Std; | ||
| 70 | getopts ("mrb:f:a:h:H:s:nc:p:o:"); | ||
| 71 | |||
| 72 | my @options = (); | ||
| 73 | |||
| 74 | sub extrapolate | ||
| 75 | { | ||
| 76 | my $str = shift; | ||
| 77 | my $res = eval "\"$str\""; # try double quotes first | ||
| 78 | return $res unless $@; | ||
| 79 | |||
| 80 | $res = eval "qq{$str}"; | ||
| 81 | return $res unless $@; | ||
| 82 | |||
| 83 | $res = eval "qq<$str>"; | ||
| 84 | return $res unless $@; | ||
| 85 | |||
| 86 | return $str; # all attempts failed | ||
| 87 | } | ||
| 88 | |||
| 89 | # Replace "\n" with newline etc. | ||
| 90 | $opt_h = extrapolate ($opt_h) if defined ($opt_h); | ||
| 91 | $opt_h = extrapolate ($opt_H) if defined ($opt_H); | ||
| 92 | $opt_s = extrapolate ($opt_s) if defined ($opt_s); | ||
| 93 | $opt_f = extrapolate ($opt_f) if defined ($opt_f); | ||
| 94 | |||
| 95 | if ($opt_f) | ||
| 96 | { | ||
| 97 | unless ($opt_a) | ||
| 98 | { | ||
| 99 | $opt_a = 0; | ||
| 100 | # count number of format parameters (i.e. number of single %) | ||
| 101 | while ($opt_f =~ /((%)|%%)/g) | ||
| 102 | { | ||
| 103 | defined($2) and $opt_a++; | ||
| 104 | } | ||
| 105 | } | ||
| 106 | } | ||
| 107 | |||
| 108 | if ($opt_c) | ||
| 109 | { | ||
| 110 | $E_recurse = 1; | ||
| 111 | my $c = 'N'; | ||
| 112 | |||
| 113 | for (my $i = 0; $i < length $opt_c; $i++) | ||
| 114 | { | ||
| 115 | $_ = substr ($opt_c, $i, 1); | ||
| 116 | |||
| 117 | /[EATN]/ and do { $c = $_; next; }; | ||
| 118 | /v/ and do { | ||
| 119 | $c =~ /[EN]/ and $E_v = 1; | ||
| 120 | $c =~ /[AN]/ and $A_v = 1; | ||
| 121 | next; | ||
| 122 | }; | ||
| 123 | /t/ and do { | ||
| 124 | $c =~ /[EN]/ and $E_t = 1; | ||
| 125 | $c =~ /[AN]/ and $A_t = 1; | ||
| 126 | $c =~ /[TN]/ and $T_t = 1; | ||
| 127 | next; | ||
| 128 | }; | ||
| 129 | /r/ and do { | ||
| 130 | $c =~ /[EN]/ and $E_r = 1; | ||
| 131 | $c =~ /[AN]/ and $A_r = 1; | ||
| 132 | $c =~ /[TN]/ and $T_r = 1; | ||
| 133 | next; | ||
| 134 | }; | ||
| 135 | /x/ and do { | ||
| 136 | $E_recurse = 0; | ||
| 137 | next; | ||
| 138 | }; | ||
| 139 | die "$0: unexpected character '$_' in -c option '$opt_c'\n"; | ||
| 140 | } | ||
| 141 | } | ||
| 142 | |||
| 143 | if ($opt_o) | ||
| 144 | { | ||
| 145 | # Prepare reordering array | ||
| 146 | @order = (0 .. $opt_a - 1); | ||
| 147 | |||
| 148 | # E.g. "1-3,0,4" | ||
| 149 | my $i = 0; | ||
| 150 | while ($opt_o =~ /(\d+)(-(\d+))?/g) | ||
| 151 | { | ||
| 152 | if (defined $3) # range | ||
| 153 | { | ||
| 154 | for (my $j = $1; $j <= $3; $j++) | ||
| 155 | { | ||
| 156 | $order[$j] = $i++; | ||
| 157 | } | ||
| 158 | } | ||
| 159 | else | ||
| 160 | { | ||
| 161 | $order[$1] = $i++; | ||
| 162 | } | ||
| 163 | } | ||
| 164 | } | ||
| 165 | |||
| 166 | sub reorder | ||
| 167 | { | ||
| 168 | my @par = (); | ||
| 169 | for (my $i = 0; $i < @order; $i++) | ||
| 170 | { | ||
| 171 | push @par, $_[$order[$i]]; | ||
| 172 | } | ||
| 173 | @par; | ||
| 174 | } | ||
| 175 | |||
| 176 | eval $opt_p if $opt_p; | ||
| 177 | die "$0: bad code (-p option) code=[$opt_p]: $@" if $@; | ||
| 178 | |||
| 179 | my $query; | ||
| 180 | eval { | ||
| 181 | $query = new XML::XQL::Query (Expr => $query_expr, @options); | ||
| 182 | }; | ||
| 183 | die "$0: invalid query expression: $@" if $@; | ||
| 184 | |||
| 185 | my $parser = new XML::DOM::Parser; | ||
| 186 | |||
| 187 | sub transform | ||
| 188 | { | ||
| 189 | my $val = shift; | ||
| 190 | |||
| 191 | return $val unless defined $val; # skip undef | ||
| 192 | |||
| 193 | my $type = ref($val); | ||
| 194 | return $val unless $type; # skip scalars | ||
| 195 | |||
| 196 | if ($type eq "ARRAY") | ||
| 197 | { | ||
| 198 | if (@$val == 0) # empty list / undef | ||
| 199 | { | ||
| 200 | #??? not sure what to do here | ||
| 201 | return "[]"; | ||
| 202 | } | ||
| 203 | else | ||
| 204 | { | ||
| 205 | #??? not sure what to do here | ||
| 206 | } | ||
| 207 | } | ||
| 208 | elsif ($val->isa ('XML::XQL::Node')) | ||
| 209 | { | ||
| 210 | my $nodeType = $val->xql_nodeType; | ||
| 211 | if ($nodeType == 1) # element node | ||
| 212 | { | ||
| 213 | if ($E_v) | ||
| 214 | { | ||
| 215 | return transform ($val->xql_value); | ||
| 216 | } | ||
| 217 | elsif ($E_t) | ||
| 218 | { | ||
| 219 | return $val->xql_text ($E_recurse); | ||
| 220 | } | ||
| 221 | elsif ($E_r) | ||
| 222 | { | ||
| 223 | return $val->xql_rawText ($E_recurse); | ||
| 224 | } | ||
| 225 | } | ||
| 226 | elsif ($nodeType == 2) # attribute node | ||
| 227 | { | ||
| 228 | if ($A_v) | ||
| 229 | { | ||
| 230 | return transform ($val->xql_value); | ||
| 231 | } | ||
| 232 | elsif ($A_t) | ||
| 233 | { | ||
| 234 | return $val->xql_text; | ||
| 235 | } | ||
| 236 | elsif ($A_r) | ||
| 237 | { | ||
| 238 | return $val->xql_rawText; | ||
| 239 | } | ||
| 240 | } | ||
| 241 | elsif ($nodeType == 3) # text node (also CDATASection, EntityRef) | ||
| 242 | { | ||
| 243 | if ($T_t) | ||
| 244 | { | ||
| 245 | return $val->xql_text; | ||
| 246 | } | ||
| 247 | elsif ($T_r) | ||
| 248 | { | ||
| 249 | return $val->xql_rawText; | ||
| 250 | } | ||
| 251 | } | ||
| 252 | $val->xql_xmlString; | ||
| 253 | } | ||
| 254 | elsif ($val->isa ('XML::XQL::PrimitiveType')) | ||
| 255 | { | ||
| 256 | #?? could add xql_normalString | ||
| 257 | $val->xql_toString; | ||
| 258 | } | ||
| 259 | else # ??? | ||
| 260 | { | ||
| 261 | "$val"; | ||
| 262 | } | ||
| 263 | } | ||
| 264 | |||
| 265 | sub solveQuery | ||
| 266 | { | ||
| 267 | my ($dom, $file) = @_; | ||
| 268 | |||
| 269 | my @result = $query->solve ($dom); | ||
| 270 | |||
| 271 | if ($opt_m) | ||
| 272 | { | ||
| 273 | #?? what if no results | ||
| 274 | |||
| 275 | if ($opt_b) | ||
| 276 | { | ||
| 277 | # backup original XML file | ||
| 278 | my $bak_file = $file . $opt_b; | ||
| 279 | unless (rename ($file, $bak_file)) | ||
| 280 | { | ||
| 281 | warn "$0: can't backup $file to $bak_file (skipping)"; | ||
| 282 | next; | ||
| 283 | } | ||
| 284 | } | ||
| 285 | if ($opt_r) # replace original file | ||
| 286 | { | ||
| 287 | eval { | ||
| 288 | $dom->printToFile ($file); | ||
| 289 | }; | ||
| 290 | if ($@) | ||
| 291 | { | ||
| 292 | warn "$0: can't open $file for writing (skipping): $@"; | ||
| 293 | next; | ||
| 294 | } | ||
| 295 | } | ||
| 296 | else # print modified file to STDOUT | ||
| 297 | { | ||
| 298 | $dom->printToFileHandle (\*STDOUT); | ||
| 299 | } | ||
| 300 | } | ||
| 301 | else # print query results | ||
| 302 | { | ||
| 303 | # transform query results | ||
| 304 | @result = map { transform ($_) } @result; | ||
| 305 | |||
| 306 | # print file header | ||
| 307 | # (don't print it if -H was specified and no results were found) | ||
| 308 | printf ($opt_h, $file) if $opt_h && (!$opt_H || @result); | ||
| 309 | |||
| 310 | if ($opt_f) | ||
| 311 | { | ||
| 312 | my $j = 0; | ||
| 313 | my $ii = $opt_a - 1; | ||
| 314 | for (my $i = 0; $i < @result; $i += $opt_a, $ii += $opt_a, $j++) | ||
| 315 | { | ||
| 316 | printf ($opt_s, $j) if $opt_s; # record header | ||
| 317 | |||
| 318 | my @par = @result[$i .. $ii]; | ||
| 319 | @par = reorder (@par) if $opt_o; | ||
| 320 | printf ($opt_f, @par); | ||
| 321 | } | ||
| 322 | } | ||
| 323 | else | ||
| 324 | { | ||
| 325 | for (my $i = 0; $i < @result; $i++) | ||
| 326 | { | ||
| 327 | printf ($opt_s, $i) if $opt_s; # record header | ||
| 328 | print $result[$i]; | ||
| 329 | print "\n" unless $opt_n; | ||
| 330 | } | ||
| 331 | } | ||
| 332 | } | ||
| 333 | } | ||
| 334 | |||
| 335 | if (@ARGV) | ||
| 336 | { | ||
| 337 | for my $file (@ARGV) | ||
| 338 | { | ||
| 339 | my $dom = $parser->parsefile ($file); | ||
| 340 | if ($@) | ||
| 341 | { | ||
| 342 | warn "$0: bad XML file '$file' (skipping)"; | ||
| 343 | next; | ||
| 344 | } | ||
| 345 | solveQuery ($dom, $file); | ||
| 346 | $dom->dispose; | ||
| 347 | } | ||
| 348 | } | ||
| 349 | else # read from STDIN | ||
| 350 | { | ||
| 351 | my $dom = $parser->parse (*STDIN); | ||
| 352 | solveQuery ($dom, "(input)"); | ||
| 353 | $dom->dispose; | ||
| 354 | } |