Class: RDFObject::Parser

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

Direct Known Subclasses

JSONParser, NTriplesParser, RDFAParser, XMLParser

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = nil) ⇒ Parser

Returns a new instance of Parser.



133
134
135
136
# File 'lib/rdf_objects/parsers.rb', line 133

def initialize(data=nil)
  @collection = Collection.new
  self.data=(data) if data
end

Instance Attribute Details

#base_uriObject

Returns the value of attribute base_uri.



63
64
65
# File 'lib/rdf_objects/parsers.rb', line 63

def base_uri
  @base_uri
end

#collectionObject

Returns the value of attribute collection.



132
133
134
# File 'lib/rdf_objects/parsers.rb', line 132

def collection
  @collection
end

Class Method Details

.init_parser(rdf, options = {}) ⇒ Object



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

def self.init_parser(rdf, options={})
  if options[:format]
    parser = case options[:format]
    when 'rdfxml' then XMLParser.new(rdf)
    when 'rdfa' then RDFAParser.new(rdf)
    when 'ntriples' then NTriplesParser.new(rdf)
    when 'json' then JSONParser.new(rdf)
    end        
  else
    # Check to see if it is a URI being passed
    if rdf.is_a?(String)
      begin
        uri = Addressable::URI.parse(rdf)
        if uri.ip_based?
          response =  HTTPClient.fetch(rdf)
          rdf = response[:content]
          options[:base_uri] = response[:uri]
        end
      rescue URI::InvalidURIError
      end
    end
    # Check if the format is XML or RDFa
    doc = XMLTestDocument.new
    p = Nokogiri::XML::SAX::Parser.new(doc)
    if rdf.respond_to?(:read)
      p.parse(rdf.read)
    else
      p.parse(rdf)
    end
    if doc.is_doc?
      if rdf.respond_to?(:read)
        rdf.rewind
      end
      if doc.is_html?

        parser = RDFAParser.new(rdf)
      elsif doc.is_rdf?              
        parser = XMLParser.new(rdf)
      end
    else
      begin
        if rdf.respond_to?(:read)
          rdf.rewind
          json = JSON.parse(rdf.read)
        else
          json = JSON.parse(rdf)
        end
        parser = JSONParser.new(json)
      rescue JSON::ParserError
        if rdf.respond_to?(:read)
          rdf.rewind
        end
        parser = NTriplesParser.new(rdf)
      end
    end
  end
  if options[:base_uri] && parser
    parser.base_uri = options[:base_uri]
  end
  parser
end

.parse(rdf, options = {}) ⇒ Object

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



65
66
67
68
# File 'lib/rdf_objects/parsers.rb', line 65

def self.parse(rdf, options={})
  parser = init_parser(rdf, options)
  parser.parse if parser
end

Instance Method Details

#sanitize_uri(uri) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/rdf_objects/parsers.rb', line 150

def sanitize_uri(uri)
  # Fix some weirdness surrounding escaped ampersands in URIs.
  uri.gsub!(/&/,"&")
  # Return if there's nothing to sanitize with
  return uri unless self.base_uri
  begin
    u = Addressable::URI.parse(uri).normalize
    return uri if u.host
    fq_uri = self.base_uri+u
    fq_uri.to_s
  rescue URI::InvalidURIError
    uri
  end
end