Class: XSD::XMLParser::LibXMLParser

Inherits:
Parser show all
Includes:
LibXML::XML::SaxParser::Callbacks
Defined in:
lib/xsd/xmlparser/libxmlparser.rb

Constant Summary collapse

ENTITY_REF_MAP =
{
  'lt' => '<',
  'gt' => '>',
  'amp' => '&',
  'quot' => '"',
  'apos' => '\''
}

Instance Attribute Summary

Attributes inherited from Parser

#charset

Instance Method Summary collapse

Methods inherited from Parser

add_factory, create_parser, factory, #initialize, #parse

Constructor Details

This class inherits a constructor from XSD::XMLParser::Parser

Instance Method Details

#do_parse(string_or_readable) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/xsd/xmlparser/libxmlparser.rb', line 27

def do_parse(string_or_readable)
  $stderr.puts "XSD::XMLParser::LibXMLParser.do_parse" if $DEBUG    
  # string = string_or_readable.respond_to?(:read) ? string_or_readable.read : string_or_readable

  @charset = 'utf-8'
  string = StringIO.new(string_or_readable)
  parser = LibXML::XML::SaxParser.io(string)
  parser.callbacks = self
  parser.parse
end

#on_cdata_block(cdata) ⇒ Object



123
124
125
# File 'lib/xsd/xmlparser/libxmlparser.rb', line 123

def on_cdata_block(cdata)
  characters(cdata)
end

#on_characters(chars) ⇒ Object



99
100
101
# File 'lib/xsd/xmlparser/libxmlparser.rb', line 99

def on_characters(chars)
  characters(chars)
end

#on_end_element(name) ⇒ Object



91
92
93
# File 'lib/xsd/xmlparser/libxmlparser.rb', line 91

def on_end_element(name)
  # end_element(name)
end

#on_end_element_ns(name, prefix, uri) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/xsd/xmlparser/libxmlparser.rb', line 79

def on_end_element_ns (name, prefix, uri)
  if prefix.nil?
    end_element(name)
  else
    end_element("#{prefix}:#{name}")
  end
end

#on_external_subset(name, external_id, system_id) ⇒ Object



127
128
129
# File 'lib/xsd/xmlparser/libxmlparser.rb', line 127

def on_external_subset(name, external_id, system_id)
  nil
end

#on_parser_error(msg) ⇒ Object

Raises:



115
116
117
# File 'lib/xsd/xmlparser/libxmlparser.rb', line 115

def on_parser_error(msg)
  raise ParseError.new(msg)
end

#on_parser_fatal_error(msg) ⇒ Object

Raises:



119
120
121
# File 'lib/xsd/xmlparser/libxmlparser.rb', line 119

def on_parser_fatal_error(msg)
  raise ParseError.new(msg)
end

#on_parser_warning(msg) ⇒ Object

def on_comment(msg)

nil

end



111
112
113
# File 'lib/xsd/xmlparser/libxmlparser.rb', line 111

def on_parser_warning(msg)
  warn(msg)
end

#on_reference(name) ⇒ Object



95
96
97
# File 'lib/xsd/xmlparser/libxmlparser.rb', line 95

def on_reference(name)
  characters(ENTITY_REF_MAP[name])
end

#on_start_element(name, attr_hash) ⇒ Object



87
88
89
# File 'lib/xsd/xmlparser/libxmlparser.rb', line 87

def on_start_element (name, attr_hash)
  # start_element(name, attr_hash)
end

#on_start_element_ns(name, attr_hash, prefix, uri, namespaces) ⇒ Object

def on_end_document()

nil

end



70
71
72
73
74
75
76
77
# File 'lib/xsd/xmlparser/libxmlparser.rb', line 70

def on_start_element_ns (name, attr_hash, prefix, uri, namespaces)
  prefixed_ns = attr_hash.merge(Hash[namespaces.map{|k,v| ["xmlns:#{k}",v]}])
  if prefix.nil?
    start_element(name, prefixed_ns)
  else
    start_element("#{prefix}:#{name}", prefixed_ns)
  end
end