Class: GraphML::Parser

Inherits:
Nokogiri::XML::SAX::Document
  • Object
show all
Defined in:
lib/ruby-graphml.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeParser

Returns a new instance of Parser.



100
101
102
103
104
105
106
107
# File 'lib/ruby-graphml.rb', line 100

def initialize
  @current_node = nil
  @current_key = nil
  @current_edge = nil
  @state = nil
  @sub_state = nil
  @graph = Graph.new
end

Instance Attribute Details

#graphObject (readonly)

Returns the value of attribute graph.



91
92
93
# File 'lib/ruby-graphml.rb', line 91

def graph
  @graph
end

Class Method Details

.parse!(path) ⇒ Object



93
94
95
96
97
98
# File 'lib/ruby-graphml.rb', line 93

def self.parse!(path)
  parser = self.new
  graph  = parser.graph
  parser = Nokogiri::XML::SAX::Parser.new(parser).parse(File.open(path, 'rb'))
  graph
end

Instance Method Details

#characters(string) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/ruby-graphml.rb', line 143

def characters(string)
  return if @sub_state.nil?
  case @state
  when 'key'
    raise 'Missing key object' if @current_key.nil?
    @current_key.append(@sub_state, string)
  when 'node'
    raise 'Missing node object' if @current_node.nil?
    @current_node.data[@sub_state] ||= ''
    @current_node.data[@sub_state] += string
  end
end

#end_element(name) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/ruby-graphml.rb', line 156

def end_element(name)
  case @state
  when 'node'
    if name == 'node'
      @current_node = nil
      @state = nil
    else
      @sub_state = nil
    end
  when 'edge'
    if name == 'edge'
      @current_edge = nil
      @state = nil
    else
      @sub_state = nil
    end        
  when 'key'
    if name == 'key'
      @current_key = nil
      @state = nil
    else
      @sub_state = nil
    end
  end
end

#start_element(name, attrs = []) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/ruby-graphml.rb', line 109

def start_element(name, attrs=[])
  case @state
  # creating a new key, node or edge
  when nil
    case name
    when 'key'
      @current_key = Key.new(attrs)
      @graph.keys[@current_key.id] = @current_key
      @graph.key_names[@current_key.name] = @current_key
    when 'node'
      @current_node = Node.new(attrs, @graph)
      @graph.nodes[@current_node.id] = @current_node
    when 'edge'
      @current_edge = Edge.new(attrs, @graph)
      @graph.edges << @current_edge
    else
      return
    end
    @state = name
    @sub_state = nil
    
  # adding to a key
  when 'key'
    raise 'Missing key object' if @current_key.nil?
    @sub_state = name
  
  # adding a key value to a node
  when 'node'
    raise 'Missing node object' if @current_node.nil?
    raise 'Unexpected attribute' if attrs[0][0] != 'key'
    @sub_state = attrs[0][1]
  end
end