Pistos (owner)

Revisions

gist: 78614 Download_button fork
public
Public Clone URL: git://gist.github.com/78614.git
Embed All Files: show embed
xml-genealogy.rb #
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
#!/usr/bin/env ruby19
 
require 'rexml/document'
 
class XMLGenealogy
  INDENTATION = ' '
 
  def initialize( argv )
    @namespace_uri = argv[ 0 ]
    @dir_mask = argv[ 1 ]
    if @namespace_uri.nil? || @namespace_uri == '--help' || @dir_mask.nil?
      puts "#{$0} <namespace URI> [dir mask]"
      exit 1
    end
    @ancestry = Hash.new { |hash,key| hash[ key ] = Array.new }
    @genealogy = Hash.new { |hash,key| hash[ key ] = Hash.new }
  end
 
  def spider
    Dir[ @dir_mask ].each do |f|
      doc = REXML::Document.new File.new( f )
      REXML::XPath.each(
        doc,
        "//*[namespace-uri()='#{@namespace_uri}' or @*[namespace-uri()='#{@namespace_uri}'] ]"
      ).each do |element|
        p = element.parent
        @ancestry[ "#{element.prefix}:#{element.name}" ] << "#{p.prefix}:#{p.name}"
      end
      break # debugging
    end
 
    @ancestry.each do |element,seen_parents|
      seen_parents.each do |parent|
        @genealogy[ parent ][ element ] = true
      end
    end
  end
 
  def print_children( parent, level )
    puts "#{INDENTATION*level}#{parent}"
    @genealogy[ parent ].keys.sort.each do |child|
      print_children( child, level + 1 )
    end
  end
 
  def report
    @genealogy.each_key do |parent|
      print_children parent, 0
    end
  end
end
 
xg = XMLGenealogy.new( ARGV.dup )
xg.spider
xg.report