-
Notifications
You must be signed in to change notification settings - Fork 6
/
XMLDocument.rb
executable file
·84 lines (67 loc) · 1.8 KB
/
XMLDocument.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# XMLDocument.rb
# Leonhard
#
# Created by greg on 03/11/10.
# Copyright 2010, 2011 Grégoire Lejeune. All rights reserved.
require 'rexml/document'
class XMLDocument
@oReXML
@oGraph
@xNodeName
@bShowText
@bShowAttrs
def dot
@oGraph
end
private
#
# Create a graph from a XML file
#
def initialize(xFile)
@xNodeName = "00000"
@bShowText = true
@bShowAttrs = true
@oReXML = REXML::Document::new( File::new( xFile ) )
@oGraph = "digraph XML {\n"
_init( @oReXML.root() )
@oGraph << "}\n"
end
def _init( oXMLNode ) #:nodoc:
xLocalNodeName = @xNodeName.clone
@xNodeName.succ!
label = oXMLNode.name
if oXMLNode.has_attributes? == true and @bShowAttrs == true
label = "{ " + oXMLNode.name
oXMLNode.attributes.each do |xName, xValue|
label << "| { #{xName} | #{xValue} } "
end
label << "}"
end
@oGraph << " #{xLocalNodeName}[label=\"#{label}\", color=\"blue\", shape=\"record\"];\n"
## Act: Search and add Text nodes
if oXMLNode.has_text? == true and @bShowText == true
xTextNodeName = xLocalNodeName.clone
xTextNodeName << "111"
xText = ""
xSep = ""
oXMLNode.texts().each do |l|
x = l.value.chomp.strip
if x.length > 0
xText << xSep << x
xSep = "\n"
end
end
if xText.length > 0
@oGraph << " #{xTextNodeName}[label=\"#{xText}\", color=\"black\", shape=\"ellipse\"];\n"
@oGraph << " #{xLocalNodeName} -> #{xTextNodeName}\n"
end
end
## Act: Search and add attributs
## TODO
oXMLNode.each_element( ) do |oXMLChild|
xChildNodeName = _init( oXMLChild )
@oGraph << " #{xLocalNodeName} -> #{xChildNodeName}\n"
end
return( xLocalNodeName )
end
end