| 1 |
# XSD4R - XML Instance 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/qname' |
|---|
| 10 |
require 'xsd/ns' |
|---|
| 11 |
require 'xsd/charset' |
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
module XSD |
|---|
| 15 |
module XMLParser |
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
class Parser |
|---|
| 19 |
class ParseError < Error; end |
|---|
| 20 |
class FormatDecodeError < ParseError; end |
|---|
| 21 |
class UnknownElementError < FormatDecodeError; end |
|---|
| 22 |
class UnknownAttributeError < FormatDecodeError; end |
|---|
| 23 |
class UnexpectedElementError < FormatDecodeError; end |
|---|
| 24 |
class ElementConstraintError < FormatDecodeError; end |
|---|
| 25 |
class ParserError < ParseError; end |
|---|
| 26 |
|
|---|
| 27 |
@@parser_factory = nil |
|---|
| 28 |
|
|---|
| 29 |
def self.factory |
|---|
| 30 |
@@parser_factory |
|---|
| 31 |
end |
|---|
| 32 |
|
|---|
| 33 |
def self.create_parser(host, opt = {}) |
|---|
| 34 |
unless @@parser_factory |
|---|
| 35 |
raise ParserError.new("illegal XML parser configuration") |
|---|
| 36 |
end |
|---|
| 37 |
@@parser_factory.new(host, opt) |
|---|
| 38 |
end |
|---|
| 39 |
|
|---|
| 40 |
def self.add_factory(factory) |
|---|
| 41 |
if $DEBUG |
|---|
| 42 |
puts "Set #{ factory } as XML processor." |
|---|
| 43 |
end |
|---|
| 44 |
@@parser_factory = factory |
|---|
| 45 |
end |
|---|
| 46 |
|
|---|
| 47 |
public |
|---|
| 48 |
|
|---|
| 49 |
attr_accessor :charset |
|---|
| 50 |
|
|---|
| 51 |
def initialize(host, opt = {}) |
|---|
| 52 |
@host = host |
|---|
| 53 |
@charset = opt[:charset] || nil |
|---|
| 54 |
end |
|---|
| 55 |
|
|---|
| 56 |
def parse(string_or_readable) |
|---|
| 57 |
@textbuf = '' |
|---|
| 58 |
prologue |
|---|
| 59 |
do_parse(string_or_readable) |
|---|
| 60 |
epilogue |
|---|
| 61 |
end |
|---|
| 62 |
|
|---|
| 63 |
private |
|---|
| 64 |
|
|---|
| 65 |
def do_parse(string_or_readable) |
|---|
| 66 |
raise ParserError.new( |
|---|
| 67 |
'Method do_parse must be defined in derived class.') |
|---|
| 68 |
end |
|---|
| 69 |
|
|---|
| 70 |
def start_element(name, attrs) |
|---|
| 71 |
@host.start_element(name, attrs) |
|---|
| 72 |
end |
|---|
| 73 |
|
|---|
| 74 |
def characters(text) |
|---|
| 75 |
@host.characters(text) |
|---|
| 76 |
end |
|---|
| 77 |
|
|---|
| 78 |
def end_element(name) |
|---|
| 79 |
@host.end_element(name) |
|---|
| 80 |
end |
|---|
| 81 |
|
|---|
| 82 |
def prologue |
|---|
| 83 |
end |
|---|
| 84 |
|
|---|
| 85 |
def epilogue |
|---|
| 86 |
end |
|---|
| 87 |
|
|---|
| 88 |
def xmldecl_encoding=(charset) |
|---|
| 89 |
if @charset.nil? |
|---|
| 90 |
@charset = charset |
|---|
| 91 |
else |
|---|
| 92 |
# Definition in a stream (like HTTP) has a priority. |
|---|
| 93 |
p "encoding definition: #{ charset } is ignored." if $DEBUG |
|---|
| 94 |
end |
|---|
| 95 |
end |
|---|
| 96 |
end |
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
end |
|---|
| 100 |
end |
|---|