~ubuntu-branches/ubuntu/trusty/ruby1.9/trusty

« back to all changes in this revision

Viewing changes to lib/rss/taxonomy.rb

  • Committer: Bazaar Package Importer
  • Author(s): Lucas Nussbaum
  • Date: 2006-05-08 22:23:12 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060508222312-w2wqeaz030ifi59j
Tags: 1.9.0+20060423-3ubuntu1
* Resynchronized with Debian.
* Only change from Debian is the addition of
  debian/patches/903_sparc_fix_define.patch to fix illegal instructions
  at runtime on sparc. (change from 1.9.0+20050921-1ubuntu1)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Experimental
2
 
 
3
1
require "rss/1.0"
 
2
require "rss/dublincore"
4
3
 
5
4
module RSS
6
5
 
7
6
  TAXO_PREFIX = "taxo"
8
 
  TAXO_NS = "http://purl.org/rss/1.0/modules/taxonomy/"
 
7
  TAXO_URI = "http://purl.org/rss/1.0/modules/taxonomy/"
9
8
 
10
 
  Element.install_ns(TAXO_PREFIX, TAXO_NS)
 
9
  RDF.install_ns(TAXO_PREFIX, TAXO_URI)
11
10
 
12
11
  TAXO_ELEMENTS = []
13
12
 
14
13
  %w(link).each do |name|
15
14
    full_name = "#{TAXO_PREFIX}_#{name}"
16
 
    BaseListener.install_get_text_element(TAXO_NS, name, "#{full_name}=")
17
 
    TAXO_ELEMENTS << "#{TAXO_PREFIX}_#{name}"
18
 
  end
19
 
    
20
 
  module TaxonomyModel
21
 
    attr_writer(*%w(title description creator subject publisher
22
 
                    contributor date format identifier source
23
 
                    language relation coverage rights
24
 
                   ).collect{|name| "#{TAXO_PREFIX}_#{name}"})
25
 
  end
26
 
  
27
 
  class Channel; extend TaxonomyModel;  end
28
 
  class Item; extend TaxonomyModel;     end
29
 
  class Image; extend TaxonomyModel;    end
30
 
  class TextInput; extend TaxonomyModel;        end
31
 
  
 
15
    BaseListener.install_get_text_element(TAXO_URI, name, "#{full_name}=")
 
16
    TAXO_ELEMENTS << "#{TAXO_PREFIX}_#{name}"
 
17
  end
 
18
 
 
19
  %w(topic topics).each do |name|
 
20
    class_name = Utils.to_class_name(name)
 
21
    BaseListener.install_class_name(TAXO_URI, name, "Taxonomy#{class_name}")
 
22
    TAXO_ELEMENTS << "#{TAXO_PREFIX}_#{name}"
 
23
  end
 
24
 
 
25
  module TaxonomyTopicsModel
 
26
    extend BaseModel
 
27
    
 
28
    def self.append_features(klass)
 
29
      super
 
30
 
 
31
      var_name = "#{TAXO_PREFIX}_topics"
 
32
      klass.install_have_child_element(var_name)
 
33
    end
 
34
 
 
35
    def taxo_validate(tags)
 
36
      found_topics = false
 
37
      tags.each do |tag|
 
38
        if tag == "topics"
 
39
          if found_topics
 
40
            raise TooMuchTagError.new(tag, tag_name)
 
41
          else
 
42
            found_topics = true
 
43
          end
 
44
        else
 
45
          raise UnknownTagError.new(tag, TAXO_URI)
 
46
        end
 
47
      end
 
48
    end
 
49
 
 
50
    class TaxonomyTopics < Element
 
51
      include RSS10
 
52
      
 
53
      Bag = ::RSS::RDF::Bag
 
54
 
 
55
      class << self
 
56
        def required_prefix
 
57
          TAXO_PREFIX
 
58
        end
 
59
        
 
60
        def required_uri
 
61
          TAXO_URI
 
62
        end
 
63
      end
 
64
 
 
65
      @tag_name = "topics"
 
66
      
 
67
      install_have_child_element("Bag")
 
68
        
 
69
      install_must_call_validator('rdf', ::RSS::RDF::URI)
 
70
 
 
71
      def initialize(bag=Bag.new)
 
72
        super()
 
73
        @Bag = bag
 
74
      end
 
75
 
 
76
      def full_name
 
77
        tag_name_with_prefix(TAXO_PREFIX)
 
