Class: StructuredDataToSql::MysqlDumpXml::XMLStreamHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/structured_data_to_sql/mysql_dump_xml/sax_parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(&event_handler) ⇒ XMLStreamHandler

Returns a new instance of XMLStreamHandler.



9
10
11
12
13
14
15
16
# File 'lib/structured_data_to_sql/mysql_dump_xml/sax_parser.rb', line 9

def initialize(&event_handler)
  @event_handler = event_handler
  @current_structure = nil
  @current_table_data = nil
  @current_row = nil
  @current_field_name = nil
  @current_field_text = nil
end

Instance Method Details

#cdata(value) ⇒ Object



40
41
42
# File 'lib/structured_data_to_sql/mysql_dump_xml/sax_parser.rb', line 40

def cdata(value)
  @current_field_text << value if @current_field_name
end

#tag_end(name) ⇒ Object



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
# File 'lib/structured_data_to_sql/mysql_dump_xml/sax_parser.rb', line 44

def tag_end(name)
  case name
  when "table_structure"
    if @current_structure
      @event_handler.call(
        :structure,
        @current_structure.name,
        @current_structure
      )
      @current_structure = nil
    end
  when "field"
    @current_row[
      @current_field_name
    ] = @current_field_text if @current_field_name && @current_row
    @current_field_name = nil
    @current_field_text = nil
  when "row"
    if @current_table_data && @current_row
      @event_handler.call(:row, @current_table_data, @current_row)
    end
    @current_row = nil
  when "table_data"
    @current_table_data = nil
  end
end

#tag_start(name, attrs) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/structured_data_to_sql/mysql_dump_xml/sax_parser.rb', line 18

def tag_start(name, attrs)
  case name
  when "table_structure"
    @current_structure = TableStructure.new(attrs["name"].to_s)
  when "table_data"
    @current_table_data = attrs["name"].to_s
  when "row"
    @current_row = {}
  when "field"
    if @current_structure
      @current_structure.add_column(attrs)
    elsif @current_table_data
      @current_field_name = attrs["name"].to_s
      @current_field_text = +""
    end
  end
end

#text(value) ⇒ Object



36
37
38
# File 'lib/structured_data_to_sql/mysql_dump_xml/sax_parser.rb', line 36

def text(value)
  @current_field_text << value if @current_field_name
end