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



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/atom/xml/parser.rb', line 111

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 element_specs; self.class.element_specs; end
    def ordered_element_specs; self.class.ordered_element_specs; end
    def attributes; self.class.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



127
128
129
130
131
132
133
134
135
136
137
# File 'lib/atom/xml/parser.rb', line 127

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

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

Returns:

  • (Boolean)


107
108
109
# File 'lib/atom/xml/parser.rb', line 107

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)


101
102
103
104
105
# File 'lib/atom/xml/parser.rb', line 101

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



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/atom/xml/parser.rb', line 76

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?
        while (xml.move_to_next_attribute == 1)
          if attributes.include?(xml.name)
            # Support attribute names with namespace prefixes
            self.send("#{xml.name.sub(/:/, '_')}=", xml.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.



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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
# File 'lib/atom/xml/parser.rb', line 143

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.each do |attribute|
    if value = self.send("#{attribute.sub(/:/, '_')}")
      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