~ubuntu-branches/ubuntu/intrepid/ruby1.8/intrepid-updates

« back to all changes in this revision

Viewing changes to lib/rss/0.9.rb

  • Committer: Bazaar Package Importer
  • Author(s): akira yamada
  • Date: 2007-03-13 22:11:58 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20070313221158-h3oql37brlaf2go2
Tags: 1.8.6-1
* new upstream version, 1.8.6.
* libruby1.8 conflicts with libopenssl-ruby1.8 (< 1.8.6) (closes: #410018)
* changed packaging style to cdbs from dbs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
require "rss/parser"
 
2
 
 
3
module RSS
 
4
 
 
5
  module RSS09
 
6
    NSPOOL = {}
 
7
    ELEMENTS = []
 
8
 
 
9
    def self.append_features(klass)
 
10
      super
 
11
      
 
12
      klass.install_must_call_validator('', "")
 
13
    end
 
14
  end
 
15
 
 
16
  class Rss < Element
 
17
 
 
18
    include RSS09
 
19
    include RootElementMixin
 
20
 
 
21
    %w(channel).each do |name|
 
22
      install_have_child_element(name, "", nil)
 
23
    end
 
24
 
 
25
    attr_accessor :rss_version, :version, :encoding, :standalone
 
26
    
 
27
    def initialize(rss_version, version=nil, encoding=nil, standalone=nil)
 
28
      super
 
29
    end
 
30
 
 
31
    def items
 
32
      if @channel
 
33
        @channel.items
 
34
      else
 
35
        []
 
36
      end
 
37
    end
 
38
 
 
39
    def image
 
40
      if @channel
 
41
        @channel.image
 
42
      else
 
43
        nil
 
44
      end
 
45
    end
 
46
 
 
47
    def textinput
 
48
      if @channel
 
49
        @channel.textInput
 
50
      else
 
51
        nil
 
52
      end
 
53
    end
 
54
 
 
55
    def setup_maker_elements(maker)
 
56
      super
 
57
      items.each do |item|
 
58
        item.setup_maker(maker.items)
 
59
      end
 
60
    end
 
61
 
 
62
    private
 
63
    def _attrs
 
64
      [
 
65
        ["version", true, "rss_version"],
 
66
      ]
 
67
    end
 
68
 
 
69
    class Channel < Element
 
70
 
 
71
      include RSS09
 
72
 
 
73
      [
 
74
        ["title", nil, :text],
 
75
        ["link", nil, :text],
 
76
        ["description", nil, :text],
 
77
        ["language", nil, :text],
 
78
        ["copyright", "?", :text],
 
79
        ["managingEditor", "?", :text],
 
80
        ["webMaster", "?", :text],
 
81
        ["rating", "?", :text],
 
82
        ["pubDate", "?", :date, :rfc822],
 
83
        ["lastBuildDate", "?", :date, :rfc822],
 
84
        ["docs", "?", :text],
 
85
        ["cloud", "?", :have_attribute],
 
86
        ["skipDays", "?", :have_child],
 
87
        ["skipHours", "?", :have_child],
 
88
        ["image", nil, :have_child],
 
89
        ["item", "*", :have_children],
 
90
        ["textInput", "?", :have_child],
 
91
      ].each do |name, occurs, type, *args|
 
92
        __send__("install_#{type}_element", name, "", occurs, name, *args)
 
93
      end
 
94
      alias date pubDate
 
95
      alias date= pubDate=
 
96
 
 
97
      private
 
98
      def maker_target(maker)
 
99
        maker.channel
 
100
      end
 
101
 
 
102
      def setup_maker_elements(channel)
 
103
        super
 
104
        [
 
105
          [skipDays, "day"],
 
106
          [skipHours, "hour"],
 
107
        ].each do |skip, key|
 
108
          if skip
 
109
            skip.__send__("#{key}s").each do |val|
 
110
              target_skips = channel.__send__("skip#{key.capitalize}s")
 
111
              new_target = target_skips.__send__("new_#{key}")
 
112
              new_target.content = val.content
 
113
            end
 
114
          end
 
115
        end
 
116
      end
 
117
 
 
118
      def not_need_to_call_setup_maker_variables
 
119
        %w(image textInput)
 
120
      end
 
121
    
 
122
      class SkipDays < Element
 
123
        include RSS09
 
124
 
 
125
        [
 
126
          ["day", "*"]
 
127
        ].each do |name, occurs|
 
128
          install_have_children_element(name, "", occurs)
 
129
        end
 
130
 
 
131
        class Day < Element
 
132
          include RSS09
 
133
 
 
134
          content_setup
 
135
 
 
136
          def initialize(*args)
 
137
            if Utils.element_initialize_arguments?(args)
 
138
              super
 
139
            else
 
140
              super()
 
141
              self.content = args[0]
 
142
            end
 
143
          end
 
144
      
 
145
        end
 
146
        
 
147
      end
 
148
      
 
149
      class SkipHours < Element
 
150
        include RSS09
 
151
 
 
152
        [
 
153
          ["hour", "*"]
 
154
        ].each do |name, occurs|
 
155
          install_have_children_element(name, "", occurs)
 
156
        end
 
157
 
 
158
        class Hour < Element
 
159
          include RSS09
 
160
 
 
161
          content_setup(:integer)
 
162
 
 
163
          def initialize(*args)
 
164
            if Utils.element_initialize_arguments?(args)
 
165
              super
 
166
            else
 
167
              super()
 
168
              self.content = args[0]
 
169
            end
 
170
          end
 
171
        end
 
172
        
 
173
      end
 
174
      
 
175
      class Image < Element
 
176
 
 
177
        include RSS09
 
178
        
 
179
        %w(url title link).each do |name|
 
180
          install_text_element(name, "", nil)
 
181
        end
 
182
        [
 
183
          ["width", :integer],
 
184
          ["height", :integer],
 
185
          ["description"],
 
186
        ].each do |name, type|
 
187
          install_text_element(name, "", "?", name, type)
 
188
        end
 
189
 
 
190
        def initialize(*args)
 
191
          if Utils.element_initialize_arguments?(args)
 
192
            super
 
193
          else
 
194
            super()
 
195
            self.url = args[0]
 
196
            self.title = args[1]
 
197
            self.link = args[2]
 
198
            self.width = args[3]
 
199
            self.height = args[4]
 
200
            self.description = args[5]
 
201
          end
 
202
        end
 
203
 
 
204
        private
 
205
        def maker_target(maker)
 
206
          maker.image
 
207
        end
 
208
      end
 
209
 
 
210
      class Cloud < Element
 
211
 
 
212
        include RSS09
 
213
 
 
214
        [
 
215
          ["domain", "", true],
 
216
          ["port", "", true, :integer],
 
217
          ["path", "", true],
 
218
          ["registerProcedure", "", true],
 
219
          ["protocol", "", true],
 
220
        ].each do |name, uri, required, type|
 
221
          install_get_attribute(name, uri, required, type)
 
222
        end
 
223
 
 
224
        def initialize(*args)
 
225
          if Utils.element_initialize_arguments?(args)
 
226
            super
 
227
          else
 
228
            super()
 
229
            self.domain = args[0]
 
230
            self.port = args[1]
 
231
            self.path = args[2]
 
232
            self.registerProcedure = args[3]
 
233
            self.protocol = args[4]
 
234
          end
 
235
        end
 
236
      end
 
237
      
 
238
      class Item < Element
 
239
        
 
240
        include RSS09
 
241
 
 
242
        [
 
243
          ["title", '?', :text],
 
244
          ["link", '?', :text],
 
245
          ["description", '?', :text],
 
246
          ["category", '*', :have_children, "categories"],
 
247
          ["source", '?', :have_child],
 
248
          ["enclosure", '?', :have_child],
 
249
        ].each do |tag, occurs, type, *args|
 
250
          __send__("install_#{type}_element", tag, "", occurs, tag, *args)
 
251
        end
 
252
 
 
253
        private
 
254
        def maker_target(items)
 
255
          if items.respond_to?("items")
 
256
            # For backward compatibility
 
257
            items = items.items
 
258
          end
 
259
          items.new_item
 
260
        end
 
261
 
 
262
        def setup_maker_element(item)
 
263
          super
 
264
          @enclosure.setup_maker(item) if @enclosure
 
265
          @source.setup_maker(item) if @source
 
266
        end
 
267
        
 
268
        class Source < Element
 
269
 
 
270
          include RSS09
 
271
 
 
272
          [
 
273
            ["url", "", true]
 
274
          ].each do |name, uri, required|
 
275
            install_get_attribute(name, uri, required)
 
276
          end
 
277
          
 
278
          content_setup
 
279
 
 
280
          def initialize(*args)
 
281
            if Utils.element_initialize_arguments?(args)
 
282
              super
 
283
            else
 
284
              super()
 
285
              self.url = args[0]
 
286
              self.content = args[1]
 
287
            end
 
288
          end
 
289
 
 
290
          private
 
291
          def maker_target(item)
 
292
            item.source
 
293
          end
 
294
 
 
295
          def setup_maker_attributes(source)
 
296
            source.url = url
 
297
            source.content = content
 
298
          end
 
299
        end
 
300
 
 
301
        class Enclosure < Element
 
302
 
 
303
          include RSS09
 
304
 
 
305
          [
 
306
            ["url", "", true],
 
307
            ["length", "", true, :integer],
 
308
            ["type", "", true],
 
309
          ].each do |name, uri, required, type|
 
310
            install_get_attribute(name, uri, required, type)
 
311
          end
 
312
 
 
313
          def initialize(*args)
 
314
            if Utils.element_initialize_arguments?(args)
 
315
              super
 
316
            else
 
317
              super()
 
318
              self.url = args[0]
 
319
              self.length = args[1]
 
320
              self.type = args[2]
 
321
            end
 
322
          end
 
323
 
 
324
          private
 
325
          def maker_target(item)
 
326
            item.enclosure
 
327
          end
 
328
 
 
329
          def setup_maker_attributes(enclosure)
 
330
            enclosure.url = url
 
331
            enclosure.length = length
 
332
            enclosure.type = type
 
333
          end
 
334
        end
 
335
 
 
336
        class Category < Element
 
337
 
 
338
          include RSS09
 
339
          
 
340
          [
 
341
            ["domain", "", false]
 
342
          ].each do |name, uri, required|
 
343
            install_get_attribute(name, uri, required)
 
344
          end
 
345
 
 
346
          content_setup
 
347
 
 
348
          def initialize(*args)
 
349
            if Utils.element_initialize_arguments?(args)
 
350
              super
 
351
            else
 
352
              super()
 
353
              self.domain = args[0]
 
354
              self.content = args[1]
 
355
            end
 
356
          end
 
357
 
 
358
          private
 
359
          def maker_target(item)
 
360
            item.new_category
 
361
          end
 
362
 
 
363
          def setup_maker_attributes(category)
 
364
            category.domain = domain
 
365
            category.content = content
 
366
          end
 
367
          
 
368
        end
 
369
 
 
370
      end
 
371
      
 
372
      class TextInput < Element
 
373
 
 
374
        include RSS09
 
375
 
 
376
        %w(title description name link).each do |name|
 
377
          install_text_element(name, "", nil)
 
378
        end
 
379
 
 
380
        def initialize(*args)
 
381
          if Utils.element_initialize_arguments?(args)
 
382
            super
 
383
          else
 
384
            super()
 
385
            self.title = args[0]
 
386
            self.description = args[1]
 
387
            self.name = args[2]
 
388
            self.link = args[3]
 
389
          end
 
390
        end
 
391
 
 
392
        private
 
393
        def maker_target(maker)
 
394
          maker.textinput
 
395
        end
 
396
      end
 
397
      
 
398
    end
 
399
    
 
400
  end
 
401
 
 
402
  RSS09::ELEMENTS.each do |name|
 
403
    BaseListener.install_get_text_element("", name, "#{name}=")
 
404
  end
 
405
 
 
406
  module ListenerMixin
 
407
    private
 
408
    def start_rss(tag_name, prefix, attrs, ns)
 
409
      check_ns(tag_name, prefix, ns, "")
 
410
      
 
411
      @rss = Rss.new(attrs['version'], @version, @encoding, @standalone)
 
412
      @rss.do_validate = @do_validate
 
413
      @rss.xml_stylesheets = @xml_stylesheets
 
414
      @last_element = @rss
 
415
      @proc_stack.push Proc.new { |text, tags|
 
416
        @rss.validate_for_stream(tags, @ignore_unknown_element) if @do_validate
 
417
      }
 
418
    end
 
419
    
 
420
  end
 
421
 
 
422
end