Module: Atom::Xml::Parseable
- Included in:
- Category, Content::Base, Entry, Feed, Generator, Link, Person, Pub::Categories, Pub::Collection, Pub::Service, Pub::Workspace, Source
- Defined in:
- lib/atom/xml/parser.rb
Overview
:nodoc:
Defined Under Namespace
Modules: DeclarationMethods Classes: ParseSpec
Class Method Summary collapse
Instance Method Summary collapse
- #==(o) ⇒ Object
- #accessor_name(name) ⇒ Object
- #current_node_is?(xml, element, ns = nil) ⇒ Boolean
- #next_node_is?(xml, element, ns = nil) ⇒ Boolean
- #parse(xml, options = {}) ⇒ Object
- #to_xml(builder = nil, root_name = self.class.name.demodulize.downcase, namespace = nil, namespace_handler = nil) ⇒ Object
Class Method Details
.included(o) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/atom/xml/parser.rb', line 148 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
166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/atom/xml/parser.rb', line 166 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
144 145 146 |
# File 'lib/atom/xml/parser.rb', line 144 def accessor_name(name) Atom.to_attrname(name) end |
#current_node_is?(xml, element, ns = nil) ⇒ Boolean
140 141 142 |
# File 'lib/atom/xml/parser.rb', line 140 def current_node_is?(xml, element, ns = nil) xml.node_type == Nokogiri::XML::Reader::TYPE_ELEMENT && xml.local_name == element && (ns.nil? || ns == xml.namespace_uri) end |
#next_node_is?(xml, element, ns = nil) ⇒ Boolean
132 133 134 135 136 137 138 |
# File 'lib/atom/xml/parser.rb', line 132 def next_node_is?(xml, element, ns = nil) # Get to the next element while xml.read && xml.node_type != Nokogiri::XML::Reader::TYPE_ELEMENT; end current_node_is?(xml, element, ns) rescue Nokogiri::XML::SyntaxError false end |
#parse(xml, options = {}) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/atom/xml/parser.rb', line 95 def parse(xml, = {}) starting_depth = xml.depth loop do case xml.node_type when Nokogiri::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? xml.attribute_nodes.each do |node| name = [(node.namespace && node.namespace.prefix), node.name].compact.join(':') value = node.value if attributes.include?(name) # Support attribute names with namespace prefixes self.send("#{accessor_name(name)}=", value) elsif uri_attributes.include?(name) value = if xml.base_uri @base_uri = xml.base_uri raw_uri = URI.parse(value) (raw_uri.relative? ? URI.parse(xml.base_uri) + raw_uri : raw_uri).to_s else value end self.send("#{accessor_name(name)}=", value) elsif self.respond_to?(:simple_extensions) href = node.namespace && node.namespace.href self[href, node.name].as_attribute = true self[href, node.name] << value end end elsif self.respond_to?(:simple_extensions) self[xml.namespace_uri, xml.local_name] << xml.inner_xml end end break unless ![:once] && xml.read && xml.depth >= starting_depth end end |
#to_xml(builder = nil, root_name = self.class.name.demodulize.downcase, namespace = nil, namespace_handler = nil) ⇒ Object
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 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/atom/xml/parser.rb', line 178 def to_xml(builder = nil, root_name = self.class.name.demodulize.downcase, namespace = nil, namespace_handler = nil) orig_builder = builder builder ||= Nokogiri::XML::Builder.new(:encoding => 'UTF-8') namespaces = {} namespaces['xmlns'] = self.class.namespace if !orig_builder && self.class.respond_to?(:namespace) && self.class.namespace self.class.extensions_namespaces.each do |ns_alias,uri| namespaces["xmlns:#{ns_alias}"] = uri end attributes = {} node = builder.send("#{root_name}_", namespaces) do |builder| namespace_handler ||= NamespaceHandler.new(builder.doc.root, self.class.namespace) self.class.ordered_element_specs.each do |spec| if spec.single? if attribute = self.send(spec.attribute) if attribute.respond_to?(:to_xml) attribute.to_xml(builder, spec.name, spec.[:namespace], namespace_handler) else namespaces = {} namespaces['xmlns'] = spec.[:namespace] if spec.[:namespace] value = (attribute.is_a?(Time)? attribute.xmlschema : attribute.to_s) builder.send("#{spec.name}_", value, namespaces) end end else self.send(spec.attribute).each do |attribute| if attribute.respond_to?(:to_xml) attribute.to_xml(builder, spec.name.singularize, nil, namespace_handler) else namespaces = {} namespaces['xmlns'] = spec.[:namespace] if spec.[:namespace] builder.send("#{spec.name.singularize}_", attribute.to_s, namespaces) end end end end (self.class.attributes + self.class.uri_attributes).each do |attribute| if value = self.send(accessor_name(attribute)) if value != 0 attributes[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 attributes["#{namespace_handler.get($1)}:#{$2}"] = value else namespace_handler.prefix(builder, $1).send("#{$2}_", value) end end else STDERR.print "Couldn't split #{name}" end end end end attributes.each do |k,v| node[k] = v end builder.doc end |