2004-01-27 22:46:13 -05:00
|
|
|
require "time"
|
|
|
|
|
2004-03-21 04:54:07 -05:00
|
|
|
class Time
|
2004-10-16 00:51:15 -04:00
|
|
|
class << self
|
|
|
|
unless respond_to?(:w3cdtf)
|
|
|
|
def w3cdtf(date)
|
|
|
|
if /\A\s*
|
|
|
|
(-?\d+)-(\d\d)-(\d\d)
|
|
|
|
(?:T
|
|
|
|
(\d\d):(\d\d)(?::(\d\d))?
|
|
|
|
(\.\d+)?
|
|
|
|
(Z|[+-]\d\d:\d\d)?)?
|
|
|
|
\s*\z/ix =~ date and (($5 and $8) or (!$5 and !$8))
|
2007-03-17 06:13:25 -04:00
|
|
|
datetime = [$1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, $6.to_i]
|
|
|
|
usec = 0
|
|
|
|
usec = $7.to_f * 1000000 if $7
|
|
|
|
zone = $8
|
|
|
|
if zone
|
|
|
|
off = zone_offset(zone, datetime[0])
|
|
|
|
datetime = apply_offset(*(datetime + [off]))
|
|
|
|
datetime << usec
|
|
|
|
time = Time.utc(*datetime)
|
|
|
|
time.localtime unless zone_utc?(zone)
|
|
|
|
time
|
2004-10-16 00:51:15 -04:00
|
|
|
else
|
2007-03-17 06:13:25 -04:00
|
|
|
datetime << usec
|
2004-10-16 00:51:15 -04:00
|
|
|
Time.local(*datetime)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
raise ArgumentError.new("invalid date: #{date.inspect}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-02-06 06:38:13 -05:00
|
|
|
unless method_defined?(:w3cdtf)
|
2007-03-17 06:13:25 -04:00
|
|
|
def w3cdtf
|
|
|
|
if usec.zero?
|
|
|
|
fraction_digits = 0
|
|
|
|
else
|
|
|
|
fraction_digits = Math.log10(usec.to_s.sub(/0*$/, '').to_i).floor + 1
|
|
|
|
end
|
|
|
|
xmlschema(fraction_digits)
|
|
|
|
end
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
2004-03-21 04:54:07 -05:00
|
|
|
end
|
|
|
|
|
2005-11-24 22:54:29 -05:00
|
|
|
|
2004-03-21 04:20:47 -05:00
|
|
|
require "English"
|
2004-01-27 22:46:13 -05:00
|
|
|
require "rss/utils"
|
|
|
|
require "rss/converter"
|
2004-03-21 04:20:47 -05:00
|
|
|
require "rss/xml-stylesheet"
|
2004-01-27 22:46:13 -05:00
|
|
|
|
|
|
|
module RSS
|
|
|
|
|
2007-11-02 07:49:20 -04:00
|
|
|
VERSION = "0.2.1"
|
2004-10-16 00:51:15 -04:00
|
|
|
|
|
|
|
URI = "http://purl.org/rss/1.0/"
|
|
|
|
|
|
|
|
DEBUG = false
|
|
|
|
|
|
|
|
class Error < StandardError; end
|
|
|
|
|
|
|
|
class OverlappedPrefixError < Error
|
|
|
|
attr_reader :prefix
|
|
|
|
def initialize(prefix)
|
|
|
|
@prefix = prefix
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class InvalidRSSError < Error; end
|
|
|
|
|
|
|
|
class MissingTagError < InvalidRSSError
|
|
|
|
attr_reader :tag, :parent
|
|
|
|
def initialize(tag, parent)
|
|
|
|
@tag, @parent = tag, parent
|
|
|
|
super("tag <#{tag}> is missing in tag <#{parent}>")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class TooMuchTagError < InvalidRSSError
|
|
|
|
attr_reader :tag, :parent
|
|
|
|
def initialize(tag, parent)
|
|
|
|
@tag, @parent = tag, parent
|
|
|
|
super("tag <#{tag}> is too much in tag <#{parent}>")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class MissingAttributeError < InvalidRSSError
|
|
|
|
attr_reader :tag, :attribute
|
|
|
|
def initialize(tag, attribute)
|
|
|
|
@tag, @attribute = tag, attribute
|
|
|
|
super("attribute <#{attribute}> is missing in tag <#{tag}>")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class UnknownTagError < InvalidRSSError
|
|
|
|
attr_reader :tag, :uri
|
|
|
|
def initialize(tag, uri)
|
|
|
|
@tag, @uri = tag, uri
|
|
|
|
super("tag <#{tag}> is unknown in namespace specified by uri <#{uri}>")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-03-17 06:13:25 -04:00
|
|
|
class NotExpectedTagError < InvalidRSSError
|
|
|
|
attr_reader :tag, :uri, :parent
|
|
|
|
def initialize(tag, uri, parent)
|
|
|
|
@tag, @uri, @parent = tag, uri, parent
|
|
|
|
super("tag <{#{uri}}#{tag}> is not expected in tag <#{parent}>")
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
|
|
|
end
|
2007-03-17 06:13:25 -04:00
|
|
|
# For backward compatibility :X
|
|
|
|
NotExceptedTagError = NotExpectedTagError
|
2004-10-16 00:51:15 -04:00
|
|
|
|
|
|
|
class NotAvailableValueError < InvalidRSSError
|
2005-11-24 22:43:48 -05:00
|
|
|
attr_reader :tag, :value, :attribute
|
|
|
|
def initialize(tag, value, attribute=nil)
|
|
|
|
@tag, @value, @attribute = tag, value, attribute
|
|
|
|
message = "value <#{value}> of "
|
|
|
|
message << "attribute <#{attribute}> of " if attribute
|
|
|
|
message << "tag <#{tag}> is not available."
|
|
|
|
super(message)
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class UnknownConversionMethodError < Error
|
|
|
|
attr_reader :to, :from
|
|
|
|
def initialize(to, from)
|
|
|
|
@to = to
|
|
|
|
@from = from
|
|
|
|
super("can't convert to #{to} from #{from}.")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
# for backward compatibility
|
|
|
|
UnknownConvertMethod = UnknownConversionMethodError
|
|
|
|
|
|
|
|
class ConversionError < Error
|
|
|
|
attr_reader :string, :to, :from
|
|
|
|
def initialize(string, to, from)
|
|
|
|
@string = string
|
|
|
|
@to = to
|
|
|
|
@from = from
|
|
|
|
super("can't convert #{@string} to #{to} from #{from}.")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-11-28 01:53:41 -05:00
|
|
|
class NotSetError < Error
|
|
|
|
attr_reader :name, :variables
|
|
|
|
def initialize(name, variables)
|
|
|
|
@name = name
|
|
|
|
@variables = variables
|
|
|
|
super("required variables of #{@name} are not set: #{@variables.join(', ')}")
|
|
|
|
end
|
|
|
|
end
|
2007-03-17 06:13:25 -04:00
|
|
|
|
|
|
|
class UnsupportedMakerVersionError < Error
|
|
|
|
attr_reader :version
|
|
|
|
def initialize(version)
|
|
|
|
@version = version
|
|
|
|
super("Maker doesn't support version: #{@version}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-10-16 00:51:15 -04:00
|
|
|
module BaseModel
|
|
|
|
include Utils
|
|
|
|
|
2007-03-17 06:13:25 -04:00
|
|
|
def install_have_child_element(tag_name, uri, occurs, name=nil, type=nil)
|
|
|
|
name ||= tag_name
|
2004-10-16 00:51:15 -04:00
|
|
|
add_need_initialize_variable(name)
|
2007-03-17 06:13:25 -04:00
|
|
|
install_model(tag_name, uri, occurs, name)
|
2004-10-16 00:51:15 -04:00
|
|
|
|
2007-03-17 06:13:25 -04:00
|
|
|
writer_type, reader_type = type
|
|
|
|
def_corresponded_attr_writer name, writer_type
|
|
|
|
def_corresponded_attr_reader name, reader_type
|
2004-10-16 00:51:15 -04:00
|
|
|
install_element(name) do |n, elem_name|
|
|
|
|
<<-EOC
|
|
|
|
if @#{n}
|
2005-02-02 08:00:31 -05:00
|
|
|
"\#{@#{n}.to_s(need_convert, indent)}"
|
2004-10-16 00:51:15 -04:00
|
|
|
else
|
|
|
|
''
|
|
|
|
end
|
2004-01-27 22:46:13 -05:00
|
|
|
EOC
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
alias_method(:install_have_attribute_element, :install_have_child_element)
|
|
|
|
|
2007-03-17 06:13:25 -04:00
|
|
|
def install_have_children_element(tag_name, uri, occurs, name=nil, plural_name=nil)
|
|
|
|
name ||= tag_name
|
2004-11-27 03:47:28 -05:00
|
|
|
plural_name ||= "#{name}s"
|
|
|
|
add_have_children_element(name, plural_name)
|
2004-12-04 04:53:01 -05:00
|
|
|
add_plural_form(name, plural_name)
|
2007-08-04 23:03:05 -04:00
|
|
|
install_model(tag_name, uri, occurs, plural_name, true)
|
2007-03-17 06:13:25 -04:00
|
|
|
|
2004-11-19 03:25:25 -05:00
|
|
|
def_children_accessor(name, plural_name)
|
|
|
|
install_element(name, "s") do |n, elem_name|
|
2004-10-16 00:51:15 -04:00
|
|
|
<<-EOC
|
2004-11-19 03:25:25 -05:00
|
|
|
rv = []
|
2004-10-16 00:51:15 -04:00
|
|
|
@#{n}.each do |x|
|
2005-02-02 08:00:31 -05:00
|
|
|
value = "\#{x.to_s(need_convert, indent)}"
|
2004-12-04 04:55:29 -05:00
|
|
|
rv << value if /\\A\\s*\\z/ !~ value
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
2004-11-19 03:25:25 -05:00
|
|
|
rv.join("\n")
|
2004-01-27 22:46:13 -05:00
|
|
|
EOC
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-08-04 23:03:05 -04:00
|
|
|
def install_text_element(tag_name, uri, occurs, name=nil, type=nil,
|
|
|
|
disp_name=nil)
|
2007-03-17 06:13:25 -04:00
|
|
|
name ||= tag_name
|
|
|
|
disp_name ||= name
|
2007-11-02 07:58:03 -04:00
|
|
|
self::ELEMENTS << name unless self::ELEMENTS.include?(name)
|
2004-10-16 00:51:15 -04:00
|
|
|
add_need_initialize_variable(name)
|
2007-03-17 06:13:25 -04:00
|
|
|
install_model(tag_name, uri, occurs, name)
|
2004-10-16 00:51:15 -04:00
|
|
|
|
2007-08-04 23:03:05 -04:00
|
|
|
def_corresponded_attr_writer(name, type, disp_name)
|
|
|
|
def_corresponded_attr_reader(name, type || :convert)
|
2004-10-16 00:51:15 -04:00
|
|
|
install_element(name) do |n, elem_name|
|
|
|
|
<<-EOC
|
2007-08-04 23:03:05 -04:00
|
|
|
if respond_to?(:#{n}_content)
|
|
|
|
content = #{n}_content
|
|
|
|
else
|
|
|
|
content = @#{n}
|
|
|
|
end
|
|
|
|
if content
|
2004-10-16 00:51:15 -04:00
|
|
|
rv = "\#{indent}<#{elem_name}>"
|
2007-08-04 23:03:05 -04:00
|
|
|
value = html_escape(content)
|
2005-02-02 08:00:31 -05:00
|
|
|
if need_convert
|
|
|
|
rv << convert(value)
|
2004-10-16 00:51:15 -04:00
|
|
|
else
|
|
|
|
rv << value
|
|
|
|
end
|
|
|
|
rv << "</#{elem_name}>"
|
|
|
|
rv
|
|
|
|
else
|
|
|
|
''
|
|
|
|
end
|
2004-01-27 22:46:13 -05:00
|
|
|
EOC
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-03-17 06:13:25 -04:00
|
|
|
def install_date_element(tag_name, uri, occurs, name=nil, type=nil, disp_name=nil)
|
|
|
|
name ||= tag_name
|
|
|
|
type ||= :w3cdtf
|
|
|
|
disp_name ||= name
|
2004-10-16 00:51:15 -04:00
|
|
|
self::ELEMENTS << name
|
|
|
|
add_need_initialize_variable(name)
|
2007-03-17 06:13:25 -04:00
|
|
|
install_model(tag_name, uri, occurs, name)
|
2004-10-16 00:51:15 -04:00
|
|
|
|
|
|
|
# accessor
|
|
|
|
convert_attr_reader name
|
2005-04-05 02:42:03 -04:00
|
|
|
date_writer(name, type, disp_name)
|
2004-10-16 00:51:15 -04:00
|
|
|
|
|
|
|
install_element(name) do |n, elem_name|
|
|
|
|
<<-EOC
|
|
|
|
if @#{n}
|
|
|
|
rv = "\#{indent}<#{elem_name}>"
|
|
|
|
value = html_escape(@#{n}.#{type})
|
2005-02-02 08:00:31 -05:00
|
|
|
if need_convert
|
|
|
|
rv << convert(value)
|
2004-10-16 00:51:15 -04:00
|
|
|
else
|
|
|
|
rv << value
|
|
|
|
end
|
|
|
|
rv << "</#{elem_name}>"
|
|
|
|
rv
|
|
|
|
else
|
|
|
|
''
|
|
|
|
end
|
2004-01-27 22:46:13 -05:00
|
|
|
EOC
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def install_element(name, postfix="")
|
|
|
|
elem_name = name.sub('_', ':')
|
2007-03-17 06:13:25 -04:00
|
|
|
method_name = "#{name}_element#{postfix}"
|
|
|
|
add_to_element_method(method_name)
|
2004-10-16 00:51:15 -04:00
|
|
|
module_eval(<<-EOC, *get_file_and_line_from_caller(2))
|
2007-03-17 06:13:25 -04:00
|
|
|
def #{method_name}(need_convert=true, indent='')
|
2004-10-16 00:51:15 -04:00
|
|
|
#{yield(name, elem_name)}
|
|
|
|
end
|
2007-03-17 06:13:25 -04:00
|
|
|
private :#{method_name}
|
2004-01-27 22:46:13 -05:00
|
|
|
EOC
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
|
|
|
|
2007-03-17 06:13:25 -04:00
|
|
|
def inherit_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}_without_inherit
|
|
|
|
convert(@#{attr})
|
|
|
|
end
|
|
|
|
|
|
|
|
def #{attr}
|
|
|
|
if @#{attr}
|
|
|
|
#{attr}_without_inherit
|
|
|
|
elsif @parent
|
|
|
|
@parent.#{attr}
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
EOC
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def uri_convert_attr_reader(*attrs)
|
2004-10-16 00:51:15 -04:00
|
|
|
attrs.each do |attr|
|
|
|
|
attr = attr.id2name if attr.kind_of?(Integer)
|
|
|
|
module_eval(<<-EOC, *get_file_and_line_from_caller(2))
|
2007-03-17 06:13:25 -04:00
|
|
|
def #{attr}_without_base
|
|
|
|
convert(@#{attr})
|
|
|
|
end
|
|
|
|
|
2004-10-16 00:51:15 -04:00
|
|
|
def #{attr}
|
2007-03-17 06:13:25 -04:00
|
|
|
value = #{attr}_without_base
|
|
|
|
return nil if value.nil?
|
|
|
|
if /\\A[a-z][a-z0-9+.\\-]*:/i =~ value
|
|
|
|
value
|
2004-10-16 00:51:15 -04:00
|
|
|
else
|
2007-03-17 06:13:25 -04:00
|
|
|
"\#{base}\#{value}"
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
|
|
|
end
|
2004-01-27 22:46:13 -05:00
|
|
|
EOC
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-03-17 06:13:25 -04:00
|
|
|
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}
|
|
|
|
convert(@#{attr})
|
|
|
|
end
|
|
|
|
EOC
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-08-04 23:03:05 -04:00
|
|
|
def yes_clean_other_attr_reader(*attrs)
|
|
|
|
attrs.each do |attr|
|
|
|
|
attr = attr.id2name if attr.kind_of?(Integer)
|
|
|
|
module_eval(<<-EOC, __FILE__, __LINE__ + 1)
|
|
|
|
attr_reader(:#{attr})
|
|
|
|
def #{attr}?
|
|
|
|
YesCleanOther.parse(@#{attr})
|
|
|
|
end
|
|
|
|
EOC
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def yes_other_attr_reader(*attrs)
|
|
|
|
attrs.each do |attr|
|
|
|
|
attr = attr.id2name if attr.kind_of?(Integer)
|
|
|
|
module_eval(<<-EOC, __FILE__, __LINE__ + 1)
|
|
|
|
attr_reader(:#{attr})
|
|
|
|
def #{attr}?
|
|
|
|
Utils::YesOther.parse(@#{attr})
|
|
|
|
end
|
|
|
|
EOC
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def csv_attr_reader(*attrs)
|
2007-09-15 00:41:28 -04:00
|
|
|
separator = nil
|
|
|
|
if attrs.last.is_a?(Hash)
|
|
|
|
options = attrs.pop
|
|
|
|
separator = options[:separator]
|
|
|
|
end
|
|
|
|
separator ||= ", "
|
2007-08-04 23:03:05 -04:00
|
|
|
attrs.each do |attr|
|
|
|
|
attr = attr.id2name if attr.kind_of?(Integer)
|
|
|
|
module_eval(<<-EOC, __FILE__, __LINE__ + 1)
|
|
|
|
attr_reader(:#{attr})
|
|
|
|
def #{attr}_content
|
|
|
|
if @#{attr}.nil?
|
|
|
|
@#{attr}
|
|
|
|
else
|
2007-09-15 00:41:28 -04:00
|
|
|
@#{attr}.join(#{separator.dump})
|
2007-08-04 23:03:05 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
EOC
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-04-05 02:42:03 -04:00
|
|
|
def date_writer(name, type, disp_name=name)
|
|
|
|
module_eval(<<-EOC, *get_file_and_line_from_caller(2))
|
|
|
|
def #{name}=(new_value)
|
2007-03-17 06:13:25 -04:00
|
|
|
if new_value.nil?
|
2005-04-05 02:42:03 -04:00
|
|
|
@#{name} = new_value
|
2007-03-17 06:13:25 -04:00
|
|
|
elsif new_value.kind_of?(Time)
|
|
|
|
@#{name} = new_value.dup
|
2005-04-05 02:42:03 -04:00
|
|
|
else
|
|
|
|
if @do_validate
|
|
|
|
begin
|
2005-11-23 04:49:02 -05:00
|
|
|
@#{name} = Time.__send__('#{type}', new_value)
|
2005-04-05 02:42:03 -04:00
|
|
|
rescue ArgumentError
|
|
|
|
raise NotAvailableValueError.new('#{disp_name}', new_value)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
@#{name} = nil
|
|
|
|
if /\\A\\s*\\z/ !~ new_value.to_s
|
|
|
|
begin
|
2007-03-17 06:13:25 -04:00
|
|
|
unless Date._parse(new_value, false).empty?
|
|
|
|
@#{name} = Time.parse(new_value)
|
|
|
|
end
|
2005-04-05 02:42:03 -04:00
|
|
|
rescue ArgumentError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Is it need?
|
|
|
|
if @#{name}
|
|
|
|
class << @#{name}
|
|
|
|
undef_method(:to_s)
|
|
|
|
alias_method(:to_s, :#{type})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
EOC
|
|
|
|
end
|
|
|
|
|
2005-11-24 22:43:48 -05:00
|
|
|
def integer_writer(name, disp_name=name)
|
|
|
|
module_eval(<<-EOC, *get_file_and_line_from_caller(2))
|
|
|
|
def #{name}=(new_value)
|
|
|
|
if new_value.nil?
|
|
|
|
@#{name} = new_value
|
|
|
|
else
|
|
|
|
if @do_validate
|
|
|
|
begin
|
|
|
|
@#{name} = Integer(new_value)
|
|
|
|
rescue ArgumentError
|
|
|
|
raise NotAvailableValueError.new('#{disp_name}', new_value)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
@#{name} = new_value.to_i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
EOC
|
|
|
|
end
|
|
|
|
|
|
|
|
def positive_integer_writer(name, disp_name=name)
|
|
|
|
module_eval(<<-EOC, *get_file_and_line_from_caller(2))
|
|
|
|
def #{name}=(new_value)
|
|
|
|
if new_value.nil?
|
|
|
|
@#{name} = new_value
|
|
|
|
else
|
|
|
|
if @do_validate
|
|
|
|
begin
|
|
|
|
tmp = Integer(new_value)
|
|
|
|
raise ArgumentError if tmp <= 0
|
|
|
|
@#{name} = tmp
|
|
|
|
rescue ArgumentError
|
|
|
|
raise NotAvailableValueError.new('#{disp_name}', new_value)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
@#{name} = new_value.to_i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
EOC
|
|
|
|
end
|
|
|
|
|
|
|
|
def boolean_writer(name, disp_name=name)
|
|
|
|
module_eval(<<-EOC, *get_file_and_line_from_caller(2))
|
|
|
|
def #{name}=(new_value)
|
|
|
|
if new_value.nil?
|
|
|
|
@#{name} = new_value
|
|
|
|
else
|
|
|
|
if @do_validate and
|
|
|
|
![true, false, "true", "false"].include?(new_value)
|
|
|
|
raise NotAvailableValueError.new('#{disp_name}', new_value)
|
|
|
|
end
|
|
|
|
if [true, false].include?(new_value)
|
|
|
|
@#{name} = new_value
|
|
|
|
else
|
|
|
|
@#{name} = new_value == "true"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
EOC
|
|
|
|
end
|
|
|
|
|
2007-03-17 06:13:25 -04:00
|
|
|
def text_type_writer(name, disp_name=name)
|
|
|
|
module_eval(<<-EOC, *get_file_and_line_from_caller(2))
|
|
|
|
def #{name}=(new_value)
|
|
|
|
if @do_validate and
|
|
|
|
!["text", "html", "xhtml", nil].include?(new_value)
|
|
|
|
raise NotAvailableValueError.new('#{disp_name}', new_value)
|
|
|
|
end
|
|
|
|
@#{name} = new_value
|
|
|
|
end
|
|
|
|
EOC
|
|
|
|
end
|
|
|
|
|
|
|
|
def content_writer(name, disp_name=name)
|
|
|
|
klass_name = "self.class::#{Utils.to_class_name(name)}"
|
|
|
|
module_eval(<<-EOC, *get_file_and_line_from_caller(2))
|
|
|
|
def #{name}=(new_value)
|
|
|
|
if new_value.is_a?(#{klass_name})
|
|
|
|
@#{name} = new_value
|
|
|
|
else
|
|
|
|
@#{name} = #{klass_name}.new
|
|
|
|
@#{name}.content = new_value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
EOC
|
|
|
|
end
|
|
|
|
|
2007-08-04 23:03:05 -04:00
|
|
|
def yes_clean_other_writer(name, disp_name=name)
|
|
|
|
module_eval(<<-EOC, __FILE__, __LINE__ + 1)
|
|
|
|
def #{name}=(value)
|
|
|
|
value = (value ? "yes" : "no") if [true, false].include?(value)
|
|
|
|
@#{name} = value
|
|
|
|
end
|
|
|
|
EOC
|
|
|
|
end
|
|
|
|
|
|
|
|
def yes_other_writer(name, disp_name=name)
|
|
|
|
module_eval(<<-EOC, __FILE__, __LINE__ + 1)
|
|
|
|
def #{name}=(new_value)
|
|
|
|
if [true, false].include?(new_value)
|
|
|
|
new_value = new_value ? "yes" : "no"
|
|
|
|
end
|
|
|
|
@#{name} = new_value
|
|
|
|
end
|
|
|
|
EOC
|
|
|
|
end
|
|
|
|
|
|
|
|
def csv_writer(name, disp_name=name)
|
|
|
|
module_eval(<<-EOC, __FILE__, __LINE__ + 1)
|
|
|
|
def #{name}=(new_value)
|
|
|
|
@#{name} = Utils::CSV.parse(new_value)
|
|
|
|
end
|
|
|
|
EOC
|
|
|
|
end
|
|
|
|
|
2007-09-15 00:41:28 -04:00
|
|
|
def csv_integer_writer(name, disp_name=name)
|
|
|
|
module_eval(<<-EOC, __FILE__, __LINE__ + 1)
|
|
|
|
def #{name}=(new_value)
|
|
|
|
@#{name} = Utils::CSV.parse(new_value) {|v| Integer(v)}
|
|
|
|
end
|
|
|
|
EOC
|
|
|
|
end
|
|
|
|
|
2004-11-27 03:47:28 -05:00
|
|
|
def def_children_accessor(accessor_name, plural_name)
|
2004-10-16 00:51:15 -04:00
|
|
|
module_eval(<<-EOC, *get_file_and_line_from_caller(2))
|
2004-11-19 03:25:25 -05:00
|
|
|
def #{plural_name}
|
2004-10-16 00:51:15 -04:00
|
|
|
@#{accessor_name}
|
|
|
|
end
|
|
|
|
|
|
|
|
def #{accessor_name}(*args)
|
|
|
|
if args.empty?
|
|
|
|
@#{accessor_name}.first
|
|
|
|
else
|
2005-11-23 04:49:02 -05:00
|
|
|
@#{accessor_name}[*args]
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
|
|
|
end
|
2004-12-04 04:53:01 -05:00
|
|
|
|
2004-10-16 00:51:15 -04:00
|
|
|
def #{accessor_name}=(*args)
|
2007-03-17 06:13:25 -04:00
|
|
|
receiver = self.class.name
|
2004-12-04 04:53:01 -05:00
|
|
|
warn("Warning:\#{caller.first.sub(/:in `.*'\z/, '')}: " \
|
2007-03-17 06:13:25 -04:00
|
|
|
"Don't use `\#{receiver}\##{accessor_name} = XXX'/" \
|
|
|
|
"`\#{receiver}\#set_#{accessor_name}(XXX)'. " \
|
2004-12-04 04:53:01 -05:00
|
|
|
"Those APIs are not sense of Ruby. " \
|
2007-03-17 06:13:25 -04:00
|
|
|
"Use `\#{receiver}\##{plural_name} << XXX' instead of them.")
|
2004-10-16 00:51:15 -04:00
|
|
|
if args.size == 1
|
|
|
|
@#{accessor_name}.push(args[0])
|
|
|
|
else
|
2005-11-23 04:49:02 -05:00
|
|
|
@#{accessor_name}.__send__("[]=", *args)
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
alias_method(:set_#{accessor_name}, :#{accessor_name}=)
|
2004-01-27 22:46:13 -05:00
|
|
|
EOC
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
2007-03-17 06:13:25 -04:00
|
|
|
end
|
2004-01-27 22:46:13 -05:00
|
|
|
|
2007-03-17 06:13:25 -04:00
|
|
|
module SetupMaker
|
|
|
|
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 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)
|
2007-08-04 23:03:05 -04:00
|
|
|
next if value.nil?
|
2007-03-17 06:13:25 -04:00
|
|
|
if value.respond_to?("setup_maker") and
|
|
|
|
!not_need_to_call_setup_maker_variables.include?(var)
|
|
|
|
value.setup_maker(target)
|
2004-11-19 03:25:25 -05:00
|
|
|
else
|
2007-03-17 06:13:25 -04:00
|
|
|
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
|
2004-11-19 03:25:25 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
2004-01-27 22:46:13 -05:00
|
|
|
|
2004-10-16 00:51:15 -04:00
|
|
|
class Element
|
|
|
|
extend BaseModel
|
|
|
|
include Utils
|
2007-08-04 23:03:05 -04:00
|
|
|
extend Utils::InheritedReader
|
2007-03-17 06:13:25 -04:00
|
|
|
include SetupMaker
|
2004-01-27 22:46:13 -05:00
|
|
|
|
2004-10-16 00:51:15 -04:00
|
|
|
INDENT = " "
|
2004-10-16 00:39:58 -04:00
|
|
|
|
2005-01-14 23:46:20 -05:00
|
|
|
MUST_CALL_VALIDATORS = {}
|
2007-03-17 06:13:25 -04:00
|
|
|
MODELS = []
|
2005-01-14 23:46:20 -05:00
|
|
|
GET_ATTRIBUTES = []
|
|
|
|
HAVE_CHILDREN_ELEMENTS = []
|
2007-03-17 06:13:25 -04:00
|
|
|
TO_ELEMENT_METHODS = []
|
2005-01-14 23:46:20 -05:00
|
|
|
NEED_INITIALIZE_VARIABLES = []
|
|
|
|
PLURAL_FORMS = {}
|
2004-10-16 00:51:15 -04:00
|
|
|
|
2007-08-04 23:03:05 -04:00
|
|
|
class << self
|
2005-01-14 23:46:20 -05:00
|
|
|
def must_call_validators
|
2007-08-04 23:03:05 -04:00
|
|
|
inherited_hash_reader("MUST_CALL_VALIDATORS")
|
2005-01-14 23:46:20 -05:00
|
|
|
end
|
2007-03-17 06:13:25 -04:00
|
|
|
def models
|
2007-08-04 23:03:05 -04:00
|
|
|
inherited_array_reader("MODELS")
|
2005-01-14 23:46:20 -05:00
|
|
|
end
|
|
|
|
def get_attributes
|
2007-08-04 23:03:05 -04:00
|
|
|
inherited_array_reader("GET_ATTRIBUTES")
|
2005-01-14 23:46:20 -05:00
|
|
|
end
|
|
|
|
def have_children_elements
|
2007-08-04 23:03:05 -04:00
|
|
|
inherited_array_reader("HAVE_CHILDREN_ELEMENTS")
|
2005-01-14 23:46:20 -05:00
|
|
|
end
|
2007-03-17 06:13:25 -04:00
|
|
|
def to_element_methods
|
2007-08-04 23:03:05 -04:00
|
|
|
inherited_array_reader("TO_ELEMENT_METHODS")
|
2007-03-17 06:13:25 -04:00
|
|
|
end
|
2005-01-14 23:46:20 -05:00
|
|
|
def need_initialize_variables
|
2007-08-04 23:03:05 -04:00
|
|
|
inherited_array_reader("NEED_INITIALIZE_VARIABLES")
|
2005-01-14 23:46:20 -05:00
|
|
|
end
|
|
|
|
def plural_forms
|
2007-08-04 23:03:05 -04:00
|
|
|
inherited_hash_reader("PLURAL_FORMS")
|
|
|
|
end
|
|
|
|
|
|
|
|
def inherited_base
|
|
|
|
::RSS::Element
|
2005-01-14 23:46:20 -05:00
|
|
|
end
|
|
|
|
|
2004-10-16 00:51:15 -04:00
|
|
|
def inherited(klass)
|
2005-01-14 23:46:20 -05:00
|
|
|
klass.const_set("MUST_CALL_VALIDATORS", {})
|
2007-03-17 06:13:25 -04:00
|
|
|
klass.const_set("MODELS", [])
|
2005-01-14 23:46:20 -05:00
|
|
|
klass.const_set("GET_ATTRIBUTES", [])
|
|
|
|
klass.const_set("HAVE_CHILDREN_ELEMENTS", [])
|
2007-03-17 06:13:25 -04:00
|
|
|
klass.const_set("TO_ELEMENT_METHODS", [])
|
2005-01-14 23:46:20 -05:00
|
|
|
klass.const_set("NEED_INITIALIZE_VARIABLES", [])
|
|
|
|
klass.const_set("PLURAL_FORMS", {})
|
|
|
|
|
2007-08-04 23:03:05 -04:00
|
|
|
tag_name = klass.name.split(/::/).last
|
|
|
|
tag_name[0, 1] = tag_name[0, 1].downcase
|
|
|
|
klass.instance_variable_set("@tag_name", tag_name)
|
|
|
|
klass.instance_variable_set("@have_content", false)
|
|
|
|
end
|
2004-10-16 00:51:15 -04:00
|
|
|
|
2007-08-04 23:03:05 -04:00
|
|
|
def install_must_call_validator(prefix, uri)
|
|
|
|
self::MUST_CALL_VALIDATORS[uri] = prefix
|
|
|
|
end
|
2004-10-16 00:51:15 -04:00
|
|
|
|
2007-08-04 23:03:05 -04:00
|
|
|
def install_model(tag, uri, occurs=nil, getter=nil, plural=false)
|
|
|
|
getter ||= tag
|
|
|
|
if m = self::MODELS.find {|t, u, o, g, p| t == tag and u == uri}
|
|
|
|
m[2] = occurs
|
|
|
|
else
|
|
|
|
self::MODELS << [tag, uri, occurs, getter, plural]
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
2007-08-04 23:03:05 -04:00
|
|
|
end
|
2004-10-16 00:51:15 -04:00
|
|
|
|
2007-08-04 23:03:05 -04:00
|
|
|
def install_get_attribute(name, uri, required=true,
|
|
|
|
type=nil, disp_name=nil,
|
|
|
|
element_name=nil)
|
|
|
|
disp_name ||= name
|
|
|
|
element_name ||= name
|
|
|
|
writer_type, reader_type = type
|
|
|
|
def_corresponded_attr_writer name, writer_type, disp_name
|
|
|
|
def_corresponded_attr_reader name, reader_type
|
|
|
|
if type == :boolean and /^is/ =~ name
|
|
|
|
alias_method "#{$POSTMATCH}?", name
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
2007-08-04 23:03:05 -04:00
|
|
|
self::GET_ATTRIBUTES << [name, uri, required, element_name]
|
|
|
|
add_need_initialize_variable(disp_name)
|
|
|
|
end
|
2004-10-16 00:51:15 -04:00
|
|
|
|
2007-08-04 23:03:05 -04:00
|
|
|
def def_corresponded_attr_writer(name, type=nil, disp_name=nil)
|
|
|
|
disp_name ||= name
|
|
|
|
case type
|
|
|
|
when :integer
|
|
|
|
integer_writer name, disp_name
|
|
|
|
when :positive_integer
|
|
|
|
positive_integer_writer name, disp_name
|
|
|
|
when :boolean
|
|
|
|
boolean_writer name, disp_name
|
|
|
|
when :w3cdtf, :rfc822, :rfc2822
|
|
|
|
date_writer name, type, disp_name
|
|
|
|
when :text_type
|
|
|
|
text_type_writer name, disp_name
|
|
|
|
when :content
|
|
|
|
content_writer name, disp_name
|
|
|
|
when :yes_clean_other
|
|
|
|
yes_clean_other_writer name, disp_name
|
|
|
|
when :yes_other
|
|
|
|
yes_other_writer name, disp_name
|
|
|
|
when :csv
|
|
|
|
csv_writer name
|
2007-09-15 00:41:28 -04:00
|
|
|
when :csv_integer
|
|
|
|
csv_integer_writer name
|
2007-08-04 23:03:05 -04:00
|
|
|
else
|
|
|
|
attr_writer name
|
2005-11-24 22:43:48 -05:00
|
|
|
end
|
2007-08-04 23:03:05 -04:00
|
|
|
end
|
2005-11-24 22:43:48 -05:00
|
|
|
|
2007-08-04 23:03:05 -04:00
|
|
|
def def_corresponded_attr_reader(name, type=nil)
|
|
|
|
case type
|
|
|
|
when :inherit
|
|
|
|
inherit_convert_attr_reader name
|
|
|
|
when :uri
|
|
|
|
uri_convert_attr_reader name
|
|
|
|
when :yes_clean_other
|
|
|
|
yes_clean_other_attr_reader name
|
|
|
|
when :yes_other
|
|
|
|
yes_other_attr_reader name
|
|
|
|
when :csv
|
|
|
|
csv_attr_reader name
|
2007-09-15 00:41:28 -04:00
|
|
|
when :csv_integer
|
|
|
|
csv_attr_reader name, :separator => ","
|
2007-08-04 23:03:05 -04:00
|
|
|
else
|
|
|
|
convert_attr_reader name
|
2007-03-17 06:13:25 -04:00
|
|
|
end
|
2007-08-04 23:03:05 -04:00
|
|
|
end
|
2007-03-17 06:13:25 -04:00
|
|
|
|
2007-08-04 23:03:05 -04:00
|
|
|
def content_setup(type=nil, disp_name=nil)
|
|
|
|
writer_type, reader_type = type
|
|
|
|
def_corresponded_attr_writer :content, writer_type, disp_name
|
|
|
|
def_corresponded_attr_reader :content, reader_type
|
|
|
|
@have_content = true
|
|
|
|
end
|
2004-10-16 00:51:15 -04:00
|
|
|
|
2007-08-04 23:03:05 -04:00
|
|
|
def have_content?
|
|
|
|
@have_content
|
|
|
|
end
|
2004-10-16 00:51:15 -04:00
|
|
|
|
2007-08-04 23:03:05 -04:00
|
|
|
def add_have_children_element(variable_name, plural_name)
|
|
|
|
self::HAVE_CHILDREN_ELEMENTS << [variable_name, plural_name]
|
|
|
|
end
|
2007-03-17 06:13:25 -04:00
|
|
|
|
2007-08-04 23:03:05 -04:00
|
|
|
def add_to_element_method(method_name)
|
|
|
|
self::TO_ELEMENT_METHODS << method_name
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_need_initialize_variable(variable_name)
|
|
|
|
self::NEED_INITIALIZE_VARIABLES << variable_name
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_plural_form(singular, plural)
|
|
|
|
self::PLURAL_FORMS[singular] = plural
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def required_prefix
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def required_uri
|
2007-03-17 06:13:25 -04:00
|
|
|
""
|
|
|
|
end
|
|
|
|
|
|
|
|
def need_parent?
|
|
|
|
false
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
2007-08-04 23:03:05 -04:00
|
|
|
|
2004-10-16 00:51:15 -04:00
|
|
|
def install_ns(prefix, uri)
|
|
|
|
if self::NSPOOL.has_key?(prefix)
|
|
|
|
raise OverlappedPrefixError.new(prefix)
|
|
|
|
end
|
|
|
|
self::NSPOOL[prefix] = uri
|
|
|
|
end
|
|
|
|
|
|
|
|
def tag_name
|
|
|
|
@tag_name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-03-17 06:13:25 -04:00
|
|
|
attr_accessor :parent, :do_validate
|
2004-10-16 00:51:15 -04:00
|
|
|
|
2007-03-17 06:13:25 -04:00
|
|
|
def initialize(do_validate=true, attrs=nil)
|
|
|
|
@parent = nil
|
2004-10-16 00:51:15 -04:00
|
|
|
@converter = nil
|
2007-03-17 06:13:25 -04:00
|
|
|
if attrs.nil? and (do_validate.is_a?(Hash) or do_validate.is_a?(Array))
|
|
|
|
do_validate, attrs = true, do_validate
|
|
|
|
end
|
2004-10-16 00:51:15 -04:00
|
|
|
@do_validate = do_validate
|
2007-03-17 06:13:25 -04:00
|
|
|
initialize_variables(attrs || {})
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def tag_name
|
|
|
|
self.class.tag_name
|
|
|
|
end
|
|
|
|
|
2004-11-19 03:25:25 -05:00
|
|
|
def full_name
|
|
|
|
tag_name
|
|
|
|
end
|
|
|
|
|
2004-10-16 00:51:15 -04:00
|
|
|
def converter=(converter)
|
|
|
|
@converter = converter
|
2005-04-13 10:48:00 -04:00
|
|
|
targets = children.dup
|
|
|
|
self.class.have_children_elements.each do |variable_name, plural_name|
|
|
|
|
targets.concat(__send__(plural_name))
|
|
|
|
end
|
|
|
|
targets.each do |target|
|
|
|
|
target.converter = converter unless target.nil?
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
|
|
|
end
|
2005-02-02 08:00:31 -05:00
|
|
|
|
|
|
|
def convert(value)
|
|
|
|
if @converter
|
|
|
|
@converter.convert(value)
|
|
|
|
else
|
|
|
|
value
|
|
|
|
end
|
|
|
|
end
|
2007-03-17 06:13:25 -04:00
|
|
|
|
|
|
|
def valid?(ignore_unknown_element=true)
|
|
|
|
validate(ignore_unknown_element)
|
|
|
|
true
|
|
|
|
rescue RSS::Error
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate(ignore_unknown_element=true)
|
|
|
|
do_validate = @do_validate
|
|
|
|
@do_validate = true
|
2004-10-16 00:51:15 -04:00
|
|
|
validate_attribute
|
2007-03-17 06:13:25 -04:00
|
|
|
__validate(ignore_unknown_element)
|
|
|
|
ensure
|
|
|
|
@do_validate = do_validate
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
|
|
|
|
2007-03-17 06:13:25 -04:00
|
|
|
def validate_for_stream(tags, ignore_unknown_element=true)
|
2004-10-16 00:51:15 -04:00
|
|
|
validate_attribute
|
2007-03-17 06:13:25 -04:00
|
|
|
__validate(ignore_unknown_element, tags, false)
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
|
|
|
|
2007-03-17 06:13:25 -04:00
|
|
|
def to_s(need_convert=true, indent='')
|
|
|
|
if self.class.have_content?
|
|
|
|
return "" if !empty_content? and !content_is_set?
|
|
|
|
rv = tag(indent) do |next_indent|
|
|
|
|
if empty_content?
|
|
|
|
""
|
|
|
|
else
|
|
|
|
xmled_content
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
rv = tag(indent) do |next_indent|
|
|
|
|
self.class.to_element_methods.collect do |method_name|
|
|
|
|
__send__(method_name, false, next_indent)
|
|
|
|
end
|
|
|
|
end
|
2004-11-27 03:47:28 -05:00
|
|
|
end
|
2007-03-17 06:13:25 -04:00
|
|
|
rv = convert(rv) if need_convert
|
|
|
|
rv
|
2004-11-27 03:47:28 -05:00
|
|
|
end
|
2007-03-17 06:13:25 -04:00
|
|
|
|
|
|
|
def have_xml_content?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def need_base64_encode?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2007-08-04 23:03:05 -04:00
|
|
|
def set_next_element(tag_name, next_element)
|
|
|
|
klass = next_element.class
|
|
|
|
prefix = ""
|
|
|
|
prefix << "#{klass.required_prefix}_" if klass.required_prefix
|
|
|
|
key = "#{prefix}#{tag_name.gsub(/-/, '_')}"
|
|
|
|
if self.class.plural_forms.has_key?(key)
|
|
|
|
ary = __send__("#{self.class.plural_forms[key]}")
|
|
|
|
ary << next_element
|
|
|
|
else
|
|
|
|
__send__("#{key}=", next_element)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
def have_required_elements?
|
|
|
|
self.class::MODELS.all? do |tag, uri, occurs, getter|
|
|
|
|
if occurs.nil? or occurs == "+"
|
|
|
|
child = __send__(getter)
|
|
|
|
if child.is_a?(Array)
|
|
|
|
children = child
|
|
|
|
children.any? {|c| c.have_required_elements?}
|
|
|
|
else
|
|
|
|
!child.to_s.empty?
|
|
|
|
end
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-10-16 00:51:15 -04:00
|
|
|
private
|
2007-03-17 06:13:25 -04:00
|
|
|
def initialize_variables(attrs)
|
|
|
|
normalized_attrs = {}
|
|
|
|
attrs.each do |key, value|
|
|
|
|
normalized_attrs[key.to_s] = value
|
|
|
|
end
|
2004-10-16 00:51:15 -04:00
|
|
|
self.class.need_initialize_variables.each do |variable_name|
|
2007-03-17 06:13:25 -04:00
|
|
|
value = normalized_attrs[variable_name.to_s]
|
|
|
|
if value
|
|
|
|
__send__("#{variable_name}=", value)
|
|
|
|
else
|
2007-08-04 23:03:05 -04:00
|
|
|
instance_variable_set("@#{variable_name}", nil)
|
2007-03-17 06:13:25 -04:00
|
|
|
end
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
|
|
|
initialize_have_children_elements
|
2007-03-17 06:13:25 -04:00
|
|
|
@content = normalized_attrs["content"] if self.class.have_content?
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def initialize_have_children_elements
|
2004-11-27 03:47:28 -05:00
|
|
|
self.class.have_children_elements.each do |variable_name, plural_name|
|
2007-08-04 23:03:05 -04:00
|
|
|
instance_variable_set("@#{variable_name}", [])
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-03-17 06:13:25 -04:00
|
|
|
def tag(indent, additional_attrs={}, &block)
|
2004-11-19 03:25:25 -05:00
|
|
|
next_indent = indent + INDENT
|
|
|
|
|
|
|
|
attrs = collect_attrs
|
|
|
|
return "" if attrs.nil?
|
|
|
|
|
2007-03-17 06:13:25 -04:00
|
|
|
return "" unless have_required_elements?
|
|
|
|
|
|
|
|
attrs.update(additional_attrs)
|
|
|
|
start_tag = make_start_tag(indent, next_indent, attrs.dup)
|
2004-11-28 01:59:34 -05:00
|
|
|
|
|
|
|
if block
|
|
|
|
content = block.call(next_indent)
|
2004-11-19 03:25:25 -05:00
|
|
|
else
|
|
|
|
content = []
|
|
|
|
end
|
|
|
|
|
|
|
|
if content.is_a?(String)
|
|
|
|
content = [content]
|
|
|
|
start_tag << ">"
|
|
|
|
end_tag = "</#{full_name}>"
|
|
|
|
else
|
|
|
|
content = content.reject{|x| x.empty?}
|
|
|
|
if content.empty?
|
2007-03-17 06:13:25 -04:00
|
|
|
return "" if attrs.empty?
|
2004-11-19 03:25:25 -05:00
|
|
|
end_tag = "/>"
|
|
|
|
else
|
|
|
|
start_tag << ">\n"
|
|
|
|
end_tag = "\n#{indent}</#{full_name}>"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
start_tag + content.join("\n") + end_tag
|
|
|
|
end
|
|
|
|
|
|
|
|
def make_start_tag(indent, next_indent, attrs)
|
|
|
|
start_tag = ["#{indent}<#{full_name}"]
|
|
|
|
unless attrs.empty?
|
|
|
|
start_tag << attrs.collect do |key, value|
|
|
|
|
%Q[#{h key}="#{h value}"]
|
|
|
|
end.join("\n#{next_indent}")
|
|
|
|
end
|
|
|
|
start_tag.join(" ")
|
|
|
|
end
|
|
|
|
|
|
|
|
def collect_attrs
|
2007-03-17 06:13:25 -04:00
|
|
|
attrs = {}
|
|
|
|
_attrs.each do |name, required, alias_name|
|
2004-11-19 03:25:25 -05:00
|
|
|
value = __send__(alias_name || name)
|
|
|
|
return nil if required and value.nil?
|
2007-03-17 06:13:25 -04:00
|
|
|
next if value.nil?
|
|
|
|
return nil if attrs.has_key?(name)
|
|
|
|
attrs[name] = value
|
2004-11-19 03:25:25 -05:00
|
|
|
end
|
2007-03-17 06:13:25 -04:00
|
|
|
attrs
|
2004-11-19 03:25:25 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def tag_name_with_prefix(prefix)
|
|
|
|
"#{prefix}:#{tag_name}"
|
|
|
|
end
|
2004-11-27 03:47:28 -05:00
|
|
|
|
2007-03-17 06:13:25 -04:00
|
|
|
# For backward compatibility
|
|
|
|
def calc_indent
|
|
|
|
''
|
2004-11-27 03:47:28 -05:00
|
|
|
end
|
2004-12-04 04:53:01 -05:00
|
|
|
|
2004-10-16 00:51:15 -04:00
|
|
|
def children
|
2007-03-17 06:13:25 -04:00
|
|
|
rv = []
|
|
|
|
self.class.models.each do |name, uri, occurs, getter|
|
|
|
|
value = __send__(getter)
|
|
|
|
next if value.nil?
|
|
|
|
value = [value] unless value.is_a?(Array)
|
|
|
|
value.each do |v|
|
|
|
|
rv << v if v.is_a?(Element)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
rv
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def _tags
|
2007-03-17 06:13:25 -04:00
|
|
|
rv = []
|
2007-08-04 23:03:05 -04:00
|
|
|
self.class.models.each do |name, uri, occurs, getter, plural|
|
2007-03-17 06:13:25 -04:00
|
|
|
value = __send__(getter)
|
|
|
|
next if value.nil?
|
2007-08-04 23:03:05 -04:00
|
|
|
if plural and value.is_a?(Array)
|
2007-03-17 06:13:25 -04:00
|
|
|
rv.concat([[uri, name]] * value.size)
|
|
|
|
else
|
|
|
|
rv << [uri, name]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
rv
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def _attrs
|
2007-03-17 06:13:25 -04:00
|
|
|
self.class.get_attributes.collect do |name, uri, required, element_name|
|
|
|
|
[element_name, required, name]
|
|
|
|
end
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
|
|
|
|
2007-03-17 06:13:25 -04:00
|
|
|
def __validate(ignore_unknown_element, tags=_tags, recursive=true)
|
2004-10-16 00:51:15 -04:00
|
|
|
if recursive
|
|
|
|
children.compact.each do |child|
|
|
|
|
child.validate
|
|
|
|
end
|
|
|
|
end
|
|
|
|
must_call_validators = self.class.must_call_validators
|
|
|
|
tags = tag_filter(tags.dup)
|
|
|
|
p tags if DEBUG
|
|
|
|
must_call_validators.each do |uri, prefix|
|
2007-03-17 06:13:25 -04:00
|
|
|
_validate(ignore_unknown_element, tags[uri], uri)
|
|
|
|
meth = "#{prefix}_validate"
|
|
|
|
if !prefix.empty? and respond_to?(meth, true)
|
|
|
|
__send__(meth, ignore_unknown_element, tags[uri], uri)
|
|
|
|
end
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_attribute
|
2004-11-19 03:25:25 -05:00
|
|
|
_attrs.each do |a_name, required, alias_name|
|
2007-03-17 06:13:25 -04:00
|
|
|
value = instance_variable_get("@#{alias_name || a_name}")
|
|
|
|
if required and value.nil?
|
2004-11-19 03:25:25 -05:00
|
|
|
raise MissingAttributeError.new(tag_name, a_name)
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
2007-03-17 06:13:25 -04:00
|
|
|
__send__("#{alias_name || a_name}=", value)
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-03-17 06:13:25 -04:00
|
|
|
def _validate(ignore_unknown_element, tags, uri, models=self.class.models)
|
2004-10-16 00:51:15 -04:00
|
|
|
count = 1
|
|
|
|
do_redo = false
|
|
|
|
not_shift = false
|
|
|
|
tag = nil
|
2007-03-17 06:13:25 -04:00
|
|
|
models = models.find_all {|model| model[1] == uri}
|
|
|
|
element_names = models.collect {|model| model[0]}
|
2004-10-16 00:51:15 -04:00
|
|
|
if tags
|
|
|
|
tags_size = tags.size
|
|
|
|
tags = tags.sort_by {|x| element_names.index(x) || tags_size}
|
|
|
|
end
|
|
|
|
|
2007-08-04 23:03:05 -04:00
|
|
|
_tags = tags.dup if tags
|
2007-03-17 06:13:25 -04:00
|
|
|
models.each_with_index do |model, i|
|
|
|
|
name, model_uri, occurs, getter = model
|
2004-10-16 00:51:15 -04:00
|
|
|
|
|
|
|
if DEBUG
|
2007-08-04 23:03:05 -04:00
|
|
|
p "before"
|
2004-10-16 00:51:15 -04:00
|
|
|
p tags
|
2007-03-17 06:13:25 -04:00
|
|
|
p model
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
if not_shift
|
|
|
|
not_shift = false
|
|
|
|
elsif tags
|
|
|
|
tag = tags.shift
|
|
|
|
end
|
|
|
|
|
|
|
|
if DEBUG
|
|
|
|
p "mid"
|
|
|
|
p count
|
|
|
|
end
|
|
|
|
|
2007-03-17 06:13:25 -04:00
|
|
|
case occurs
|
2004-10-16 00:51:15 -04:00
|
|
|
when '?'
|
|
|
|
if count > 2
|
2007-03-17 06:13:25 -04:00
|
|
|
raise TooMuchTagError.new(name, tag_name)
|
2004-10-16 00:51:15 -04:00
|
|
|
else
|
2007-03-17 06:13:25 -04:00
|
|
|
if name == tag
|
2004-10-16 00:51:15 -04:00
|
|
|
do_redo = true
|
|
|
|
else
|
|
|
|
not_shift = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
when '*'
|
2007-03-17 06:13:25 -04:00
|
|
|
if name == tag
|
2004-10-16 00:51:15 -04:00
|
|
|
do_redo = true
|
|
|
|
else
|
|
|
|
not_shift = true
|
|
|
|
end
|
|
|
|
when '+'
|
2007-03-17 06:13:25 -04:00
|
|
|
if name == tag
|
2004-10-16 00:51:15 -04:00
|
|
|
do_redo = true
|
|
|
|
else
|
|
|
|
if count > 1
|
|
|
|
not_shift = true
|
|
|
|
else
|
2007-03-17 06:13:25 -04:00
|
|
|
raise MissingTagError.new(name, tag_name)
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
2007-03-17 06:13:25 -04:00
|
|
|
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)
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
|
|
|
else
|
2007-03-17 06:13:25 -04:00
|
|
|
raise MissingTagError.new(name, tag_name)
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if DEBUG
|
|
|
|
p "after"
|
|
|
|
p not_shift
|
|
|
|
p do_redo
|
|
|
|
p tag
|
|
|
|
end
|
|
|
|
|
|
|
|
if do_redo
|
|
|
|
do_redo = false
|
|
|
|
count += 1
|
|
|
|
redo
|
|
|
|
else
|
|
|
|
count = 1
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2007-03-17 06:13:25 -04:00
|
|
|
if !ignore_unknown_element and !tags.nil? and !tags.empty?
|
|
|
|
raise NotExpectedTagError.new(tags.first, uri, tag_name)
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
def tag_filter(tags)
|
|
|
|
rv = {}
|
|
|
|
tags.each do |tag|
|
|
|
|
rv[tag[0]] = [] unless rv.has_key?(tag[0])
|
|
|
|
rv[tag[0]].push(tag[1])
|
|
|
|
end
|
|
|
|
rv
|
|
|
|
end
|
|
|
|
|
2007-03-17 06:13:25 -04:00
|
|
|
def empty_content?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def content_is_set?
|
|
|
|
if have_xml_content?
|
|
|
|
__send__(self.class.xml_getter)
|
|
|
|
else
|
2007-08-04 23:03:05 -04:00
|
|
|
content
|
2007-03-17 06:13:25 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def xmled_content
|
|
|
|
if have_xml_content?
|
|
|
|
__send__(self.class.xml_getter).to_s
|
|
|
|
else
|
2007-08-04 23:03:05 -04:00
|
|
|
_content = content
|
|
|
|
_content = Base64.encode64(_content) if need_base64_encode?
|
|
|
|
h(_content)
|
2007-03-17 06:13:25 -04:00
|
|
|
end
|
|
|
|
end
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
module RootElementMixin
|
|
|
|
|
2004-11-19 03:25:25 -05:00
|
|
|
include XMLStyleSheetMixin
|
|
|
|
|
2004-10-16 00:51:15 -04:00
|
|
|
attr_reader :output_encoding
|
2007-03-17 06:13:25 -04:00
|
|
|
attr_reader :feed_type, :feed_subtype, :feed_version
|
|
|
|
attr_accessor :version, :encoding, :standalone
|
|
|
|
def initialize(feed_version, version=nil, encoding=nil, standalone=nil)
|
2004-10-16 00:51:15 -04:00
|
|
|
super()
|
2007-03-17 06:13:25 -04:00
|
|
|
@feed_type = nil
|
|
|
|
@feed_subtype = nil
|
|
|
|
@feed_version = feed_version
|
2004-10-16 00:51:15 -04:00
|
|
|
@version = version || '1.0'
|
|
|
|
@encoding = encoding
|
|
|
|
@standalone = standalone
|
|
|
|
@output_encoding = nil
|
|
|
|
end
|
|
|
|
|
2007-03-17 06:13:25 -04:00
|
|
|
def feed_info
|
|
|
|
[@feed_type, @feed_version, @feed_subtype]
|
|
|
|
end
|
|
|
|
|
2004-10-16 00:51:15 -04:00
|
|
|
def output_encoding=(enc)
|
|
|
|
@output_encoding = enc
|
|
|
|
self.converter = Converter.new(@output_encoding, @encoding)
|
|
|
|
end
|
2004-01-27 22:46:13 -05:00
|
|
|
|
2004-11-27 03:47:28 -05:00
|
|
|
def setup_maker(maker)
|
|
|
|
maker.version = version
|
|
|
|
maker.encoding = encoding
|
|
|
|
maker.standalone = standalone
|
|
|
|
|
|
|
|
xml_stylesheets.each do |xss|
|
|
|
|
xss.setup_maker(maker)
|
|
|
|
end
|
|
|
|
|
2007-03-17 06:13:25 -04:00
|
|
|
super
|
2004-11-27 03:47:28 -05:00
|
|
|
end
|
2007-03-17 06:13:25 -04:00
|
|
|
|
|
|
|
def to_feed(type, &block)
|
|
|
|
Maker.make(type) do |maker|
|
|
|
|
setup_maker(maker)
|
|
|
|
block.call(maker) if block
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_rss(type, &block)
|
|
|
|
to_feed("rss#{type}", &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_atom(type, &block)
|
|
|
|
to_feed("atom:#{type}", &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_xml(type=nil, &block)
|
|
|
|
if type.nil? or same_feed_type?(type)
|
|
|
|
to_s
|
|
|
|
else
|
|
|
|
to_feed(type, &block).to_s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-01-27 22:46:13 -05:00
|
|
|
private
|
2007-03-17 06:13:25 -04:00
|
|
|
def same_feed_type?(type)
|
|
|
|
if /^(atom|rss)?(\d+\.\d+)?(?::(.+))?$/i =~ type
|
|
|
|
feed_type = ($1 || @feed_type).downcase
|
|
|
|
feed_version = $2 || @feed_version
|
|
|
|
feed_subtype = $3 || @feed_subtype
|
|
|
|
[feed_type, feed_version, feed_subtype] == feed_info
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def tag(indent, attrs={}, &block)
|
|
|
|
rv = super(indent, ns_declarations.merge(attrs), &block)
|
|
|
|
return rv if rv.empty?
|
|
|
|
"#{xmldecl}#{xml_stylesheet_pi}#{rv}"
|
2004-11-19 03:25:25 -05:00
|
|
|
end
|
|
|
|
|
2004-10-16 00:51:15 -04:00
|
|
|
def xmldecl
|
|
|
|
rv = %Q[<?xml version="#{@version}"]
|
|
|
|
if @output_encoding or @encoding
|
|
|
|
rv << %Q[ encoding="#{@output_encoding or @encoding}"]
|
|
|
|
end
|
2004-11-19 03:25:25 -05:00
|
|
|
rv << %Q[ standalone="yes"] if @standalone
|
|
|
|
rv << "?>\n"
|
2004-10-16 00:51:15 -04:00
|
|
|
rv
|
|
|
|
end
|
2004-10-16 00:39:58 -04:00
|
|
|
|
2004-11-19 03:25:25 -05:00
|
|
|
def ns_declarations
|
2007-03-17 06:13:25 -04:00
|
|
|
decls = {}
|
2004-11-19 03:25:25 -05:00
|
|
|
self.class::NSPOOL.collect do |prefix, uri|
|
2004-10-16 00:51:15 -04:00
|
|
|
prefix = ":#{prefix}" unless prefix.empty?
|
2007-03-17 06:13:25 -04:00
|
|
|
decls["xmlns#{prefix}"] = uri
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
2007-03-17 06:13:25 -04:00
|
|
|
decls
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
2007-03-17 06:13:25 -04:00
|
|
|
|
|
|
|
def maker_target(target)
|
|
|
|
target
|
2004-11-27 03:47:28 -05:00
|
|
|
end
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
2004-01-27 22:46:13 -05:00
|
|
|
end
|