diff options
author | kou <kou@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2007-03-17 10:13:25 +0000 |
---|---|---|
committer | kou <kou@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2007-03-17 10:13:25 +0000 |
commit | 966a25465aab5c2972e6c453f631a15fc2223256 () | |
tree | 847090e856c9901ab2cc19251c179b9b0985e65b /lib | |
parent | 53cbab048452742b537ac8bccf494630d1c184c8 (diff) |
* lib/rss, test/rss:
- supported Atom. - bumped version 0.1.6 to 0.1.7. * sample/rss/convert.rb: added new sample. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12087 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | lib/rss.rb | 5 | ||||
-rw-r--r-- | lib/rss/0.9.rb | 532 | ||||
-rw-r--r-- | lib/rss/1.0.rb | 486 | ||||
-rw-r--r-- | lib/rss/2.0.rb | 97 | ||||
-rw-r--r-- | lib/rss/atom.rb | 749 | ||||
-rw-r--r-- | lib/rss/content.rb | 25 | ||||
-rw-r--r-- | lib/rss/converter.rb | 19 | ||||
-rw-r--r-- | lib/rss/dublincore.rb | 62 | ||||
-rw-r--r-- | lib/rss/dublincore/1.0.rb | 13 | ||||
-rw-r--r-- | lib/rss/dublincore/2.0.rb | 13 | ||||
-rw-r--r-- | lib/rss/dublincore/atom.rb | 17 | ||||
-rw-r--r-- | lib/rss/image.rb | 116 | ||||
-rw-r--r-- | lib/rss/maker.rb | 19 | ||||
-rw-r--r-- | lib/rss/maker/0.9.rb | 372 | ||||
-rw-r--r-- | lib/rss/maker/1.0.rb | 328 | ||||
-rw-r--r-- | lib/rss/maker/2.0.rb | 134 | ||||
-rw-r--r-- | lib/rss/maker/atom.rb | 172 | ||||
-rw-r--r-- | lib/rss/maker/base.rb | 710 | ||||
-rw-r--r-- | lib/rss/maker/dublincore.rb | 100 | ||||
-rw-r--r-- | lib/rss/maker/entry.rb | 167 | ||||
-rw-r--r-- | lib/rss/maker/feed.rb | 429 | ||||
-rw-r--r-- | lib/rss/maker/image.rb | 100 | ||||
-rw-r--r-- | lib/rss/maker/taxonomy.rb | 73 | ||||
-rw-r--r-- | lib/rss/maker/trackback.rb | 85 | ||||
-rw-r--r-- | lib/rss/parser.rb | 172 | ||||
-rw-r--r-- | lib/rss/rss.rb | 661 | ||||
-rw-r--r-- | lib/rss/syndication.rb | 32 | ||||
-rw-r--r-- | lib/rss/taxonomy.rb | 128 | ||||
-rw-r--r-- | lib/rss/trackback.rb | 106 | ||||
-rw-r--r-- | lib/rss/utils.rb | 7 | ||||
-rw-r--r-- | lib/rss/xml.rb | 71 |
31 files changed, 4032 insertions, 1968 deletions
@@ -1,4 +1,4 @@ -# Copyright (c) 2003-2005 Kouhei Sutou. You can redistribute it and/or # modify it under the same terms as Ruby. # # Author:: Kouhei Sutou <[email protected]> @@ -6,11 +6,12 @@ require 'rss/1.0' require 'rss/2.0' require 'rss/content' require 'rss/dublincore' require 'rss/image' require 'rss/syndication' -#require 'rss/taxonomy' require 'rss/trackback' require "rss/maker" @@ -9,7 +9,7 @@ module RSS def self.append_features(klass) super - klass.install_must_call_validator('', nil) end end @@ -17,22 +17,18 @@ module RSS include RSS09 include RootElementMixin - # include XMLStyleSheetMixin - - [ - ["channel", nil], - ].each do |tag, occurs| - install_model(tag, occurs) - end %w(channel).each do |name| - install_have_child_element(name) end - attr_accessor :rss_version, :version, :encoding, :standalone - - def initialize(rss_version, version=nil, encoding=nil, standalone=nil) super end def items @@ -58,34 +54,20 @@ module RSS nil end end - - def to_s(need_convert=true, indent=calc_indent) - rv = tag(indent, ns_declarations) do |next_indent| - [ - channel_element(false, next_indent), - other_element(false, next_indent), - ] - end - rv = convert(rv) if need_convert - rv - end - private - def children - [@channel] - end - - def _tags - [ - [nil, 'channel'], - ].delete_if do |uri, name| - __send__(name).nil? end end def _attrs [ - ["version", true, "rss_version"], ] end @@ -94,119 +76,30 @@ module RSS include RSS09 [ - ["title", nil], - ["link", nil], - ["description", nil], - ["language", nil], - ["copyright", "?"], - ["managingEditor", "?"], - ["webMaster", "?"], - ["rating", "?"], - ["docs", "?"], - ].each do |name, occurs| - install_text_element(name) - install_model(name, occurs) - end - - [ - ["pubDate", "?"], - ["lastBuildDate", "?"], - ].each do |name, occurs| - install_date_element(name, 'rfc822') - install_model(name, occurs) end alias date pubDate alias date= pubDate= - [ - ["skipDays", "?"], - ["skipHours", "?"], - ["image", nil], - ["textInput", "?"], - ].each do |name, occurs| - install_have_child_element(name) - install_model(name, occurs) - end - - [ - ["cloud", "?"] - ].each do |name, occurs| - install_have_attribute_element(name) - install_model(name, occurs) - end - - [ - ["item", "*"] - ].each do |name, occurs| - install_have_children_element(name) - install_model(name, occurs) - end - - def initialize() - super() - end - - def to_s(need_convert=true, indent=calc_indent) - rv = tag(indent) do |next_indent| - [ - title_element(false, next_indent), - link_element(false, next_indent), - description_element(false, next_indent), - language_element(false, next_indent), - copyright_element(false, next_indent), - managingEditor_element(false, next_indent), - webMaster_element(false, next_indent), - rating_element(false, next_indent), - pubDate_element(false, next_indent), - lastBuildDate_element(false, next_indent), - docs_element(false, next_indent), - cloud_element(false, next_indent), - skipDays_element(false, next_indent), - skipHours_element(false, next_indent), - image_element(false, next_indent), - item_elements(false, next_indent), - textInput_element(false, next_indent), - other_element(false, next_indent), - ] - end - rv = convert(rv) if need_convert - rv - end - private - def children - [@skipDays, @skipHours, @image, @textInput, @cloud, *@item] - end - - def _tags - rv = [ - "title", - "link", - "description", - "language", - "copyright", - "managingEditor", - "webMaster", - "rating", - "docs", - "skipDays", - "skipHours", - "image", - "textInput", - "cloud", - ].delete_if do |name| - __send__(name).nil? - end.collect do |elem| - [nil, elem] - end - - @item.each do - rv << [nil, "item"] - end - - rv - end - def maker_target(maker) maker.channel end @@ -237,29 +130,7 @@ module RSS [ ["day", "*"] ].each do |name, occurs| - install_have_children_element(name) - install_model(name, occurs) - end - - def to_s(need_convert=true, indent=calc_indent) - rv = tag(indent) do |next_indent| - [ - day_elements(false, next_indent) - ] - end - rv = convert(rv) if need_convert - rv - end - - private - def children - @day - end - - def _tags - @day.compact.collect do - [nil, "day"] - end end class Day < Element @@ -267,9 +138,13 @@ module RSS content_setup - def initialize(content=nil) - super() - self.content = content end end @@ -282,29 +157,7 @@ module RSS [ ["hour", "*"] ].each do |name, occurs| - install_have_children_element(name) - install_model(name, occurs) - end - - def to_s(need_convert=true, indent=calc_indent) - rv = tag(indent) do |next_indent| - [ - hour_elements(false, next_indent) - ] - end - rv = convert(rv) if need_convert - rv - end - - private - def children - @hour - end - - def _tags - @hour.compact.collect do - [nil, "hour"] - end end class Hour < Element @@ -312,9 +165,13 @@ module RSS content_setup(:integer) - def initialize(content=nil) - super() - self.content = content end end @@ -325,54 +182,31 @@ module RSS include RSS09 %w(url title link).each do |name| - install_text_element(name) - install_model(name, nil) end [ ["width", :integer], ["height", :integer], ["description"], ].each do |name, type| - install_text_element(name, type) - install_model(name, "?") - end - - def initialize(url=nil, title=nil, link=nil, width=nil, height=nil, - description=nil) - super() - self.url = url - self.title = title - self.link = link - self.width = width - self.height = height - self.description = description end - def to_s(need_convert=true, indent=calc_indent) - rv = tag(indent) do |next_indent| - [ - url_element(false, next_indent), - title_element(false, next_indent), - link_element(false, next_indent), - width_element(false, next_indent), - height_element(false, next_indent), - description_element(false, next_indent), - other_element(false, next_indent), - ] end - rv = convert(rv) if need_convert - rv end private - def _tags - %w(url title link width height description).delete_if do |name| - __send__(name).nil? - end.collect do |elem| - [nil, elem] - end - end - def maker_target(maker) maker.image end @@ -381,106 +215,47 @@ module RSS class Cloud < Element include RSS09 - [ - ["domain", nil, true], - ["port", nil, true, :integer], - ["path", nil, true], - ["registerProcedure", nil, true], - ["protocol", nil, true], ].each do |name, uri, required, type| install_get_attribute(name, uri, required, type) end - def initialize(domain=nil, port=nil, path=nil, rp=nil, protocol=nil) - super() - self.domain = domain - self.port = port - self.path = path - self.registerProcedure = rp - self.protocol = protocol - end - - def to_s(need_convert=true, indent=calc_indent) - rv = tag(indent) - rv = convert(rv) if need_convert - rv - end - - private - def _attrs - %w(domain port path registerProcedure protocol).collect do |attr| - [attr, true] end end - end class Item < Element include RSS09 - %w(title link description).each do |name| - install_text_element(name) - end - - %w(source enclosure).each do |name| - install_have_child_element(name) - end - [ - %w(category categories), - ].each do |name, plural_name| - install_have_children_element(name, plural_name) - end - - [ - ["title", '?'], - ["link", '?'], - ["description", '?'], - ["category", '*'], - ["source", '?'], - ["enclosure", '?'], - ].each do |tag, occurs| - install_model(tag, occurs) - end - - def to_s(need_convert=true, indent=calc_indent) - rv = tag(indent) do |next_indent| - [ - title_element(false, next_indent), - link_element(false, next_indent), - description_element(false, next_indent), - category_elements(false, next_indent), - source_element(false, next_indent), - enclosure_element(false, next_indent), - other_element(false, next_indent), - ] - end - rv = convert(rv) if need_convert - rv end private - def children - [@source, @enclosure, *@category].compact - end - - def _tags - rv = %w(title link description author comments - source enclosure).delete_if do |name| - __send__(name).nil? - end.collect do |name| - [nil, name] - end - - @category.each do - rv << [nil, "category"] - end - - rv - end - def maker_target(items) if items.respond_to?("items") # For backward compatibility @@ -500,31 +275,24 @@ module RSS include RSS09 [ - ["url", nil, true] ].each do |name, uri, required| install_get_attribute(name, uri, required) end content_setup - def initialize(url=nil, content=nil) - super() - self.url = url - self.content = content end private - def _tags - [] - end - - def _attrs - [ - ["url", true] - ] - end - - def maker_target(item) item.source end @@ -540,35 +308,25 @@ module RSS include RSS09 [ - ["url", nil, true], - ["length", nil, true, :integer], - ["type", nil, true], ].each do |name, uri, required, type| install_get_attribute(name, uri, required, type) end - def initialize(url=nil, length=nil, type=nil) - super() - self.url = url - self.length = length - self.type = type - end - - def to_s(need_convert=true, indent=calc_indent) - rv = tag(indent) - rv = convert(rv) if need_convert - rv end private - def _attrs - [ - ["url", true], - ["length", true], - ["type", true], - ] - end - def maker_target(item) item.enclosure end @@ -585,26 +343,24 @@ module RSS include RSS09 [ - ["domain", nil, false] ].each do |name, uri, required| install_get_attribute(name, uri, required) end content_setup - def initialize(domain=nil, content=nil) - super() - self.domain = domain - self.content = content end private - def _attrs - [ - ["domain", false] - ] - end - def maker_target(item) item.new_category end @@ -623,41 +379,22 @@ module RSS include RSS09 %w(title description name link).each do |name| - install_text_element(name) - install_model(name, nil) end - def initialize(title=nil, description=nil, name=nil, link=nil) - super() - self.title = title - self.description = description - self.name = name - self.link = link - end - - def to_s(need_convert=true, indent=calc_indent) - rv = tag(indent) do |next_indent| - [ - title_element(false, next_indent), - description_element(false, next_indent), - name_element(false, next_indent), - link_element(false, next_indent), - other_element(false, next_indent), - ] end - rv = convert(rv) if need_convert - rv end private - def _tags - %w(title description name link).each do |name| - __send__(name).nil? - end.collect do |elem| - [nil, elem] - end - end - def maker_target(maker) maker.textinput end @@ -668,21 +405,22 @@ module RSS end RSS09::ELEMENTS.each do |name| - BaseListener.install_get_text_element(nil, name, "#{name}=") end module ListenerMixin private - def start_rss(tag_name, prefix, attrs, ns) - check_ns(tag_name, prefix, ns, nil) @rss = Rss.new(attrs['version'], @version, @encoding, @standalone) @rss.do_validate = @do_validate @rss.xml_stylesheets = @xml_stylesheets @last_element = @rss - @proc_stack.push Proc.new { |text, tags| - @rss.validate_for_stream(tags) if @do_validate - } end end @@ -38,62 +38,22 @@ module RSS [ ["channel", nil], ["image", "?"], - ["item", "+"], ["textinput", "?"], - ].each do |tag, occurs| - install_model(tag, occurs) end - %w(channel image textinput).each do |name| - install_have_child_element(name) - end - - install_have_children_element("item") - - attr_accessor :rss_version, :version, :encoding, :standalone - def initialize(version=nil, encoding=nil, standalone=nil) super('1.0', version, encoding, standalone) end def full_name tag_name_with_prefix(PREFIX) end - - def to_s(need_convert=true, indent=calc_indent) - rv = tag(indent, ns_declarations) do |next_indent| - [ - channel_element(false, next_indent), - image_element(false, next_indent), - item_elements(false, next_indent), - textinput_element(false, next_indent), - other_element(false, next_indent), - ] - end - rv = convert(rv) if need_convert - rv - end - - private - def rdf_validate(tags) - _validate(tags, []) - end - - def children - [@channel, @image, @textinput, *@item] - end - - def _tags - rv = [ - [::RSS::URI, "channel"], - [::RSS::URI, "image"], - ].delete_if {|uri, name| __send__(name).nil?} - @item.each do |item| - rv << [::RSS::URI, "item"] - end - rv << [::RSS::URI, "textinput"] if @textinput - rv - end class Li < Element @@ -106,32 +66,23 @@ module RSS end [ - ["resource", [URI, nil], true] ].each do |name, uri, required| install_get_attribute(name, uri, required) end - def initialize(resource=nil) - super() - self.resource = resource end def full_name tag_name_with_prefix(PREFIX) end - - def to_s(need_convert=true, indent=calc_indent) - rv = tag(indent) - rv = convert(rv) if need_convert - rv - end - - private - def _attrs - [ - ["resource", true] - ] - end end class Seq < Element @@ -148,21 +99,15 @@ module RSS @tag_name = 'Seq' - install_have_children_element("li") - install_must_call_validator('rdf', ::RSS::RDF::URI) - def initialize(li=[]) - super() - @li = li - end - - def to_s(need_convert=true, indent=calc_indent) - tag(indent) do |next_indent| - [ - li_elements(need_convert, next_indent), - other_element(need_convert, next_indent), - ] end end @@ -175,23 +120,6 @@ module RSS target << li.resource end end - - private - def children - @li - end - - def rdf_validate(tags) - _validate(tags, [["li", '*']]) - end - - def _tags - rv = [] - @li.each do |li| - rv << [URI, "li"] - end - rv - end end class Bag < Element @@ -208,21 +136,15 @@ module RSS @tag_name = 'Bag' - install_have_children_element("li") - - install_must_call_validator('rdf', ::RSS::RDF::URI) - - def initialize(li=[]) - super() - @li = li - end - def to_s(need_convert=true, indent=calc_indent) - tag(indent) do |next_indent| - [ - li_elements(need_convert, next_indent), - other_element(need_convert, next_indent), - ] end end @@ -235,23 +157,6 @@ module RSS target << li.resource end end - - private - def children - @li - end - - def rdf_validate(tags) - _validate(tags, [["li", '*']]) - end - - def _tags - rv = [] - @li.each do |li| - rv << [URI, "li"] - end - rv - end end class Channel < Element @@ -269,73 +174,31 @@ module RSS [ ["about", URI, true] ].each do |name, uri, required| - install_get_attribute(name, uri, required) - end - - %w(title link description).each do |name| - install_text_element(name) end - %w(image items textinput).each do |name| - install_have_child_element(name) - end - [ - ['title', nil], - ['link', nil], - ['description', nil], - ['image', '?'], - ['items', nil], - ['textinput', '?'], - ].each do |tag, occurs| - install_model(tag, occurs) - end - - def initialize(about=nil) - super() - self.about = about - end - - def to_s(need_convert=true, indent=calc_indent) - rv = tag(indent) do |next_indent| - [ - title_element(false, next_indent), - link_element(false, next_indent), - description_element(false, next_indent), - image_element(false, next_indent), - items_element(false, next_indent), - textinput_element(false, next_indent), - other_element(false, next_indent), - ] end - rv = convert(rv) if need_convert - rv end private - def children - [@image, @items, @textinput] - end - - def _tags - [ - [::RSS::URI, 'title'], - [::RSS::URI, 'link'], - [::RSS::URI, 'description'], - [::RSS::URI, 'image'], - [::RSS::URI, 'items'], - [::RSS::URI, 'textinput'], - ].delete_if do |uri, name| - __send__(name).nil? - end - end - - def _attrs - [ - ["#{PREFIX}:about", true, "about"] - ] - end - def maker_target(maker) maker.channel end @@ -359,25 +222,17 @@ module RSS [ ["resource", URI, true] ].each do |name, uri, required| - install_get_attribute(name, uri, required) end - def initialize(resource=nil) - super() - self.resource = resource - end - - def to_s(need_convert=true, indent=calc_indent) - rv = tag(indent) - rv = convert(rv) if need_convert - rv - end - - private - def _attrs - [ - ["#{PREFIX}:resource", true, "resource"] - ] end end @@ -396,25 +251,17 @@ module RSS [ ["resource", URI, true] ].each do |name, uri, required| - install_get_attribute(name, uri, required) end - def initialize(resource=nil) - super() - self.resource = resource - end - - def to_s(need_convert=true, indent=calc_indent) - rv = tag(indent) - rv = convert(rv) if need_convert - rv - end - - private - def _attrs - [ - ["#{PREFIX}:resource", true, "resource"] - ] end end @@ -432,22 +279,17 @@ module RSS end - install_have_child_element("Seq") - - install_must_call_validator('rdf', ::RSS::RDF::URI) - def initialize(seq=Seq.new) - super() - @Seq = seq - end - - def to_s(need_convert=true, indent=calc_indent) - rv = tag(indent) do |next_indent| - [ - Seq_element(need_convert, next_indent), - other_element(need_convert, next_indent), - ] end end def resources @@ -459,21 +301,6 @@ module RSS [] end end - - private - def children - [@Seq] - end - - def _tags - rv = [] - rv << [URI, 'Seq'] unless @Seq.nil? - rv - end - - def rdf_validate(tags) - _validate(tags, [["Seq", nil]]) - end end end @@ -488,60 +315,28 @@ module RSS end end - [ ["about", URI, true] ].each do |name, uri, required| - install_get_attribute(name, uri, required) end %w(title url link).each do |name| - install_text_element(name) - end - - [ - ['title', nil], - ['url', nil], - ['link', nil], - ].each do |tag, occurs| - install_model(tag, occurs) end - def initialize(about=nil) - super() - self.about = about - end - - def to_s(need_convert=true, indent=calc_indent) - rv = tag(indent) do |next_indent| - [ - title_element(false, next_indent), - url_element(false, next_indent), - link_element(false, next_indent), - other_element(false, next_indent), - ] end - rv = convert(rv) if need_convert - rv end private - def _tags - [ - [::RSS::URI, 'title'], - [::RSS::URI, 'url'], - [::RSS::URI, 'link'], - ].delete_if do |uri, name| - __send__(name).nil? - end - end - - def _attrs - [ - ["#{PREFIX}:about", true, "about"] - ] - end - def maker_target(maker) maker.image end @@ -559,14 +354,12 @@ module RSS end [ ["about", URI, true] ].each do |name, uri, required| - install_get_attribute(name, uri, required) - end - - %w(title link description).each do |name| - install_text_element(name) end [ @@ -574,44 +367,19 @@ module RSS ["link", nil], ["description", "?"], ].each do |tag, occurs| - install_model(tag, occurs) end - def initialize(about=nil) - super() - self.about = about - end - - def to_s(need_convert=true, indent=calc_indent) - rv = tag(indent) do |next_indent| - [ - title_element(false, next_indent), - link_element(false, next_indent), - description_element(false, next_indent), - other_element(false, next_indent), - ] - end - rv = convert(rv) if need_convert - rv - end - - private - def _tags - [ - [::RSS::URI, 'title'], - [::RSS::URI, 'link'], - [::RSS::URI, 'description'], - ].delete_if do |uri, name| - __send__(name).nil? end end - def _attrs - [ - ["#{PREFIX}:about", true, "about"] - ] - end - def maker_target(items) if items.respond_to?("items") # For backward compatibility @@ -636,59 +404,24 @@ module RSS [ ["about", URI, true] ].each do |name, uri, required| - install_get_attribute(name, uri, required) end %w(title description name link).each do |name| - install_text_element(name) - end - - [ - ["title", nil], - ["description", nil], - ["name", nil], - ["link", nil], - ].each do |tag, occurs| - install_model(tag, occurs) - end - - def initialize(about=nil) - super() - self.about = about end - def to_s(need_convert=true, indent=calc_indent) - rv = tag(indent) do |next_indent| - [ - title_element(false, next_indent), - description_element(false, next_indent), - name_element(false, next_indent), - link_element(false, next_indent), - other_element(false, next_indent), - ] end - rv = convert(rv) if need_convert - rv end private - def _tags - [ - [::RSS::URI, 'title'], - [::RSS::URI, 'description'], - [::RSS::URI, 'name'], - [::RSS::URI, 'link'], - ].delete_if do |uri, name| - __send__(name).nil? - end - end - - def _attrs - [ - ["#{PREFIX}:about", true, "about"] - ] - end - def maker_target(maker) maker.textinput end @@ -697,21 +430,22 @@ module RSS end RSS10::ELEMENTS.each do |name| - BaseListener.install_get_text_element(URI, name, "#{name}=") end module ListenerMixin private - def start_RDF(tag_name, prefix, attrs, ns) check_ns(tag_name, prefix, ns, RDF::URI) @rss = RDF.new(@version, @encoding, @standalone) @rss.do_validate = @do_validate @rss.xml_stylesheets = @xml_stylesheets @last_element = @rss - @proc_stack.push Proc.new { |text, tags| - @rss.validate_for_stream(tags) if @do_validate - } end end @@ -10,52 +10,20 @@ module RSS ["generator"], ["ttl", :integer], ].each do |name, type| - install_text_element(name, type) - install_model(name, '?') end [ %w(category categories), ].each do |name, plural_name| - install_have_children_element(name, plural_name) - install_model(name, '*') end - [ ["image", "?"], ["language", "?"], ].each do |name, occurs| - install_model(name, occurs) - end - - def other_element(need_convert, indent) - rv = <<-EOT -#{category_elements(need_convert, indent)} -#{generator_element(need_convert, indent)} -#{ttl_element(need_convert, indent)} -EOT - rv << super - end - - private - alias children09 children - def children - children09 + @category.compact - end - - alias _tags09 _tags - def _tags - rv = %w(generator ttl).delete_if do |name| - __send__(name).nil? - end.collect do |elem| - [nil, elem] - end + _tags09 - - @category.each do - rv << [nil, "category"] - end - - rv end Category = Item::Category @@ -66,15 +34,13 @@ EOT ["comments", "?"], ["author", "?"], ].each do |name, occurs| - install_text_element(name) - install_model(name, occurs) end [ ["pubDate", '?'], ].each do |name, occurs| - install_date_element(name, 'rfc822') - install_model(name, occurs) end alias date pubDate alias date= pubDate= @@ -82,37 +48,10 @@ EOT [ ["guid", '?'], ].each do |name, occurs| - install_have_child_element(name) - install_model(name, occurs) - end - - def other_element(need_convert, indent) - rv = [ - super, - *%w(author comments pubDate guid).collect do |name| - __send__("#{name}_element", false, indent) - end - ].reject do |value| - /\A\s*\z/.match(value) - end - rv.join("\n") end private - alias children09 children - def children - children09 + [@guid].compact - end - - alias _tags09 _tags - def _tags - %w(comments author pubDate guid).delete_if do |name| - __send__(name).nil? - end.collect do |elem| - [nil, elem] - end + _tags09 - end - alias _setup_maker_element setup_maker_element def setup_maker_element(item) _setup_maker_element(item) @@ -124,17 +63,21 @@ EOT include RSS09 [ - ["isPermaLink", nil, false, :boolean] ].each do |name, uri, required, type| install_get_attribute(name, uri, required, type) end content_setup - def initialize(isPermaLink=nil, content=nil) - super() - self.isPermaLink = isPermaLink - self.content = content end alias_method :_PermaLink?, :PermaLink? @@ -145,12 +88,6 @@ EOT end private - def _attrs - [ - ["isPermaLink", false] - ] - end - def maker_target(item) item.guid end @@ -168,7 +105,7 @@ EOT end RSS09::ELEMENTS.each do |name| - BaseListener.install_get_text_element(nil, name, "#{name}=") end end @@ -0,0 +1,749 @@ @@ -15,28 +15,13 @@ module RSS def self.append_features(klass) super - - klass.module_eval(<<-EOC, *get_file_and_line_from_caller(1)) - %w(encoded).each do |name| - install_text_element("\#{CONTENT_PREFIX}_\#{name}") - end - EOC - end - - def content_validate(tags) - counter = {} - ELEMENTS.each do |name| - counter[name] = 0 - end - tags.each do |tag| - key = "#{CONTENT_PREFIX}_#{tag}" - raise UnknownTagError.new(tag, CONTENT_URI) unless counter.has_key?(key) - counter[key] += 1 - raise TooMuchTagError.new(tag, tag_name) if counter[key] > 1 end end - end class RDF @@ -47,7 +32,7 @@ module RSS ContentModel::ELEMENTS.uniq! ContentModel::ELEMENTS.each do |full_name| name = full_name[prefix_size..-1] - BaseListener.install_get_text_element(CONTENT_URI, name, "#{full_name}=") end end @@ -66,7 +66,7 @@ module RSS end end - def def_uconv_convert_if_can(meth, to_enc, from_enc) begin require "uconv" def_convert(1) do |value| @@ -79,24 +79,31 @@ module RSS EOC end rescue LoadError - def_iconv_convert(to_enc, from_enc, 1) end end def def_to_euc_jp_from_utf_8 - def_uconv_convert_if_can('u8toeuc', 'EUC-JP', 'UTF-8') end def def_to_utf_8_from_euc_jp - def_uconv_convert_if_can('euctou8', 'UTF-8', 'EUC-JP') end def def_to_shift_jis_from_utf_8 - def_uconv_convert_if_can('u8tosjis', 'Shift_JIS', 'UTF-8') end def def_to_utf_8_from_shift_jis - def_uconv_convert_if_can('sjistou8', 'UTF-8', 'Shift_JIS') end def def_to_euc_jp_from_shift_jis @@ -1,11 +1,8 @@ -require "rss/1.0" module RSS - DC_PREFIX = 'dc' DC_URI = "http://purl.org/dc/elements/1.1/" - - RDF.install_ns(DC_PREFIX, DC_URI) module BaseDublinCoreModel def append_features(klass) @@ -17,10 +14,10 @@ module RSS full_name = "#{DC_PREFIX}_#{name}" full_plural_name = "#{DC_PREFIX}_#{plural}" klass_name = "DublinCore#{Utils.to_class_name(name)}" klass.module_eval(<<-EOC, *get_file_and_line_from_caller(0)) - install_have_children_element(#{full_name.dump}, - #{full_plural_name.dump}) - remove_method :#{full_name} remove_method :#{full_name}= remove_method :set_#{full_name} @@ -36,8 +33,17 @@ module RSS EOC end klass.module_eval(<<-EOC, *get_file_and_line_from_caller(0)) - alias date #{DC_PREFIX}_date - alias date= #{DC_PREFIX}_date= # For backward compatibility alias #{DC_PREFIX}_rightses #{DC_PREFIX}_rights_list @@ -72,7 +78,7 @@ module RSS } ELEMENT_NAME_INFOS = DublinCoreModel::TEXT_ELEMENTS.to_a - DublinCoreModel::DATE_ELEMENTS.each do |name, _| ELEMENT_NAME_INFOS << [name, nil] end @@ -100,9 +106,13 @@ module RSS alias_method(:value, :content) alias_method(:value=, :content=) - def initialize(content=nil) - super() - self.content = content end def full_name @@ -121,39 +131,23 @@ module RSS end DATE_ELEMENTS.each do |name, type| module_eval(<<-EOC, *get_file_and_line_from_caller(0)) class DublinCore#{Utils.to_class_name(name)} < Element remove_method(:content=) remove_method(:value=) - date_writer("content", #{type.dump}, #{name.dump}) - alias_method(:value=, :content=) end EOC end - - def dc_validate(tags) - tags.each do |tag| - key = "#{DC_PREFIX}_#{tag}" - unless DublinCoreModel::ELEMENTS.include?(key) - raise UnknownTagError.new(tag, DC_URI) - end - end - end - end # For backward compatibility DublincoreModel = DublinCoreModel - class RDF - class Channel; include DublinCoreModel; end - class Image; include DublinCoreModel; end - class Item; include DublinCoreModel; end - class Textinput; include DublinCoreModel; end - end - DublinCoreModel::ELEMENTS.each do |name| class_name = Utils.to_class_name(name) BaseListener.install_class_name(DC_URI, name, "DublinCore#{class_name}") @@ -161,3 +155,7 @@ module RSS DublinCoreModel::ELEMENTS.collect! {|name| "#{DC_PREFIX}_#{name}"} end @@ -0,0 +1,13 @@ @@ -0,0 +1,13 @@ @@ -0,0 +1,17 @@ @@ -17,9 +17,11 @@ module RSS end module ImageModelUtils - def validate_one_tag_name(name, tags) - invalid = tags.find {|tag| tag != name} - raise UnknownTagError.new(invalid, IMAGE_URI) if invalid raise TooMuchTagError.new(name, tag_name) if tags.size > 1 end end @@ -31,13 +33,11 @@ module RSS def self.append_features(klass) super - klass.install_have_child_element("#{IMAGE_PREFIX}_item") end - def image_validate(tags) - validate_one_tag_name("item", tags) - end - class ImageItem < Element include RSS10 include DublinCoreModel @@ -53,19 +53,23 @@ module RSS IMAGE_URI end end - [ ["about", ::RSS::RDF::URI, true], ["resource", ::RSS::RDF::URI, false], ].each do |name, uri, required| - install_get_attribute(name, uri, required) end %w(width height).each do |tag| full_name = "#{IMAGE_PREFIX}_#{tag}" disp_name = "#{IMAGE_PREFIX}:#{tag}" - install_text_element(full_name, :integer, disp_name) - BaseListener.install_get_text_element(IMAGE_URI, tag, "#{full_name}=") end alias width= image_width= @@ -73,43 +77,21 @@ module RSS alias height= image_height= alias height image_height - def initialize(about=nil, resource=nil) - super() - self.about = about - self.resource = resource end def full_name tag_name_with_prefix(IMAGE_PREFIX) end - - def to_s(need_convert=true, indent=calc_indent) - rv = tag(indent) do |next_indent| - [ - other_element(false, next_indent), - ] - end - rv = convert(rv) if need_convert - rv - end private - def _tags - [ - [IMAGE_URI, 'width'], - [IMAGE_URI, 'height'], - ].delete_if do |uri, name| - __send__(name).nil? - end - end - - def _attrs - [ - ["#{::RSS::RDF::PREFIX}:about", true, "about"], - ["#{::RSS::RDF::PREFIX}:resource", false, "resource"], - ] - end - def maker_target(target) target.image_item end @@ -129,14 +111,12 @@ module RSS super unless klass.class == Module - klass.install_have_child_element("#{IMAGE_PREFIX}_favicon") end end - def image_validate(tags) - validate_one_tag_name("favicon", tags) - end - class ImageFavicon < Element include RSS10 include DublinCoreModel @@ -152,12 +132,13 @@ module RSS IMAGE_URI end end - [ - ["about", ::RSS::RDF::URI, true], - ["size", IMAGE_URI, true], - ].each do |name, uri, required| - install_get_attribute(name, uri, required) end AVAILABLE_SIZES = %w(small medium large) @@ -171,40 +152,27 @@ module RSS raise NotAvailableValueError.new(full_name, new_value, attr_name) end end - funcall(:_size=, new_value) end alias image_size= size= alias image_size size - def initialize(about=nil, size=nil) - super() - self.about = about - self.size = size end def full_name tag_name_with_prefix(IMAGE_PREFIX) end - - def to_s(need_convert=true, indent=calc_indent) - rv = tag(indent) do |next_indent| - [ - other_element(false, next_indent), - ] - end - rv = convert(rv) if need_convert - rv - end private - def _attrs - [ - ["#{::RSS::RDF::PREFIX}:about", true, "about"], - ["#{IMAGE_PREFIX}:size", true, "size"], - ] - end - def maker_target(target) target.image_favicon end @@ -1,14 +1,14 @@ require "rss/rss" module RSS - module Maker - MAKERS = {} - class << self def make(version, &block) - maker(version).make(&block) end def maker(version) @@ -19,16 +19,21 @@ module RSS MAKERS[version] = maker end - def filename_to_version(filename) - File.basename(filename, ".*") end end end - end require "rss/maker/1.0" require "rss/maker/2.0" require "rss/maker/content" require "rss/maker/dublincore" require "rss/maker/syndication" @@ -7,13 +7,14 @@ module RSS class RSS09 < RSSBase - def initialize(rss_version="0.91") super end private - def make_rss - Rss.new(@rss_version, @version, @encoding, @standalone) end def setup_elements(rss) @@ -22,40 +23,34 @@ module RSS class Channel < ChannelBase - def to_rss(rss) channel = Rss::Channel.new set = setup_values(channel) - if set rss.channel = channel setup_items(rss) setup_image(rss) setup_textinput(rss) - setup_other_elements(rss) - if rss.channel.image - rss - else - nil - end - elsif variable_is_set? - raise NotSetError.new("maker.channel", not_set_required_variables) end end - def have_required_values? - @title and @link and @description and @language - end - private def setup_items(rss) - @maker.items.to_rss(rss) end def setup_image(rss) - @maker.image.to_rss(rss) end def setup_textinput(rss) - @maker.textinput.to_rss(rss) end def variables @@ -63,162 +58,409 @@ module RSS end def required_variable_names - %w(title link description language) end - class SkipDays < SkipDaysBase - def to_rss(rss, channel) unless @days.empty? skipDays = Rss::Channel::SkipDays.new channel.skipDays = skipDays @days.each do |day| - day.to_rss(rss, skipDays.days) end end end class Day < DayBase - def to_rss(rss, days) day = Rss::Channel::SkipDays::Day.new set = setup_values(day) if set days << day - setup_other_elements(rss) end end - def have_required_values? - @content end end end class SkipHours < SkipHoursBase - def to_rss(rss, channel) unless @hours.empty? skipHours = Rss::Channel::SkipHours.new channel.skipHours = skipHours @hours.each do |hour| - hour.to_rss(rss, skipHours.hours) end end end class Hour < HourBase - def to_rss(rss, hours) hour = Rss::Channel::SkipHours::Hour.new set = setup_values(hour) if set hours << hour - setup_other_elements(rss) end end - def have_required_values? - @content end end end class Cloud < CloudBase - def to_rss(*args) end end class Categories < CategoriesBase - def to_rss(*args) end class Category < CategoryBase end end end - class Image < ImageBase - def to_rss(rss) image = Rss::Channel::Image.new set = setup_values(image) if set image.link = link rss.channel.image = image - setup_other_elements(rss) end end - - def have_required_values? - @url and @title and link end end class Items < ItemsBase - def to_rss(rss) if rss.channel normalize.each do |item| - item.to_rss(rss) end - setup_other_elements(rss) end end class Item < ItemBase - def to_rss(rss) item = Rss::Channel::Item.new set = setup_values(item) - if set rss.items << item - setup_other_elements(rss) end end - - private def have_required_values? - @title and @link end class Guid < GuidBase - def to_rss(*args) end end - class Enclosure < EnclosureBase - def to_rss(*args) end end - class Source < SourceBase - def to_rss(*args) end end - class Categories < CategoriesBase - def to_rss(*args) end class Category < CategoryBase end end - end end class Textinput < TextinputBase - def to_rss(rss) textInput = Rss::Channel::TextInput.new set = setup_values(textInput) if set rss.channel.textInput = textInput - setup_other_elements(rss) end end private - def have_required_values? - @title and @description and @name and @link end end end - add_maker(filename_to_version(__FILE__), RSS09) - add_maker(filename_to_version(__FILE__) + "1", RSS09) end end @@ -9,10 +9,11 @@ module RSS def initialize super("1.0") end private - def make_rss RDF.new(@version, @encoding, @standalone) end @@ -25,43 +26,46 @@ module RSS class Channel < ChannelBase - def to_rss(rss) - set = false - if @about - channel = RDF::Channel.new(@about) - set = setup_values(channel) - if set channel.dc_dates.clear rss.channel = channel setup_items(rss) setup_image(rss) setup_textinput(rss) - setup_other_elements(rss) end end - - if (!@about or !set) and variable_is_set? - raise NotSetError.new("maker.channel", not_set_required_variables) - end - end - - def have_required_values? - @about and @title and @link and @description end private def setup_items(rss) items = RDF::Channel::Items.new seq = items.Seq - @maker.items.normalize.each do |item| - seq.lis << RDF::Channel::Items::Seq::Li.new(item.link) end rss.channel.items = items end def setup_image(rss) if @maker.image.have_required_values? - rss.channel.image = RDF::Channel::Image.new(@maker.image.url) end end @@ -69,15 +73,23 @@ module RSS if @maker.textinput.have_required_values? textinput = RDF::Channel::Textinput.new(@maker.textinput.link) rss.channel.textinput = textinput end end def required_variable_names - %w(about title link description) end - class SkipDays < SkipDaysBase - def to_rss(*args) end class Day < DayBase @@ -85,7 +97,7 @@ module RSS end class SkipHours < SkipHoursBase - def to_rss(*args) end class Hour < HourBase @@ -93,112 +105,330 @@ module RSS end class Cloud < CloudBase - def to_rss(*args) end end class Categories < CategoriesBase - def to_rss(*args) end class Category < CategoryBase end end end class Image < ImageBase - def to_rss(rss) if @url image = RDF::Image.new(@url) set = setup_values(image) if set rss.image = image - setup_other_elements(rss) end end end def have_required_values? - @url and @title and link and @maker.channel.have_required_values? end private def variables super + ["link"] end end class Items < ItemsBase - def to_rss(rss) if rss.channel normalize.each do |item| - item.to_rss(rss) end - setup_other_elements(rss) end end class Item < ItemBase - def to_rss(rss) - if @link - item = RDF::Item.new(@link) set = setup_values(item) if set item.dc_dates.clear rss.items << item - setup_other_elements(rss) end end end - def have_required_values? - @title and @link end class Guid < GuidBase - def to_rss(*args) end end - class Enclosure < EnclosureBase - def to_rss(*args) end end - class Source < SourceBase - def to_rss(*args) end end - class Categories < CategoriesBase - def to_rss(*args) end class Category < CategoryBase end end end end class Textinput < TextinputBase - def to_rss(rss) if @link textinput = RDF::Textinput.new(@link) set = setup_values(textinput) if set rss.textinput = textinput - setup_other_elements(rss) end end end def have_required_values? - @title and @description and @name and @link and - @maker.channel.have_required_values? end end end - add_maker(filename_to_version(__FILE__), RSS10) end end @@ -7,16 +7,13 @@ module RSS class RSS20 < RSS09 - def initialize(rss_version="2.0") super end class Channel < RSS09::Channel - def have_required_values? - @title and @link and @description - end - def required_variable_names %w(title link description) end @@ -32,47 +29,64 @@ module RSS end class Cloud < RSS09::Channel::Cloud - def to_rss(rss, channel) cloud = Rss::Channel::Cloud.new set = setup_values(cloud) if set channel.cloud = cloud - setup_other_elements(rss) end end - def have_required_values? - @domain and @port and @path and - @registerProcedure and @protocol end end class Categories < RSS09::Channel::Categories - def to_rss(rss, channel) @categories.each do |category| - category.to_rss(rss, channel) end end class Category < RSS09::Channel::Categories::Category - def to_rss(rss, channel) category = Rss::Channel::Category.new set = setup_values(category) if set channel.categories << category - setup_other_elements(rss) end end - - def have_required_values? - @content end end end - end class Image < RSS09::Image end class Items < RSS09::Items @@ -84,85 +98,123 @@ module RSS end private def variables super + ["pubDate"] end class Guid < RSS09::Items::Item::Guid - def to_rss(rss, item) guid = Rss::Channel::Item::Guid.new set = setup_values(guid) if set item.guid = guid - setup_other_elements(rss) end end - - def have_required_values? - @content end end class Enclosure < RSS09::Items::Item::Enclosure - def to_rss(rss, item) enclosure = Rss::Channel::Item::Enclosure.new set = setup_values(enclosure) if set item.enclosure = enclosure - setup_other_elements(rss) end end - - def have_required_values? - @url and @length and @type end end class Source < RSS09::Items::Item::Source - def to_rss(rss, item) source = Rss::Channel::Item::Source.new set = setup_values(source) if set item.source = source - setup_other_elements(rss) end end - - def have_required_values? - @url and @content end end class Categories < RSS09::Items::Item::Categories - def to_rss(rss, item) @categories.each do |category| - category.to_rss(rss, item) end end class Category < RSS09::Items::Item::Categories::Category - def to_rss(rss, item) category = Rss::Channel::Item::Category.new set = setup_values(category) if set item.categories << category setup_other_elements(rss) end end - - def have_required_values? - @content end end end end - end class Textinput < RSS09::Textinput end end - add_maker(filename_to_version(__FILE__), RSS20) end end @@ -0,0 +1,172 @@ @@ -4,9 +4,7 @@ require 'rss/rss' module RSS module Maker - module Base - def self.append_features(klass) super @@ -46,28 +44,60 @@ module RSS NEED_INITIALIZE_VARIABLES end - def self.def_array_element(name) include Enumerable extend Forwardable - def_delegators("@\#{name}", :<<, :[], :[]=, :first, :last) - def_delegators("@\#{name}", :push, :pop, :shift, :unshift) - def_delegators("@\#{name}", :each, :size) - - add_need_initialize_variable(name, "[]") end EOC end def initialize(maker) @maker = maker initialize_variables end def have_required_values? - true end - private def initialize_variables self.class.need_initialize_variables.each do |variable_name, init_value| @@ -75,16 +105,32 @@ module RSS end end - def setup_other_elements(rss) self.class.other_elements.each do |element| - __send__("setup_#{element}", rss, current_element(rss)) end end - def current_element(rss) - rss end - def setup_values(target) set = false if have_required_values? @@ -102,6 +148,10 @@ module RSS set end def variables self.class.need_initialize_variables.find_all do |name, init| "nil" == init @@ -110,10 +160,6 @@ module RSS end end - def variable_is_set? - variables.find {|var| !__send__(var).nil?} - end - def not_set_required_variables required_variable_names.find_all do |var| __send__(var).nil? @@ -126,7 +172,92 @@ module RSS end true end - end class RSSBase @@ -143,8 +274,8 @@ module RSS add_need_initialize_variable(element, "make_#{element}") module_eval(<<-EOC, __FILE__, __LINE__) private - def setup_#{element}(rss) - @#{element}.to_rss(rss) end def make_#{element} @@ -153,12 +284,15 @@ module RSS EOC end - attr_reader :rss_version attr_accessor :version, :encoding, :standalone - - def initialize(rss_version) super(self) - @rss_version = rss_version @version = "1.0" @encoding = "UTF-8" @standalone = nil @@ -167,19 +301,19 @@ EOC def make if block_given? yield(self) - to_rss else nil end end - def to_rss - rss = make_rss - setup_xml_stylesheets(rss) - setup_elements(rss) - setup_other_elements(rss) - if rss.channel - rss else nil end @@ -190,25 +324,12 @@ EOC def make_xml_stylesheets XMLStyleSheets.new(self) end - end class XMLStyleSheets include Base - def_array_element("xml_stylesheets") - - def to_rss(rss) - @xml_stylesheets.each do |xss| - xss.to_rss(rss) - end - end - - def new_xml_stylesheet - xss = XMLStyleSheet.new(@maker) - @xml_stylesheets << xss - xss - end class XMLStyleSheet include Base @@ -218,19 +339,15 @@ EOC add_need_initialize_variable(attribute) end - def to_rss(rss) xss = ::RSS::XMLStyleSheet.new guess_type_if_need(xss) set = setup_values(xss) if set - rss.xml_stylesheets << xss end end - def have_required_values? - @href and @type - end - private def guess_type_if_need(xss) if @type.nil? @@ -238,20 +355,27 @@ EOC @type = xss.type end end end end class ChannelBase include Base - %w(cloud categories skipDays skipHours).each do |element| attr_reader element add_other_element(element) add_need_initialize_variable(element, "make_#{element}") module_eval(<<-EOC, __FILE__, __LINE__) private - def setup_#{element}(rss, current) - @#{element}.to_rss(rss, current) end def make_#{element} @@ -260,34 +384,102 @@ EOC EOC end - %w(about title link description language copyright managingEditor webMaster rating docs date - lastBuildDate generator ttl).each do |element| attr_accessor element add_need_initialize_variable(element) end - alias_method(:pubDate, :date) - alias_method(:pubDate=, :date=) - def current_element(rss) - rss.channel end class SkipDaysBase include Base - def_array_element("days") - - def new_day - day = self.class::Day.new(@maker) - @days << day - day - end - - def current_element(rss) - rss.channel.skipDays - end class DayBase include Base @@ -296,28 +488,13 @@ EOC attr_accessor element add_need_initialize_variable(element) end - - def current_element(rss) - rss.channel.skipDays.last - end - end end class SkipHoursBase include Base - def_array_element("hours") - - def new_hour - hour = self.class::Hour.new(@maker) - @hours << hour - hour - end - - def current_element(rss) - rss.channel.skipHours - end class HourBase include Base @@ -326,11 +503,6 @@ EOC attr_accessor element add_need_initialize_variable(element) end - - def current_element(rss) - rss.channel.skipHours.last - end - end end @@ -341,33 +513,88 @@ EOC attr_accessor element add_need_initialize_variable(element) end - - def current_element(rss) - rss.channel.cloud - end - end class CategoriesBase include Base - - def_array_element("categories") - def new_category - category = self.class::Category.new(@maker) - @categories << category - category - end class CategoryBase include Base - %w(domain content).each do |element| attr_accessor element add_need_initialize_variable(element) end end end end class ImageBase @@ -377,21 +604,17 @@ EOC attr_accessor element add_need_initialize_variable(element) end - def link @maker.channel.link end - - def current_element(rss) - rss.image - end end class ItemsBase include Base - def_array_element("items") - attr_accessor :do_sort, :max_size def initialize(maker) @@ -407,17 +630,7 @@ EOC sort_if_need[0..@max_size] end end - - def current_element(rss) - rss.items - end - def new_item - item = self.class::Item.new(@maker) - @items << item - item - end - private def sort_if_need if @do_sort.respond_to?(:call) @@ -435,15 +648,17 @@ EOC class ItemBase include Base - - %w(guid enclosure source categories).each do |element| attr_reader element add_other_element(element) add_need_initialize_variable(element, "make_#{element}") module_eval(<<-EOC, __FILE__, __LINE__) private - def setup_#{element}(rss, current) - @#{element}.to_rss(rss, current) end def make_#{element} @@ -451,30 +666,77 @@ EOC end EOC end - - %w(title link description date author comments).each do |element| attr_accessor element add_need_initialize_variable(element) end - alias_method(:pubDate, :date) - alias_method(:pubDate=, :date=) def <=>(other) - if date and other.date - date <=> other.date - elsif date 1 - elsif other.date -1 else 0 end end - - def current_element(rss) - rss.items.last - end class GuidBase include Base @@ -484,7 +746,7 @@ EOC add_need_initialize_variable(element) end end - class EnclosureBase include Base @@ -493,18 +755,168 @@ EOC add_need_initialize_variable(element) end end - class SourceBase include Base - %w(url content).each do |element| attr_accessor element add_need_initialize_variable(element) end end - CategoriesBase = ChannelBase::CategoriesBase - end end @@ -515,12 +927,6 @@ EOC attr_accessor element add_need_initialize_variable(element) end - - def current_element(rss) - rss.textinput - end - end - end end @@ -24,8 +24,8 @@ module RSS #{full_plural_klass_name}.new(@maker) end - def setup_#{full_plural_name}(rss, current) - @#{full_plural_name}.to_rss(rss, current) end def #{full_name} @@ -36,6 +36,17 @@ module RSS @#{full_plural_name}[0] = #{full_klass_name}.new(self) @#{full_plural_name}[0].value = new_value end EOC end @@ -48,25 +59,14 @@ EOC ::RSS::DublinCoreModel::ELEMENT_NAME_INFOS.each do |name, plural_name| plural_name ||= "#{name}s" klass_name = Utils.to_class_name(name) plural_klass_name = "DublinCore#{Utils.to_class_name(plural_name)}" module_eval(<<-EOC, __FILE__, __LINE__) class #{plural_klass_name}Base include Base - def_array_element(#{plural_name.dump}) - - def new_#{name} - #{name} = self.class::#{klass_name}.new(self) - @#{plural_name} << #{name} - #{name} - end - def to_rss(rss, current) - @#{plural_name}.each do |#{name}| - #{name}.to_rss(rss, current) - end - end - class #{klass_name}Base include Base @@ -78,6 +78,13 @@ EOC def have_required_values? @value end end end EOC @@ -88,16 +95,9 @@ EOC plural_name ||= "#{name}s" klass_name = Utils.to_class_name(name) plural_klass_name = "DublinCore#{Utils.to_class_name(plural_name)}" - full_klass_name = "DublinCore#{klass_name}" - klass.module_eval(<<-EOC, *Utils.get_file_and_line_from_caller(1)) class #{plural_klass_name} < #{plural_klass_name}Base class #{klass_name} < #{klass_name}Base - def to_rss(rss, current) - if value and current.respond_to?(:dc_#{name}) - new_item = current.class::#{full_klass_name}.new(value) - current.dc_#{plural_name} << new_item - end - end end end EOC @@ -107,64 +107,36 @@ EOC class ChannelBase include DublinCoreModel - - remove_method(:date) - remove_method(:date=) - alias_method(:date, :dc_date) - alias_method(:date=, :dc_date=) end class ImageBase; include DublinCoreModel; end class ItemsBase class ItemBase include DublinCoreModel - - remove_method(:date) - remove_method(:date=) - alias_method(:date, :dc_date) - alias_method(:date=, :dc_date=) end end class TextinputBase; include DublinCoreModel; end - class RSS10 - class Channel - DublinCoreModel.install_dublin_core(self) - end - - class Image - DublinCoreModel.install_dublin_core(self) - end - - class Items - class Item DublinCoreModel.install_dublin_core(self) end - end - class Textinput - DublinCoreModel.install_dublin_core(self) - end - end - - class RSS09 - class Channel - DublinCoreModel.install_dublin_core(self) - end - class Image - DublinCoreModel.install_dublin_core(self) - end - class Items - class Item DublinCoreModel.install_dublin_core(self) end - end - - class Textinput - DublinCoreModel.install_dublin_core(self) - end end end end @@ -0,0 +1,167 @@ @@ -0,0 +1,429 @@ @@ -11,11 +11,11 @@ module RSS name = "#{RSS::IMAGE_PREFIX}_item" klass.add_need_initialize_variable(name, "make_#{name}") klass.add_other_element(name) - klass.module_eval(<<-EOC, __FILE__, __LINE__+1) attr_reader :#{name} - def setup_#{name}(rss, current) if @#{name} - @#{name}.to_rss(rss, current) end end @@ -25,6 +25,14 @@ module RSS EOC end class ImageItemBase include Base include Maker::DublinCoreModel @@ -42,6 +50,15 @@ EOC def have_required_values? @about end end end @@ -54,9 +71,9 @@ EOC klass.add_other_element(name) klass.module_eval(<<-EOC, __FILE__, __LINE__+1) attr_reader :#{name} - def setup_#{name}(rss, current) if @#{name} - @#{name}.to_rss(rss, current) end end @@ -66,6 +83,14 @@ EOC EOC end class ImageFaviconBase include Base include Maker::DublinCoreModel @@ -79,6 +104,15 @@ EOC def have_required_values? @about and @image_size end end end @@ -88,58 +122,18 @@ EOC class ItemBase; include Maker::ImageItemModel; end end - class RSS10 - class Items - class Item - class ImageItem < ImageItemBase - DublinCoreModel.install_dublin_core(self) - def to_rss(rss, current) - if @about - item = ::RSS::ImageItemModel::ImageItem.new(@about, @resource) - setup_values(item) - setup_other_elements(item) - current.image_item = item - end - end - end end - end - - class Channel - class ImageFavicon < ImageFaviconBase - DublinCoreModel.install_dublin_core(self) - def to_rss(rss, current) - if @about and @image_size - args = [@about, @image_size] - favicon = ::RSS::ImageFaviconModel::ImageFavicon.new(*args) - setup_values(favicon) - setup_other_elements(favicon) - current.image_favicon = favicon - end - end - end - end - end - class RSS09 - class Items - class Item - class ImageItem < ImageItemBase - DublinCoreModel.install_dublin_core(self) - def to_rss(*args) - end end end - end - - class Channel - class ImageFavicon < ImageFaviconBase - DublinCoreModel.install_dublin_core(self) - def to_rss(*args) - end - end - end end - end end @@ -15,17 +15,17 @@ module RSS def make_taxo_topics self.class::TaxonomyTopics.new(@maker) end - - def setup_taxo_topics(rss, current) - @taxo_topics.to_rss(rss, current) end EOC end def self.install_taxo_topics(klass) - klass.module_eval(<<-EOC, *Utils.get_file_and_line_from_caller(1)) class TaxonomyTopics < TaxonomyTopicsBase - def to_rss(rss, current) if current.respond_to?(:taxo_topics) topics = current.class::TaxonomyTopics.new bag = topics.Bag @@ -43,7 +43,8 @@ EOC include Base attr_reader :resources - def_array_element("resources") end end @@ -59,8 +60,8 @@ EOC self.class::TaxonomyTopics.new(@maker) end - def setup_taxo_topics(rss, current) - @taxo_topics.to_rss(rss, current) end def taxo_topic @@ -75,25 +76,21 @@ EOC end def self.install_taxo_topic(klass) - klass.module_eval(<<-EOC, *Utils.get_file_and_line_from_caller(1)) class TaxonomyTopics < TaxonomyTopicsBase class TaxonomyTopic < TaxonomyTopicBase DublinCoreModel.install_dublin_core(self) TaxonomyTopicsModel.install_taxo_topics(self) - def to_rss(rss, current) if current.respond_to?(:taxo_topics) topic = current.class::TaxonomyTopic.new(value) topic.taxo_link = value - taxo_topics.to_rss(rss, topic) if taxo_topics current.taxo_topics << topic - setup_other_elements(rss) end end - - def current_element(rss) - super.taxo_topics.last - end end end EOC @@ -102,20 +99,8 @@ EOC class TaxonomyTopicsBase include Base - def_array_element("taxo_topics") - - def new_taxo_topic - taxo_topic = self.class::TaxonomyTopic.new(self) - @taxo_topics << taxo_topic - taxo_topic - end - def to_rss(rss, current) - @taxo_topics.each do |taxo_topic| - taxo_topic.to_rss(rss, current) - end - end - class TaxonomyTopicBase include Base include DublinCoreModel @@ -147,32 +132,20 @@ EOC end end - class RSS10 - TaxonomyTopicModel.install_taxo_topic(self) - - class Channel - TaxonomyTopicsModel.install_taxo_topics(self) - end - class Items - class Item TaxonomyTopicsModel.install_taxo_topics(self) end - end - end - - class RSS09 - TaxonomyTopicModel.install_taxo_topic(self) - - class Channel - TaxonomyTopicsModel.install_taxo_topics(self) - end - class Items - class Item - TaxonomyTopicsModel.install_taxo_topics(self) end - end end end end @@ -11,9 +11,9 @@ module RSS name = "#{RSS::TRACKBACK_PREFIX}_ping" klass.add_need_initialize_variable(name) klass.add_other_element(name) - klass.module_eval(<<-EOC, __FILE__, __LINE__+1) attr_accessor :#{name} - def setup_#{name}(rss, current) if #{name} and current.respond_to?(:#{name}=) current.#{name} = #{name} end @@ -23,14 +23,14 @@ module RSS name = "#{RSS::TRACKBACK_PREFIX}_abouts" klass.add_need_initialize_variable(name, "make_#{name}") klass.add_other_element(name) - klass.module_eval(<<-EOC, __FILE__, __LINE__+1) attr_accessor :#{name} def make_#{name} self.class::TrackBackAbouts.new(self) end - def setup_#{name}(rss, current) - @#{name}.to_rss(rss, current) end EOC end @@ -38,20 +38,8 @@ module RSS class TrackBackAboutsBase include Base - def_array_element("abouts") - - def new_about - about = self.class::TrackBackAbout.new(@maker) - @abouts << about - about - end - def to_rss(rss, current) - @abouts.each do |about| - about.to_rss(rss, current) - end - end - class TrackBackAboutBase include Base @@ -62,65 +50,38 @@ module RSS alias_method(:resource=, :value=) alias_method(:content, :value) alias_method(:content=, :value=) - def have_required_values? @value end - - end - end - end - - class ItemsBase - class ItemBase; include TrackBackModel; end - end - class RSS10 - class Items - class Item - class TrackBackAbouts < TrackBackAboutsBase - class TrackBackAbout < TrackBackAboutBase - def to_rss(rss, current) - if resource - about = ::RSS::TrackBackModel10::TrackBackAbout.new(resource) - current.trackback_abouts << about - end - end end end end end end - class RSS09 - class Items - class Item - class TrackBackAbouts < TrackBackAboutsBase - def to_rss(*args) - end - class TrackBackAbout < TrackBackAboutBase - end - end - end - end end - - class RSS20 - class Items - class Item - class TrackBackAbouts < TrackBackAboutsBase - class TrackBackAbout < TrackBackAboutBase - def to_rss(rss, current) - if content - about = ::RSS::TrackBackModel20::TrackBackAbout.new(content) - current.trackback_abouts << about - end end end end end - end end - end end @@ -2,6 +2,7 @@ require "forwardable" require "open-uri" require "rss/rss" module RSS @@ -63,7 +64,8 @@ module RSS end end - def parse(rss, do_validate=true, ignore_unknown_element=true, parser_class=default_parser) parser = new(rss, parser_class) parser.do_validate = do_validate parser.ignore_unknown_element = ignore_unknown_element @@ -103,7 +105,7 @@ module RSS return rss if rss.is_a?(::URI::Generic) begin - URI(rss) rescue ::URI::Error rss end @@ -158,26 +160,26 @@ module RSS class << self - @@setters = {} @@registered_uris = {} @@class_names = {} - def install_setter(uri, tag_name, setter) - @@setters[uri] ||= {} - @@setters[uri][tag_name] = setter - end - def setter(uri, tag_name) - begin - @@setters[uri][tag_name] - rescue NameError nil end end def available_tags(uri) begin - @@setters[uri].keys rescue NameError [] end @@ -205,8 +207,8 @@ module RSS end end - def install_get_text_element(uri, name, setter) - install_setter(uri, name, setter) def_get_text_element(uri, name, *get_file_and_line_from_caller(1)) end @@ -215,20 +217,18 @@ module RSS end private def def_get_text_element(uri, name, file, line) register_uri(uri, name) unless private_instance_methods(false).include?("start_#{name}".to_sym) module_eval(<<-EOT, file, line) def start_#{name}(name, prefix, attrs, ns) - uri = ns[prefix] if self.class.uri_registered?(uri, #{name.inspect}) - if @do_validate - tags = self.class.available_tags(uri) - unless tags.include?(name) - raise UnknownTagError.new(name, uri) - end - end start_get_text_element(name, prefix, ns, uri) else start_else_element(name, prefix, attrs, ns) @@ -244,7 +244,6 @@ module RSS end module ListenerMixin - attr_reader :rss attr_accessor :ignore_unknown_element @@ -254,13 +253,16 @@ module RSS @rss = nil @ignore_unknown_element = true @do_validate = true - @ns_stack = [{}] @tag_stack = [[]] @text_stack = [''] @proc_stack = [] @last_element = nil @version = @encoding = @standalone = nil @xml_stylesheets = [] end def xmldecl(version, encoding, standalone) @@ -271,7 +273,7 @@ module RSS if name == "xml-stylesheet" params = parse_pi_content(content) if params.has_key?("href") - @xml_stylesheets << XMLStyleSheet.new(*params) end end end @@ -291,12 +293,41 @@ module RSS @ns_stack.push(ns) prefix, local = split_name(name) - @tag_stack.last.push([ns[prefix], local]) @tag_stack.push([]) - if respond_to?("start_#{local}", true) - __send__("start_#{local}", local, prefix, attrs, ns.dup) else - start_else_element(local, prefix, attrs, ns.dup) end end @@ -313,10 +344,17 @@ module RSS end def text(data) - @text_stack.last << data end private CONTENT_PATTERN = /\s*([^=]+)=(["'])([^\2]+?)\2/ def parse_pi_content(content) @@ -328,20 +366,20 @@ module RSS end def start_else_element(local, prefix, attrs, ns) - class_name = self.class.class_name(ns[prefix], local) current_class = @last_element.class if current_class.const_defined?(class_name) next_class = current_class.const_get(class_name) start_have_something_element(local, prefix, attrs, ns, next_class) else - if @ignore_unknown_element @proc_stack.push(nil) else parent = "ROOT ELEMENT???" if current_class.tag_name parent = current_class.tag_name end - raise NotExceptedTagError.new(local, parent) end end end @@ -353,41 +391,48 @@ module RSS end def check_ns(tag_name, prefix, ns, require_uri) - if @do_validate - if ns[prefix] == require_uri - #ns.delete(prefix) - else raise NSError.new(tag_name, prefix, require_uri) end end end def start_get_text_element(tag_name, prefix, ns, required_uri) - @proc_stack.push Proc.new {|text, tags| setter = self.class.setter(required_uri, tag_name) - setter ||= "#{tag_name}=" if @last_element.respond_to?(setter) @last_element.__send__(setter, text.to_s) else - if @do_validate and not @ignore_unknown_element - raise NotExceptedTagError.new(tag_name, @last_element.tag_name) end end - } end def start_have_something_element(tag_name, prefix, attrs, ns, klass) check_ns(tag_name, prefix, ns, klass.required_uri) - args = [] - - klass.get_attributes.each do |a_name, a_uri, required| if a_uri.is_a?(String) or !a_uri.respond_to?(:include?) a_uri = [a_uri] end - unless a_uri == [nil] for prefix, uri in ns if a_uri.include?(uri) val = attrs["#{prefix}:#{a_name}"] @@ -395,12 +440,12 @@ module RSS end end end - if val.nil? and a_uri.include?(nil) val = attrs[a_name] end if @do_validate and required and val.nil? - unless a_uri.include?(nil) for prefix, uri in ns if a_uri.include?(uri) a_name = "#{prefix}:#{a_name}" @@ -410,20 +455,37 @@ module RSS raise MissingAttributeError.new(tag_name, a_name) end - args << val end previous = @last_element - next_element = klass.new(*args) - next_element.do_validate = @do_validate - previous.funcall(:set_next_element, tag_name, next_element) @last_element = next_element - @proc_stack.push Proc.new { |text, tags| p(@last_element.class) if DEBUG - @last_element.content = text if klass.have_content? - @last_element.validate_for_stream(tags) if @do_validate @last_element = previous - } end end @@ -11,11 +11,19 @@ class Time (\.\d+)? (Z|[+-]\d\d:\d\d)?)? \s*\z/ix =~ date and (($5 and $8) or (!$5 and !$8)) - datetime = [$1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, $6.to_i] - datetime << $7.to_f * 1000000 if $7 - if $8 - Time.utc(*datetime) - zone_offset($8) else Time.local(*datetime) end else @@ -26,7 +34,14 @@ class Time end unless method_defined?(:w3cdtf) - alias w3cdtf iso8601 end end @@ -38,7 +53,7 @@ require "rss/xml-stylesheet" module RSS - VERSION = "0.1.6" URI = "http://purl.org/rss/1.0/" @@ -87,13 +102,15 @@ module RSS end end - class NotExceptedTagError < InvalidRSSError - attr_reader :tag, :parent - def initialize(tag, parent) - @tag, @parent = tag, parent - super("tag <#{tag}> is not expected in tag <#{parent}>") end end class NotAvailableValueError < InvalidRSSError attr_reader :tag, :value, :attribute @@ -135,15 +152,27 @@ module RSS super("required variables of #{@name} are not set: #{@variables.join(', ')}") end end - module BaseModel include Utils - def install_have_child_element(name) add_need_initialize_variable(name) - attr_accessor name install_element(name) do |n, elem_name| <<-EOC if @#{n} @@ -156,11 +185,13 @@ EOC end alias_method(:install_have_attribute_element, :install_have_child_element) - def install_have_children_element(name, plural_name=nil) plural_name ||= "#{name}s" add_have_children_element(name, plural_name) add_plural_form(name, plural_name) - def_children_accessor(name, plural_name) install_element(name, "s") do |n, elem_name| <<-EOC @@ -174,9 +205,12 @@ EOC end end - def install_text_element(name, type=nil, disp_name=name) self::ELEMENTS << name add_need_initialize_variable(name) def_corresponded_attr_writer name, type, disp_name convert_attr_reader name @@ -199,9 +233,13 @@ EOC end end - def install_date_element(name, type, disp_name=name) self::ELEMENTS << name add_need_initialize_variable(name) # accessor convert_attr_reader name @@ -230,34 +268,76 @@ EOC private def install_element(name, postfix="") elem_name = name.sub('_', ':') module_eval(<<-EOC, *get_file_and_line_from_caller(2)) - def #{name}_element#{postfix}(need_convert=true, indent='') #{yield(name, elem_name)} end - private :#{name}_element#{postfix} EOC end - def convert_attr_reader(*attrs) attrs.each do |attr| attr = attr.id2name if attr.kind_of?(Integer) module_eval(<<-EOC, *get_file_and_line_from_caller(2)) def #{attr} - if @converter - @converter.convert(@#{attr}) else - @#{attr} end end EOC end end def date_writer(name, type, disp_name=name) module_eval(<<-EOC, *get_file_and_line_from_caller(2)) def #{name}=(new_value) - if new_value.nil? or new_value.kind_of?(Time) @#{name} = new_value else if @do_validate begin @@ -269,7 +349,9 @@ EOC @#{name} = nil if /\\A\\s*\\z/ !~ new_value.to_s begin - @#{name} = Time.parse(new_value) rescue ArgumentError end end @@ -350,6 +432,32 @@ EOC EOC end def def_children_accessor(accessor_name, plural_name) module_eval(<<-EOC, *get_file_and_line_from_caller(2)) def #{plural_name} @@ -365,10 +473,12 @@ EOC end def #{accessor_name}=(*args) warn("Warning:\#{caller.first.sub(/:in `.*'\z/, '')}: " \ - "Don't use `#{accessor_name} = XXX'/`set_#{accessor_name}(XXX)'. " \ "Those APIs are not sense of Ruby. " \ - "Use `#{plural_name} << XXX' instead of them.") if args.size == 1 @#{accessor_name}.push(args[0]) else @@ -378,36 +488,70 @@ EOC alias_method(:set_#{accessor_name}, :#{accessor_name}=) EOC end - def def_content_only_to_s - module_eval(<<-EOC, *get_file_and_line_from_caller(2)) - def to_s(need_convert=true, indent=calc_indent) - if @content - rv = tag(indent) do |next_indent| - h(@content) - end - rv = convert(rv) if need_convert - rv else - "" end end -EOC end - end class Element extend BaseModel include Utils INDENT = " " MUST_CALL_VALIDATORS = {} - MODEL = [] GET_ATTRIBUTES = [] HAVE_CHILDREN_ELEMENTS = [] NEED_INITIALIZE_VARIABLES = [] PLURAL_FORMS = {} @@ -416,8 +560,8 @@ EOC def must_call_validators MUST_CALL_VALIDATORS end - def model - MODEL end def get_attributes GET_ATTRIBUTES @@ -425,6 +569,9 @@ EOC def have_children_elements HAVE_CHILDREN_ELEMENTS end def need_initialize_variables NEED_INITIALIZE_VARIABLES end @@ -435,9 +582,10 @@ EOC def inherited(klass) klass.const_set("MUST_CALL_VALIDATORS", {}) - klass.const_set("MODEL", []) klass.const_set("GET_ATTRIBUTES", []) klass.const_set("HAVE_CHILDREN_ELEMENTS", []) klass.const_set("NEED_INITIALIZE_VARIABLES", []) klass.const_set("PLURAL_FORMS", {}) @@ -446,14 +594,13 @@ EOC @tag_name = name.split(/::/).last @tag_name[0,1] = @tag_name[0,1].downcase - @indent_size = name.split(/::/).size - 2 @have_content = false def self.must_call_validators super.merge(MUST_CALL_VALIDATORS) end - def self.model - MODEL + super end def self.get_attributes GET_ATTRIBUTES + super @@ -461,6 +608,9 @@ EOC def self.have_children_elements HAVE_CHILDREN_ELEMENTS + super end def self.need_initialize_variables NEED_INITIALIZE_VARIABLES + super end @@ -473,25 +623,32 @@ EOC MUST_CALL_VALIDATORS[uri] = prefix end - def self.install_model(tag, occurs=nil) - if m = MODEL.find {|t, o| t == tag} - m[1] = occurs else - MODEL << [tag, occurs] end end def self.install_get_attribute(name, uri, required=true, - type=nil, disp_name=name) - def_corresponded_attr_writer name, type, disp_name - convert_attr_reader name if type == :boolean and /^is/ =~ name alias_method "\#{$POSTMATCH}?", name end - GET_ATTRIBUTES << [name, uri, required] end - def self.def_corresponded_attr_writer(name, type=nil, disp_name=name) case type when :integer integer_writer name, disp_name @@ -499,15 +656,32 @@ EOC positive_integer_writer name, disp_name when :boolean boolean_writer name, disp_name else attr_writer name end end - def self.content_setup(type=nil) - def_corresponded_attr_writer "content", type - convert_attr_reader :content - def_content_only_to_s @have_content = true end @@ -519,6 +693,10 @@ EOC HAVE_CHILDREN_ELEMENTS << [variable_name, plural_name] end def self.add_need_initialize_variable(variable_name) NEED_INITIALIZE_VARIABLES << variable_name end @@ -535,7 +713,11 @@ EOC end def required_uri - nil end def install_ns(prefix, uri) @@ -548,19 +730,18 @@ EOC def tag_name @tag_name end - - def indent_size - @indent_size - end - end - attr_accessor :do_validate - def initialize(do_validate=true) @converter = nil @do_validate = do_validate - initialize_variables end def tag_name @@ -571,10 +752,6 @@ EOC tag_name end - def indent_size - self.class.indent_size - end - def converter=(converter) @converter = converter targets = children.dup @@ -593,33 +770,73 @@ EOC value end end - - def validate validate_attribute - __validate end - def validate_for_stream(tags) validate_attribute - __validate(tags, false) end - def setup_maker(maker) - target = maker_target(maker) - unless target.nil? - setup_maker_attributes(target) - setup_maker_element(target) - setup_maker_elements(target) end end - private - def initialize_variables self.class.need_initialize_variables.each do |variable_name| - instance_eval("@#{variable_name} = nil") end initialize_have_children_elements - @content = "" if self.class.have_content? end def initialize_have_children_elements @@ -628,14 +845,16 @@ EOC end end - def tag(indent, additional_attrs=[], &block) next_indent = indent + INDENT attrs = collect_attrs return "" if attrs.nil? - attrs += additional_attrs - start_tag = make_start_tag(indent, next_indent, attrs) if block content = block.call(next_indent) @@ -650,6 +869,7 @@ EOC else content = content.reject{|x| x.empty?} if content.empty? end_tag = "/>" else start_tag << ">\n" @@ -671,58 +891,24 @@ EOC end def collect_attrs - _attrs.collect do |name, required, alias_name| value = __send__(alias_name || name) return nil if required and value.nil? - [name, value] - end.reject do |name, value| - value.nil? end end def tag_name_with_prefix(prefix) "#{prefix}:#{tag_name}" end - - def calc_indent - INDENT * (self.class.indent_size) - end - def maker_target(maker) - nil - end - - def setup_maker_attributes(target) - end - - def setup_maker_element(target) - self.class.need_initialize_variables.each do |var| - value = __send__(var) - if value.respond_to?("setup_maker") and - !not_need_to_call_setup_maker_variables.include?(var) - value.setup_maker(target) - else - setter = "#{var}=" - if target.respond_to?(setter) - target.__send__(setter, value) - end - end - end - end - - def not_need_to_call_setup_maker_variables - [] - end - - def setup_maker_elements(parent) - self.class.have_children_elements.each do |name, plural_name| - if parent.respond_to?(plural_name) - target = parent.__send__(plural_name) - __send__(plural_name).each do |elem| - elem.setup_maker(target) - end - end - end end def set_next_element(tag_name, next_element) @@ -737,22 +923,41 @@ EOC __send__("#{prefix}#{tag_name}=", next_element) end end - - # not String class children. def children - [] end - # default #validate() argument. def _tags - [] end def _attrs - [] end - def __validate(tags=_tags, recursive=true) if recursive children.compact.each do |child| child.validate @@ -761,54 +966,44 @@ EOC must_call_validators = self.class.must_call_validators tags = tag_filter(tags.dup) p tags if DEBUG - self.class::NSPOOL.each do |prefix, uri| - if tags.has_key?(uri) and !must_call_validators.has_key?(uri) - meth = "#{prefix}_validate" - __send__(meth, tags[uri]) if respond_to?(meth, true) - end - end must_call_validators.each do |uri, prefix| - __send__("#{prefix}_validate", tags[uri]) end end def validate_attribute _attrs.each do |a_name, required, alias_name| - if required and __send__(alias_name || a_name).nil? raise MissingAttributeError.new(tag_name, a_name) end end end - def other_element(need_convert, indent='') - rv = [] - private_methods.each do |meth| - if /\A([^_]+)_[^_]+_elements?\z/ =~ meth.to_s and - self.class::NSPOOL.has_key?($1) - res = __send__(meth, need_convert, indent) - rv << res if /\A\s*\z/ !~ res - end - end - rv.join("\n") - end - - def _validate(tags, model=self.class.model) count = 1 do_redo = false not_shift = false tag = nil - element_names = model.collect {|elem| elem[0]} if tags tags_size = tags.size tags = tags.sort_by {|x| element_names.index(x) || tags_size} end - model.each_with_index do |elem, i| if DEBUG p "before" p tags - p elem end if not_shift @@ -822,41 +1017,41 @@ EOC p count end - case elem[1] when '?' if count > 2 - raise TooMuchTagError.new(elem[0], tag_name) else - if elem[0] == tag do_redo = true else not_shift = true end end when '*' - if elem[0] == tag do_redo = true else not_shift = true end when '+' - if elem[0] == tag do_redo = true else if count > 1 not_shift = true else - raise MissingTagError.new(elem[0], tag_name) end end else - if elem[0] == tag - if model[i+1] and model[i+1][0] != elem[0] and - tags and tags.first == elem[0] - raise TooMuchTagError.new(elem[0], tag_name) end else - raise MissingTagError.new(elem[0], tag_name) end end @@ -877,8 +1072,8 @@ EOC end - if !tags.nil? and !tags.empty? - raise NotExceptedTagError.new(tag, tag_name) end end @@ -892,6 +1087,43 @@ EOC rv end end module RootElementMixin @@ -899,16 +1131,23 @@ EOC include XMLStyleSheetMixin attr_reader :output_encoding - - def initialize(rss_version, version=nil, encoding=nil, standalone=nil) super() - @rss_version = rss_version @version = version || '1.0' @encoding = encoding @standalone = standalone @output_encoding = nil end def output_encoding=(enc) @output_encoding = enc self.converter = Converter.new(@output_encoding, @encoding) @@ -923,14 +1162,48 @@ EOC xss.setup_maker(maker) end - setup_maker_elements(maker) end - private - def tag(indent, attrs, &block) - rv = xmldecl + xml_stylesheet_pi - rv << super(indent, attrs, &block) - rv end def xmldecl @@ -944,18 +1217,16 @@ EOC end def ns_declarations self.class::NSPOOL.collect do |prefix, uri| prefix = ":#{prefix}" unless prefix.empty? - ["xmlns#{prefix}", uri] end end - - def setup_maker_elements(maker) - channel.setup_maker(maker) if channel - image.setup_maker(maker) if image - textinput.setup_maker(maker) if textinput - super(maker) end end - end @@ -15,20 +15,26 @@ module RSS def self.append_features(klass) super - - klass.module_eval(<<-EOC, *get_file_and_line_from_caller(1)) [ ["updatePeriod"], ["updateFrequency", :positive_integer] ].each do |name, type| - install_text_element("\#{SY_PREFIX}_\#{name}", type, - "\#{SY_PREFIX}:\#{name}") end %w(updateBase).each do |name| - install_date_element("\#{SY_PREFIX}_\#{name}", 'w3cdtf', name) end alias_method(:_sy_updatePeriod=, :sy_updatePeriod=) def sy_updatePeriod=(new_value) new_value = new_value.strip @@ -38,20 +44,6 @@ module RSS EOC end - def sy_validate(tags) - counter = {} - ELEMENTS.each do |name| - counter[name] = 0 - end - - tags.each do |tag| - key = "#{SY_PREFIX}_#{tag}" - raise UnknownTagError.new(tag, SY_URI) unless counter.has_key?(key) - counter[key] += 1 - raise TooMuchTagError.new(tag, tag_name) if counter[key] > 1 - end - end - private SY_UPDATEPERIOD_AVAILABLE_VALUES = %w(hourly daily weekly monthly yearly) def validate_sy_updatePeriod(value) @@ -69,7 +61,7 @@ module RSS SyndicationModel::ELEMENTS.uniq! SyndicationModel::ELEMENTS.each do |full_name| name = full_name[prefix_size..-1] - BaseListener.install_get_text_element(SY_URI, name, "#{full_name}=") end end @@ -12,7 +12,7 @@ module RSS %w(link).each do |name| full_name = "#{TAXO_PREFIX}_#{name}" - BaseListener.install_get_text_element(TAXO_URI, name, "#{full_name}=") TAXO_ELEMENTS << "#{TAXO_PREFIX}_#{name}" end @@ -28,22 +28,10 @@ module RSS def self.append_features(klass) super - var_name = "#{TAXO_PREFIX}_topics" - klass.install_have_child_element(var_name) - end - - def taxo_validate(tags) - found_topics = false - tags.each do |tag| - if tag == "topics" - if found_topics - raise TooMuchTagError.new(tag, tag_name) - else - found_topics = true - end - else - raise UnknownTagError.new(tag, TAXO_URI) - end end end @@ -64,13 +52,17 @@ module RSS @tag_name = "topics" - install_have_child_element("Bag") - - install_must_call_validator('rdf', ::RSS::RDF::URI) - def initialize(bag=Bag.new) - super() - @Bag = bag end def full_name @@ -80,15 +72,6 @@ module RSS def maker_target(target) target.taxo_topics end - - def to_s(need_convert=true, indent=calc_indent) - rv = tag(indent) do |next_indent| - [ - Bag_element(need_convert, next_indent), - other_element(need_convert, next_indent), - ] - end - end def resources if @Bag @@ -99,21 +82,6 @@ module RSS [] end end - - private - def children - [@Bag] - end - - def _tags - rv = [] - rv << [::RSS::RDF::URI, 'Bag'] unless @Bag.nil? - rv - end - - def rdf_validate(tags) - _validate(tags, [["Bag", nil]]) - end end end @@ -123,15 +91,7 @@ module RSS def self.append_features(klass) super var_name = "#{TAXO_PREFIX}_topic" - klass.install_have_children_element(var_name) - end - - def taxo_validate(tags) - tags.each do |tag| - if tag != "topic" - raise UnknownTagError.new(tag, TAXO_URI) - end - end end class TaxonomyTopic < Element @@ -152,62 +112,26 @@ module RSS @tag_name = "topic" - install_get_attribute("about", ::RSS::RDF::URI, true) - install_text_element("#{TAXO_PREFIX}_link") - def initialize(about=nil) - super() - @about = about end def full_name tag_name_with_prefix(TAXO_PREFIX) end - - def to_s(need_convert=true, indent=calc_indent) - rv = tag(indent) do |next_indent| - [ - other_element(need_convert, next_indent), - ] - end - end - - def taxo_validate(tags) - elements = %w(link topics) - counter = {} - - tags.each do |tag| - if elements.include?(tag) - counter[tag] ||= 0 - counter[tag] += 1 - raise TooMuchTagError.new(tag, tag_name) if counter[tag] > 1 - else - raise UnknownTagError.new(tag, TAXO_URI) - end - end - end def maker_target(target) target.new_taxo_topic end - - private - def children - [@taxo_link, @taxo_topics] - end - - def _attrs - [ - ["#{RDF::PREFIX}:about", true, "about"] - ] - end - - def _tags - rv = [] - rv << [TAXO_URI, "link"] unless @taxo_link.nil? - rv << [TAXO_URI, "topics"] unless @taxo_topics.nil? - rv - end end end @@ -11,28 +11,15 @@ module RSS module TrackBackUtils private - def trackback_validate(tags) - counter = {} - %w(ping about).each do |name| - counter["#{TRACKBACK_PREFIX}_#{name}"] = 0 - end - - tags.each do |tag| - key = "#{TRACKBACK_PREFIX}_#{tag}" - raise UnknownTagError.new(tag, TRACKBACK_URI) unless counter.has_key?(key) - counter[key] += 1 - if tag != "about" and counter[key] > 1 - raise TooMuchTagError.new(tag, tag_name) - end - end - - if counter["#{TRACKBACK_PREFIX}_ping"].zero? and - counter["#{TRACKBACK_PREFIX}_about"].nonzero? raise MissingTagError.new("#{TRACKBACK_PREFIX}:ping", tag_name) end end end - module BaseTrackBackModel ELEMENTS = %w(ping about) @@ -43,10 +30,11 @@ module RSS unless klass.class == Module klass.module_eval {include TrackBackUtils} %w(ping).each do |name| var_name = "#{TRACKBACK_PREFIX}_#{name}" klass_name = "TrackBack#{Utils.to_class_name(name)}" - klass.install_have_child_element(var_name) klass.module_eval(<<-EOC, __FILE__, __LINE__) remove_method :#{var_name} def #{var_name} @@ -63,7 +51,8 @@ module RSS [%w(about s)].each do |name, postfix| var_name = "#{TRACKBACK_PREFIX}_#{name}" klass_name = "TrackBack#{Utils.to_class_name(name)}" - klass.install_have_children_element(var_name) klass.module_eval(<<-EOC, __FILE__, __LINE__) remove_method :#{var_name} def #{var_name}(*args) @@ -124,38 +113,28 @@ module RSS end @tag_name = "ping" - [ ["resource", ::RSS::RDF::URI, true] ].each do |name, uri, required| - install_get_attribute(name, uri, required) end alias_method(:value, :resource) alias_method(:value=, :resource=) - - def initialize(resource=nil) - super() - @resource = resource end def full_name tag_name_with_prefix(TRACKBACK_PREFIX) end - - def to_s(need_convert=true, indent=calc_indent) - rv = tag(indent) - rv = convert(rv) if need_convert - rv - end - - private - def _attrs - [ - ["#{::RSS::RDF::PREFIX}:resource", true, "resource"], - ] - end - end class TrackBackAbout < Element @@ -174,38 +153,31 @@ module RSS end @tag_name = "about" - [ ["resource", ::RSS::RDF::URI, true] ].each do |name, uri, required| - install_get_attribute(name, uri, required) end alias_method(:value, :resource) alias_method(:value=, :resource=) - def initialize(resource=nil) - super() - @resource = resource end def full_name tag_name_with_prefix(TRACKBACK_PREFIX) end - - def to_s(need_convert=true, indent=calc_indent) - rv = tag(indent) - rv = convert(rv) if need_convert - rv - end private - def _attrs - [ - ["#{::RSS::RDF::PREFIX}:resource", true, "resource"], - ] - end - def maker_target(abouts) abouts.new_about end @@ -243,9 +215,13 @@ module RSS alias_method(:value, :content) alias_method(:value=, :content=) - def initialize(content=nil) - super() - @content = content end def full_name @@ -276,9 +252,13 @@ module RSS alias_method(:value, :content) alias_method(:value=, :content=) - def initialize(content=nil) - super() - @content = content end def full_name @@ -1,7 +1,5 @@ module RSS - module Utils - module_function def to_class_name(name) name.split(/_/).collect do |part| @@ -26,6 +24,9 @@ module RSS klass.new(value) end end - end end @@ -0,0 +1,71 @@ |