Return-Path: X-Original-To: apmail-commonsrdf-commits-archive@minotaur.apache.org Delivered-To: apmail-commonsrdf-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 149D319243 for ; Sun, 27 Mar 2016 18:45:31 +0000 (UTC) Received: (qmail 96088 invoked by uid 500); 27 Mar 2016 18:45:31 -0000 Delivered-To: apmail-commonsrdf-commits-archive@commonsrdf.apache.org Received: (qmail 96059 invoked by uid 500); 27 Mar 2016 18:45:30 -0000 Mailing-List: contact commits-help@commonsrdf.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commonsrdf.incubator.apache.org Delivered-To: mailing list commits@commonsrdf.incubator.apache.org Received: (qmail 96050 invoked by uid 99); 27 Mar 2016 18:45:30 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd4-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 27 Mar 2016 18:45:30 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd4-us-west.apache.org (ASF Mail Server at spamd4-us-west.apache.org) with ESMTP id 7DC25C0140 for ; Sun, 27 Mar 2016 18:45:30 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd4-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -3.221 X-Spam-Level: X-Spam-Status: No, score=-3.221 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, KAM_LAZY_DOMAIN_SECURITY=1, RCVD_IN_DNSWL_HI=-5, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, RP_MATCHES_RCVD=-0.001] autolearn=disabled Received: from mx1-lw-eu.apache.org ([10.40.0.8]) by localhost (spamd4-us-west.apache.org [10.40.0.11]) (amavisd-new, port 10024) with ESMTP id I3925ZQD-MbL for ; Sun, 27 Mar 2016 18:45:28 +0000 (UTC) Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx1-lw-eu.apache.org (ASF Mail Server at mx1-lw-eu.apache.org) with SMTP id F1DB05F393 for ; Sun, 27 Mar 2016 18:45:26 +0000 (UTC) Received: (qmail 95936 invoked by uid 99); 27 Mar 2016 18:45:26 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 27 Mar 2016 18:45:26 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 1A332DFBA3; Sun, 27 Mar 2016 18:45:26 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: stain@apache.org To: commits@commonsrdf.incubator.apache.org Date: Sun, 27 Mar 2016 18:45:26 -0000 Message-Id: X-Mailer: ASF-Git Admin Mailer Subject: [1/3] incubator-commonsrdf git commit: Add RDFSyntax, enumeration of RDF languages Repository: incubator-commonsrdf Updated Branches: refs/heads/rdfsyntax-interface [created] f6e92cbb8 Add RDFSyntax, enumeration of RDF languages e.g. Turtle, JSON-LD This enumeration deliberately only lists RDF 1.1 standardized syntaxes (e.g. does not include N3). The only field (beyond the name for toString()) is the official mediaType, which can be used with byMediaType for a look up from Content-Type. Project: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/commit/1a63f483 Tree: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/tree/1a63f483 Diff: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/diff/1a63f483 Branch: refs/heads/rdfsyntax-interface Commit: 1a63f48379b39755d279cbe10a2b4e187b11ddb8 Parents: 3884ef5 Author: Stian Soiland-Reyes Authored: Sun Mar 27 18:13:45 2016 +0100 Committer: Stian Soiland-Reyes Committed: Sun Mar 27 18:13:45 2016 +0100 ---------------------------------------------------------------------- .../org/apache/commons/rdf/api/RDFSyntax.java | 152 +++++++++++++++++++ .../apache/commons/rdf/api/package-info.java | 3 + .../apache/commons/rdf/api/RDFSyntaxTest.java | 76 ++++++++++ 3 files changed, 231 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/1a63f483/api/src/main/java/org/apache/commons/rdf/api/RDFSyntax.java ---------------------------------------------------------------------- diff --git a/api/src/main/java/org/apache/commons/rdf/api/RDFSyntax.java b/api/src/main/java/org/apache/commons/rdf/api/RDFSyntax.java new file mode 100644 index 0000000..c1b5ea1 --- /dev/null +++ b/api/src/main/java/org/apache/commons/rdf/api/RDFSyntax.java @@ -0,0 +1,152 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.rdf.api; + +import java.util.Locale; +import java.util.Optional; + +/** + * Enumeration of the RDF 1.1 serialization syntaxes. + *

+ * This enumeration lists the W3C standardized + * RDF 1.1 syntaxes like {@link #TURTLE} and {@link #JSONLD}. + * Note the existence of other RDF syntaxes that are not included here, e.g. N3 + * and TriX. + * + * @see RDF 1.1 Primer + * + */ +public enum RDFSyntax { + + /** + * JSON-LD 1.0 + * + * @see https://www.w3.org/TR/json-ld/ + * + */ + JSONLD("JSON-LD 1.0", "application/ld+json"), + + /** + * RDF 1.1 Turtle + * + * @see RDF 1.1 Turtle + * + */ + TURTLE("RDF 1.1 Turtle", "text/turtle"), + + /** + * RDF 1.1 N-Quads + * + * @see https://www.w3.org/TR/n-quads/ + */ + NQUADS("RDF 1.1 N-Quads", "application/n-quads"), + + /** + * RDF 1.1 N-Triples + * + * @see https://www.w3.org/TR/n-triples/ + */ + NTRIPLES("RDF 1.1 N-Triples", "application/n-triples"), + + /** + * HTML+RDFa 1.1 + * + * @see https://www.w3.org/TR/html-rdfa/ + */ + RDFA_HTML("HTML+RDFa 1.1", "text/html"), + + /** + * XHTML+RDFa 1.1 + * + * @see https://www.w3.org/TR/xhtml-rdfa/ + */ + RDFA_XHTML("XHTML+RDFa 1.1", "application/xhtml+xml"), + + /** + * RDF 1.1 XML Syntax + * + * @see https://www.w3.org/TR/rdf-syntax-grammar/ + */ + RDFXML("RDF 1.1 XML Syntax", "application/rdf+xml"), + + /** + * RDF 1.1 TriG + * + * @see https://www.w3.org/TR/trig/ + */ + TRIG("RDF 1.1 TriG", "application/trig"); + + /** + * The IANA media type for the RDF syntax. + *

+ * The media type can be used as part of + * Content-Type + * and Accept for content negotiation in the + * HTTP protocol. + */ + public final String mediaType; + + private final String name; + + /** + * A human-readable name for the RDF syntax. + *

+ * The name is equivalent to the the title of the corresponding W3C Specification. + */ + @Override + public String toString() { + return name; + } + + private RDFSyntax(String name, String mediaType) { + this.name = name; + this.mediaType = mediaType; + } + + /** + * Return the RDFSyntax with the specified media type. + *

+ * The mediaType is compared in lower case, therefore it might + * not be equal to the {@link RDFSyntax#mediaType} of the returned + * RDFSyntax. + *

+ * For convenience matching of media types used in a + * Content-Type header, if the mediaType contains + * the characters ;, , or white space, only the + * part of the string to the left of those characters are considered. + * + * @param mediaType + * The media type to match + * @return If {@link Optional#isPresent()}, the {@link RDFSyntax} which has + * a matching {@link RDFSyntax#mediaType}, otherwise + * {@link Optional#empty()} indicating that + * no matching syntax was found. + */ + public static Optional byMediaType(String mediaType) { + mediaType = mediaType.toLowerCase(Locale.ENGLISH); + mediaType = mediaType.split("\\s*[;,]")[0]; + + for (RDFSyntax syntax : RDFSyntax.values()) { + if (mediaType.equals(syntax.mediaType)) { + return Optional.of(syntax); + } + } + return Optional.empty(); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/1a63f483/api/src/main/java/org/apache/commons/rdf/api/package-info.java ---------------------------------------------------------------------- diff --git a/api/src/main/java/org/apache/commons/rdf/api/package-info.java b/api/src/main/java/org/apache/commons/rdf/api/package-info.java index 43c6aaa..5fd5f5b 100644 --- a/api/src/main/java/org/apache/commons/rdf/api/package-info.java +++ b/api/src/main/java/org/apache/commons/rdf/api/package-info.java @@ -38,6 +38,9 @@ * MAY be mutable (e.g. support methods like * {@link org.apache.commons.rdf.api.Graph#add(Triple)}). *

+ * {@link org.apache.commons.rdf.api.RDFSyntax} enumerates the + * W3C standard RDF 1.1 syntaxes and their media types. + *

* For further documentation and contact details, see the * Commons RDF * web site. http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/1a63f483/api/src/test/java/org/apache/commons/rdf/api/RDFSyntaxTest.java ---------------------------------------------------------------------- diff --git a/api/src/test/java/org/apache/commons/rdf/api/RDFSyntaxTest.java b/api/src/test/java/org/apache/commons/rdf/api/RDFSyntaxTest.java new file mode 100644 index 0000000..701545f --- /dev/null +++ b/api/src/test/java/org/apache/commons/rdf/api/RDFSyntaxTest.java @@ -0,0 +1,76 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.rdf.api; + +import static org.junit.Assert.*; + +import java.util.Optional; + +import org.junit.Test; + +public class RDFSyntaxTest { + + @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("application/rdf+xml", RDFSyntax.RDFXML.mediaType); + assertEquals("application/trig", RDFSyntax.TRIG.mediaType); + assertEquals("text/turtle", RDFSyntax.TURTLE.mediaType); + } + + @Test + 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.RDFXML, RDFSyntax.byMediaType("application/rdf+xml").get()); + assertEquals(RDFSyntax.TRIG, RDFSyntax.byMediaType("application/trig").get()); + assertEquals(RDFSyntax.TURTLE, RDFSyntax.byMediaType("text/turtle").get()); + } + + @Test + public void name() throws Exception { + assertEquals("JSON-LD 1.0", RDFSyntax.JSONLD.toString()); + assertEquals("RDF 1.1 Turtle", RDFSyntax.TURTLE.toString()); + } + + @Test + public void byMediaTypeUnknown() throws Exception { + assertEquals(Optional.empty(), RDFSyntax.byMediaType("application/octet-stream")); + } + + @Test + public void byMediaTypeLowerCase() throws Exception { + assertEquals(RDFSyntax.JSONLD, RDFSyntax.byMediaType("APPLICATION/ld+JSON").get()); + } + + @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()); + assertEquals(RDFSyntax.TURTLE, RDFSyntax.byMediaType("text/turtle, text/plain").get()); + assertEquals(Optional.empty(), RDFSyntax.byMediaType(" text/turtle")); + } + +}