1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 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 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 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 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
|
module ROXML
class RequiredElementMissing < ArgumentError # :nodoc:
end
#
# Internal base class that represents an XML - Class binding.
#
class XMLRef # :nodoc:
attr_reader :opts
delegate :required?, :array?, :accessor, :default, :wrapper, :to => :opts
def initialize(opts, instance)
@opts = opts
@instance = instance
end
def blocks
opts.blocks || []
end
def to_xml(instance)
val = instance.__send__(accessor)
opts.to_xml.respond_to?(:call) ? opts.to_xml.call(val) : val
end
def name
opts.name_explicit? ? opts.name : conventionize(opts.name)
end
def xpath_name
namespacify(name)
end
def value_in(xml)
xml = XML::Node.from(xml)
value = fetch_value(xml)
value = default if default && (value.nil? || value.to_s.empty?)
value = apply_blocks(value)
value = freeze(value) if value && opts.frozen?
value
end
private
def conventionize(what)
convention ||= @instance.class.respond_to?(:roxml_naming_convention) && @instance.class.roxml_naming_convention
if !what.blank? && convention.respond_to?(:call)
URI.decode_www_form_component(convention.call(URI.encode_www_form_component(what)))
else
what
end
end
def namespacify(what)
if what.to_s.present? && opts.namespace != false && ns = [opts.namespace, @instance.class.roxml_namespace, @default_namespace].compact.map(&:to_s).first
require "rexml/xpath_parser"
parser = REXML::Parsers::XPathParser.new
parsed = parser.parse what
parsed.each_cons(4).with_index.each do |a,i|
if a[0..2] == [:child, :qname, ""]
if ns == "*"
parsed[i+1,3] = [:any, :predicate, [:eq, [:function, "local-name", []], [:literal, a[3]]]] if a[3] != "*"
else
a[2].replace ns
end
end
end
parser.abbreviate parsed
else
what
end
end
def apply_blocks(val)
blocks.inject(val) {|val, block| block.call(val) }
rescue Exception => ex
raise ex, "#{accessor}: #{ex.message}"
end
def freeze(val)
val.each(&:freeze) if val.is_a?(Enumerable)
val.freeze
end
def xpath
opts.wrapper ? "#{namespacify(opts.wrapper)}/#{xpath_name}" : xpath_name.to_s
end
def auto_wrapper
namespacify(conventionize(opts.name.pluralize))
end
def auto_xpath
"#{auto_wrapper}/#{xpath_name}" if array?
end
def several?
array?
end
def wrap(xml, opts = {:always_create => false})
wrap_with = defined?(@auto_vals) && @auto_vals ? auto_wrapper : wrapper
return xml if !wrap_with || xml.name == wrap_with
wraps = wrap_with.to_s.split('/')
wraps.inject(xml) do |node,wrap|
if !opts[:always_create] && (child = node.children.find {|c| c.name == wrap })
child
else
XML.add_node(node, wrap)
end
end
end
def nodes_in(xml)
@default_namespace = XML.default_namespace(xml)
vals = XML.search(xml, xpath, @instance.class.roxml_namespaces)
if several? && vals.empty? && !wrapper && auto_xpath
vals = XML.search(xml, auto_xpath, @instance.class.roxml_namespaces)
@auto_vals = !vals.empty?
end
if vals.empty?
raise RequiredElementMissing, "#{name} from #{xml} for #{accessor}" if required?
default
elsif several?
vals.map do |val|
yield val
end
else
yield(vals.first)
end
end
end
# Interal class representing an XML attribute binding
#
# In context:
# <element attribute="XMLAttributeRef">
# XMLTextRef
# </element>
class XMLAttributeRef < XMLRef # :nodoc:
# Updates the attribute in the given XML block to
# the value provided.
def update_xml(xml, values)
if array?
values.each do |value|
wrap(xml, :always_create => true).tap do |node|
XML.set_attribute(node, name, value.to_s)
end
end
else
wrap(xml).tap do |xml|
XML.set_attribute(xml, name, values.to_s)
end
end
end
private
def fetch_value(xml)
nodes_in(xml) do |node|
node.value
end
end
def xpath_name
"@#{name}"
end
end
# Interal class representing XML content text binding
#
# In context:
# <element attribute="XMLAttributeRef">
# XMLTextRef
# </element>
class XMLTextRef < XMLRef # :nodoc:
delegate :cdata?, :content?, :name?, :to => :opts
# Updates the text in the given _xml_ block to
# the _value_ provided.
def update_xml(xml, value)
wrap(xml).tap do |xml|
if content?
add(xml, value)
elsif name?
xml.name = value
elsif array?
value.each do |v|
add(XML.add_node(xml, name), v)
end
else
add(XML.add_node(xml, name), value)
end
end
end
private
def fetch_value(xml)
if content? || name?
value =
if content?
xml.content.to_s
elsif name?
xml.name
end
if value.blank?
raise RequiredElementMissing, "#{name} from #{xml} for #{accessor}" if required?
default
else
value
end
else
nodes_in(xml) do |node|
node.content
end
end
end
def add(dest, value)
if cdata?
XML.add_cdata(dest, value.to_s)
else
XML.set_content(dest, value.to_s)
end
end
end
class XMLNameSpaceRef < XMLRef # :nodoc:
private
def fetch_value(xml)
xml.namespace.prefix
end
end
class XMLHashRef < XMLTextRef # :nodoc:
delegate :hash, :to => :opts
def initialize(opts, inst)
super(opts, inst)
@key = opts.hash.key.to_ref(inst)
@value = opts.hash.value.to_ref(inst)
end
def several?
true
end
# Updates the composed XML object in the given XML block to
# the value provided.
def update_xml(xml, value)
wrap(xml).tap do |xml|
value.each_pair do |k, v|
node = XML.add_node(xml, hash.wrapper)
@key.update_xml(node, k)
@value.update_xml(node, v)
end
end
end
private
def fetch_value(xml)
nodes_in(xml) do |node|
[@key.value_in(node), @value.value_in(node)]
end
end
def apply_blocks(vals)
unless blocks.empty?
vals.collect! do |kvp|
super(kvp)
end
end
to_hash(vals) if vals
end
def freeze(vals)
vals.each_pair{|k, v| k.freeze; v.freeze }
vals.freeze
end
def to_hash(array)
hash = array.inject({}) do |result, (k, v)|
result[k] ||= []
result[k] << v
result
end
hash.each_pair do |k, v|
hash[k] = v.first if v.size == 1
end
end
end
class XMLObjectRef < XMLTextRef # :nodoc:
delegate :sought_type, :to => :opts
# Updates the composed XML object in the given XML block to
# the value provided.
def update_xml(xml, value)
wrap(xml).tap do |xml|
params = {:name => name, :namespace => opts.namespace}
if array?
value.each do |v|
XML.add_child(xml, v.to_xml(params))
end
elsif value.is_a?(ROXML)
XML.add_child(xml, value.to_xml(params))
else
XML.add_node(xml, name).tap do |node|
XML.set_content(node, value.to_xml)
end
end
end
end
private
def fetch_value(xml)
nodes_in(xml) do |node|
if sought_type.respond_to? :from_xml
sought_type.from_xml(node)
else
sought_type.new(node)
end
end
end
end
end
|