78
      end
 
79
 
 
80
      def maker_target(target)
 
81
        target.taxo_topics
 
82
      end
 
83
      
 
84
      def to_s(need_convert=true, indent=calc_indent)
 
85
        rv = tag(indent) do |next_indent|
 
86
          [
 
87
           Bag_element(need_convert, next_indent),
 
88
           other_element(need_convert, next_indent),
 
89
          ]
 
90
        end
 
91
      end
 
92
 
 
93
      def resources
 
94
        if @Bag
 
95
          @Bag.lis.collect do |li|
 
96
            li.resource
 
97
          end
 
98
        else
 
99
          []
 
100
        end
 
101
      end
 
102
 
 
103
      private
 
104
      def children
 
105
        [@Bag]
 
106
      end
 
107
 
 
108
      def _tags
 
109
        rv = []
 
110
        rv << [::RSS::RDF::URI, 'Bag'] unless @Bag.nil?
 
111
        rv
 
112
      end
 
113
      
 
114
      def rdf_validate(tags)
 
115
        _validate(tags, [["Bag", nil]])
 
116
      end
 
117
    end
 
118
  end
 
119
  
 
120
  module TaxonomyTopicModel
 
121
    extend BaseModel
 
122
    
 
123
    def self.append_features(klass)
 
124
      super
 
125
      var_name = "#{TAXO_PREFIX}_topic"
 
126
      klass.install_have_children_element(var_name)
 
127
    end
 
128
 
 
129
    def taxo_validate(tags)
 
130
      tags.each do |tag|
 
131
        if tag != "topic"
 
132
          raise UnknownTagError.new(tag, TAXO_URI)
 
133
        end
 
134
      end
 
135
    end
 
136
 
 
137
    class TaxonomyTopic < Element
 
138
      include RSS10
 
139
 
 
140
      include DublinCoreModel
 
141
      include TaxonomyTopicsModel
 
142
      
 
143
      class << self
 
144
        def required_prefix
 
145
          TAXO_PREFIX
 
146
        end
 
147
        
 
148
        def required_uri
 
149
          TAXO_URI
 
150
        end
 
151
      end
 
152
 
 
153
      @tag_name = "topic"
 
154
 
 
155
      install_get_attribute("about", ::RSS::RDF::URI, true)
 
156
      install_text_element("#{TAXO_PREFIX}_link")
 
157
        
 
158
      def initialize(about=nil)
 
159
        super()
 
160
        @about = about
 
161
      end
 
162
 
 
163
      def full_name
 
164
        tag_name_with_prefix(TAXO_PREFIX)
 
165
      end
 
166
      
 
167
      def to_s(need_convert=true, indent=calc_indent)
 
168
        rv = tag(indent) do |next_indent|
 
169
          [
 
170
           other_element(need_convert, next_indent),
 
171
          ]
 
172
        end
 
173
      end
 
174
 
 
175
      def taxo_validate(tags)
 
176
        elements = %w(link topics)
 
177
        counter = {}
 
178
        
 
179
        tags.each do |tag|
 
180
          if elements.include?(tag)
 
181
            counter[tag] ||= 0
 
182
            counter[tag] += 1
 
183
            raise TooMuchTagError.new(tag, tag_name) if counter[tag] > 1
 
184
          else
 
185
            raise UnknownTagError.new(tag, TAXO_URI)
 
186
          end
 
187
        end
 
188
      end
 
189
 
 
190
      def maker_target(target)
 
191
        target.new_taxo_topic
 
192
      end
 
193
      
 
194
      private
 
195
      def children
 
196
        [@taxo_link, @taxo_topics]
 
197
      end
 
198
 
 
199
      def _attrs
 
200
        [
 
201
         ["#{RDF::PREFIX}:about", true, "about"]
 
202
        ]
 
203
      end
 
204
      
 
205
      def _tags
 
206
        rv = []
 
207
        rv << [TAXO_URI, "link"] unless @taxo_link.nil?
 
208
        rv << [TAXO_URI, "topics"] unless @taxo_topics.nil?
 
209
        rv
 
210
      end
 
211
    end
 
212
  end
 
213
 
 
214
  class RDF
 
215
    include TaxonomyTopicModel
 
216
    class Channel
 
217
      include TaxonomyTopicsModel
 
218
    end
 
219
    class Item; include TaxonomyTopicsModel; end
 
220
  end
32
221
end