Skip to content

Commit

Permalink
JENA-1563: Accept lang and dt=rdf:langString
Browse files Browse the repository at this point in the history
  • Loading branch information
afs committed Jun 13, 2018
1 parent 874ad4e commit 79a04c0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import org.apache.jena.sparql.resultset.ResultSetException;
import org.apache.jena.sparql.resultset.SPARQLResult;
import org.apache.jena.sparql.util.Context;
import org.apache.jena.vocabulary.RDF;

/** Read JSON format SPARQL Results.
* <p>
Expand Down Expand Up @@ -209,8 +210,16 @@ private static Node parseOneTerm(JsonObject term, LabelToNode labelMap) {
if ( kLiteral.equals(type) || kTypedLiteral.equals(type) ) {
String lang = stringOrNull(term, kXmlLang);
String dtStr = stringOrNull(term, kDatatype);
if ( lang != null && dtStr != null )
throw new ResultSetException("Both language and datatype defined: " + term);
if ( lang != null ) {
// Strictly, xml:lang=... and datatype=rdf:langString is wrong (the datatype should be absent)
// The RDF specs recommend omitting the datatype. They did however come after the SPARQL 1.1 docs
// it's more of a "SHOULD" than a "MUST".
// datatype=xsd:string is also unnecessary.
if ( dtStr != null && ! dtStr.equals(RDF.dtLangString.getURI() ) ) {
// Must agree.
throw new ResultSetException("Both language and datatype defined, datatype is not rdf:langString:\n" + term);
}
}
RDFDatatype dt = TypeMapper.getInstance().getSafeTypeByName(dtStr);
return NodeFactory.createLiteral(v, lang, dt);
}
Expand Down
18 changes: 11 additions & 7 deletions jena-core/src/main/java/org/apache/jena/datatypes/TypeMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,18 @@ public TypeMapper() {


/**
* Version of getTypeByName which will treat unknown URIs as typed
* literals but with just the default implementation
* Version of getTypeByName which will treat unknown URIs as typed literals but with
* just the default implementation
* <p>
* RDF 1.1: null for {@code uri} returns null and it wil mean {@code xsd:string}
* because plain literals (no lang tag) and xsd:strings are now the same.
*
* @param uri the URI of the desired datatype
* @return Datatype the datatype definition
* registered at uri, if there is no such registered type it
* returns a new instance of the default datatype implementation, if the
* uri is null it returns null (indicating a plain RDF literal).
* @param uri
* the URI of the desired datatype
* @return Datatype the datatype definition registered at uri, if there is no such
* registered type it returns a new instance of the default datatype
* implementation, if the uri is null it returns null (indicating a plain RDF
* literal).
*/
public RDFDatatype getSafeTypeByName(final String uri) {
RDFDatatype dtype = uriToDT.get(uri);
Expand Down

0 comments on commit 79a04c0

Please sign in to comment.