| 1 |
# XSD4R - XMLScan XML parser library. |
|---|
| 2 |
# Copyright (C) 2000-2007 NAKAMURA, Hiroshi <nahi@ruby-lang.org>. |
|---|
| 3 |
|
|---|
| 4 |
# This program is copyrighted free software by NAKAMURA, Hiroshi. You can |
|---|
| 5 |
# redistribute it and/or modify it under the same terms of Ruby's license; |
|---|
| 6 |
# either the dual license version in 2003, or any later version. |
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
require 'xsd/xmlparser' |
|---|
| 10 |
require 'xmlscan/scanner' |
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
module XSD |
|---|
| 14 |
module XMLParser |
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
class XMLScanner < XSD::XMLParser::Parser |
|---|
| 18 |
include XMLScan::Visitor |
|---|
| 19 |
|
|---|
| 20 |
def do_parse(string_or_readable) |
|---|
| 21 |
@attrs = {} |
|---|
| 22 |
@curattr = nil |
|---|
| 23 |
@scanner = XMLScan::XMLScanner.new(self) |
|---|
| 24 |
@scanner.kcode = XSD::Charset.charset_str(charset) if charset |
|---|
| 25 |
@scanner.parse(string_or_readable) |
|---|
| 26 |
end |
|---|
| 27 |
|
|---|
| 28 |
def scanner_kcode=(charset) |
|---|
| 29 |
@scanner.kcode = XSD::Charset.charset_str(charset) if charset |
|---|
| 30 |
self.xmldecl_encoding = charset |
|---|
| 31 |
end |
|---|
| 32 |
|
|---|
| 33 |
ENTITY_REF_MAP = { |
|---|
| 34 |
'lt' => '<', |
|---|
| 35 |
'gt' => '>', |
|---|
| 36 |
'amp' => '&', |
|---|
| 37 |
'quot' => '"', |
|---|
| 38 |
'apos' => '\'' |
|---|
| 39 |
} |
|---|
| 40 |
|
|---|
| 41 |
def parse_error(msg) |
|---|
| 42 |
raise ParseError.new(msg) |
|---|
| 43 |
end |
|---|
| 44 |
|
|---|
| 45 |
def wellformed_error(msg) |
|---|
| 46 |
raise NotWellFormedError.new(msg) |
|---|
| 47 |
end |
|---|
| 48 |
|
|---|
| 49 |
def valid_error(msg) |
|---|
| 50 |
raise NotValidError.new(msg) |
|---|
| 51 |
end |
|---|
| 52 |
|
|---|
| 53 |
def warning(msg) |
|---|
| 54 |
warn(msg) |
|---|
| 55 |
end |
|---|
| 56 |
|
|---|
| 57 |
# def on_xmldecl; end |
|---|
| 58 |
|
|---|
| 59 |
def on_xmldecl_version(str) |
|---|
| 60 |
# 1.0 expected. |
|---|
| 61 |
end |
|---|
| 62 |
|
|---|
| 63 |
def on_xmldecl_encoding(str) |
|---|
| 64 |
self.scanner_kcode = str |
|---|
| 65 |
end |
|---|
| 66 |
|
|---|
| 67 |
# def on_xmldecl_standalone(str); end |
|---|
| 68 |
|
|---|
| 69 |
# def on_xmldecl_other(name, value); end |
|---|
| 70 |
|
|---|
| 71 |
# def on_xmldecl_end; end |
|---|
| 72 |
|
|---|
| 73 |
# def on_doctype(root, pubid, sysid); end |
|---|
| 74 |
|
|---|
| 75 |
# def on_prolog_space(str); end |
|---|
| 76 |
|
|---|
| 77 |
# def on_comment(str); end |
|---|
| 78 |
|
|---|
| 79 |
# def on_pi(target, pi); end |
|---|
| 80 |
|
|---|
| 81 |
def on_chardata(str) |
|---|
| 82 |
characters(str) |
|---|
| 83 |
end |
|---|
| 84 |
|
|---|
| 85 |
def on_cdata(str) |
|---|
| 86 |
characters(str) |
|---|
| 87 |
end |
|---|
| 88 |
|
|---|
| 89 |
def on_etag(name) |
|---|
| 90 |
end_element(name) |
|---|
| 91 |
end |
|---|
| 92 |
|
|---|
| 93 |
def on_entityref(ref) |
|---|
| 94 |
characters(ENTITY_REF_MAP[ref]) |
|---|
| 95 |
end |
|---|
| 96 |
|
|---|
| 97 |
def on_charref(code) |
|---|
| 98 |
characters([code].pack('U')) |
|---|
| 99 |
end |
|---|
| 100 |
|
|---|
| 101 |
def on_charref_hex(code) |
|---|
| 102 |
on_charref(code) |
|---|
| 103 |
end |
|---|
| 104 |
|
|---|
| 105 |
# def on_start_document; end |
|---|
| 106 |
|
|---|
| 107 |
# def on_end_document; end |
|---|
| 108 |
|
|---|
| 109 |
def on_stag(name) |
|---|
| 110 |
@attrs = {} |
|---|
| 111 |
end |
|---|
| 112 |
|
|---|
| 113 |
def on_attribute(name) |
|---|
| 114 |
@attrs[name] = @curattr = '' |
|---|
| 115 |
end |
|---|
| 116 |
|
|---|
| 117 |
def on_attr_value(str) |
|---|
| 118 |
@curattr << str |
|---|
| 119 |
end |
|---|
| 120 |
|
|---|
| 121 |
def on_attr_entityref(ref) |
|---|
| 122 |
@curattr << ENTITY_REF_MAP[ref] |
|---|
| 123 |
end |
|---|
| 124 |
|
|---|
| 125 |
def on_attr_charref(code) |
|---|
| 126 |
@curattr << [code].pack('U') |
|---|
| 127 |
end |
|---|
| 128 |
|
|---|
| 129 |
def on_attr_charref_hex(code) |
|---|
| 130 |
on_attr_charref(code) |
|---|
| 131 |
end |
|---|
| 132 |
|
|---|
| 133 |
# def on_attribute_end(name); end |
|---|
| 134 |
|
|---|
| 135 |
def on_stag_end_empty(name) |
|---|
| 136 |
on_stag_end(name) |
|---|
| 137 |
on_etag(name) |
|---|
| 138 |
end |
|---|
| 139 |
|
|---|
| 140 |
def on_stag_end(name) |
|---|
| 141 |
start_element(name, @attrs) |
|---|
| 142 |
end |
|---|
| 143 |
|
|---|
| 144 |
add_factory(self) |
|---|
| 145 |
end |
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 |
end |
|---|
| 149 |
end |
|---|