You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commonsrdf.apache.org by st...@apache.org on 2016/07/02 16:14:28 UTC

[11/50] [abbrv] incubator-commonsrdf git commit: Add RDFSyntax, enumeration of RDF languages

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/ada99978
Tree: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/tree/ada99978
Diff: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/diff/ada99978

Branch: refs/heads/jena
Commit: ada99978e9e8e19e9cf39c26080b70d8515c31bc
Parents: c817d18
Author: Stian Soiland-Reyes <st...@apache.org>
Authored: Sun Mar 27 18:13:45 2016 +0100
Committer: Stian Soiland-Reyes <st...@apache.org>
Committed: Sun Mar 27 21:25:04 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/ada99978/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.
+ * <p>
+ * 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. <a href="http://www.w3.org/TeamSubmission/n3/">N3</a> 
+ * and <a href="https://en.wikipedia.org/wiki/TriX_%28syntax%29">TriX</a>.
+ * 
+ * @see <a href="https://www.w3.org/TR/rdf11-primer/#section-graph-syntax">RDF 1.1 Primer</a>
+ *
+ */
+public enum RDFSyntax {
+	
+	/**
+	 * JSON-LD 1.0
+	 * 
+	 * @see <a href="https://www.w3.org/TR/json-ld/">https://www.w3.org/TR/json-ld/</a>
+	 * 
+	 */
+	JSONLD("JSON-LD 1.0", "application/ld+json"),
+
+	/**
+	 * RDF 1.1 Turtle
+	 * 
+	 * @see <a href="https://www.w3.org/TR/turtle/">RDF 1.1 Turtle</a>
+	 *
+	 */
+	TURTLE("RDF 1.1 Turtle", "text/turtle"), 
+
+	/**
+	 * RDF 1.1 N-Quads
+	 * 
+	 * @see <a href="https://www.w3.org/TR/n-quads/">https://www.w3.org/TR/n-quads/</a>
+	 */
+	NQUADS("RDF 1.1 N-Quads", "application/n-quads"),
+
+	/**
+	 * RDF 1.1 N-Triples
+	 * 
+	 * @see <a href="https://www.w3.org/TR/n-triples/">https://www.w3.org/TR/n-triples/</a>
+	 */
+	NTRIPLES("RDF 1.1 N-Triples", "application/n-triples"),
+	
+	/**
+	 * HTML+RDFa 1.1
+	 * 
+	 * @see <a href="https://www.w3.org/TR/html-rdfa/">https://www.w3.org/TR/html-rdfa/</a>
+	 */
+	RDFA_HTML("HTML+RDFa 1.1", "text/html"),
+	
+	/**
+	 * XHTML+RDFa 1.1 
+	 * 
+	 * @see <a href="https://www.w3.org/TR/xhtml-rdfa/">https://www.w3.org/TR/xhtml-rdfa/</a> 
+	 */
+	RDFA_XHTML("XHTML+RDFa 1.1", "application/xhtml+xml"),
+	
+	/**
+	 * RDF 1.1 XML Syntax
+	 * 
+	 * @see <a href="https://www.w3.org/TR/rdf-syntax-grammar/">https://www.w3.org/TR/rdf-syntax-grammar/</a>
+	 */
+	RDFXML("RDF 1.1 XML Syntax", "application/rdf+xml"),
+	
+	/**
+	 * RDF 1.1 TriG
+	 * 
+	 * @see <a href="https://www.w3.org/TR/trig/">https://www.w3.org/TR/trig/</a>
+	 */
+	TRIG("RDF 1.1 TriG", "application/trig");
+
+	/** 
+	 * The <a href="https://tools.ietf.org/html/rfc2046">IANA media type</a> for the RDF syntax.
+	 * <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>. 
+	 */
+	public final String mediaType;
+		
+	private final String name;
+	
+	/** 
+	 * A human-readable name for the RDF syntax.
+	 * <p>
+	 * 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.
+	 * <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.
+	 * <p>
+	 * For convenience matching of media types used in a
+	 * <code>Content-Type</code> header, if the <code>mediaType</code> contains
+	 * the characters <code>;</code>, <code>,</code> 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<RDFSyntax> 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/ada99978/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)}).
  * <p>
+ * {@link org.apache.commons.rdf.api.RDFSyntax} enumerates the 
+ * W3C standard RDF 1.1 syntaxes and their media types.
+ * <p>
  * For further documentation and contact details, see the
  * <a href="http://commonsrdf.incubator.apache.org/">Commons RDF</a>
  * web site.

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/ada99978/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"));
+	}
+
+}