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
- #current_node_is?(xml, element, ns = nil) ⇒ Boolean
- #next_node_is?(xml, element, ns = nil) ⇒ Boolean
- #parse(xml, options = {}) ⇒ Object
- #to_xml(nodeonly = false, root_name = self.class.name.demodulize.downcase, namespace = nil, namespace_map = nil) ⇒ Object
Class Method Details
.included(o) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/atom/xml/parser.rb', line 89 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 end o.send(:extend, DeclarationMethods) end |
Instance Method Details
#==(o) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/atom/xml/parser.rb', line 102 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
85 86 87 |
# File 'lib/atom/xml/parser.rb', line 85 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
81 82 83 |
# File 'lib/atom/xml/parser.rb', line 81 def next_node_is?(xml, element, ns = nil) xml.next == 1 && current_node_is?(xml, element, ns) end |
#parse(xml, options = {}) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/atom/xml/parser.rb', line 56 def parse(xml, = {}) starting_depth = xml.depth loop do case xml.node_type when XML::Reader::TYPE_ELEMENT if element_specs.include?(xml.local_name) && [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_string end end break unless ![: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
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 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 |
# File 'lib/atom/xml/parser.rb', line 114 def to_xml(nodeonly = false, root_name = self.class.name.demodulize.downcase, namespace = nil, namespace_map = nil) namespace_map = NamespaceMap.new if namespace_map.nil? node = XML::Node.new(root_name) node['xmlns'] = self.class.namespace unless nodeonly || !self.class.respond_to?(:namespace) 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.[:namespace], namespace_map) else n = XML::Node.new(spec.name) n['xmlns'] = spec.[: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.[: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 |