1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
#! /usr/bin/env ruby
#
# Copyright (c) 2001 by Jim Menard <jimm@io.com>
#
# Released under the same license as Ruby. See
# http://www.ruby-lang.org/en/LICENSE.txt.
#
# Using the Tokenizer, this script prints an XML document with all
# text reversed. We achieve this by overriding Text#to_s to return its
# text reversed, then using the Tokenizer to step through the document
# so we can print each entity.
#
# usage:
#
# reverseText.rb <file.xml>
#
# Start looking for NQXML classes in the directory above this one.
# This forces us to use the local copy of NQXML, even if there is
# a previously installed version out there somewhere.
$LOAD_PATH[0, 0] = '..'
require 'nqxml/info' # For version string
require 'nqxml/tokenizer'
# Modify the Text class for our own nefarious purposes.
module NQXML
# Override Entity#to_s.
class Text
# Return the text string reversed. We replace all '<' with '<'
# so that the result is still legal in an XML document.
def to_s
return @text.reverse.gsub(/\</, '<')
end
end
end
# Fail with usage message if there is no command line argument.
if ARGV.length == 0
$stderr.puts "usage: #$0 <file.xml>"
exit(1)
end
# Print the current version of NQXML.
puts "NQXML version #{NQXML::Version}"
tokenizer = nil
begin
# Create the tokenizer.
tokenizer = NQXML::Tokenizer.new(File.new(ARGV[0]))
# Print each entity from the XML input stream. Text entities will
# print reversed.
tokenizer.each { | entity |
print entity.to_s
}
rescue NQXML::ParserError => ex
# Handle parser errors by printing the input line number and error
# message.
puts "\n NQXML parser error, line #{ex.line} column #{ex.column}: #{$!}"
end
|