Class: Automatic::OPML::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/automatic/opml.rb

Overview

Model

Instance Method Summary collapse

Constructor Details

#initialize(port) ⇒ Parser

Returns a new instance of Parser.



124
125
126
127
# File 'lib/automatic/opml.rb', line 124

def initialize(port)
  @p = REXML::Parsers::PullParser.new(port)
  @opml = nil
end

Instance Method Details

#each_outlineObject

parse_tree



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/automatic/opml.rb', line 174

def each_outline
  root = cur = nil
  while event=@p.pull
    case event.event_type
    when :xmldecl
      encoding = event[1]
    when :start_element
      case event[0]
      when "opml"
        root = cur = DOM::OPML.new
      when "head"
        e = DOM::Head.new
        cur << e
        cur = e
      when "body"
        e = DOM::Body.new
        cur << e
        cur = e
      when "outline"
        e = DOM::Outline.new
        # cur << e
        cur = e
      else
        cur['_' + event[0]] = read_text
      end
      event[1].each {|k,v|
        cur[k] = OPML::unnormalize(v)
      }
      if event[0]=="outline"
        yield root, cur
      end
    when :end_element
      cur = cur.parent if cur.kind_of? DOM::Element
    when :text
    when :cdata
    when :start_doctype
    when :end_doctype,:comment
    when :end_document
      break
    else
      p event.event_type
      p event
      raise "unknown event"
    end
  end # while
end

#parse_treeObject



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/automatic/opml.rb', line 129

def parse_tree
  root = cur = nil
  while event=@p.pull
    case event.event_type
    when :xmldecl
      encoding = event[1]
    when :start_element
      case event[0]
      when "opml"
        root = cur = DOM::OPML.new
      when "head"
        e = DOM::Head.new
        cur << e
        cur = e
      when "body"
        e = DOM::Body.new
        cur << e
        cur = e
      when "outline"
        e = DOM::Outline.new
        cur << e
        cur = e
      else
        cur['.' + event[0]] = read_text
      end
      event[1].each {|k,v|
        cur[k] = OPML::unnormalize(v)
      }
    when :end_element
      cur = cur.parent
    when :text
    when :cdata
    when :start_doctype
    when :end_doctype,:comment
    when :end_document
      break
    else
      p event.event_type
      p event
      raise "unknown event"
    end
  end # while
  root
end

#read_textObject

each_outline



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/automatic/opml.rb', line 221

def read_text
  text = ""
  while event = @p.pull
    case event.event_type
    when :end_element
      break
    when :text
      text << OPML::unnormalize(event[0])
    when :cdata
      text << event[0]
    else
      raise
    end
  end
  text
end