Class: Rosemary::Parser

Inherits:
HTTParty::Parser
  • Object
show all
Includes:
LibXML::XML::SaxParser::Callbacks
Defined in:
lib/rosemary/parser.rb

Overview

The XML parser capable of understanding the custom OSM XML format.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#collectionObject

Returns the value of attribute collection.



8
9
10
# File 'lib/rosemary/parser.rb', line 8

def collection
  @collection
end

#contextObject

Returns the value of attribute context.



8
9
10
# File 'lib/rosemary/parser.rb', line 8

def context
  @context
end

#descriptionObject

Returns the value of attribute description.



8
9
10
# File 'lib/rosemary/parser.rb', line 8

def description
  @description
end

#langObject

Returns the value of attribute lang.



8
9
10
# File 'lib/rosemary/parser.rb', line 8

def lang
  @lang
end

Instance Method Details

#coderObject



19
20
21
# File 'lib/rosemary/parser.rb', line 19

def coder
  @coder ||= HTMLEntities.new
end

#on_characters(chars) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/rosemary/parser.rb', line 99

def on_characters(chars)
  case @context.class.name
  when 'Rosemary::User'
    @context.description = chars if @description
    @context.languages << chars if @lang
  when 'Rosemary::Note'
    @context.id = chars if @id
    @context.text << chars if @text
    @context.user = chars if @user
    @context.action = chars if @action
  end
end

#on_end_documentObject

:nodoc:



50
51
52
# File 'lib/rosemary/parser.rb', line 50

def on_end_document     # :nodoc:
  end_document if respond_to?(:end_document)
end

#on_end_element(name) ⇒ Object

:nodoc:



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/rosemary/parser.rb', line 87

def on_end_element(name)   # :nodoc:
  case name
  when 'description'  then @description = false
  when 'lang'         then @lang        = false
  when 'id'           then @id          = false
  when 'text'         then @text        = false
  when 'action'       then @action      = false
  when 'user'         then @user        = false
  when 'changeset'    then _end_changeset
  end
end

#on_start_documentObject

:nodoc:



45
46
47
48
# File 'lib/rosemary/parser.rb', line 45

def on_start_document   # :nodoc:
  @collection = []
  start_document if respond_to?(:start_document)
end

#on_start_element(name, attr_hash) ⇒ Object

:nodoc:



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
84
85
# File 'lib/rosemary/parser.rb', line 54

def on_start_element(name, attr_hash)   # :nodoc:
  case @context.class.name
  when 'Rosemary::User'
    case name
    when 'description'  then @description = true
    when 'lang'         then @lang        = true
    end
  when 'Rosemary::Note'
    case name
    when 'id'           then @id          = true
    when 'text'         then @text        = true
    when 'user'         then @user        = true
    when 'action'       then @action      = true
    end
  else
    case name
    when 'node'         then _start_node(attr_hash)
    when 'way'          then _start_way(attr_hash)
    when 'relation'     then _start_relation(attr_hash)
    when 'changeset'    then _start_changeset(attr_hash)
    when 'user'         then _start_user(attr_hash)
    when 'tag'          then _tag(attr_hash)
    when 'nd'           then _nd(attr_hash)
    when 'member'       then _member(attr_hash)
    when 'home'         then _home(attr_hash)
    when 'permissions'  then _start_permissions(attr_hash)
    when 'permission'   then _start_permission(attr_hash)
    when 'note'         then _start_note(attr_hash)
    when 'bounds'       then _start_bounds(attr_hash)
    end
  end
end

#parseObject



10
11
12
13
14
15
16
17
# File 'lib/rosemary/parser.rb', line 10

def parse
  return nil if body.nil? || body.empty?
  if supports_format?
    self.send(format) # This is a hack, cause the xml format would not be recognized ways, but for nodes and relations
  else
    body
  end
end

#plainObject



41
42
43
# File 'lib/rosemary/parser.rb', line 41

def plain
  body
end

#xmlObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rosemary/parser.rb', line 23

def xml
  # instead of using
  # LibXML::XML::default_substitute_entities = true
  # we change the options of the xml context:
  ctx = XML::Parser::Context.string(body)
  ctx.options = XML::Parser::Options::NOENT
  @parser = LibXML::XML::SaxParser.new(ctx)

  @parser.callbacks = self
  @parser.parse

  if @bounding_box
    @bounding_box
  else
    @collection.empty? ? @context : @collection
  end
end