PageRenderTime 94ms CodeModel.GetById 87ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://github.com/agross/netopenspace
Ruby | 50 lines | 32 code | 11 blank | 7 comment | 0 complexity | 657a6a0f228fa155bc6ede2137b6b2a5 MD5 | raw file
 1# XSD4R - XMLParser XML parser library.
 2# Copyright (C) 2001, 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/xmlparser'
10require 'xml/parser'
11
12
13module XSD
14module XMLParser
15
16
17class XMLParser < XSD::XMLParser::Parser
18  class Listener < XML::Parser
19    begin
20      require 'xml/encoding-ja'
21      include XML::Encoding_ja
22    rescue LoadError
23      # uconv may not be installed.
24    end
25  end
26
27  def do_parse(string_or_readable)
28    # XMLParser passes a String in utf-8.
29    @charset = 'utf-8'
30    @parser = Listener.new
31    @parser.parse(string_or_readable) do |type, name, data|
32      case type
33      when XML::Parser::START_ELEM
34	start_element(name, data)
35      when XML::Parser::END_ELEM
36	end_element(name)
37      when XML::Parser::CDATA
38	characters(data)
39      else
40	raise FormatDecodeError.new("Unexpected XML: #{ type }/#{ name }/#{ data }.")
41      end
42    end
43  end
44
45  add_factory(self)
46end
47
48
49end
50end