diff options
author | kou <kou@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2006-06-19 16:12:45 +0000 |
---|---|---|
committer | kou <kou@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2006-06-19 16:12:45 +0000 |
commit | b60b8361fdf87961e0538522624d692b8e82cc4c (patch) | |
tree | 485e53f3169d084c1f54f9a704095a4952eb0542 /lib | |
parent | eb41c8d2e33603bbc993ca0778421ea0f12e7fb4 (diff) |
* lib/rss/rss.rb:
- cleanup validation mechanism. Now, #XXX_validation is
needless.
- changed internal variable name RSS::Element::MODEL to
RSS::Element::MODELS.
- RSS::Element.install_model requires uri.
* lib/rss/0.9.rb: followed new validation API.
* lib/rss/1.0.rb: ditto.
* lib/rss/2.0.rb: ditto.
* lib/rss/content.rb: ditto.
* lib/rss/dublincore.rb: ditto.
* lib/rss/image.rb: ditto.
* lib/rss/syndication.rb: ditto.
* lib/rss/taxonomy.rb: ditto.
* lib/rss/trackback.rb: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@10334 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib')
-rw-r--r-- | lib/rss/0.9.rb | 16 | ||||
-rw-r--r-- | lib/rss/1.0.rb | 30 | ||||
-rw-r--r-- | lib/rss/2.0.rb | 12 | ||||
-rw-r--r-- | lib/rss/content.rb | 25 | ||||
-rw-r--r-- | lib/rss/dublincore.rb | 17 | ||||
-rw-r--r-- | lib/rss/image.rb | 14 | ||||
-rw-r--r-- | lib/rss/rss.rb | 66 | ||||
-rw-r--r-- | lib/rss/syndication.rb | 29 | ||||
-rw-r--r-- | lib/rss/taxonomy.rb | 53 | ||||
-rw-r--r-- | lib/rss/trackback.rb | 26 |
10 files changed, 90 insertions, 198 deletions
diff --git a/lib/rss/0.9.rb b/lib/rss/0.9.rb index d69e807d30..c05af201b8 100644 --- a/lib/rss/0.9.rb +++ b/lib/rss/0.9.rb @@ -22,7 +22,7 @@ module RSS [ ["channel", nil], ].each do |tag, occurs| - install_model(tag, occurs) + install_model(tag, "", occurs) end %w(channel).each do |name| @@ -109,7 +109,7 @@ module RSS ["textInput", "?", :have_child], ].each do |name, occurs, type, *args| __send__("install_#{type}_element", name, *args) - install_model(name, occurs) + install_model(name, "", occurs) end alias date pubDate alias date= pubDate= @@ -179,7 +179,7 @@ module RSS ["day", "*"] ].each do |name, occurs| install_have_children_element(name) - install_model(name, occurs) + install_model(name, "", occurs) end private @@ -218,7 +218,7 @@ module RSS ["hour", "*"] ].each do |name, occurs| install_have_children_element(name) - install_model(name, occurs) + install_model(name, "", occurs) end private @@ -255,7 +255,7 @@ module RSS %w(url title link).each do |name| install_text_element(name) - install_model(name, nil) + install_model(name, "", nil) end [ ["width", :integer], @@ -263,7 +263,7 @@ module RSS ["description"], ].each do |name, type| install_text_element(name, type) - install_model(name, "?") + install_model(name, "", "?") end def initialize(*args) @@ -335,7 +335,7 @@ module RSS ["enclosure", '?', :have_child], ].each do |tag, occurs, type, *args| __send__("install_#{type}_element", tag, *args) - install_model(tag, occurs) + install_model(tag, "", occurs) end private @@ -486,7 +486,7 @@ module RSS %w(title description name link).each do |name| install_text_element(name) - install_model(name, nil) + install_model(name, "", nil) end def initialize(*args) diff --git a/lib/rss/1.0.rb b/lib/rss/1.0.rb index 060de8ff30..bd673661e5 100644 --- a/lib/rss/1.0.rb +++ b/lib/rss/1.0.rb @@ -41,7 +41,7 @@ module RSS ["item", "+"], ["textinput", "?"], ].each do |tag, occurs| - install_model(tag, occurs) + install_model(tag, ::RSS::URI, occurs) if occurs == "+" install_have_children_element(tag) else @@ -121,7 +121,7 @@ module RSS @tag_name = 'Seq' install_have_children_element("li") - + install_model("li", URI, "*") install_must_call_validator('rdf', ::RSS::RDF::URI) def initialize(*args) @@ -147,10 +147,6 @@ module RSS def children @li end - - def rdf_validate(ignore_unknown_element, tags, uri) - _validate(ignore_unknown_element, tags, uri, [["li", '*']]) - end def _tags rv = [] @@ -176,7 +172,7 @@ module RSS @tag_name = 'Bag' install_have_children_element("li") - + install_model("li", URI, "*") install_must_call_validator('rdf', ::RSS::RDF::URI) def initialize(*args) @@ -202,10 +198,6 @@ module RSS def children @li end - - def rdf_validate(ignore_unknown_element, tags, uri) - _validate(ignore_unknown_element, tags, uri, [["li", '*']]) - end def _tags rv = [] @@ -251,7 +243,7 @@ module RSS ['items', nil], ['textinput', '?'], ].each do |tag, occurs| - install_model(tag, occurs) + install_model(tag, ::RSS::URI, occurs) end def initialize(*args) @@ -362,8 +354,8 @@ module RSS end install_have_child_element("Seq") - - install_must_call_validator('rdf', ::RSS::RDF::URI) + install_model("Seq", URI, nil) + install_must_call_validator('rdf', URI) def initialize(*args) if Utils.element_initialize_arguments?(args) @@ -395,10 +387,6 @@ module RSS rv << [URI, 'Seq'] unless @Seq.nil? rv end - - def rdf_validate(ignore_unknown_element, tags, uri) - _validate(ignore_unknown_element, tags, uri, [["Seq", nil]]) - end end end @@ -430,7 +418,7 @@ module RSS ['url', nil], ['link', nil], ].each do |tag, occurs| - install_model(tag, occurs) + install_model(tag, ::RSS::URI, occurs) end def initialize(*args) @@ -487,7 +475,7 @@ module RSS ["link", nil], ["description", "?"], ].each do |tag, occurs| - install_model(tag, occurs) + install_model(tag, ::RSS::URI, occurs) end def initialize(*args) @@ -548,7 +536,7 @@ module RSS ["name", nil], ["link", nil], ].each do |tag, occurs| - install_model(tag, occurs) + install_model(tag, ::RSS::URI, occurs) end def initialize(*args) diff --git a/lib/rss/2.0.rb b/lib/rss/2.0.rb index 5b3feab300..8d76411df6 100644 --- a/lib/rss/2.0.rb +++ b/lib/rss/2.0.rb @@ -11,21 +11,21 @@ module RSS ["ttl", :integer], ].each do |name, type| install_text_element(name, type) - install_model(name, '?') + install_model(name, "", '?') end [ %w(category categories), ].each do |name, plural_name| install_have_children_element(name, plural_name) - install_model(name, '*') + install_model(name, "", '*') end [ ["image", "?"], ["language", "?"], ].each do |name, occurs| - install_model(name, occurs) + install_model(name, "", occurs) end private @@ -58,14 +58,14 @@ module RSS ["author", "?"], ].each do |name, occurs| install_text_element(name) - install_model(name, occurs) + install_model(name, "", occurs) end [ ["pubDate", '?'], ].each do |name, occurs| install_date_element(name, 'rfc822') - install_model(name, occurs) + install_model(name, "", occurs) end alias date pubDate alias date= pubDate= @@ -74,7 +74,7 @@ module RSS ["guid", '?'], ].each do |name, occurs| install_have_child_element(name) - install_model(name, occurs) + install_model(name, "", occurs) end private diff --git a/lib/rss/content.rb b/lib/rss/content.rb index db96074a38..545aecb4df 100644 --- a/lib/rss/content.rb +++ b/lib/rss/content.rb @@ -15,30 +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(ignore_unknown_element, tags, uri) - counter = {} - ELEMENTS.each do |name| - counter[name] = 0 - end - tags.each do |tag| - key = "#{CONTENT_PREFIX}_#{tag}" - if !ignore_unknown_element and !counter.has_key?(key) - raise UnknownTagError.new(tag, CONTENT_URI) - end - counter[key] += 1 - raise TooMuchTagError.new(tag, tag_name) if counter[key] > 1 + klass.install_must_call_validator(CONTENT_PREFIX, CONTENT_URI) + %w(encoded).each do |name| + klass.install_text_element("#{CONTENT_PREFIX}_#{name}") + klass.install_model(name, CONTENT_URI, "?") end end - end class RDF diff --git a/lib/rss/dublincore.rb b/lib/rss/dublincore.rb index 605b1f7e42..8badc8bb1e 100644 --- a/lib/rss/dublincore.rb +++ b/lib/rss/dublincore.rb @@ -17,10 +17,10 @@ module RSS full_name = "#{DC_PREFIX}_#{name}" full_plural_name = "#{DC_PREFIX}_#{plural}" klass_name = "DublinCore#{Utils.to_class_name(name)}" + klass.install_must_call_validator(DC_PREFIX, DC_URI) + klass.install_model(name, DC_URI, "*") + klass.install_have_children_element(full_name, full_plural_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} @@ -133,17 +133,6 @@ module RSS end EOC end - - def dc_validate(ignore_unknown_element, tags, uri) - tags.each do |tag| - key = "#{DC_PREFIX}_#{tag}" - if !ignore_unknown_element and - !DublinCoreModel::ELEMENTS.include?(key) - raise UnknownTagError.new(tag, DC_URI) - end - end - end - end # For backward compatibility diff --git a/lib/rss/image.rb b/lib/rss/image.rb index 325b751187..624bc6aceb 100644 --- a/lib/rss/image.rb +++ b/lib/rss/image.rb @@ -34,12 +34,10 @@ module RSS super klass.install_have_child_element("#{IMAGE_PREFIX}_item") + klass.install_must_call_validator(IMAGE_PREFIX, IMAGE_URI) + klass.install_model("item", IMAGE_URI, "?") end - def image_validate(ignore_unknown_element, tags, uri) - validate_one_tag_name(ignore_unknown_element, "item", tags) - end - class ImageItem < Element include RSS10 include DublinCoreModel @@ -56,6 +54,8 @@ module RSS end end + install_must_call_validator(IMAGE_PREFIX, IMAGE_URI) + [ ["about", ::RSS::RDF::URI, true], ["resource", ::RSS::RDF::URI, false], @@ -120,13 +120,11 @@ module RSS unless klass.class == Module klass.install_have_child_element("#{IMAGE_PREFIX}_favicon") + klass.install_must_call_validator(IMAGE_PREFIX, IMAGE_URI) + klass.install_model("favicon", IMAGE_URI, "?") end end - def image_validate(ignore_unknown_element, tags, uri) - validate_one_tag_name(ignore_unknown_element, "favicon", tags) - end - class ImageFavicon < Element include RSS10 include DublinCoreModel diff --git a/lib/rss/rss.rb b/lib/rss/rss.rb index 3dc9999e6f..a12a78bb0e 100644 --- a/lib/rss/rss.rb +++ b/lib/rss/rss.rb @@ -391,7 +391,7 @@ EOC INDENT = " " MUST_CALL_VALIDATORS = {} - MODEL = [] + MODELS = [] GET_ATTRIBUTES = [] HAVE_CHILDREN_ELEMENTS = [] TO_ELEMENT_METHODS = [] @@ -403,8 +403,8 @@ EOC def must_call_validators MUST_CALL_VALIDATORS end - def model - MODEL + def models + MODELS end def get_attributes GET_ATTRIBUTES @@ -425,7 +425,7 @@ EOC def inherited(klass) klass.const_set("MUST_CALL_VALIDATORS", {}) - klass.const_set("MODEL", []) + klass.const_set("MODELS", []) klass.const_set("GET_ATTRIBUTES", []) klass.const_set("HAVE_CHILDREN_ELEMENTS", []) klass.const_set("TO_ELEMENT_METHODS", []) @@ -442,8 +442,8 @@ EOC def self.must_call_validators super.merge(MUST_CALL_VALIDATORS) end - def self.model - MODEL + super + def self.models + MODELS + super end def self.get_attributes GET_ATTRIBUTES + super @@ -466,11 +466,11 @@ 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 + def self.install_model(tag, uri, occurs=nil) + if m = MODELS.find {|t, u, o| t == tag and u == uri} + m[2] = occurs else - MODEL << [tag, occurs] + MODELS << [tag, uri, occurs] end end @@ -783,16 +783,12 @@ 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" - if respond_to?(meth, true) - __send__(meth, ignore_unknown_element, tags[uri], uri) - end - end - end must_call_validators.each do |uri, prefix| - __send__("#{prefix}_validate", ignore_unknown_element, tags[uri], uri) + _validate(ignore_unknown_element, tags[uri], uri) + meth = "#{prefix}_validate" + if respond_to?(meth, true) + __send__(meth, ignore_unknown_element, tags[uri], uri) + end end end @@ -804,23 +800,25 @@ EOC end end - def _validate(ignore_unknown_element, tags, uri, model=self.class.model) + def _validate(ignore_unknown_element, tags, uri, models=self.class.models) count = 1 do_redo = false not_shift = false tag = nil - element_names = model.collect {|elem| elem[0]} + models = models.find_all {|model| model[1] == uri} + element_names = models.collect {|model| model[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| + models.each_with_index do |model, i| + name, model_uri, occurs = model if DEBUG p "before" p tags - p elem + p model end if not_shift @@ -834,41 +832,41 @@ EOC p count end - case elem[1] + case occurs when '?' if count > 2 - raise TooMuchTagError.new(elem[0], tag_name) + raise TooMuchTagError.new(name, tag_name) else - if elem[0] == tag + if name == tag do_redo = true else not_shift = true end end when '*' - if elem[0] == tag + if name == tag do_redo = true else not_shift = true end when '+' - if elem[0] == tag + if name == tag do_redo = true else if count > 1 not_shift = true else - raise MissingTagError.new(elem[0], tag_name) + raise MissingTagError.new(name, 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) + if name == tag + if models[i+1] and models[i+1][0] != name and + tags and tags.first == name + raise TooMuchTagError.new(name, tag_name) end else - raise MissingTagError.new(elem[0], tag_name) + raise MissingTagError.new(name, tag_name) end end diff --git a/lib/rss/syndication.rb b/lib/rss/syndication.rb index fc93d03832..bb4a250a24 100644 --- a/lib/rss/syndication.rb +++ b/lib/rss/syndication.rb @@ -15,18 +15,21 @@ module RSS def self.append_features(klass) super - - klass.module_eval(<<-EOC, *get_file_and_line_from_caller(1)) + + klass.install_must_call_validator(SY_PREFIX, SY_URI) + klass.module_eval do [ ["updatePeriod"], ["updateFrequency", :positive_integer] ].each do |name, type| - install_text_element("\#{SY_PREFIX}_\#{name}", type, - "\#{SY_PREFIX}:\#{name}") + install_text_element("#{SY_PREFIX}_#{name}", type, + "#{SY_PREFIX}:#{name}") + install_model(name, SY_URI, "?") end %w(updateBase).each do |name| - install_date_element("\#{SY_PREFIX}_\#{name}", 'w3cdtf', name) + install_date_element("#{SY_PREFIX}_#{name}", 'w3cdtf', name) + install_model(name, SY_URI, "?") end alias_method(:_sy_updatePeriod=, :sy_updatePeriod=) @@ -35,22 +38,6 @@ module RSS validate_sy_updatePeriod(new_value) if @do_validate self._sy_updatePeriod = new_value end - EOC - end - - def sy_validate(ignore_unknown_element, tags, uri) - counter = {} - ELEMENTS.each do |name| - counter[name] = 0 - end - - tags.each do |tag| - key = "#{SY_PREFIX}_#{tag}" - if ignore_unknown_element and !counter.has_key?(key) - raise UnknownTagError.new(tag, SY_URI) - end - counter[key] += 1 - raise TooMuchTagError.new(tag, tag_name) if counter[key] > 1 end end diff --git a/lib/rss/taxonomy.rb b/lib/rss/taxonomy.rb index d788976d92..e9bd38742d 100644 --- a/lib/rss/taxonomy.rb +++ b/lib/rss/taxonomy.rb @@ -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(ignore_unknown_element, tags, uri) - found_topics = false - tags.each do |tag| - if tag == "topics" - if found_topics - raise TooMuchTagError.new(tag, tag_name) - else - found_topics = true - end - elsif !ignore_unknown_element - raise UnknownTagError.new(tag, TAXO_URI) - end + klass.install_must_call_validator(TAXO_PREFIX, TAXO_URI) + %w(topics).each do |name| + klass.install_have_child_element("#{TAXO_PREFIX}_#{name}") + klass.install_model(name, TAXO_URI, "?") end end @@ -65,8 +53,8 @@ module RSS @tag_name = "topics" install_have_child_element("Bag") - - install_must_call_validator('rdf', ::RSS::RDF::URI) + install_model("Bag", RDF::URI, nil) + install_must_call_validator('rdf', RDF::URI) def initialize(*args) if Utils.element_initialize_arguments?(args) @@ -103,13 +91,9 @@ module RSS def _tags rv = [] - rv << [::RSS::RDF::URI, 'Bag'] unless @Bag.nil? + rv << [RDF::URI, 'Bag'] unless @Bag.nil? rv end - - def rdf_validate(ignore_unknown_element, tags, uri) - _validate(ignore_unknown_element, tags, uri, [["Bag", nil]]) - end end end @@ -122,14 +106,6 @@ module RSS klass.install_have_children_element(var_name) end - def taxo_validate(ignore_unknown_element, tags, uri) - tags.each do |tag| - if !ignore_unknown_element and tag != "topic" - raise UnknownTagError.new(tag, TAXO_URI) - end - end - end - class TaxonomyTopic < Element include RSS10 @@ -165,21 +141,6 @@ module RSS tag_name_with_prefix(TAXO_PREFIX) end - def taxo_validate(ignore_unknown_element, tags, uri) - 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 - elsif !ignore_unknown_element - raise UnknownTagError.new(tag, TAXO_URI) - end - end - end - def maker_target(target) target.new_taxo_topic end diff --git a/lib/rss/trackback.rb b/lib/rss/trackback.rb index e7983b8a6f..d77238934c 100644 --- a/lib/rss/trackback.rb +++ b/lib/rss/trackback.rb @@ -12,29 +12,14 @@ module RSS module TrackBackUtils private def trackback_validate(ignore_unknown_element, tags, uri) - counter = {} - %w(ping about).each do |name| - counter["#{TRACKBACK_PREFIX}_#{name}"] = 0 - end - - tags.each do |tag| - key = "#{TRACKBACK_PREFIX}_#{tag}" - if !ignore_unknown_element and !counter.has_key?(key) - raise UnknownTagError.new(tag, TRACKBACK_URI) - end - 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? + return if tags.nil? + if tags.find {|tag| tag == "about"} and + !tags.find {|tag| tag == "ping"} raise MissingTagError.new("#{TRACKBACK_PREFIX}:ping", tag_name) end end end - + module BaseTrackBackModel ELEMENTS = %w(ping about) @@ -45,10 +30,12 @@ module RSS unless klass.class == Module klass.module_eval {include TrackBackUtils} + klass.install_must_call_validator(TRACKBACK_PREFIX, TRACKBACK_URI) %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.install_model(name, TRACKBACK_URI, "?") klass.module_eval(<<-EOC, __FILE__, __LINE__) remove_method :#{var_name} def #{var_name} @@ -66,6 +53,7 @@ module RSS var_name = "#{TRACKBACK_PREFIX}_#{name}" klass_name = "TrackBack#{Utils.to_class_name(name)}" klass.install_have_children_element(var_name) + klass.install_model(name, TRACKBACK_URI, "*") klass.module_eval(<<-EOC, __FILE__, __LINE__) remove_method :#{var_name} def #{var_name}(*args) |