Module: Atom::Xml::Parseable

Overview

:nodoc:

Defined Under Namespace

Modules: DeclarationMethods Classes: ParseSpec

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(o) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/atom/xml/parser.rb', line 129

def Parseable.included(o)
  o.class_eval do
    def o.ordered_element_specs;  @ordered_element_specs ||= []; end
    def o.element_specs;  @element_specs ||= {}; end
    def o.attributes; @attributes ||= []; end
    def o.uri_attributes; @uri_attributes ||= []; end
    def element_specs; self.class.element_specs; end
    def ordered_element_specs; self.class.ordered_element_specs; end
    def attributes; self.class.attributes; end
    def uri_attributes; self.class.uri_attributes; end
    def o.namespace(ns = @namespace); @namespace = ns; end
    def o.add_extension_namespace(ns, url); self.extensions_namespaces[ns.to_s] = url; end
    def o.extensions_namespaces; @extensions_namespaces ||= {} end
    def o.known_namespaces; @known_namespaces ||= [] end
  end
  o.send(:extend, DeclarationMethods)
end

Instance Method Details

#==(o) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
# File 'lib/atom/xml/parser.rb', line 147

def ==(o)
  if self.object_id == o.object_id
    true
  elsif o.instance_of?(self.class)
    self.class.element_specs.values.all? do |spec|
      self.send(spec.attribute) == o.send(spec.attribute)
    end
  else
    false
  end
end

#accessor_name(name) ⇒ Object



125
126
127
# File 'lib/atom/xml/parser.rb', line 125

def accessor_name(name)
  Atom.to_attrname(name)
end

#current_node_is?(xml, element, ns = nil) ⇒ Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/atom/xml/parser.rb', line 121

def current_node_is?(xml, element, ns = nil)
  xml.node_type == XML::Reader::TYPE_ELEMENT && xml.local_name == element && (ns.nil? || ns == xml.namespace_uri)
end

#next_node_is?(xml, element, ns = nil) ⇒ Boolean

Returns:

  • (Boolean)


115
116
117
118
119
# File 'lib/atom/xml/parser.rb', line 115

def next_node_is?(xml, element, ns = nil)
  # Get to the next element
  while xml.next == 1 && xml.node_type != XML::Reader::TYPE_ELEMENT; end
  current_node_is?(xml, element, ns)
end

#parse(xml, options = {}) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/atom/xml/parser.rb', line 81

def parse(xml, options = {})
  starting_depth = xml.depth
  loop do
    case xml.node_type
    when XML::Reader::TYPE_ELEMENT
      if element_specs.include?(xml.local_name) && (self.class.known_namespaces + [Atom::NAMESPACE, Atom::Pub::NAMESPACE]).include?(xml.namespace_uri)
        element_specs[xml.local_name].parse(self, xml)
      elsif attributes.any? || uri_attributes.any?
        while (xml.move_to_next_attribute == 1)
          if attributes.include?(xml.name)
            # Support attribute names with namespace prefixes
            self.send("#{accessor_name(xml.name)}=", xml.value)
          elsif uri_attributes.include?(xml.name)
            value = if xml.base_uri
              @base_uri = xml.base_uri
              raw_uri = URI.parse(xml.value)
              (raw_uri.relative? ? URI.parse(xml.base_uri) + raw_uri : raw_uri).to_s
            else
              xml.value
            end
            self.send("#{accessor_name(xml.name)}=", value)
          elsif self.respond_to?(:simple_extensions)
            self[xml.namespace_uri, xml.local_name].as_attribute = true
            self[xml.namespace_uri, xml.local_name] << xml.value
          end
        end
      elsif self.respond_to?(:simple_extensions)
        self[xml.namespace_uri, xml.local_name] << xml.read_inner_xml
      end
    end
    break unless !options[:once] && xml.next == 1 && xml.depth >= starting_depth
  end
end

#to_xml(nodeonly = false, root_name = self.class.name.demodulize.downcase, namespace = nil, namespace_map = nil) ⇒ Object

There doesn’t seem to be a way to set namespaces using libxml-ruby, so ratom has to manage namespace to URI prefixing itself, which makes this method more complicated that it needs to be.



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/atom/xml/parser.rb', line 163

def to_xml(nodeonly = false, root_name = self.class.name.demodulize.downcase, namespace = nil, namespace_map = nil)
  namespace_map = NamespaceMap.new(self.class.namespace) if namespace_map.nil?
  node = XML::Node.new(root_name)
  node['xmlns'] = self.class.namespace unless nodeonly || !self.class.respond_to?(:namespace)
  self.class.extensions_namespaces.each do |ns_alias,uri|
    node["xmlns:#{ns_alias}"] = uri
  end

  self.class.ordered_element_specs.each do |spec|
    if spec.single?
      if attribute = self.send(spec.attribute)
        if attribute.respond_to?(:to_xml)
          node << attribute.to_xml(true, spec.name, spec.options[:namespace], namespace_map)
        else
          n =  XML::Node.new(spec.name)
          n['xmlns'] = spec.options[:namespace] if spec.options[:namespace]
          n << (attribute.is_a?(Time)? attribute.xmlschema : attribute.to_s)
          node << n
        end
      end
    else
      self.send(spec.attribute).each do |attribute|
        if attribute.respond_to?(:to_xml)
          node << attribute.to_xml(true, spec.name.singularize, nil, namespace_map)
        else
          n = XML::Node.new(spec.name.singularize)
          n['xmlns'] = spec.options[:namespace] if spec.options[:namespace]
          n << attribute.to_s
          node << n
        end
      end
    end
  end
  
  (self.class.attributes + self.class.uri_attributes).each do |attribute|
    if value = self.send(accessor_name(attribute))
      if value != 0
        node[attribute] = value.to_s
      end
    end
  end
  
  if self.respond_to?(:simple_extensions) && self.simple_extensions
    self.simple_extensions.each do |name, value_array|
      if name =~ /\{(.*),(.*)\}/
        value_array.each do |value|
          if value_array.as_attribute
            node["#{namespace_map.get($1)}:#{$2}"] = value
          else
            ext = XML::Node.new("#{namespace_map.get($1)}:#{$2}")
            ext << value
            node << ext
          end
        end
      else
        STDERR.print "Couldn't split #{name}"
      end
    end
  end
  
  unless nodeonly
    namespace_map.each do |ns, prefix|
      node["xmlns:#{prefix}"] = ns
    end
    
    doc = XML::Document.new
    doc.root = node
    doc.to_s
  else
    node
  end
end