class XML::Parser::SAXDriver

Public Class Methods

new() click to toggle source
# File lib/xml/saxdriver.rb, line 202
def initialize
  handler = XML::SAX::HandlerBase.new
  @attributes = nil
  @documentHandler = handler
  @dtdHandler = handler
  @errorHandler = handler
  @entityResolver = handler
  @dataBuf = ''
  @locators = []
end

Public Instance Methods

character(data) click to toggle source
# File lib/xml/saxdriver.rb, line 264
def character(data)
  @dataBuf << data
end
endElement(name) click to toggle source
# File lib/xml/saxdriver.rb, line 268
def endElement(name)
  flushData;
  @documentHandler.endElement(name)
end
getColumnNumber() click to toggle source

implementation of Locator

# File lib/xml/saxdriver.rb, line 336
def getColumnNumber
  @locators[-1].getColumnNumber
end
getLength() click to toggle source

implementation of AttributeList

# File lib/xml/saxdriver.rb, line 287
def getLength
  @attributes.length
end
getLineNumber() click to toggle source

implementation of Locator

# File lib/xml/saxdriver.rb, line 331
def getLineNumber
  @locators[-1].getLineNumber
end
getName(pos) click to toggle source

implementation of AttributeList

# File lib/xml/saxdriver.rb, line 292
def getName(pos)
  @attributes.keys[pos]
end
getPublicId() click to toggle source

implementation of Locator

# File lib/xml/saxdriver.rb, line 321
def getPublicId
  @locators[-1].getPublicId
end
getSystemId() click to toggle source

implementation of Locator

# File lib/xml/saxdriver.rb, line 326
def getSystemId
  @locators[-1].getSystemId
end
getType(pos) click to toggle source

implementation of AttributeList

# File lib/xml/saxdriver.rb, line 306
def getType(pos)
  ## expat cannot get attribyte type
  return "CDATA"
end
getValue(pos) click to toggle source

implementation of AttributeList

# File lib/xml/saxdriver.rb, line 297
def getValue(pos)
  if pos.kind_of?(String)
    @attributes[pos]
  else
    @attributes.values[pos]
  end
end
notationDecl(name, base, sysid, pubid) click to toggle source
# File lib/xml/saxdriver.rb, line 278
def notationDecl(name, base, sysid, pubid)
  @dtdHandler.notationDecl(name, pubid, sysid)
end
parse(sysid) click to toggle source

implementation of Parser

# File lib/xml/saxdriver.rb, line 341
def parse(sysid)
  @documentHandler.setDocumentLocator(self)
  if sysid.kind_of?(XML::SAX::InputSource)
    inputSource = openInputStream(sysid.dup)
  else
    inputSource = openInputStream(XML::SAX::InputSource.new(sysid))
  end
  encoding = inputSource.getEncoding
  if encoding
    parser = SAXParser.new(self, encoding)
  else
    parser = SAXParser.new(self)
  end

  pushLocator(DummyLocator.new(inputSource.getSystemId))
  begin
    @documentHandler.startDocument
    parser.parse(inputSource)
    @documentHandler.endDocument
  rescue XML::Parser::Error
    @errorHandler.fatalError(XML::SAX::SAXParseException.new($!.to_s,
                                                             self))
  rescue
    @errorHandler.fatalError($!)
  end
end
popLocator() click to toggle source
# File lib/xml/saxdriver.rb, line 316
def popLocator
  @locators.pop
end
processingInstruction(target, data) click to toggle source
# File lib/xml/saxdriver.rb, line 273
def processingInstruction(target, data)
  flushData;
  @documentHandler.processingInstruction(target, data)
end
pushLocator(locator) click to toggle source

locator is DummyLoacator or SAXParser

# File lib/xml/saxdriver.rb, line 312
def pushLocator(locator)
  @locators.push(locator)
end
setDTDHandler(handler) click to toggle source

implementation of Parser

# File lib/xml/saxdriver.rb, line 230
def setDTDHandler(handler)
  if !handler.kind_of?(XML::SAX::DTDHandler)
    raise TypeError.new("parameter error")
  end
  @dtdHandler = handler
end
setDocumentHandler(handler) click to toggle source

implementation of Parser

# File lib/xml/saxdriver.rb, line 222
def setDocumentHandler(handler)
  if !handler.kind_of?(XML::SAX::DocumentHandler)
    raise TypeError.new("parameter error")
  end
  @documentHandler = handler
end
setEntityResolver(handler) click to toggle source

implementation of Parser

# File lib/xml/saxdriver.rb, line 214
def setEntityResolver(handler)
  if !handler.kind_of?(XML::SAX::EntityResolver)
    raise TypeError.new("parameter error")
  end
  @entityResolver = handler
end
setErrorHandler(handler) click to toggle source

implementation of Parser

# File lib/xml/saxdriver.rb, line 238
def setErrorHandler(handler)
  if !handler.kind_of?(XML::SAX::ErrorHandler)
    raise TypeError.new("parameter error")
  end
  @errorHandler = handler
end
setLocale(locale) click to toggle source

implementation of Parser

# File lib/xml/saxdriver.rb, line 246
def setLocale(locale)
  raise SAXException.new("locale not supported")
end
startElement(name, attrs) click to toggle source
# File lib/xml/saxdriver.rb, line 258
def startElement(name, attrs)
  flushData;
  @attributes = attrs
  @documentHandler.startElement(name, self)
end
unparsedEntityDecl(name, base, sysid, pubid, notation) click to toggle source
# File lib/xml/saxdriver.rb, line 282
def unparsedEntityDecl(name, base, sysid, pubid, notation)
  @dtdHandler.unparsedEntityDecl(name, pubid, sysid, notation)
end
xmlOpen(base, systemId, publicId) click to toggle source
# File lib/xml/saxdriver.rb, line 183
def xmlOpen(base, systemId, publicId)
  if base.nil? || base == ""
    file = URL.new(systemId)
  else
    file = URL.new(URL.new(base), systemId)
  end
  if !@entityResolver.nil?
    stream = @entityResolver.resolveEntity(file.to_s, publicId)
    return openInputStream(stream) if stream
  end
  if file.scheme == 'file' && file.login == 'localhost'
    stream = open(file.urlpath)
    is = XML::SAX::InputSource.new(stream)
    is.setSystemId(file.to_s)
    is.setPublicId(publicId)
    return is
  end
end

Private Instance Methods

flushData() click to toggle source
# File lib/xml/saxdriver.rb, line 250
def flushData
  if @dataBuf.length > 0
    @documentHandler.characters(@dataBuf, 0, @dataBuf.length)
    @dataBuf = ''
  end
end
openInputStream(stream) click to toggle source

open stream if it is not opened

# File lib/xml/saxdriver.rb, line 168
def openInputStream(stream)
  if stream.getByteStream
    return stream
  else stream.getSystemId
    url = URL.new(stream.getSystemId)
    if url.scheme == 'file' && url.login == 'localhost'
      s = open(url.urlpath)
      stream.setByteStream(s)
      return stream
    end
  end
  return nil
end