Skip to content
Permalink
Browse files
COMMONSRDF-47: Add RDFSyntax.iri()
as proposed by @ansell in
#27 (comment)
  • Loading branch information
stain committed Feb 9, 2017
1 parent db17f1a commit 81d5b320b62e7165b178e0a34c5157105f57e4af
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 37 deletions.
@@ -106,8 +106,7 @@ public interface RDFSyntax {
/**
* A short name of the RDF Syntax e.g. <code>JSONLD</code>.
* <p>
* The name is specific to Commons RDF and carries no particular meaning
* except that it matches the constants like {@link #JSONLD}.
* The name is specific to Commons RDF and carries no particular meaning.
*
* @return Short name for RDF syntax
*/
@@ -148,7 +147,43 @@ public interface RDFSyntax {
* Datasets</a>.
*/
public boolean supportsDataset();


/**
* Return the {@link IRI} that <em>identifies</em> the RDF syntax.
* <p>
* Note that the identifying IRI is generally distinct from the IRI of the
* document that <em>specifies</em> the RDF syntax.
*
* @return Identifying IRI, e.g.
* <code>http://www.w3.org/ns/formats/JSON-LD</code>
*/
public IRI iri();

/**
* Compare this RDFSyntax with another object.
* <p>
* Two {@link RDFSyntax}es are considered equal if their
* {@link #mediaType()}s are equal when compared as lower case strings
* according to {@link String#toLowerCase(Locale)} with the locale
* {@link Locale#ROOT}.
*
* @param obj
* @return
*/
@Override
boolean equals(Object obj);

/**
* The hash code of an RDFSyntax is equivalent to the hash code
* of the {@link #mediaType()} in lower case according to
* {@link String#toLowerCase(Locale)} with the locale
* {@link Locale#ROOT}.
*
* @return Hash code of RDFSyntax
*/
@Override
int hashCode();

/**
* Return the RDF 1.1 serialization syntaxes.
* <p>
@@ -235,30 +270,6 @@ public static Optional<RDFSyntax> byFileExtension(final String fileExtension) {
public static Optional<RDFSyntax> byName(final String name) {
return w3cSyntaxes().stream().filter(t -> t.name().equals(name)).findAny();
}

/**
* Compare this RDFSyntax with another object.
* <p>
* Two {@link RDFSyntax}es are considered equal if their
* {@link #mediaType()}s are equal when compared as lower case strings
* according to {@link String#toLowerCase(Locale)} with the locale
* {@link Locale#ROOT}.
*
* @param obj
* @return
*/
@Override
boolean equals(Object obj);

/**
* The hash code of an RDFSyntax is equivalent to the hash code
* of the {@link #mediaType()} in lower case according to
* {@link String#toLowerCase(Locale)} with the locale
* {@link Locale#ROOT}.
*
* @return Hash code of RDFSyntax
*/
@Override
int hashCode();


}
@@ -41,19 +41,66 @@
*/
final class W3CRDFSyntax implements RDFSyntax {


/**
* IRI representing a <a href="https://www.w3.org/ns/formats/">W3C RDF
* format</a>.
*/
private final static class FormatIRI implements IRI {
private static String BASE = "http://www.w3.org/ns/formats/";
private final String format;

private FormatIRI(final String format) {
this.format = format;
}

@Override
public String getIRIString() {
return BASE + format;
}

@Override
public String ntriplesString() {
return "<" + getIRIString() + ">";
}

@Override
public String toString() {
return ntriplesString();
}

@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (obj == null || !(obj instanceof IRI)) {
return false;
}
final IRI other = (IRI) obj;
return getIRIString().equals(other.getIRIString());
}

@Override
public int hashCode() {
return getIRIString().hashCode();
}
}


static final RDFSyntax JSONLD, TURTLE, NQUADS, NTRIPLES, RDFA_HTML, RDFA_XHTML, RDFXML, TRIG;
static final Set<RDFSyntax> syntaxes;

static {
// Initialize within static block to avoid inserting nulls
JSONLD = new W3CRDFSyntax("JSONLD", "JSON-LD 1.0", "application/ld+json", ".jsonld", true);
TURTLE = new W3CRDFSyntax("TURTLE", "RDF 1.1 Turtle", "text/turtle", ".ttl", false);
NQUADS = new W3CRDFSyntax("NQUADS", "RDF 1.1 N-Quads", "application/n-quads", ".nq", true);
NTRIPLES = new W3CRDFSyntax("NTRIPLES", "RDF 1.1 N-Triples", "application/n-triples", ".nt", false);
RDFA_HTML = new W3CRDFSyntax("RDFA_HTML", "HTML+RDFa 1.1", "text/html", ".html", false);
RDFA_XHTML = new W3CRDFSyntax("RDFA_XHTML", "XHTML+RDFa 1.1", "application/xhtml+xml", ".xhtml", false);
RDFXML = new W3CRDFSyntax("RDFXML", "RDF 1.1 XML Syntax", "application/rdf+xml", ".rdf", false);
TRIG = new W3CRDFSyntax("TRIG", "RDF 1.1 TriG", "application/trig", ".trig", true);
JSONLD = new W3CRDFSyntax("JSON-LD", "JSON-LD 1.0", "application/ld+json", ".jsonld", true);
TURTLE = new W3CRDFSyntax("Turtle", "RDF 1.1 Turtle", "text/turtle", ".ttl", false);
NQUADS = new W3CRDFSyntax("N-Quads", "RDF 1.1 N-Quads", "application/n-quads", ".nq", true);
NTRIPLES = new W3CRDFSyntax("N-Triples", "RDF 1.1 N-Triples", "application/n-triples", ".nt", false);
RDFA_HTML = new W3CRDFSyntax("RDFa", "HTML+RDFa 1.1", "text/html", ".html", false);
RDFA_XHTML = new W3CRDFSyntax("RDFa", "XHTML+RDFa 1.1", "application/xhtml+xml", ".xhtml", false);
RDFXML = new W3CRDFSyntax("RDF_XML", "RDF 1.1 XML Syntax", "application/rdf+xml", ".rdf", false);
TRIG = new W3CRDFSyntax("TriG", "RDF 1.1 TriG", "application/trig", ".trig", true);

syntaxes = Collections.unmodifiableSet(new LinkedHashSet<>(
Arrays.asList(JSONLD, NQUADS, NTRIPLES, RDFA_HTML, RDFA_XHTML, RDFXML, TRIG, TURTLE)));
@@ -64,17 +111,20 @@ final class W3CRDFSyntax implements RDFSyntax {
private final String mediaType;

private final String fileExtension;

private final boolean supportsDataset;

private final String name;

private final IRI iri;

private W3CRDFSyntax(String name, String title, String mediaType, String fileExtension, boolean supportsDataset) {
this.name = name;
this.title = title;
this.mediaType = mediaType.toLowerCase(Locale.ROOT);
this.fileExtension = fileExtension.toLowerCase(Locale.ROOT);
this.supportsDataset = supportsDataset;
this.iri = new FormatIRI(name);
}

/**
@@ -115,6 +165,11 @@ public String title() {
public String name() {
return name;
}

@Override
public IRI iri() {
return iri;
}

@Override
public boolean equals(Object obj) {

0 comments on commit 81d5b32

Please sign in to comment.