lib/graphviz/graphml.rb in ruby-graphviz-1.0.5 vs lib/graphviz/graphml.rb in ruby-graphviz-1.0.6
- old
+ new
@@ -17,10 +17,13 @@
require 'graphviz'
require 'rexml/document'
class GraphViz
+ class GraphMLError < RuntimeError
+ end
+
class GraphML
attr_reader :attributes
def attributs
warn "`GraphViz::GraphML#attributs` is deprecated, please, use `GraphViz::GraphML#attributes`"
return @attributes
@@ -28,14 +31,18 @@
# The GraphViz object
attr_accessor :graph
DEST = {
- 'node' => [:nodes],
- 'edge' => [:edges],
- 'graph' => [:graphs],
- 'all' => [:nodes, :edges, :graphs]
+ 'node' => [:nodes],
+ 'edge' => [:edges],
+ 'graph' => [:graphs],
+ 'graphml' => [:graphml],
+ 'hyperedge' => [:hyperedge],
+ 'port' => [:port],
+ 'endpoint' => [:endpoint],
+ 'all' => [:nodes, :edges, :graphs, :graphml, :hyperedge, :port, :endpoint]
}
GTYPE = {
'directed' => :digraph,
'undirected' => :graph
@@ -46,62 +53,71 @@
data = ((File.file?( file_or_str )) ? File::new(file_or_str) : file_or_str)
@xmlDoc = REXML::Document::new( data )
@attributes = {
:nodes => {},
:edges => {},
- :graphs => {}
+ :graphs => {},
+ :graphml => {},
+ :endpoint => {},
+ :port => {},
+ :hyperedge => {}
}
+ @ignored_keys = []
@graph = nil
@current_attr = nil
@current_node = nil
@current_edge = nil
@current_graph = nil
parse( @xmlDoc.root )
end
def parse( node ) #:nodoc:
- #begin
- send( node.name.to_sym, node )
- #rescue NoMethodError => e
- # raise "ERROR node #{node.name} can be root"
- #end
+ send( node.name.to_sym, node )
end
def graphml( node ) #:nodoc:
node.each_element( ) do |child|
- #begin
+ begin
send( "graphml_#{child.name}".to_sym, child )
- #rescue NoMethodError => e
- # raise "ERROR node #{child.name} can be child of graphml"
- #end
+ rescue NoMethodError => e
+ raise GraphMLError, "node #{child.name} can be child of graphml"
+ end
end
end
+
+ def graphml_data(node)
+ warn "graphml/data not supported!"
+ end
def graphml_key( node ) #:nodoc:
id = node.attributes['id']
@current_attr = {
:name => node.attributes['attr.name'],
:type => node.attributes['attr.type']
}
- DEST[node.attributes['for']].each do |d|
- @attributes[d][id] = @current_attr
- end
-
- node.each_element( ) do |child|
- begin
- send( "graphml_key_#{child.name}".to_sym, child )
- rescue NoMethodError => e
- raise "ERROR node #{child.name} can be child of graphml"
+ if @current_attr[:name].nil?
+ @ignored_keys << id
+ else
+ DEST[node.attributes['for']].each do |d|
+ @attributes[d][id] = @current_attr
end
+
+ node.each_element( ) do |child|
+ begin
+ send( "graphml_key_#{child.name}".to_sym, child )
+ rescue NoMethodError => e
+ raise GraphMLError, "node #{child.name} can be child of key"
+ end
+ end
end
@current_attr = nil
end
def graphml_key_default( node ) #:nodoc:
- @current_attr[:default] = node.texts().to_s
+ @current_attr[:default] = node.texts().join('\n')
end
def graphml_graph( node ) #:nodoc:
@current_node = nil
@@ -113,32 +129,44 @@
previous_graph = @current_graph
@current_graph = previous_graph.add_graph( node.attributes['id'] )
end
@attributes[:graphs].each do |id, data|
- @current_graph.graph[data[:name]] = data[:default] if data.has_key?(:default)
+ begin
+ @current_graph.graph[data[:name]] = data[:default] if data.has_key?(:default)
+ rescue ArgumentError => e
+ warn e
+ end
end
@attributes[:nodes].each do |id, data|
- @current_graph.node[data[:name]] = data[:default] if data.has_key?(:default)
+ begin
+ @current_graph.node[data[:name]] = data[:default] if data.has_key?(:default)
+ rescue ArgumentError => e
+ warn e
+ end
end
@attributes[:edges].each do |id, data|
- @current_graph.edge[data[:name]] = data[:default] if data.has_key?(:default)
+ begin
+ @current_graph.edge[data[:name]] = data[:default] if data.has_key?(:default)
+ rescue ArgumentError => e
+ warn e
+ end
end
node.each_element( ) do |child|
- #begin
- send( "graphml_graph_#{child.name}".to_sym, child )
- #rescue NoMethodError => e
- # raise "ERROR node #{child.name} can be child of graphml"
- #end
+ send( "graphml_graph_#{child.name}".to_sym, child )
end
@current_graph = previous_graph
end
def graphml_graph_data( node ) #:nodoc:
- @current_graph[@attributes[:graphs][node.attributes['key']][:name]] = node.texts().to_s
+ begin
+ @current_graph[@attributes[:graphs][node.attributes['key']][:name]] = node.texts().join('\n')
+ rescue ArgumentError => e
+ warn e
+ end
end
def graphml_graph_node( node ) #:nodoc:
@current_node = {}
@@ -148,28 +176,36 @@
graphml_graph( child )
else
begin
send( "graphml_graph_node_#{child.name}".to_sym, child )
rescue NoMethodError => e
- raise "ERROR node #{child.name} can be child of graphml"
+ raise GraphMLError, "node #{child.name} can be child of node"
end
end
end
unless @current_node.nil?
node = @current_graph.add_nodes( node.attributes['id'] )
@current_node.each do |k, v|
- node[k] = v
+ begin
+ node[k] = v
+ rescue ArgumentError => e
+ warn e
+ end
end
end
@current_node = nil
end
def graphml_graph_node_data( node ) #:nodoc:
- #@current_node[@attributes[:nodes][node.attributes['key']][:name]] = node.texts().to_s
- @current_node[@attributes[:nodes][node.attributes['key']][:name]] = node.texts().join(sep='\n',)
+ return if @ignored_keys.include?(node.attributes['key'])
+ begin
+ @current_node[@attributes[:nodes][node.attributes['key']][:name]] = node.texts().join('\n')
+ rescue ArgumentError => e
+ warn e
+ end
end
def graphml_graph_node_port( node ) #:nodoc:
@current_node[:shape] = :record
port = node.attributes['name']
@@ -188,21 +224,26 @@
target = {target => node.attributes['targetport']} if node.attributes['targetport']
@current_edge = @current_graph.add_edges( source, target )
node.each_element( ) do |child|
- #begin
+ begin
send( "graphml_graph_edge_#{child.name}".to_sym, child )
- #rescue NoMethodError => e
- # raise "ERROR node #{child.name} can be child of graphml"
- #end
+ rescue NoMethodError => e
+ raise GraphMLError, "node #{child.name} can be child of edge"
+ end
end
@current_edge = nil
end
def graphml_graph_edge_data( node ) #:nodoc:
- @current_edge[@attributes[:edges][node.attributes['key']][:name]] = node.texts().to_s
+ return if @ignored_keys.include?(node.attributes['key'])
+ begin
+ @current_edge[@attributes[:edges][node.attributes['key']][:name]] = node.texts().join('\n')
+ rescue ArgumentError => e
+ warn e
+ end
end
def graphml_graph_hyperedge( node ) #:nodoc:
list = []