Class: MultiSAX::SAX
- Inherits:
-
Object
- Object
- MultiSAX::SAX
- Defined in:
- lib/multisax.rb
Overview
The class to handle XML libraries.
Instance Method Summary collapse
-
#initialize ⇒ SAX
constructor
constructor.
-
#open(*list) ⇒ Object
Library loader.
-
#parse(source, listener) ⇒ Object
The main parsing method.
-
#parsefile(filename, listener) ⇒ Object
Parses file as XML.
-
#parser ⇒ Object
Returns which module is actually chosen.
-
#reset ⇒ Object
Reset MultiSAX state so that you can re-open() another library.
Constructor Details
#initialize ⇒ SAX
constructor. list cannot be passed directly, since you should check the retval of open().
26 27 28 |
# File 'lib/multisax.rb', line 26 def initialize @parser=nil end |
Instance Method Details
#open(*list) ⇒ Object
Library loader. Arguments are list (or Array) of libraries.
if list is empty or :XML, the following are searched (order by speed):
:ox, :libxml, :xmlparser, :nokogiri, :oga, :rexmlstream, :rexmlsax2
if list is :HTML, the following are searched (order by speed):
:oxhtml, :nokogirihtml, :ogahtml
You can also specify libraries individually.
If multiple selected, MultiSAX will try the libraries one by one and use the first usable one.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/multisax.rb', line 37 def open(*list) return @parser if @parser list=XML_PARSERS_INSTALLABLE+XML_PARSERS_DEFAULT if list.empty?||list==[:XML] list=HTML_PARSERS_INSTALLABLE if list==[:HTML] list.each{|e_module| case e_module when :ox,:oxhtml begin require 'ox' require 'stringio' #this should be standard module. rescue LoadError;next end @parser=e_module @saxhelper=Class.new(::Ox::Sax){ def __init__(obj) @obj=obj @saxwrapper_tag=nil @saxwrapper_attr={} self end def end_element(tag) @obj.sax_tag_end(tag.to_s) end def cdata(txt) @obj.sax_cdata(txt) end def text(txt) @obj.sax_text(txt) end def comment(txt) @obj.sax_comment(txt) end def start_element(tag) if @after_error @obj.sax_tag_start(tag.to_s,{}) @after_error=false else # I hope provided Listener's sax_tag_start will NOT be used elsewhere. #alias :attrs_done :attrs_done_normal @saxwrapper_tag=tag @saxwrapper_attr={} end end def attr(name,str) @saxwrapper_attr[name.to_s]=str end def attrs_done_xmldecl @obj.sax_xmldecl(@saxwrapper_attr['version'],@saxwrapper_attr['encoding'],@saxwrapper_attr['standalone']) end def attrs_done_normal @obj.sax_tag_start(@saxwrapper_tag.to_s,@saxwrapper_attr) end def attrs_done @saxwrapper_tag ? attrs_done_normal : attrs_done_xmldecl end def error(s,i,j) @after_error=true if s.end_with?('closed but not opened') end } break when :libxml begin require 'libxml' rescue LoadError;next end @parser=e_module @saxhelper=Class.new{ include ::LibXML::XML::SaxParser::Callbacks def __init__(obj) @obj=obj self end def on_start_element(tag,attrs) @obj.sax_tag_start(tag,attrs) end def on_end_element(tag) @obj.sax_tag_end(tag) end def on_characters(txt) @obj.sax_text(txt) end def on_cdata_block(txt) @obj.sax_cdata(txt) end def on_comment(txt) @obj.sax_comment(txt) end #actually unused def xmldecl(version,encoding,standalone) @obj.sax_xmldecl(version,encoding,standalone) end } break when :nokogiri,:nokogirihtml #nokogiri 1.5.x are supported on Ruby 1.8.7. #next if RUBY_VERSION<'1.9' begin require 'nokogiri' rescue LoadError;next end @parser=e_module @saxhelper=Class.new(::Nokogiri::XML::SAX::Document){ def __init__(obj) @obj=obj self end def start_element(tag,attrs) @obj.sax_tag_start(tag,attrs.is_a?(Array) ? Hash[*attrs.flatten(1)] : attrs) end def end_element(tag) @obj.sax_tag_end(tag) end def characters(txt) @obj.sax_text(txt) end def cdata_block(txt) @obj.sax_cdata(txt) end def comment(txt) @obj.sax_comment(txt) end def xmldecl(version,encoding,standalone) @obj.sax_xmldecl(version,encoding,standalone) end } break when :xmlparser begin require 'xml/saxdriver' rescue LoadError;next end @parser=e_module @saxhelper=Class.new(::XML::Parser){ def __init__(obj) @obj=obj @cdata=false self end def startElement(tag,attrs) @obj.sax_tag_start(tag,attrs) end def endElement(tag) @obj.sax_tag_end(tag) end def comment(txt) @obj.sax_comment(txt) end def xmlDecl(version,encoding,standalone) @obj.sax_xmldecl(version,encoding,standalone) end def character(txt) if @cdata @obj.sax_cdata(txt) else @obj.sax_text(txt) end end def startCdata @cdata=true end def endCdata @cdata=false end } break when :oga,:ogahtml next if RUBY_VERSION<'1.9' begin require 'oga' rescue LoadError;next end @parser=e_module @saxhelper=Class.new{ def __init__(obj) @obj=obj self end def on_element(ns,tag,attrs) tag_name=(ns ? (ns+':') : '')+tag @obj.sax_tag_start(tag_name,Hash[*attrs.flatten(1)]) return tag_name end def after_element(ns,tag) tag_name=(ns ? (ns+':') : '')+tag @obj.sax_tag_end(tag_name) end def on_text(txt) @obj.sax_text(txt) end def on_cdata(txt) @obj.sax_cdata(txt) end def on_comment(txt) @obj.sax_comment(txt) end def on_xml_decl(args) attrs=Hash[*args.flatten(1)] @obj.sax_xmldecl(attrs['version'],attrs['encoding'],attrs['standalone']) end } break # :nocov: when :xerces begin require 'Xerces' rescue LoadError;next end @parser=e_module @saxhelper=Class.new(::XercesR::DocumentHandler){ def __init__(obj) @obj=obj #@cdata=false self end def startElement(tag,attrs) @obj.sax_tag_start(tag,Hash[*attrs.getLength.times.map{|i|[attrs.getName(i),attrs.getValue(i)]}.flatten(1)]) end def endElement(tag) @obj.sax_tag_end(tag) end def comment(txt) @obj.sax_comment(txt) end #def xmlDecl(version,encoding,standalone) @obj.sax_xmldecl(version,encoding,standalone) end #def notationDecl(name, publicId, systemId) end def characters(txt,len) #if @cdata @obj.sax_cdata(txt) #else @obj.sax_text(txt) #end end #def startCdata # @cdata=true #end #def endCdata # @cdata=false #end } break # :nocov: when :rexmlstream begin require 'rexml/parsers/baseparser' require 'rexml/parsers/streamparser' require 'rexml/streamlistener' rescue LoadError;next end @parser=e_module @saxhelper=Class.new{ include ::REXML::StreamListener def __init__(obj) @obj=obj self end def tag_start(tag,attrs) @obj.sax_tag_start(tag,attrs) end def tag_end(tag) @obj.sax_tag_end(tag) end def text(txt) @obj.sax_text(txt) end def cdata(txt) @obj.sax_cdata(txt) end def comment(txt) @obj.sax_comment(txt) end def xmldecl(version,encoding,standalone) @obj.sax_xmldecl(version,encoding,standalone) end } break when :rexmlsax2 begin require 'rexml/parsers/sax2parser' require 'rexml/sax2listener' rescue LoadError;next end @parser=e_module @saxhelper=Class.new{ include ::REXML::SAX2Listener def __init__(obj) @obj=obj self end def start_element(uri,tag,qname,attrs) @obj.sax_tag_start(qname,attrs) end def end_element(uri,tag,qname) @obj.sax_tag_end(qname) end def characters(txt) @obj.sax_text(txt) end def cdata(txt) @obj.sax_cdata(txt) end def comment(txt) @obj.sax_comment(txt) end def xmldecl(version,encoding,standalone) @obj.sax_xmldecl(version,encoding,standalone) end } break end } return @parser end |
#parse(source, listener) ⇒ Object
The main parsing method. Listener can be Class.newMultiSAX::Callbacks.new. Returns the listener after SAX is applied. If you have not called open(), this will call it using default value (all libraries).
From 0.0.1, source can be IO as well as String.
SAX's listeners are usually modified destructively.
So instances shouldn't be provided.
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/multisax.rb', line 275 def parse(source,listener) if !@parser && !open raise "Failed to open SAX library. REXML, which is a standard Ruby module, might be also corrupted." end saxhelper=@saxhelper.new.__init__(listener) if source.is_a?(String) case @parser when :ox then Ox.sax_parse(saxhelper,StringIO.new(source),:convert_special=>true) when :oxhtml then Ox.sax_parse(saxhelper,StringIO.new(source),:convert_special=>true,:smart=>true) when :libxml then parser=LibXML::XML::SaxParser.string(source);parser.callbacks=saxhelper;parser.parse when :nokogiri then parser=Nokogiri::XML::SAX::Parser.new(saxhelper);parser.parse(source) when :nokogirihtml then parser=Nokogiri::HTML::SAX::Parser.new(saxhelper);parser.parse(source) when :xmlparser then saxhelper.parse(source) when :oga then parser=Oga::XML::SaxParser.new(saxhelper,source);parser.parse when :ogahtml then parser=Oga::HTML::SaxParser.new(saxhelper,source);parser.parse when :xerces then parser=XercesR::SAXParser.new;parser.setDocumentHandler(saxhelper);parser.parsebuf(source) when :rexmlstream then REXML::Parsers::StreamParser.new(source,saxhelper).parse when :rexmlsax2 then parser=REXML::Parsers::SAX2Parser.new(source);parser.listen(saxhelper);parser.parse end else case @parser when :ox then Ox.sax_parse(saxhelper,source,:convert_special=>true) when :oxhtml then Ox.sax_parse(saxhelper,source,:convert_special=>true,:smart=>true) when :libxml then parser=LibXML::XML::SaxParser.io(source);parser.callbacks=saxhelper;parser.parse when :nokogiri then parser=Nokogiri::XML::SAX::Parser.new(saxhelper);parser.parse(source) when :nokogirihtml then parser=Nokogiri::HTML::SAX::Parser.new(saxhelper);parser.parse(source.read) # fixme: nokogirihtml IO doesn't allow errors. when :xmlparser then saxhelper.parse(source) when :oga then parser=Oga::XML::SaxParser.new(saxhelper,source);parser.parse when :ogahtml then parser=Oga::HTML::SaxParser.new(saxhelper,source);parser.parse when :xerces then parser=XercesR::SAXParser.new;parser.setDocumentHandler(saxhelper);parser.parsebuf(source.read) when :rexmlstream then REXML::Parsers::StreamParser.new(source,saxhelper).parse when :rexmlsax2 then parser=REXML::Parsers::SAX2Parser.new(source);parser.listen(saxhelper);parser.parse end end listener end |
#parsefile(filename, listener) ⇒ Object
Parses file as XML. Error handling might be changed in the future.
313 314 315 316 317 318 319 320 321 322 |
# File 'lib/multisax.rb', line 313 def parsefile(filename,listener) #begin return nil unless FileTest::readable?(filename) File.open(filename,'rb'){|f| return parse(f,listener) } #rescue # return nil #end end |
#parser ⇒ Object
Returns which module is actually chosen.
267 |
# File 'lib/multisax.rb', line 267 def parser() @parser end |
#reset ⇒ Object
Reset MultiSAX state so that you can re-open() another library.
265 |
# File 'lib/multisax.rb', line 265 def reset() @parser=nil end |