Class: RDFObject::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/rdf_objects/parsers.rb

Class Method Summary collapse

Class Method Details

.parse(rdf) ⇒ Object

Choose the best format parser from an admittedly small group of choices.



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
# File 'lib/rdf_objects/parsers.rb', line 145

def self.parse(rdf)
  begin
    # Check if the format is XML or RDFa
    doc = Nokogiri::XML.parse(rdf, nil, nil, Nokogiri::XML::ParseOptions::PEDANTIC)
    raise "Unable to parse XML/HTML document -- no namespace declared" unless doc.root.namespaces
    if doc.root.namespaces.values.index("http://www.w3.org/1999/xhtml")
      collection = RDFAParser.parse(doc)
    else
      collection = XMLParser.parse(doc)
    end
  rescue Nokogiri::XML::SyntaxError
    begin
      if rdf.respond_to?(:read)
        json = JSON.parse(rdf.read)
      else
        json = JSON.parse(rdf)
      end
      collection = JSONParser.parse(json)
    rescue JSON::ParserError
      if rdf.respond_to?(:read)
        rdf.rewind
      end
      collection = NTriplesParser.parse(rdf)
    end
  end
  collection  
end