rdfobjects 0.8.0 → 0.8.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/rdf_objects/parsers.rb +50 -9
  2. metadata +2 -2
@@ -75,17 +75,25 @@ module RDFObject
75
75
  when 'json' then JSONParser.new(rdf)
76
76
  end
77
77
  else
78
- begin
79
- # Check if the format is XML or RDFa
80
- doc = Nokogiri::XML.parse(rdf, nil, nil, Nokogiri::XML::ParseOptions::PEDANTIC)
81
- raise "Unable to parse XML/HTML document -- no namespace declared" unless doc.root.namespaces
82
- if doc.root.namespaces.values.index("http://www.w3.org/1999/xhtml")
83
- parser = RDFAParser.new(doc)
84
- else
85
- doc = nil
78
+
79
+ # Check if the format is XML or RDFa
80
+ doc = XMLTestDocument.new
81
+ p = Nokogiri::XML::SAX::Parser.new(doc)
82
+ if rdf.respond_to?(:read)
83
+ p.parse(rdf.read)
84
+ else
85
+ p.parse(rdf)
86
+ end
87
+ if doc.is_doc?
88
+ if rdf.respond_to?(:read)
89
+ rdf.rewind
90
+ end
91
+ if doc.is_html?
92
+ parser = RDFAParser.new(rdf)
93
+ else
86
94
  parser = XMLParser.new(rdf)
87
95
  end
88
- rescue Nokogiri::XML::SyntaxError
96
+ else
89
97
  begin
90
98
  if rdf.respond_to?(:read)
91
99
  rdf.rewind
@@ -187,6 +195,39 @@ module RDFObject
187
195
  end
188
196
  end
189
197
 
198
+ class XMLTestDocument < Nokogiri::XML::SAX::Document
199
+ def initialize
200
+ @xml_start = false
201
+ @xml_end = false
202
+ @namespaces = []
203
+ end
204
+
205
+ def start_element(name, attrs=[])
206
+ @xml_start = name
207
+ attrs.each do | attrib |
208
+ next unless attrib.is_a?(Array)
209
+ if attrib.first =~ /^xmlns(:|\b)/
210
+ @namespaces << attrib.last
211
+ end
212
+ end
213
+ end
214
+
215
+ def end_element(name)
216
+ if @xml_start
217
+ @xml_end = true if name = @xml_start
218
+ end
219
+ end
220
+
221
+ def is_doc?
222
+ return true if @xml_start && @xml_end
223
+ return false
224
+ end
225
+
226
+ def is_html?
227
+ return true if @namespaces.index("http://www.w3.org/1999/xhtml")
228
+ return false
229
+ end
230
+ end
190
231
 
191
232
  class XMLParser < RDFObject::Parser
192
233
  def initialize(data=nil)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rdfobjects
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ross Singer
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-08 00:00:00 -05:00
12
+ date: 2010-01-11 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency