PageRenderTime 426ms CodeModel.GetById 419ms app.highlight 5ms RepoModel.GetById 0ms app.codeStats 1ms

/tools/Ruby/lib/ruby/1.8/xsd/xmlparser/parser.rb

http://github.com/agross/netopenspace
Ruby | 96 lines | 65 code | 25 blank | 6 comment | 3 complexity | 33a96b0c0162381379e63ca73da1225c MD5 | raw file
 1# XSD4R - XML Instance parser library.
 2# Copyright (C) 2002, 2003  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
 9require 'xsd/qname'
10require 'xsd/ns'
11require 'xsd/charset'
12
13
14module XSD
15module XMLParser
16
17
18class 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
26  @@parser_factory = nil
27
28  def self.factory
29    @@parser_factory
30  end
31
32  def self.create_parser(host, opt = {})
33    @@parser_factory.new(host, opt)
34  end
35
36  def self.add_factory(factory)
37    if $DEBUG
38      puts "Set #{ factory } as XML processor."
39    end
40    @@parser_factory = factory
41  end
42
43public
44
45  attr_accessor :charset
46
47  def initialize(host, opt = {})
48    @host = host
49    @charset = opt[:charset] || nil
50  end
51
52  def parse(string_or_readable)
53    @textbuf = ''
54    prologue
55    do_parse(string_or_readable)
56    epilogue
57  end
58
59private
60
61  def do_parse(string_or_readable)
62    raise NotImplementError.new(
63      'Method do_parse must be defined in derived class.')
64  end
65
66  def start_element(name, attrs)
67    @host.start_element(name, attrs)
68  end
69
70  def characters(text)
71    @host.characters(text)
72  end
73
74  def end_element(name)
75    @host.end_element(name)
76  end
77
78  def prologue
79  end
80
81  def epilogue
82  end
83
84  def xmldecl_encoding=(charset)
85    if @charset.nil?
86      @charset = charset
87    else
88      # Definition in a stream (like HTTP) has a priority.
89      p "encoding definition: #{ charset } is ignored." if $DEBUG
90    end
91  end
92end
93
94
95end
96end