File: saxtest.rb

package info (click to toggle)
libxml-parser-ruby 0.6.8-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 908 kB
  • ctags: 1,524
  • sloc: ruby: 11,080; ansic: 1,958; xml: 467; makefile: 69
file content (70 lines) | stat: -rwxr-xr-x 1,547 bytes parent folder | download | duplicates (3)
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
#! /usr/local/bin/ruby

## SAX test
## 1999 yoshidam

require 'xml/saxdriver'

class TestHandler < XML::SAX::HandlerBase
  def getPos
    [@locator.getSystemId, @locator.getLineNumber]
  end

  def getAttrs(attrs)
    ret = []
    for i in 0...attrs.getLength
      ret .push([attrs.getName(i), attrs.getValue(i)])
    end
    ret
  end

  def startDocument
    p ["startDocument", getPos]
  end
  def endDocument
    p ["endDocument", getPos]
  end
  def startElement(name, attr)
    p ["startElement", name, getAttrs(attr), getPos]
  end
  def endElement(name)
    p ["endElement", name, getPos]
  end
  def characters(ch, start, length)
    p ["characters", ch[start, length], getPos]
  end
  def processingInstruction(target, data)
    p ["processingInstruction", target, data, getPos]
  end
  def notationDecl(name, pubid, sysid)
    p ["notationDecl", name, pubid, sysid, getPos]
  end
  def unparsedEntityDecl(name, pubid, sysid, notation)
    p ["unparsedEntityDecl", name, pubid, sysid, notation, getPos]
  end

  def resolveEntity(pubid, sysid)
    p ["resolveEntity", pubid, sysid]
  end

  def setDocumentLocator(loc)
    @locator = loc
  end

  def fatalError(e)
    print "*** FATAL ERROR ***\n"
    raise e
  end
end

p = XML::SAX::Helpers::ParserFactory.makeParser("XML::Parser::SAXDriver")
h = TestHandler.new
p.setDocumentHandler(h)
p.setDTDHandler(h)
p.setEntityResolver(h)
p.setErrorHandler(h)
begin
  p.parse(ARGV[0])
rescue XML::SAX::SAXParseException
  p(["ParseError", $!.getSystemId, $!.getLineNumber, $!.getMessage])
end