Skip to content

Commit

Permalink
Added ID checking, passing some more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tommorris committed Oct 16, 2008
1 parent bdbb29c commit 73e9498
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 121 deletions.
30 changes: 23 additions & 7 deletions lib/rena/rdfxmlparser.rb
Expand Up @@ -41,21 +41,41 @@ def is_rdf_root? (node)
end

private
def fail_check(el)
if el.attributes.get_attribute_ns(SYNTAX_BASE, "aboutEach")
raise Rena::AboutEachException
end
if el.attributes.get_attribute_ns(SYNTAX_BASE, "aboutEachPrefix")
raise Rena::AboutEachException
end
end

def parse_subject(el)
fail_check(el)

if el.attributes.get_attribute_ns(SYNTAX_BASE, "about")
return URIRef.new(el.attributes.get_attribute_ns(SYNTAX_BASE, "about").value)
elsif el.attributes.get_attribute_ns(SYNTAX_BASE, "ID")
return url_helper("#" + el.attributes.get_attribute_ns(SYNTAX_BASE, "ID").value, "", el.base)
id = el.attributes.get_attribute_ns(SYNTAX_BASE, "ID")
if id_check?(id.value)
return url_helper("#" + id.value, "", el.base)
else
raise
end
elsif el.attributes.get_attribute_ns(SYNTAX_BASE, "nodeID")
return BNode.new(el.attributes.get_attribute_ns(SYNTAX_BASE, "nodeID").value)
else
return BNode.new
end
end

private
def id_check?(id)
!(!(id =~ /^[a-zA-Z_]\w*$/))
end

