1 | # XSD4R - XMLScan XML parser library.
|
---|
2 | # Copyright (C) 2002, 2003, 2005 NAKAMURA, Hiroshi <[email protected]>.
|
---|
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 | p msg if $DEBUG
|
---|
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); end
|
---|
86 |
|
---|
87 | def on_etag(name)
|
---|
88 | end_element(name)
|
---|
89 | end
|
---|
90 |
|
---|
91 | def on_entityref(ref)
|
---|
92 | characters(ENTITY_REF_MAP[ref])
|
---|
93 | end
|
---|
94 |
|
---|
95 | def on_charref(code)
|
---|
96 | characters([code].pack('U'))
|
---|
97 | end
|
---|
98 |
|
---|
99 | def on_charref_hex(code)
|
---|
100 | on_charref(code)
|
---|
101 | end
|
---|
102 |
|
---|
103 | # def on_start_document; end
|
---|
104 |
|
---|
105 | # def on_end_document; end
|
---|
106 |
|
---|
107 | def on_stag(name)
|
---|
108 | @attrs = {}
|
---|
109 | end
|
---|
110 |
|
---|
111 | def on_attribute(name)
|
---|
112 | @attrs[name] = @curattr = ''
|
---|
113 | end
|
---|
114 |
|
---|
115 | def on_attr_value(str)
|
---|
116 | @curattr << str
|
---|
117 | end
|
---|
118 |
|
---|
119 | def on_attr_entityref(ref)
|
---|
120 | @curattr << ENTITY_REF_MAP[ref]
|
---|
121 | end
|
---|
122 |
|
---|
123 | def on_attr_charref(code)
|
---|
124 | @curattr << [code].pack('U')
|
---|
125 | end
|
---|
126 |
|
---|
127 | def on_attr_charref_hex(code)
|
---|
128 | on_attr_charref(code)
|
---|
129 | end
|
---|
130 |
|
---|
131 | # def on_attribute_end(name); end
|
---|
132 |
|
---|
133 | def on_stag_end_empty(name)
|
---|
134 | on_stag_end(name)
|
---|
135 | on_etag(name)
|
---|
136 | end
|
---|
137 |
|
---|
138 | def on_stag_end(name)
|
---|
139 | start_element(name, @attrs)
|
---|
140 | end
|
---|
141 |
|
---|
142 | add_factory(self)
|
---|
143 | end
|
---|
144 |
|
---|
145 |
|
---|
146 | end
|
---|
147 | end
|
---|