Class: XML::Parser::SAXDriver

Inherits:
Object
  • Object
show all
Includes:
SAX::AttributeList, SAX::Locator, SAX::Parser
Defined in:
lib/xml/saxdriver.rb

Defined Under Namespace

Classes: DummyLocator, SAXParser, URL

Instance Method Summary collapse

Constructor Details

#initializeSAXDriver

Returns a new instance of SAXDriver.



203
204
205
206
207
208
209
210
211
212
# File 'lib/xml/saxdriver.rb', line 203

def initialize
  handler = XML::SAX::HandlerBase.new
  @attributes = nil
  @documentHandler = handler
  @dtdHandler = handler
  @errorHandler = handler
  @entityResolver = handler
  @dataBuf = ''
  @locators = []
end

Instance Method Details

#character(data) ⇒ Object



265
266
267
# File 'lib/xml/saxdriver.rb', line 265

def character(data)
  @dataBuf << data
end

#endElement(name) ⇒ Object



269
270
271
272
# File 'lib/xml/saxdriver.rb', line 269

def endElement(name)
  flushData;
  @documentHandler.endElement(name)
end

#getColumnNumberObject

implementation of Locator



337
338
339
# File 'lib/xml/saxdriver.rb', line 337

def getColumnNumber
  @locators[-1].getColumnNumber
end

#getLengthObject

implementation of AttributeList



288
289
290
# File 'lib/xml/saxdriver.rb', line 288

def getLength
  @attributes.length
end

#getLineNumberObject

implementation of Locator



332
333
334
# File 'lib/xml/saxdriver.rb', line 332

def getLineNumber
  @locators[-1].getLineNumber
end

#getName(pos) ⇒ Object

implementation of AttributeList



293
294
295
# File 'lib/xml/saxdriver.rb', line 293

def getName(pos)
  @attributes.keys[pos]
end

#getPublicIdObject

implementation of Locator



322
323
324
# File 'lib/xml/saxdriver.rb', line 322

def getPublicId
  @locators[-1].getPublicId
end

#getSystemIdObject

implementation of Locator



327
328
329
# File 'lib/xml/saxdriver.rb', line 327

def getSystemId
  @locators[-1].getSystemId
end

#getType(pos) ⇒ Object

implementation of AttributeList



307
308
309
310
# File 'lib/xml/saxdriver.rb', line 307

def getType(pos)
  ## expat cannot get attribyte type
  return "CDATA"
end

#getValue(pos) ⇒ Object

implementation of AttributeList



298
299
300
301
302
303
304
# File 'lib/xml/saxdriver.rb', line 298

def getValue(pos)
  if pos.kind_of?(String)
    @attributes[pos]
  else
    @attributes.values[pos]
  end
end

#notationDecl(name, base, sysid, pubid) ⇒ Object



279
280
281
# File 'lib/xml/saxdriver.rb', line 279

def notationDecl(name, base, sysid, pubid)
  @dtdHandler.notationDecl(name, pubid, sysid)
end

#parse(sysid) ⇒ Object

implementation of Parser



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
# File 'lib/xml/saxdriver.rb', line 342

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

#popLocatorObject



317
318
319
# File 'lib/xml/saxdriver.rb', line 317

def popLocator
  @locators.pop
end

#processingInstruction(target, data) ⇒ Object



274
275
276
277
# File 'lib/xml/saxdriver.rb', line 274

def processingInstruction(target, data)
  flushData;
  @documentHandler.processingInstruction(target, data)
end

#pushLocator(locator) ⇒ Object

locator is DummyLoacator or SAXParser



313
314
315
# File 'lib/xml/saxdriver.rb', line 313

def pushLocator(locator)
  @locators.push(locator)
end

#setDocumentHandler(handler) ⇒ Object

implementation of Parser



223
224
225
226
227
228
# File 'lib/xml/saxdriver.rb', line 223

def setDocumentHandler(handler)
  if !handler.kind_of?(XML::SAX::DocumentHandler)
    raise TypeError.new("parameter error")
  end
  @documentHandler = handler
end

#setDTDHandler(handler) ⇒ Object

implementation of Parser



231
232
233
234
235
236
# File 'lib/xml/saxdriver.rb', line 231

def setDTDHandler(handler)
  if !handler.kind_of?(XML::SAX::DTDHandler)
    raise TypeError.new("parameter error")
  end
  @dtdHandler = handler
end

#setEntityResolver(handler) ⇒ Object

implementation of Parser



215
216
217
218
219
220
# File 'lib/xml/saxdriver.rb', line 215

def setEntityResolver(handler)
  if !handler.kind_of?(XML::SAX::EntityResolver)
    raise TypeError.new("parameter error")
  end
  @entityResolver = handler
end

#setErrorHandler(handler) ⇒ Object

implementation of Parser



239
240
241
242
243
244
# File 'lib/xml/saxdriver.rb', line 239

def setErrorHandler(handler)
  if !handler.kind_of?(XML::SAX::ErrorHandler)
    raise TypeError.new("parameter error")
  end
  @errorHandler = handler
end

#setLocale(locale) ⇒ Object

implementation of Parser

Raises:

  • (SAXException)


247
248
249
# File 'lib/xml/saxdriver.rb', line 247

def setLocale(locale)
  raise SAXException.new("locale not supported")
end

#startElement(name, attrs) ⇒ Object



259
260
261
262
263
# File 'lib/xml/saxdriver.rb', line 259

def startElement(name, attrs)
  flushData;
  @attributes = attrs
  @documentHandler.startElement(name, self)
end

#unparsedEntityDecl(name, base, sysid, pubid, notation) ⇒ Object



283
284
285
# File 'lib/xml/saxdriver.rb', line 283

def unparsedEntityDecl(name, base, sysid, pubid, notation)
  @dtdHandler.unparsedEntityDecl(name, pubid, sysid, notation)
end

#xmlOpen(base, systemId, publicId) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/xml/saxdriver.rb', line 184

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. == 'localhost'
    stream = open(file.urlpath)
    is = XML::SAX::InputSource.new(stream)
    is.setSystemId(file.to_s)
    is.setPublicId(publicId)
    return is
  end
end