Search code examples
javardfjenaturtle-rdf

Reading URIs with spaces in Turtle using Jena?


I'm trying to read RDF from a Turtle file (shown below), but I'm having two problems. First, is there a problem with whitespace in RDF or Turtle? Somtimes, I have a problem reading URIs l ike <I/O Performance>. The problem disappears when I remove the spaces, giving <IOPerformance>. In the file below, I have this problem with <Standard(M1) - Small(default)>. Here is my code for loading the file and listing the subject, predicate, and object of each triple.

  StmtIterator iter = model.listStatements();
    while (iter.hasNext()) {
        Statement stmt = iter.nextStatement();
        Resource subject = stmt.getSubject(); // sujeito

        Property predicate = stmt.getPredicate(); // predicado

        RDFNode object = stmt.getObject(); // objeto

        System.out.println((subject.getLocalName());
        System.out.println((predicate.getLocalName().toString()); 

        System.out.println(StringUtils.substringBetween(object.toString(),"", "^"));

   }
@prefix dc:      <http://purl.org/dc/elements/1.1/> .
@prefix legal:   <http://www.linked-usdl.org/ns/usdl-legal#> .
@prefix foaf:    <http://xmlns.com/foaf/0.1/> .
@prefix vann:    <http://purl.org/vocab/vann/> .
@prefix org:     <http://www.w3.org/ns/org#> .
@prefix rdfs:    <http://www.w3.org/2000/01/rdf-schema#> .
@prefix price:   <http://www.linked-usdl.org/ns/usdl-price#> .
@prefix usdl:    <http://www.linked-usdl.org/ns/usdl#> .
@prefix xsd:     <http://www.w3.org/2001/XMLSchema#> .
@prefix owl:     <http://www.w3.org/2002/07/owl#> .
@prefix rdf:     <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix dei:     <http://dei.uc.pt/rdf/dei#> .
@prefix gr:      <http://purl.org/goodrelations/v1#> .
@prefix skos:    <http://www.w3.org/2004/02/skos/core#> .

<Standard(M1) - Small(default)>
      rdfs:CPU "1 EC2 Compute Unit"^^xsd:string ;
      rdfs:Cost "0.08"^^xsd:float ;
      rdfs:EBS-OptimizedAvailable
              "false"^^xsd:boolean ;
      rdfs:IOPerformance "Moderate"^^xsd:string ;
      rdfs:OS "Linux/UNIX"^^xsd:string ;
      rdfs:Platform "32-bit"^^xsd:string ;
      rdfs:RAM "1.7"^^xsd:float ;
      rdfs:Storage "160"^^xsd:float .

Solution

  • <Standard(M1) - Small(default)> is not a legal URI.

    1. It has spaces in it.
    2. It's a relative URI (relative to where the file is read from) which makes it hard to guess the full, absolute URI.

    Either add @base or use a prefixed name.