require 'graphviz'
require 'rexml/document'
class GraphViz
class XML
attr_accessor :graph
def output( *options )
warn "GraphViz::XML.output is deprecated, use GraphViz::XML.graph.output"
@graph.output( *options )
end
private
def initialize( xml_file, *options )
@node_name = "00000"
@show_text = true
@show_attributes = true
if options and options[0]
options[0].each do |xKey, xValue|
case xKey.to_s
when "text"
@show_text = xValue
options[0].delete( xKey )
when "attrs"
@show_attributes = xValue
options[0].delete( xKey )
end
end
end
@rexml_document = REXML::Document::new( File::new( xml_file ) )
@graph = GraphViz::new( "XML", *options )
parse_xml_node( @rexml_document.root() )
end
def parse_xml_node( xml_node ) local_node_name = @node_name.clone
@node_name.succ!
label = xml_node.name
if xml_node.has_attributes? and @show_attributes
label = "{ " + xml_node.name
xml_node.attributes.each do |xName, xValue|
label << "| { #{xName} | #{xValue} } "
end
label << "}"
end
@graph.add_nodes( local_node_name, "label" => label, "color" => "blue", "shape" => "record" )
if xml_node.has_text? and @show_text
text_node_name = local_node_name.clone
text_node_name << "111"
xText = ""
xSep = ""
xml_node.texts().each do |l|
x = l.value.chomp.strip
if x.length > 0
xText << xSep << x
xSep = "\n"
end
end
if xText.length > 0
@graph.add_nodes( text_node_name, "label" => xText, "color" => "black", "shape" => "ellipse" )
@graph.add_edges( local_node_name, text_node_name )
end
end
xml_node.each_element( ) do |xml_child_node|
child_node_name = parse_xml_node( xml_child_node )
@graph.add_edges( local_node_name, child_node_name )
end
return( local_node_name )
end
end
end