class XML::DOM::DOMBuilder

Class XML::DOM::DOMBuilder

superclass

XML::Parser

Constants

NSSEP

Attributes

createCDATASection[RW]
createEntityReference[RW]

Public Class Methods

new(document = nil, *args) click to toggle source

Class Methods

Calls superclass method XML::ParserNS::new
# File lib/xml/dom2/dombuilder.rb, line 50
def self.new(document = nil, *args)
  document ||= Document.new
  if args[0].is_a?(self.class)
    ret = super(*args)
    ret.__initialize__(document, true)
  else
    ret = super(args[0], NSSEP)
    ret.__initialize__(document, false)
  end
  ret.setReturnNSTriplet(true)
  ret
end

Public Instance Methods

__initialize__(document, external = false) click to toggle source
# File lib/xml/dom2/dombuilder.rb, line 63
def __initialize__(document, external = false)
  @tree = nil
  @entityResolver = DOMEntityResolverImpl.new
  @createCDATASection = false
  @createEntityReference = false
  @document = document
  @external = external
end
character(data) click to toggle source
# File lib/xml/dom2/dombuilder.rb, line 176
      def character(data)
##        if @cdata_f
          @cdata_buf << data
##        else
##          cdata = @document.createTextNode(data)
##          @current.appendChild(cdata)
##        end
      end
comment(data) click to toggle source
# File lib/xml/dom2/dombuilder.rb, line 235
def comment(data)
  text
  comment = @document.createComment(data)
  @current.appendChild(comment)
end
endCdata() click to toggle source
# File lib/xml/dom2/dombuilder.rb, line 227
def endCdata
  return unless @createCDATASection
  cdata = @document.createCDATASection(@cdata_buf)
  @current.appendChild(cdata)
  @cdata_buf = ''
  @cdata_f = false
end
endElement(name) click to toggle source
# File lib/xml/dom2/dombuilder.rb, line 171
def endElement(name)
  text
  @current = @current.parentNode
end
entityResolver() click to toggle source
DOM3?
# File lib/xml/dom2/dombuilder.rb, line 267
def entityResolver; @entityResolver; end
entityResolver=(resolver) click to toggle source
# File lib/xml/dom2/dombuilder.rb, line 268
def entityResolver=(resolver)
  raise ArgumentError, 'invalid value for DOMEntityResolver' unless
    resolver.is_a?(DOMEntityResolver)
  @entityResolver = resolver
end
externalEntityRef(context, base, systemId, publicId) click to toggle source
# File lib/xml/dom2/dombuilder.rb, line 191
def externalEntityRef(context, base, systemId, publicId)
  text
  tree = nil
  if @parse_ext
    extp = self.class.new(@document, self, context)
    extp.setBase(base) if base
    file = systemId
    if systemId !~ /^\/|^\.|^http:|^ftp:/ && !base.nil? # /
      file = base + systemId
    end
    begin
      xml = @entityResolver.resolveEntity(nil, file).byteStream.read
      tree = extp.parse(xml, @parse_ext)
    rescue XML::Parser::Error
      raise XML::Parser::Error.new("#{systemId}(#{extp.line}): #{$!}")
    rescue Errno::ENOENT
      raise
    end
    extp.done
  end
  if @createEntityReference
    entref = @document.createEntityReference(context)
    @current.appendChild(entref)
    entref.appendChild(tree) if tree
  else
    @current.appendChild(tree) if tree
  end
end
parse(xml, parse_ext = false) click to toggle source

Methods

Calls superclass method XML::ParserNS#parse
# File lib/xml/dom2/dombuilder.rb, line 89
def parse(xml, parse_ext = false)
  if @external
    @tree = @document.createDocumentFragment
  else
    @tree = @document
  end
  @parse_ext = parse_ext
  @current = @tree
  @inDocDecl = 0
  @decl = ""
  @inDecl = 0
  @idRest = 0
  @extID = nil
  @cdata_f = false
  @cdata_buf = ''
  @nsdecl = []
  super(xml)
  @tree
end
parseURI(uri) click to toggle source
# File lib/xml/dom2/dombuilder.rb, line 109
def parseURI(uri)
  uri =~ /^((\w+):\/\/.+\/).*$/ ## /
  setBase($1) if $1
  xml = @entityResolver.resolveEntity(nil, uri).byteStream.read
  parse(xml, true)
end
processingInstruction(name, data) click to toggle source
# File lib/xml/dom2/dombuilder.rb, line 185
def processingInstruction(name, data)
  text
  pi = @document.createProcessingInstruction(name, data)
  @current.appendChild(pi)
end
startCdata() click to toggle source
# File lib/xml/dom2/dombuilder.rb, line 220
      def startCdata
        return unless @createCDATASection
        text
        @cdata_f = true
##        @cdata_buf = ''
      end
startDoctypeDecl(name, pubid, sysid, internal_subset) click to toggle source
# File lib/xml/dom2/dombuilder.rb, line 241
def startDoctypeDecl(name, pubid, sysid, internal_subset)
  doctype = @document.implementation.createDocumentType(name,
                                                        pubid, sysid)
  @current.appendChild(doctype)
end
startElement(name, data) click to toggle source
# File lib/xml/dom2/dombuilder.rb, line 123
      def startElement(name, data)
        text
        if !name.index(NSSEP)
          qname = name
          uri = nil
        else
          uri, localname, prefix = name.split(NSSEP)
          if prefix.nil?
            qname = localname
          else
            qname = prefix + ':' + localname
          end
        end
        elem = @document.createElementNS(uri, qname)

        @nsdecl.each do |nsdecl|
          elem.setAttributeNode(nsdecl)
        end
        @nsdecl = []

        attr = {}
        specified = getSpecifiedAttributes
        ## not implemented
        ## elem.idAttribute = getIdAttribute

        data.each do |key, value|
          if !key.index(NSSEP)
            qname = key
            uri = nil
          else
            uri, localname, prefix = key.split(NSSEP)
            if prefix.nil?
              qname = localname
            else
              qname = prefix + ':' + localname
            end
          end
          attr = @document.createAttributeNS(uri, qname)
          attr.appendChild(@document.createTextNode(value))
##          attr.specified = specified[key]
          attr.specified = specified.include?(key)
          elem.setAttributeNode(attr)
        end

        @current.appendChild(elem)
        @current = elem
      end
startNamespaceDecl(prefix, uri) click to toggle source
# File lib/xml/dom2/dombuilder.rb, line 247
def startNamespaceDecl(prefix, uri)
  qname = 'xmlns'
  if prefix
    qname << ':' + prefix
  end
  attr = @document.createAttributeNS(nil, qname)
  attr.appendChild(@document.createTextNode(uri))
  attr.specified = true
  @nsdecl << attr
end
text() click to toggle source
# File lib/xml/dom2/dombuilder.rb, line 116
def text
  return if @cdata_buf == ''
  textnode = @document.createTextNode(@cdata_buf)
  @current.appendChild(textnode)
  @cdata_buf = ''
end