ruby-graphml 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/Rakefile +1 -0
- data/lib/ruby-graphml.rb +183 -0
- data/ruby-graphml.gemspec +22 -0
- metadata +61 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/ruby-graphml.rb
ADDED
@@ -0,0 +1,183 @@
|
|
1
|
+
require 'nokogiri'
|
2
|
+
|
3
|
+
module GraphML
|
4
|
+
VERSION = "0.0.1"
|
5
|
+
|
6
|
+
class Key
|
7
|
+
attr_accessor :id, :for, :name, :type, :desc, :default
|
8
|
+
def initialize(attrs)
|
9
|
+
attrs.each do |key, val|
|
10
|
+
case key
|
11
|
+
when 'id'
|
12
|
+
@id = val
|
13
|
+
when 'for'
|
14
|
+
@for = val
|
15
|
+
when 'attr.name'
|
16
|
+
@name = val
|
17
|
+
when 'attr.type'
|
18
|
+
@type = val
|
19
|
+
end
|
20
|
+
end
|
21
|
+
@desc = ''
|
22
|
+
@default = ''
|
23
|
+
end
|
24
|
+
|
25
|
+
def append(key, val)
|
26
|
+
if key == 'desc'
|
27
|
+
@desc += val
|
28
|
+
elsif key == 'default'
|
29
|
+
@default += val
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
class Edge
|
35
|
+
def initialize(attrs, graph)
|
36
|
+
attrs.each do |key, val|
|
37
|
+
if key == 'source'
|
38
|
+
@source = val
|
39
|
+
elsif key == 'target'
|
40
|
+
@target = val
|
41
|
+
end
|
42
|
+
end
|
43
|
+
@graph = graph
|
44
|
+
source.out_edges << self
|
45
|
+
target.in_edges << self
|
46
|
+
end
|
47
|
+
|
48
|
+
def source
|
49
|
+
@graph.nodes[@source]
|
50
|
+
end
|
51
|
+
|
52
|
+
def target
|
53
|
+
@graph.nodes[@target]
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
class Node
|
58
|
+
attr_accessor :id, :data, :in_edges, :out_edges
|
59
|
+
def initialize(attrs, graph)
|
60
|
+
attrs.each do |key, val|
|
61
|
+
if key == 'id'
|
62
|
+
@id = val
|
63
|
+
end
|
64
|
+
end
|
65
|
+
@data = {}
|
66
|
+
@in_edges = []
|
67
|
+
@out_edges = []
|
68
|
+
@graph = graph
|
69
|
+
end
|
70
|
+
|
71
|
+
def key(name)
|
72
|
+
key_obj = @graph.key_names[name]
|
73
|
+
val = @data[key_obj.id]
|
74
|
+
return key_obj.default if val.nil?
|
75
|
+
val
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
class Graph
|
80
|
+
attr_accessor :keys, :key_names, :nodes, :edges
|
81
|
+
|
82
|
+
def initialize
|
83
|
+
@keys = {}
|
84
|
+
@key_names = {}
|
85
|
+
@nodes = {}
|
86
|
+
@edges = []
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
class Parser < Nokogiri::XML::SAX::Document
|
91
|
+
attr_reader :graph
|
92
|
+
|
93
|
+
def self.parse!(path)
|
94
|
+
parser = self.new
|
95
|
+
graph = parser.graph
|
96
|
+
parser = Nokogiri::XML::SAX::Parser.new(parser).parse(File.open(path, 'rb'))
|
97
|
+
graph
|
98
|
+
end
|
99
|
+
|
100
|
+
def initialize
|
101
|
+
@current_node = nil
|
102
|
+
@current_key = nil
|
103
|
+
@current_edge = nil
|
104
|
+
@state = nil
|
105
|
+
@sub_state = nil
|
106
|
+
@graph = Graph.new
|
107
|
+
end
|
108
|
+
|
109
|
+
def start_element(name, attrs=[])
|
110
|
+
case @state
|
111
|
+
# creating a new key, node or edge
|
112
|
+
when nil
|
113
|
+
case name
|
114
|
+
when 'key'
|
115
|
+
@current_key = Key.new(attrs)
|
116
|
+
@graph.keys[@current_key.id] = @current_key
|
117
|
+
@graph.key_names[@current_key.name] = @current_key
|
118
|
+
when 'node'
|
119
|
+
@current_node = Node.new(attrs, @graph)
|
120
|
+
@graph.nodes[@current_node.id] = @current_node
|
121
|
+
when 'edge'
|
122
|
+
@current_edge = Edge.new(attrs, @graph)
|
123
|
+
@graph.edges << @current_edge
|
124
|
+
else
|
125
|
+
return
|
126
|
+
end
|
127
|
+
@state = name
|
128
|
+
@sub_state = nil
|
129
|
+
|
130
|
+
# adding to a key
|
131
|
+
when 'key'
|
132
|
+
raise 'Missing key object' if @current_key.nil?
|
133
|
+
@sub_state = name
|
134
|
+
|
135
|
+
# adding a key value to a node
|
136
|
+
when 'node'
|
137
|
+
raise 'Missing node object' if @current_node.nil?
|
138
|
+
raise 'Unexpected attribute' if attrs[0][0] != 'key'
|
139
|
+
@sub_state = attrs[0][1]
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
def characters(string)
|
144
|
+
return if @sub_state.nil?
|
145
|
+
case @state
|
146
|
+
when 'key'
|
147
|
+
raise 'Missing key object' if @current_key.nil?
|
148
|
+
@current_key.append(@sub_state, string)
|
149
|
+
when 'node'
|
150
|
+
raise 'Missing node object' if @current_node.nil?
|
151
|
+
@current_node.data[@sub_state] ||= ''
|
152
|
+
@current_node.data[@sub_state] += string
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
def end_element(name)
|
157
|
+
case @state
|
158
|
+
when 'node'
|
159
|
+
if name == 'node'
|
160
|
+
@current_node = nil
|
161
|
+
@state = nil
|
162
|
+
else
|
163
|
+
@sub_state = nil
|
164
|
+
end
|
165
|
+
when 'edge'
|
166
|
+
if name == 'edge'
|
167
|
+
@current_edge = nil
|
168
|
+
@state = nil
|
169
|
+
else
|
170
|
+
@sub_state = nil
|
171
|
+
end
|
172
|
+
when 'key'
|
173
|
+
if name == 'key'
|
174
|
+
@current_key = nil
|
175
|
+
@state = nil
|
176
|
+
else
|
177
|
+
@sub_state = nil
|
178
|
+
end
|
179
|
+
end
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "ruby-graphml"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "ruby-graphml"
|
7
|
+
s.version = GraphML::VERSION
|
8
|
+
s.authors = ["Will"]
|
9
|
+
s.email = ["me@willcannings.com"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{Ruby GraphML Parser}
|
12
|
+
s.description = %q{Ruby GraphML Parser}
|
13
|
+
|
14
|
+
s.files = `git ls-files`.split("\n")
|
15
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
16
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
|
19
|
+
# specify any dependencies here; for example:
|
20
|
+
# s.add_development_dependency "rspec"
|
21
|
+
s.add_runtime_dependency "nokogiri"
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruby-graphml
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Will
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-10-25 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: nokogiri
|
16
|
+
requirement: &70124370008660 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70124370008660
|
25
|
+
description: Ruby GraphML Parser
|
26
|
+
email:
|
27
|
+
- me@willcannings.com
|
28
|
+
executables: []
|
29
|
+
extensions: []
|
30
|
+
extra_rdoc_files: []
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- Gemfile
|
34
|
+
- Rakefile
|
35
|
+
- lib/ruby-graphml.rb
|
36
|
+
- ruby-graphml.gemspec
|
37
|
+
homepage: ''
|
38
|
+
licenses: []
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options: []
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
none: false
|
45
|
+
requirements:
|
46
|
+
- - ! '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
requirements: []
|
56
|
+
rubyforge_project:
|
57
|
+
rubygems_version: 1.8.10
|
58
|
+
signing_key:
|
59
|
+
specification_version: 3
|
60
|
+
summary: Ruby GraphML Parser
|
61
|
+
test_files: []
|