diff options
author | Hans Hagen <pragma@wxs.nl> | 2006-05-10 23:26:00 +0200 |
---|---|---|
committer | Hans Hagen <pragma@wxs.nl> | 2006-05-10 23:26:00 +0200 |
commit | b2b303273a3e05aaea01777309356ae8aeb7a8bf (patch) | |
tree | 517313f22051964dfd783868825958d7ce043fac /scripts/context/ruby/rscortool.rb | |
parent | 88eab06af697b8d0fd8ef6d7762eae4901b191d2 (diff) | |
download | context-b2b303273a3e05aaea01777309356ae8aeb7a8bf.tar.gz |
stable 2006.05.10 23:26
Diffstat (limited to 'scripts/context/ruby/rscortool.rb')
-rw-r--r-- | scripts/context/ruby/rscortool.rb | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/scripts/context/ruby/rscortool.rb b/scripts/context/ruby/rscortool.rb new file mode 100644 index 000000000..c656fed85 --- /dev/null +++ b/scripts/context/ruby/rscortool.rb @@ -0,0 +1,63 @@ +# program : rscortool +# copyright : PRAGMA Publishing On Demand +# version : 1.00 - 2002 +# author : Hans Hagen +# +# project : eXaMpLe +# concept : Hans Hagen +# info : j.hagen@xs4all.nl +# www : www.pragma-pod.com / www.pragma-ade.com + +require 'rexml/document.rb' + +class Array + + def downcase + self.collect { |l| l.to_s.downcase } + end + +end + +class SortedXML + + def initialize (filename) + return nil if not filename or filename.empty? or not test(?e,filename) + @data = REXML::Document.new(File.new(filename), + {:ignore_whitespace_nodes => :all, + :compress_whitespace => :all}) + end + + def save (filename) + # filename += '.xml' unless filename.match(/\..*?$/) + filename += '.xml' unless filename =~ /\..*?$/ + if not filename.empty? and f = open(filename,'w') + @data.write(f,0) + f.close + end + end + + def sort + keys = REXML::XPath.match(@data.root,"/contacts/contact/@label") + return unless keys + keys = keys.downcase + records = @data.elements.to_a("/contacts/contact") + @data.elements.delete_all("/contacts/contact") + keys = keys.collect do |l| # prepare numbers + l.gsub(/(\d+)/) do |d| sprintf('%05d', d) end + end + keys.sort.each do |s| + @data.root.add_element(records[keys.index(s)]) + end + end + +end + +def sortfile (filename) + c = SortedXML.new(filename) + c.sort + c.save('test.xml') +end + +exit if ARGV[0] == nil or ARGV[0].empty? + +sortfile(ARGV[0]) |