@Override public boolean equals(final Object obj) { if (obj == this) { return true; } if (!(obj instanceof RDFSyntax)) { return false; } final RDFSyntax other = (RDFSyntax) obj; return mediaType.equalsIgnoreCase(other.mediaType()); }
/** * Return {@link RDFSyntax} corresponding to a Jena {@link Lang}. * * @param lang * {@link Lang} to convert * @return Matched {@link RDFSyntax}, otherwise {@link Optional#empty()} */ public Optional<RDFSyntax> asRDFSyntax(final Lang lang) { return RDFSyntax.byMediaType(lang.getContentType().getContentType()); }
/** * Get a default profile IRI from the syntax and/or identifier. * * @param syntax the RDF syntax * @param identifier the resource identifier * @param defaultJsonLdProfile a user-supplied default * @return a profile IRI usable by the output streamer */ public static IRI getDefaultProfile(final RDFSyntax syntax, final IRI identifier, final String defaultJsonLdProfile) { return RDFA.equals(syntax) ? identifier : ofNullable(defaultJsonLdProfile).map(rdf::createIRI).orElse(compacted); }
/** * Return the RDFSyntax with the specified {@link #name()}. * <p> * This method support all syntaxes returned by {@link #w3cSyntaxes()}. * * @param name * The name to match, , e.g. <code>"JSONLD"</code> * @return If {@link Optional#isPresent()}, the {@link RDFSyntax} which has * a matching {@link RDFSyntax#name()}, otherwise * {@link Optional#empty()} indicating that no matching name was found. */ public static Optional<RDFSyntax> byName(final String name) { return w3cSyntaxes().stream().filter(t -> t.name().equals(name)).findAny(); }
/** * Return the RDFSyntax with the specified file extension. * <p> * 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()}. * * @param fileExtension * The fileExtension to match, starting with <code>.</code> * @return If {@link Optional#isPresent()}, the {@link RDFSyntax} which has * a matching {@link RDFSyntax#fileExtension()}, otherwise * {@link Optional#empty()} indicating that no matching file * extension was found. */ public static Optional<RDFSyntax> byFileExtension(final String fileExtension) { final String ext = fileExtension.toLowerCase(Locale.ROOT); return w3cSyntaxes().stream().filter(t -> t.fileExtensions().contains(ext)) .findAny(); }
@Test public void testSparqlUpdate() { assertEquals("SPARQL-Update", SPARQL_UPDATE.name(), "Incorrect name for SPARQL-Update!"); assertEquals("SPARQL 1.1 Update", SPARQL_UPDATE.title(), "Incorrect title for SPARQL-Update!"); assertEquals("application/sparql-update", SPARQL_UPDATE.mediaType(), "Incorrect mediaType for SPARQL-Update!"); assertEquals(".ru", SPARQL_UPDATE.fileExtension(), "Incorrect file extension for SPARQL-Update!"); assertEquals("http://www.w3.org/TR/sparql11-update/", SPARQL_UPDATE.iri().getIRIString(), "Incorrect IRI for SPARQL-Update!"); assertFalse(SPARQL_UPDATE.supportsDataset(), "SPARQL-Update shouldn't be supporting datasets!"); assertEquals(SPARQL_UPDATE.title(), SPARQL_UPDATE.toString(), "String version isn't the same as the title!"); assertNotEquals(SPARQL_UPDATE, LD_PATCH, "SPARQL-Update equals LD-PATCH???"); assertNotEquals(SPARQL_UPDATE, "blah blah", "SPARQL-Update equals blah blah???"); assertEquals(SPARQL_UPDATE, SPARQL_UPDATE, "SPARQL-Update doesn't act like a singleton!"); assertEquals(SPARQL_UPDATE.mediaType().hashCode(), SPARQL_UPDATE.hashCode(), "SPARQL-Update has an unexpected hash code!"); }
@Test public void byName() throws Exception { for (final RDFSyntax s : RDFSyntax.w3cSyntaxes()) { assertEquals(s, RDFSyntax.byName(s.name()).get()); } }
/** * 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. * * @return The file extensions corresponding to the RDF Syntax */ default public Set<String> fileExtensions() { return Collections.singleton(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 testLDPatch() { assertEquals("LD-Patch", LD_PATCH.name(), "Incorrect name for LD-Patch"); assertEquals("Linked Data Patch Format", LD_PATCH.title(), "Incorrect title for LD-Patch!"); assertEquals("text/ldpatch", LD_PATCH.mediaType(), "Incorrect mediaType for LD-Patch!"); assertEquals(".ldp", LD_PATCH.fileExtension(), "Incorrect extension for LD-Patch!"); assertEquals("http://www.w3.org/ns/formats/LD_Patch", LD_PATCH.iri().getIRIString(), "Incorrect IRI for LD-Patch!"); assertFalse(LD_PATCH.supportsDataset(), "LD-Patch shouldn't be supporting datasets!"); assertEquals(LD_PATCH.title(), LD_PATCH.toString(), "LD-Patch string version isn't the same as the title"); assertNotEquals(LD_PATCH, SPARQL_UPDATE, "LD-Patch matches SPARQL-Update???"); assertNotEquals(LD_PATCH, "blah blah", "LD-Patch matches 'blah blah'???"); assertEquals(LD_PATCH, LD_PATCH, "LD-Patch doesn't act like a singleton!"); assertEquals(LD_PATCH.mediaType().hashCode(), LD_PATCH.hashCode(), "LD-Patch has unexpected hash code!"); } }
@Test public void fileExtension() throws Exception { assertEquals(".jsonld", RDFSyntax.JSONLD.fileExtension()); assertEquals(".nq", RDFSyntax.NQUADS.fileExtension()); assertEquals(".nt", RDFSyntax.NTRIPLES.fileExtension()); assertEquals(".html", RDFSyntax.RDFA.fileExtension()); assertEquals(".rdf", RDFSyntax.RDFXML.fileExtension()); assertEquals(".trig", RDFSyntax.TRIG.fileExtension()); assertEquals(".ttl", RDFSyntax.TURTLE.fileExtension()); }
@Override public boolean equals(final Object obj) { if (obj == this) { return true; } if (!(obj instanceof RDFSyntax)) { return false; } final RDFSyntax other = (RDFSyntax) obj; return mediaType.equalsIgnoreCase(other.mediaType()); }
/** * Given a list of acceptable media types and an RDF syntax, get the relevant profile data, if * relevant. * * @param acceptableTypes the types from HTTP headers * @param syntax an RDF syntax * @return a profile IRI if relevant */ public static IRI getProfile(final List<MediaType> acceptableTypes, final RDFSyntax syntax) { for (final MediaType type : acceptableTypes) { if (RDFSyntax.byMediaType(type.toString()).filter(isEqual(syntax)).isPresent() && type.getParameters().containsKey("profile")) { return rdf.createIRI(type.getParameters().get("profile").split(" ")[0].trim()); } } return null; }
@Override public void update(final Graph graph, final String update, final RDFSyntax syntax, final String base) { requireNonNull(graph, "The input graph may not be null"); requireNonNull(update, "The update command may not be null"); requireNonNull(syntax, "The RDF syntax may not be null"); if (!SPARQL_UPDATE.equals(syntax)) { throw new RuntimeTrellisException("The syntax " + syntax + " is not supported for updates."); } try { final org.apache.jena.graph.Graph g = rdf.asJenaGraph(graph); execute(create(update, base), g); } catch (final UpdateException | QueryParseException ex) { throw new RuntimeTrellisException(ex); } }
/** * 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. * * @return The media types corresponding to the RDF Syntax */ default public Set<String> mediaTypes() { return Collections.singleton(mediaType()); }
@Test public void byMediaTypeContentType() throws Exception { assertEquals(RDFSyntax.TURTLE, RDFSyntax.byMediaType("text/turtle; charset=\"UTF-8\"").get()); assertEquals(RDFSyntax.TURTLE, RDFSyntax.byMediaType("text/turtle ; charset=\"UTF-8\"").get()); // That's a Content-Type, not media type; we won't split by "," assertEquals(Optional.empty(), RDFSyntax.byMediaType("text/turtle, text/plain")); // no trimming will be done assertEquals(Optional.empty(), RDFSyntax.byMediaType(" text/turtle")); }
@Override public void update(final Graph graph, final String update, final RDFSyntax syntax, final String base) { requireNonNull(graph, "The input graph may not be null"); requireNonNull(update, "The update command may not be null"); requireNonNull(syntax, "The RDF syntax may not be null"); if (!SPARQL_UPDATE.equals(syntax)) { throw new RuntimeTrellisException("The syntax " + syntax + " is not supported for updates."); } try { final org.apache.jena.graph.Graph g = rdf.asJenaGraph(graph); execute(create(update, base), g); } catch (final UpdateException | QueryParseException ex) { throw new RuntimeTrellisException(ex); } }
@Override public boolean equals(final Object obj) { if (obj == this) { return true; } if (!(obj instanceof RDFSyntax)) { return false; } final RDFSyntax other = (RDFSyntax) obj; return mediaType.equals(other.mediaType().toLowerCase(Locale.ROOT)); }