Skip to content
Permalink
Browse files
COMMONSRDF-47: Add plural fileExtensions() mediaTypes()
merged RDFa types for HTML and XHTML
byMediaType() and byFileExtension() now checks the set
  • Loading branch information
stain committed Feb 10, 2017
1 parent 701e47e commit a574473c04746c7d1e67ddf4a3a77de6dee32f5f
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 35 deletions.
@@ -17,6 +17,7 @@
*/
package org.apache.commons.rdf.api;

import java.util.Collections;
import java.util.Locale;
import java.util.Optional;
import java.util.Set;
@@ -72,20 +73,14 @@ public interface RDFSyntax {
public static RDFSyntax NTRIPLES = W3CRDFSyntax.NTRIPLES;

/**
* HTML+RDFa 1.1
* HTML+RDFa 1.1 and XHTML+RDFa 1.1
*
* @see <a href=
* "https://www.w3.org/TR/html-rdfa/">https://www.w3.org/TR/html-rdfa/</a>
*/
public static RDFSyntax RDFA_HTML = W3CRDFSyntax.RDFA_HTML;

/**
* XHTML+RDFa 1.1
*
* @see <a href=
* "https://www.w3.org/TR/xhtml-rdfa/">https://www.w3.org/TR/xhtml-rdfa/</a>
*/
public static RDFSyntax RDFA_XHTML = W3CRDFSyntax.RDFA_XHTML;
public static RDFSyntax RDFA = W3CRDFSyntax.RDFA;

/**
* RDF 1.1 XML Syntax
@@ -133,6 +128,22 @@ public interface RDFSyntax {
*/
public String mediaType();

/**
* Set of <a href="https://tools.ietf.org/html/rfc2046">IANA media types/a> that
* covers this RDF syntax, including any non-official media types.
* <p>
* The media type can be used as part of <code>Content-Type</code> and
* <code>Accept</code> for <em>content negotiation</em> in the
* <a href="https://tools.ietf.org/html/rfc7231#section-3.1.1.1">HTTP
* protocol</a>.
* <p>
* The returned Set MUST include the value {@link #mediaType()}; this is the
* behaviour of the default implementation.
*/
default public Set<String> mediaTypes() {
return Collections.singleton(mediaType());
}

/**
* The <a href="https://tools.ietf.org/html/rfc2046">IANA-registered</a>
* file extension.
@@ -141,6 +152,18 @@ public interface RDFSyntax {
*/
public String fileExtension();

/**
* Set of file extensions for this RDF syntax, including any non-official extensions.
* <p>
* The file extension includes the leading period, e.g. <code>.jsonld</code>
* <p>
* The returned Set MUST include the value from {@link #fileExtension()}; this is
* the behaviour of the default implementation.
*/
default public Set<String> fileExtensions() {
return Collections.singleton(fileExtension());
}

/**
* Indicate if this RDF syntax supports
* <a href="https://www.w3.org/TR/rdf11-concepts/#section-dataset">RDF
@@ -211,9 +234,9 @@ public static Set<RDFSyntax> w3cSyntaxes() {
/**
* Return the RDFSyntax with the specified media type.
* <p>
* The <code>mediaType</code> is compared in lower case, therefore it might
* not be equal to the {@link RDFSyntax#mediaType} of the returned
* RDFSyntax.
* The <code>mediaType</code> is compared in lower case to all media types
* supported, therefore it might not be equal to the
* {@link RDFSyntax#mediaType} of the returned RDFSyntax.
* <p>
* If the media type specifies parameters, e.g.
* <code>text/turtle; charset=ascii</code>, only the part of the string to
@@ -230,16 +253,16 @@ public static Set<RDFSyntax> w3cSyntaxes() {
*/
public static Optional<RDFSyntax> byMediaType(final String mediaType) {
final String type = mediaType.toLowerCase(Locale.ROOT).split("\\s*;", 2)[0];
return w3cSyntaxes().stream().filter(t -> t.mediaType().equals(type))
return w3cSyntaxes().stream().filter(t -> t.mediaTypes().contains(type))
.findAny();
}

/**
* Return the RDFSyntax with the specified file extension.
* <p>
* The <code>fileExtension</code> is compared in lower case, therefore it
* might not be equal to the {@link RDFSyntax#fileExtension} of the returned
* RDFSyntax.
* The <code>fileExtension</code> is compared in lower case to all
* extensions supported, therefore it might not be equal to the
* {@link RDFSyntax#fileExtension} of the returned RDFSyntax.
* <p>
* This method support all syntaxes returned by {@link #w3cSyntaxes()}.
*
@@ -252,7 +275,7 @@ public static Optional<RDFSyntax> byMediaType(final String mediaType) {
*/
public static Optional<RDFSyntax> byFileExtension(final String fileExtension) {
final String ext = fileExtension.toLowerCase(Locale.ROOT);
return w3cSyntaxes().stream().filter(t -> t.fileExtension().equals(ext))
return w3cSyntaxes().stream().filter(t -> t.fileExtensions().contains(ext))
.findAny();
}

@@ -39,8 +39,7 @@
* 1.1 Primer</a>
* @see org.apache.commons.rdf.experimental.RDFParser
*/
final class W3CRDFSyntax implements RDFSyntax {

class W3CRDFSyntax implements RDFSyntax {

/**
* IRI representing a <a href="https://www.w3.org/ns/formats/">W3C RDF
@@ -88,7 +87,7 @@ public int hashCode() {
}


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

static {
@@ -97,13 +96,24 @@ public int hashCode() {
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);

TRIG = new W3CRDFSyntax("TriG", "RDF 1.1 TriG", "application/trig", ".trig", true);
RDFA = new W3CRDFSyntax("RDFa", "HTML+RDFa 1.1", "text/html", ".html", false) {
private Set<String> types = Collections.unmodifiableSet(new LinkedHashSet<>(
Arrays.asList("text/html", "application/xhtml+xml")));
private Set<String> extensions = Collections.unmodifiableSet(new LinkedHashSet<>(
Arrays.asList(".html", ".xhtml")));
@Override
public Set<String> mediaTypes() {
return types;
}
@Override
public Set<String> fileExtensions() {
return extensions;
}
};
syntaxes = Collections.unmodifiableSet(new LinkedHashSet<>(
Arrays.asList(JSONLD, NQUADS, NTRIPLES, RDFA_HTML, RDFA_XHTML, RDFXML, TRIG, TURTLE)));
Arrays.asList(JSONLD, NQUADS, NTRIPLES, RDFA, RDFXML, TRIG, TURTLE)));
}

private final String title;
@@ -30,8 +30,7 @@ public void byFileExtension() throws Exception {
assertEquals(RDFSyntax.JSONLD, RDFSyntax.byFileExtension(".jsonld").get());
assertEquals(RDFSyntax.NQUADS, RDFSyntax.byFileExtension(".nq").get());
assertEquals(RDFSyntax.NTRIPLES, RDFSyntax.byFileExtension(".nt").get());
assertEquals(RDFSyntax.RDFA_HTML, RDFSyntax.byFileExtension(".html").get());
assertEquals(RDFSyntax.RDFA_XHTML, RDFSyntax.byFileExtension(".xhtml").get());
assertEquals(RDFSyntax.RDFA, RDFSyntax.byFileExtension(".html").get());
assertEquals(RDFSyntax.RDFXML, RDFSyntax.byFileExtension(".rdf").get());
assertEquals(RDFSyntax.TRIG, RDFSyntax.byFileExtension(".trig").get());
assertEquals(RDFSyntax.TURTLE, RDFSyntax.byFileExtension(".ttl").get());
@@ -57,8 +56,8 @@ public void byMediaType() throws Exception {
assertEquals(RDFSyntax.JSONLD, RDFSyntax.byMediaType("application/ld+json").get());
assertEquals(RDFSyntax.NQUADS, RDFSyntax.byMediaType("application/n-quads").get());
assertEquals(RDFSyntax.NTRIPLES, RDFSyntax.byMediaType("application/n-triples").get());
assertEquals(RDFSyntax.RDFA_HTML, RDFSyntax.byMediaType("text/html").get());
assertEquals(RDFSyntax.RDFA_XHTML, RDFSyntax.byMediaType("application/xhtml+xml").get());
assertEquals(RDFSyntax.RDFA, RDFSyntax.byMediaType("text/html").get());
assertEquals(RDFSyntax.RDFA, RDFSyntax.byMediaType("application/xhtml+xml").get());
assertEquals(RDFSyntax.RDFXML, RDFSyntax.byMediaType("application/rdf+xml").get());
assertEquals(RDFSyntax.TRIG, RDFSyntax.byMediaType("application/trig").get());
assertEquals(RDFSyntax.TURTLE, RDFSyntax.byMediaType("text/turtle").get());
@@ -89,34 +88,59 @@ public void fileExtension() throws Exception {
assertEquals(".jsonld", RDFSyntax.JSONLD.fileExtension());
assertEquals(".nq", RDFSyntax.NQUADS.fileExtension());
assertEquals(".nt", RDFSyntax.NTRIPLES.fileExtension());
assertEquals(".html", RDFSyntax.RDFA_HTML.fileExtension());
assertEquals(".xhtml", RDFSyntax.RDFA_XHTML.fileExtension());
assertEquals(".html", RDFSyntax.RDFA.fileExtension());
assertEquals(".rdf", RDFSyntax.RDFXML.fileExtension());
assertEquals(".trig", RDFSyntax.TRIG.fileExtension());
assertEquals(".ttl", RDFSyntax.TURTLE.fileExtension());
}

@Test
public void fileExtensions() throws Exception {
assertTrue(RDFSyntax.JSONLD.fileExtensions().contains(".jsonld"));
assertTrue(RDFSyntax.NQUADS.fileExtensions().contains(".nq"));
assertTrue(RDFSyntax.NTRIPLES.fileExtensions().contains(".nt"));
assertTrue(RDFSyntax.RDFA.fileExtensions().contains(".html"));
assertTrue(RDFSyntax.RDFA.fileExtensions().contains(".xhtml"));
assertTrue(RDFSyntax.RDFXML.fileExtensions().contains(".rdf"));
assertTrue(RDFSyntax.TRIG.fileExtensions().contains(".trig"));
assertTrue(RDFSyntax.TURTLE.fileExtensions().contains(".ttl"));
}

@Test
public void mediaType() throws Exception {
assertEquals("application/ld+json", RDFSyntax.JSONLD.mediaType());
assertEquals("application/n-quads", RDFSyntax.NQUADS.mediaType());
assertEquals("application/n-triples", RDFSyntax.NTRIPLES.mediaType());
assertEquals("text/html", RDFSyntax.RDFA_HTML.mediaType());
assertEquals("application/xhtml+xml", RDFSyntax.RDFA_XHTML.mediaType());
assertEquals("text/html", RDFSyntax.RDFA.mediaType());
assertEquals("application/rdf+xml", RDFSyntax.RDFXML.mediaType());
assertEquals("application/trig", RDFSyntax.TRIG.mediaType());
assertEquals("text/turtle", RDFSyntax.TURTLE.mediaType());
}


@Test
public void mediaTypes() throws Exception {
assertTrue(RDFSyntax.JSONLD.mediaTypes().contains("application/ld+json"));
assertTrue(RDFSyntax.NQUADS.mediaTypes().contains("application/n-quads"));
assertTrue(RDFSyntax.NTRIPLES.mediaTypes().contains("application/n-triples"));
assertTrue(RDFSyntax.RDFA.mediaTypes().contains("text/html"));
assertTrue(RDFSyntax.RDFA.mediaTypes().contains("application/xhtml+xml"));
assertTrue(RDFSyntax.RDFXML.mediaTypes().contains("application/rdf+xml"));
assertTrue(RDFSyntax.TRIG.mediaTypes().contains("application/trig"));
assertTrue(RDFSyntax.TURTLE.mediaTypes().contains("text/turtle"));
}

@Test
public void name() throws Exception {
public void string() throws Exception {
assertEquals("JSON-LD 1.0", RDFSyntax.JSONLD.toString());
assertEquals("RDF 1.1 Turtle", RDFSyntax.TURTLE.toString());
}

@Test
public void valueOf() throws Exception {
assertEquals(RDFSyntax.TURTLE, RDFSyntax.byName("TURTLE").get());
public void byName() throws Exception {
for (RDFSyntax s : RDFSyntax.w3cSyntaxes()) {
assertEquals(s, RDFSyntax.byName(s.name()).get());
}
}

}

0 comments on commit a574473

Please sign in to comment.