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 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
|
## -*- Ruby -*-
## XML::DOM
## 1998-2001 by yoshidam
##
require 'xml/dom2/node'
require 'xml/dom2/domexception'
require 'xml/dom2/nodelist'
require 'xml/dom2/namednodemap'
module XML
module DOM
=begin
== Class XML::DOM::Element
=== superclass
Node
=end
class Element<Node
=begin
=== Class Methods
--- Element.new(tag = nil, attrs = nil, *children)
create a new Element.
=end
## new(tag, attrs, [child1, child2, ...]) or
## new(tag, attrs, child1, child2, ...)
## tag: String
## attrs: Hash, Attr or Array of Attr (or nil)
## child?: String or Node
def initialize(tag = nil, attr = nil, *children)
super(*children)
raise "parameter error" if !tag
@name = nil
@prefix = nil
@localname = nil
@uri = nil
@idAttribute = nil
if tag.is_a?(Array)
## namespaces
raise "parameter error" if tag.length != 2
@localname = tag[1]
if tag[1].index(':')
@prefix, @localname = tag[1].split(':')
end
@name = tag[1] ## qualified name
@uri = tag[0] ## namespace URI
else
@name = tag
end
@name.freeze
@localname.freeze
@uri.freeze
@prefix.freeze
if attr.nil?
@attr = NamedNodeMap.new([])
elsif attr.is_a?(Hash)
nodes = []
attr.each do |key, value|
nodes.push(Attr.new(key, value))
end
@attr = NamedNodeMap.new(nodes)
elsif attr.is_a?(Array)
@attr = NamedNodeMap.new(attr)
elsif attr.nodeType == ATTRIBUTE_NODE
@attr = NamedNodeMap.new([attr])
else
raise "parameter error: #{attr}"
end
@attr.each do |attr|
attr.ownerElement = self
end
end
=begin
=== Methods
--- Element#nodeType()
[DOM]
returns the nodeType.
=end
## [DOM]
def nodeType
ELEMENT_NODE
end
=begin
--- Element#nodeName()
[DOM]
returns the nodeName.
=end
## [DOM]
def nodeName
@name
end
=begin
--- Element#attributes()
[DOM]
returns the attributes of this Element.
=end
## [DOM]
def attributes
if iterator?
@attr.each do |key, value|
yield(value)
end if @attr
else
@attr
end
end
def _getNamespaces(parentNamespaces = {}, all = false)
if !parentNamespaces
parentNamespaces = parentNode._getNamespaces(nil, true)
end
namespaces = {}
attributes.each do |a|
namespaces[a.prefix] = a.namespaceURI if a.prefix
end
if @localname
namespaces[@prefix] = @uri
end
parentNamespaces.each do |prefix, uri|
if all
if !namespaces.include?(prefix)
namespaces[prefix] = uri
end
else
if namespaces[prefix] == parentNamespaces[prefix]
namespaces.delete(prefix)
end
end
end
namespaces
end
=begin
--- Element#to_s()
return the string representation of the Element.
=end
def to_s
attr = ''
namespaces = {}
attributes.each do |a|
namespaces[a.prefix] = a.namespaceURI if a.prefix
end
if @localname
namespaces[@prefix] = @uri
end
namespaces.each do |prefix, uri|
## skip the namespace declaration of xml or xmlns.
next if prefix == 'xml' or
uri == 'http://www.w3.org/2000/xmlns/'
nsattrname = 'xmlns'
nsattrname << ':' + prefix if prefix
## skip duplicated namespace declarations.
next if @attr.getNamedItem(nsattrname)
attr << " #{nsattrname}=\"#{uri}\""
end
@attr.each do |a|
attr << ' ' + a.to_s
end if @attr
content = super
if content != ''
ret = "<#{@name}#{attr}>#{content}</#{@name}>"
else
ret = "<#{@name}#{attr}/>"
end
ret << "\n" if parentNode.nodeType == DOCUMENT_NODE
ret
end
=begin
--- Element#dump(depth = 0)
dumps the Element.
=end
def dump(depth = 0)
attr = ''
@attr.each do |a| ## self.attributes do |a|
attr += a.to_s + ", "
end if @attr
attr.chop!
attr.chop!
print ' ' * depth * 2
print "#{@name}(#{attr})\n"
@children.each do |child|
child.dump(depth + 1)
end if @children
end
=begin
--- Element#tagName()
[DOM]
alias of nodeName.
=end
## [DOM]
alias tagName nodeName
=begin
--- Element#getAttribute(name)
[DOM]
retrieves an attribute value by name.
=end
## [DOM]
def getAttribute(name)
attr = getAttributeNode(name)
if attr.nil?
''
else
attr.nodeValue
end
end
=begin
--- Element#setAttribute(name, value)
[DOM]
adds a new attribute.
=end
## [DOM]
def setAttribute(name, value)
if @ownerElement
attr = @ownerDocument.createAttribute(name)
attr.appendChild(@ownerDocument.createTextNode(value))
else
attr = Attr.new(name)
attr.appendChild(Text.new(value))
end
setAttributeNode(attr)
end
=begin
--- Element#removeAttribute(name)
[DOM]
remove an attribute by name.
=end
## [DOM]
def removeAttribute(name)
ret = getAttributeNode(name)
removeAttributeNode(ret) if ret
end
=begin
--- Element#getAttributeNode(name)
[DOM]
retrieves an Attr node by name.
=end
## [DOM]
def getAttributeNode(name)
@attr.getNamedItem(name)
end
=begin
--- Element#setAttributeNode(newAttr)
[DOM]
adds a new attribute.
=end
## [DOM]
def setAttributeNode(newAttr)
ret = getAttributeNode(newAttr.nodeName)
if ret == newAttr
raise DOMException.new(DOMException::INUSE_ATTRIBUTE_ERR)
end
ret.ownerElement = nil if ret
@attr.setNamedItem(newAttr)
newAttr.ownerElement = self
ret
end
=begin
--- Element#removeAttributeNode(oldAttr)
[DOM]
removes the specified attribute.
=end
## [DOM]
def removeAttributeNode(oldAttr)
ret = getAttributeNode(oldAttr.nodeName)
if ret.nil? || ret != oldAttr
raise DOMException.new(DOMException::NOT_FOUND_ERR)
end
@attr.removeNamedItem(oldAttr.nodeName)
ret.ownerElement = nil
ret
end
=begin
--- Element#getElementsByTagName(tagname)
[DOM]
returns a NodeList of all descendant elements with given tag name.
=end
## [DOM] (but this is not "live")
def getElementsByTagName(tagname)
ret = NodeList.new
@children.each do |node|
if node.nodeType == ELEMENT_NODE
if tagname == '*' || node.nodeName == tagname
ret << node
end
ret << node.getElementsByTagName(tagname)
end
end if @children
ret
end
=begin
--- Element#normalize
[DOM]
puts all Text nodes in the full depth of the sub-tree under this
Eelemnt.
=end
## [DOM]
def normalize
return if @children.nil?
old = nil
children = @children.to_a.dup
children.each do |child|
if !old.nil? && old.nodeType == TEXT_NODE &&
child.nodeType == TEXT_NODE
old.appendData(child.nodeValue)
self.removeChild(child)
else
if child.nodeType == ELEMENT_NODE
child.normalize
end
old = child
end
end
end
=begin
--- Element#cloneNode(deep = true)
[DOM]
returns the copy of the Element.
=end
## [DOM]
def cloneNode(deep = true)
attrs = []
@attr.each do |attr|
attrs.push(attr.cloneNode(true))
end
super(deep, @name, attrs)
end
## get the list of nodeValues by IDs
## [experimental implement]
def _getIDVals(ids = nil)
if ids.nil?
doc = ownerDocument
return [] if doc.nil?
ids = doc._getIDAttrs
end
idelem = []
if !ids[nodeName].nil?
return attributes._getValues(ids[nodeName])
elsif !ids['*'].nil?
return attributes._getValues(ids['*'])
end
return []
end
=begin
--- Element#trim(preserve = false)
trim extra whitespaces.
=end
## trim extra whitespaces
## if attribute 'xml:space' is 'preserve',
## don't trim any white spaces
def trim(preserve = false)
if !attributes['xml:space'].nil?
value = attributes['xml:space'].nodeValue
if value == 'preserve'
preserve = true
elsif value == 'default'
preserve = false
end
end
return nil if @children.nil?
children = @children.to_a.dup
children.each do |child|
if !preserve && (child.nodeType == TEXT_NODE ||
child.nodeType == CDATA_SECTION_NODE)
if child.trim == ""
self.removeChild(child)
end
else
child.trim(preserve)
end
end
nil
end
## [DOM2]
def namespaceURI; @uri; end
## [DOM2]
def prefix; @prefix; end
## [DOM2]
def prefix=(prefix);
## to be checked
@prefix = prefix
@name = @prefix + ':' + @localname
@prefix.freeze
@name.freeze
end
## [DOM2]
def localname; @localname; end
## [DOM2]
def hasAttributes()
attributes.length > 0
end
## [DOM2]
def getAttributeNS(nsuri, localname)
attr = getAttributeNodeNS(nsuri, localname)
if attr.nil?
""
else
attr.nodeValue
end
end
## [DOM2]
def setAttributeNS(nsuri, qname, value)
if qname.index(':')
prefix, localname = qname.split(':')
raise DOMException.new(DOMException::NAMESPACE_ERR) if
nsuri.nil? or
(prefix == 'xml' and
nsuri != 'http://www.w3.org/XML/1998/namespace')
else
raise DOMException.new(DOMException::NAMESPACE_ERR) if
qname == 'xmlns' and
nsuri != 'http://www.w3.org/2000/xmlns/'
end
attr = @ownerDocument.createAttributeNS(nsuri, qname)
attr.appendChild(@ownerDocument.createTextNode(value))
setAttributeNodeNS(attr)
end
## [DOM2]
def removeAttributeNS(nsuri, localname)
ret = getAttributeNodeNS(nsuri, localname)
removeAttributeNode(ret) if ret
end
## [DOM2]
def getAttributeNodeNS(nsuri, localname)
attributes.each do |attr|
return attr if
attr.namespaceURI == nsuri && attr.localname == localname
end
nil
end
## [DOM2]
def setAttributeNodeNS(newAttr)
ret = getAttributeNodeNS(newAttr.namespaceURI, newAttr.localname)
removeAttributeNode(ret) if ret
setAttributeNode(newAttr)
ret
end
## [DOM2]
def getElementsByTagNameNS(nsuri, localname)
ret = NodeList.new
@children.each do |node|
if node.nodeType == ELEMENT_NODE
if (localname == '*' || node.localname == localname) and
(nsuri == '*' || node.namespaceURI == nsuri)
ret << node
end
ret << node.getElementsByTagNameNS(nsuri, localname)
end
end if @children
ret
end
## [DOM2]
def hasAttribute(name)
!getAttributeNode(name).nil?
end
## [DOM2]
def hasAttributeNS(nsuri, localname)
!getAttributeNodeNS(nsuri, localname).nil?
end
def idAttribute; @idAttribute; end
def idAttribute=(name); @idAttribute = name; end
def _checkNode(node)
unless node.nodeType == ELEMENT_NODE ||
node.nodeType == TEXT_NODE ||
node.nodeType == COMMENT_NODE ||
node.nodeType == PROCESSING_INSTRUCTION_NODE ||
node.nodeType == CDATA_SECTION_NODE ||
node.nodeType == ENTITY_REFERENCE_NODE
raise DOMException.new(DOMException::HIERARCHY_REQUEST_ERR)
end
end
end
end
end
|