def parse_descriptions (node, subject = nil)
node.each_element { |el|
fail_check(el)
# detect a subject
subject = parse_subject(el) if subject.nil?

Expand Down Expand Up @@ -115,11 +135,7 @@ def extract_name(str)
end

def smells_like_xml?(str)
if str =~ /xmlns/
true
else
false
end
!(!(str =~ /xmlns/))
end

def url_helper(name, ns, base = nil)
Expand Down
229 changes: 115 additions & 114 deletions spec/parser_spec.rb
Expand Up @@ -5,125 +5,126 @@
# w3c test suite: http://www.w3.org/TR/rdf-testcases/

describe "RDF/XML Parser" do
# it "should be able to parse a simple single-triple document" do
it "should be able to parse a simple single-triple document" do
sampledoc = <<-EOF;
<?xml version="1.0" ?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:ex="http://www.example.org/" xml:lang="en" xml:base="http://www.example.org/foo">
<ex:Thing rdf:about="http://example.org/joe" ex:name="bar">
<ex:belongsTo rdf:resource="http://tommorris.org/" />
<ex:sampleText rdf:datatype="http://www.w3.org/2001/XMLSchema#string">foo</ex:sampleText>
<ex:hadADodgyRelationshipWith>
<rdf:Description>
<ex:name>Tom</ex:name>
<ex:hadADodgyRelationshipWith>
<rdf:Description>
<ex:name>Rob</ex:name>
<ex:hadADodgyRelationshipWith>
<rdf:Description>
<ex:name>Mary</ex:name>
</rdf:Description>
</ex:hadADodgyRelationshipWith>
</rdf:Description>
</ex:hadADodgyRelationshipWith>
</rdf:Description>
</ex:hadADodgyRelationshipWith>
</ex:Thing>
</rdf:RDF>
EOF

graph = RdfXmlParser.new(sampledoc)
graph.graph.size.should == 9
end

it "should raise an error if rdf:aboutEach is used, as per the negative parser test rdfms-abouteach-error001 (rdf:aboutEach attribute)" do
sampledoc = <<-EOF;
<?xml version="1.0" ?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:eg="http://example.org/">
<rdf:Bag rdf:ID="node">
<rdf:li rdf:resource="http://example.org/node2"/>
</rdf:Bag>
<rdf:Description rdf:aboutEach="#node">
<dc:rights xmlns:dc="http://purl.org/dc/elements/1.1/">me</dc:rights>
</rdf:Description>
</rdf:RDF>
EOF

lambda do
graph = RdfXmlParser.new(sampledoc)
end.should raise_error
end

it "should raise an error if rdf:aboutEachPrefix is used, as per the negative parser test rdfms-abouteach-error002 (rdf:aboutEachPrefix attribute)" do
sampledoc = <<-EOF;
<?xml version="1.0" ?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:eg="http://example.org/">
<rdf:Description rdf:about="http://example.org/node">
<eg:property>foo</eg:property>
</rdf:Description>
<rdf:Description rdf:aboutEachPrefix="http://example.org/">
<dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">me</dc:creator>
</rdf:Description>
</rdf:RDF>
EOF

lambda do
graph = RdfXmlParser.new(sampledoc)
end.should raise_error
end

it "should fail if given a non-ID as an ID (as per rdfcore-rdfms-rdf-id-error001)" do
sampledoc = <<-EOF;
<?xml version="1.0"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<rdf:Description rdf:ID='333-555-666' />
</rdf:RDF>
EOF

lambda do
graph = RdfXmlParser.new(sampledoc)
end.should raise_error
end

# it "should make sure that the value of rdf:ID attributes match the XML Name production (child-element version)" do
# sampledoc = <<-EOF;
# <?xml version="1.0" ?>
# <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
# xmlns:ex="http://www.example.org/" xml:lang="en" xml:base="http://www.example.org/foo">
# <ex:Thing rdf:about="http://example.org/joe" ex:name="bar">
# <ex:belongsTo rdf:resource="http://tommorris.org/" />
# <ex:sampleText rdf:datatype="http://www.w3.org/2001/XMLSchema#string">foo</ex:sampleText>
# <ex:hadADodgyRelationshipWith>
# <rdf:Description>
# <ex:name>Tom</ex:name>
# <ex:hadADodgyRelationshipWith>
# <rdf:Description>
# <ex:name>Rob</ex:name>
# <ex:hadADodgyRelationshipWith>
# <rdf:Description>
# <ex:name>Mary</ex:name>
# </rdf:Description>
# </ex:hadADodgyRelationshipWith>
# </rdf:Description>
# </ex:hadADodgyRelationshipWith>
# </rdf:Description>
# </ex:hadADodgyRelationshipWith>
# </ex:Thing>
# </rdf:RDF>
# <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
# xmlns:eg="http://example.org/">
# <rdf:Description>
# <eg:prop rdf:ID="q:name" />
# </rdf:Description>
# </rdf:RDF>
# EOF
#
# graph = RdfXmlParser.new(sampledoc)
# graph.graph.size.should == 9
# lambda do
# graph = RdfXmlParser.new(sampledoc)
# end.should raise_error
# end
#
# it "should raise an error if rdf:aboutEach is used, as per the negative parser test rdfms-abouteach-error001 (rdf:aboutEach attribute)" do
# sampledoc = <<-EOF;
# <?xml version="1.0" ?>
# <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
# xmlns:eg="http://example.org/">
#
# <rdf:Bag rdf:ID="node">
# <rdf:li rdf:resource="http://example.org/node2"/>
# </rdf:Bag>
#
# <rdf:Description rdf:aboutEach="#node">
# <dc:rights xmlns:dc="http://purl.org/dc/elements/1.1/">me</dc:rights>
#
# </rdf:Description>
#
# </rdf:RDF>
# EOF
#
# lambda do
# graph = RdfXmlParser.new(sampledoc)
# end.should raise_error
# end
#
# it "should raise an error if rdf:aboutEachPrefix is used, as per the negative parser test rdfms-abouteach-error002 (rdf:aboutEachPrefix attribute)" do
# sampledoc = <<-EOF;
# <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
# xmlns:eg="http://example.org/">
#
# <rdf:Description rdf:about="http://example.org/node">
# <eg:property>foo</eg:property>
# </rdf:Description>
#
# <rdf:Description rdf:aboutEachPrefix="http://example.org/">
# <dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">me</dc:creator>
#
# </rdf:Description>
#
# </rdf:RDF>
# EOF
#
# lambda do
# graph = RdfXmlParser.new(sampledoc)
# end.should raise_error
# end
#
# it "should fail if given a non-ID as an ID (as per rdfcore-rdfms-rdf-id-error001)" do
# sampledoc = <<-EOF;
# <?xml version="1.0"?>
# <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
# <rdf:Description rdf:ID='333-555-666' />
# </rdf:RDF>
# EOF
#
# lambda do
# graph = RdfXmlParser.new(sampledoc)
# end.should raise_error
# end
#
# it "should make sure that the value of rdf:ID attributes match the XML Name production (child-element version)" do
# sampledoc = <<-EOF;
# <?xml version="1.0" ?>
# <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
# xmlns:eg="http://example.org/">
# <rdf:Description>
# <eg:prop rdf:ID="q:name" />
# </rdf:Description>
# </rdf:RDF>
# EOF
#
# lambda do
# graph = RdfXmlParser.new(sampledoc)
# end.should raise_error
# end
#
# it "should make sure that the value of rdf:ID attributes match the XML Name production (data attribute version)" do
# sampledoc = <<-EOF;
# <?xml version="1.0" ?>
# <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
# xmlns:eg="http://example.org/">
# <rdf:Description rdf:ID="a/b" eg:prop="val" />
# </rdf:RDF>
# EOF
#
# lambda do
# graph = RdfXmlParser.new(sampledoc)
# end.should raise_error
# end
#

it "should make sure that the value of rdf:ID attributes match the XML Name production (data attribute version)" do
sampledoc = <<-EOF;
<?xml version="1.0" ?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:eg="http://example.org/">
<rdf:Description rdf:ID="a/b" eg:prop="val" />
</rdf:RDF>
EOF

lambda do
graph = RdfXmlParser.new(sampledoc)
end.should raise_error
end

it "should handle parseType=Literal according to xml-literal-namespaces-test001.rdf test" do
sampledoc = <<-EOF;
<?xml version="1.0"?>
Expand Down

0 comments on commit 73e9498

Please sign in to comment.