commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From st...@apache.org
Subject [01/12] commons-rdf git commit: COMMONSRDF-47 RDFSyntax as an interface
Date Thu, 16 Feb 2017 11:56:16 GMT
Repository: commons-rdf
Updated Branches:
  refs/heads/master f6ea05832 -> ef7f4442a


COMMONSRDF-47 RDFSyntax as an interface

OfficialRDFSyntax is the enum - which implements the interface

basically .mediaType -> .mediaType() etc


Project: http://git-wip-us.apache.org/repos/asf/commons-rdf/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-rdf/commit/ee725a5a
Tree: http://git-wip-us.apache.org/repos/asf/commons-rdf/tree/ee725a5a
Diff: http://git-wip-us.apache.org/repos/asf/commons-rdf/diff/ee725a5a

Branch: refs/heads/master
Commit: ee725a5aaf5d94c9ab5ff7f355bb5d0cb370bfa7
Parents: 34c9b42
Author: Stian Soiland-Reyes <stain@apache.org>
Authored: Fri Oct 28 16:34:49 2016 +0100
Committer: Stian Soiland-Reyes <stain@apache.org>
Committed: Fri Oct 28 16:35:52 2016 +0100

----------------------------------------------------------------------
 .../org/apache/commons/rdf/api/RDFSyntax.java   | 212 +++++++++++++------
 .../apache/commons/rdf/api/AbstractRDFTest.java |   1 -
 .../apache/commons/rdf/api/RDFSyntaxTest.java   |  32 +--
 .../org/apache/commons/rdf/jena/JenaRDF.java    |   2 +-
 .../simple/experimental/AbstractRDFParser.java  |   2 +-
 5 files changed, 165 insertions(+), 84 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-rdf/blob/ee725a5a/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
index 2637a96..e267157 100644
--- a/api/src/main/java/org/apache/commons/rdf/api/RDFSyntax.java
+++ b/api/src/main/java/org/apache/commons/rdf/api/RDFSyntax.java
@@ -22,6 +22,124 @@ import java.util.Locale;
 import java.util.Optional;
 
 /**
+ * An RDF syntax, e.g. as used for parsing and writing RDF.
+ * <p>
+ * An RDF syntax is uniquely identified by its {@link #mediaType()}, and has a
+ * suggested {@link #fileExtension()}.
+ * <p>
+ * Some of the RDF syntaxes may {@link #supportsDataset()}, meaning they can
+ * represent {@link Quad}s.
+ * <p>
+ * An enumeration of the official RDF 1.1 syntaxes is available in 
+ * {@link OfficialRDFSyntax} - for convenience they are also accessible
+ * as constants here, e.g. <code>RDFSyntax.JSONLD</code>.
+ * 
+ */
+public interface RDFSyntax {
+ 
+    public static OfficialRDFSyntax JSONLD = OfficialRDFSyntax.JSONLD;
+    public static OfficialRDFSyntax TURTLE = OfficialRDFSyntax.TURTLE;
+    public static OfficialRDFSyntax NQUADS = OfficialRDFSyntax.NQUADS;
+    public static OfficialRDFSyntax NTRIPLES = OfficialRDFSyntax.NTRIPLES;
+    public static OfficialRDFSyntax RDFA_HTML = OfficialRDFSyntax.RDFA_HTML;
+    public static OfficialRDFSyntax RDFA_XHTML = OfficialRDFSyntax.RDFA_XHTML;
+    public static OfficialRDFSyntax RDFXML = OfficialRDFSyntax.RDFXML;
+    public static OfficialRDFSyntax TRIG = OfficialRDFSyntax.TRIG;
+    
+    /**
+     * A short name of the RDF Syntax.
+     * <p>
+     * The name typically corresponds to the {@link Enum#name()} of for
+     * {@link OfficialRDFSyntax}, e.g. <code>JSONLD</code>.
+     * 
+     * @return Short name for RDF syntax
+     */
+    public String name();
+
+    /**
+     * The title of the RDF Syntax.
+     * <p>
+     * This is generally the title of the corresponding standard, 
+     * e.g. <em>RDF 1.1 Turtle</em>.
+     * 
+     * @return Title of RDF Syntax
+     */
+    public String title();    
+    
+    /**
+     * 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 String mediaType();
+
+    /**
+     * The <a href="https://tools.ietf.org/html/rfc2046">IANA-registered</a>
+     * file extension.
+     * <p>
+     * The file extension includes the leading period, e.g. <code>.jsonld</code>
+     */
+    public String fileExtension();
+
+    /**
+     * Indicate if this RDF syntax supports
+     * <a href="https://www.w3.org/TR/rdf11-concepts/#section-dataset">RDF
+     * Datasets</a>.
+     */
+    public boolean supportsDataset();
+
+
+    /**
+     * 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) {
+        final String type = mediaType.toLowerCase(Locale.ENGLISH).split("\\s*[;,]", 2)[0];
+        return Arrays.stream(OfficialRDFSyntax.values()).filter(t -> t.mediaType().equals(type))
+                .map(RDFSyntax.class::cast).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.
+     * 
+     * @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(String fileExtension) {
+        final String ext = fileExtension.toLowerCase(Locale.ENGLISH);        
+        return Arrays.stream(OfficialRDFSyntax.values()).filter(t -> t.fileExtension().equals(ext))
+                .map(RDFSyntax.class::cast).findAny();
+    }    
+    
+
+/**
  * Enumeration of the RDF 1.1 serialization syntaxes.
  * <p>
  * This enumeration lists the W3C standardized RDF 1.1 syntaxes like
@@ -34,7 +152,7 @@ import java.util.Optional;
  *      1.1 Primer</a>
  * @see org.apache.commons.rdf.experimental.RDFParser
  */
-public enum RDFSyntax {
+  public enum OfficialRDFSyntax implements RDFSyntax {
 
     /**
      * JSON-LD 1.0
@@ -103,92 +221,56 @@ public enum RDFSyntax {
     TRIG("RDF 1.1 TriG", "application/trig", ".trig", true);
 
     /**
-     * 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>.
+     * Deprecated, use {@link #mediaType()}. 
      */
+    @Deprecated
     public final String mediaType;
 
     /**
-     * The <a href="https://tools.ietf.org/html/rfc2046">IANA-registered</a>
-     * file extension.
-     * <p>
-     * The file extension includes the leading period, e.g. <code>.jsonld</code>
+     * Deprecated, use {@link #fileExtension()}.
      */
+    @Deprecated
     public final String fileExtension;
 
     /**
-     * Indicate if this RDF syntax supports
-     * <a href="https://www.w3.org/TR/rdf11-concepts/#section-dataset">RDF
-     * Datasets</a>.
+     * Deprecated, use {@link #supportsDataset()}.
      */
+    @Deprecated
     public final boolean supportsDataset;
 
-    private final String name;
+    private final String title;
 
-    /**
-     * 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;
+        return title();
     }
 
-    private RDFSyntax(String name, String mediaType, String fileExtension, boolean supportsDataset)
{
-        this.name = name;
+    private OfficialRDFSyntax(String title, String mediaType, String fileExtension, boolean
supportsDataset) {
+        this.title = title;
         this.mediaType = mediaType;
         this.fileExtension = fileExtension;
         this.supportsDataset = supportsDataset;
     }
 
-    /**
-     * 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) {
-        final String type = mediaType.toLowerCase(Locale.ENGLISH).split("\\s*[;,]", 2)[0];
-        return Arrays.stream(RDFSyntax.values()).filter(t -> t.mediaType.equals(type)).findAny();
+    @Override
+    public String mediaType() {
+        return mediaType;
     }
 
-    /**
-     * 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.
-     * 
-     * @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(String fileExtension) {
-        final String ext = fileExtension.toLowerCase(Locale.ENGLISH);
-        return Arrays.stream(RDFSyntax.values()).filter(t -> t.fileExtension.equals(ext)).findAny();
+    @Override
+    public String fileExtension() {
+        return fileExtension;
+    }
+
+    @Override
+    public boolean supportsDataset() {
+        return supportsDataset;
+    }
+    
+    @Override
+    public String title() {
+        return title;
     }
 
-}
+  }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/commons-rdf/blob/ee725a5a/api/src/test/java/org/apache/commons/rdf/api/AbstractRDFTest.java
----------------------------------------------------------------------
diff --git a/api/src/test/java/org/apache/commons/rdf/api/AbstractRDFTest.java b/api/src/test/java/org/apache/commons/rdf/api/AbstractRDFTest.java
index e58ede6..bd3865a 100644
--- a/api/src/test/java/org/apache/commons/rdf/api/AbstractRDFTest.java
+++ b/api/src/test/java/org/apache/commons/rdf/api/AbstractRDFTest.java
@@ -24,7 +24,6 @@ import static org.junit.Assert.assertNotSame;
 
 import java.util.Objects;
 
-import org.junit.Assume;
 import org.junit.Before;
 import org.junit.Test;
 

http://git-wip-us.apache.org/repos/asf/commons-rdf/blob/ee725a5a/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
index a1cb62e..335eb90 100644
--- a/api/src/test/java/org/apache/commons/rdf/api/RDFSyntaxTest.java
+++ b/api/src/test/java/org/apache/commons/rdf/api/RDFSyntaxTest.java
@@ -84,26 +84,26 @@ public class RDFSyntaxTest {
 
     @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_HTML.fileExtension);
-        assertEquals(".xhtml", RDFSyntax.RDFA_XHTML.fileExtension);
-        assertEquals(".rdf", RDFSyntax.RDFXML.fileExtension);
-        assertEquals(".trig", RDFSyntax.TRIG.fileExtension);
-        assertEquals(".ttl", RDFSyntax.TURTLE.fileExtension);
+        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(".rdf", RDFSyntax.RDFXML.fileExtension());
+        assertEquals(".trig", RDFSyntax.TRIG.fileExtension());
+        assertEquals(".ttl", RDFSyntax.TURTLE.fileExtension());
     }
 
     @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);
+        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

http://git-wip-us.apache.org/repos/asf/commons-rdf/blob/ee725a5a/jena/src/main/java/org/apache/commons/rdf/jena/JenaRDF.java
----------------------------------------------------------------------
diff --git a/jena/src/main/java/org/apache/commons/rdf/jena/JenaRDF.java b/jena/src/main/java/org/apache/commons/rdf/jena/JenaRDF.java
index b49656f..036e084 100644
--- a/jena/src/main/java/org/apache/commons/rdf/jena/JenaRDF.java
+++ b/jena/src/main/java/org/apache/commons/rdf/jena/JenaRDF.java
@@ -551,7 +551,7 @@ public final class JenaRDF implements RDF {
      * @return Matched {@link Lang}, otherwise {@link Optional#empty()}
      */
     public Optional<Lang> asJenaLang(RDFSyntax rdfSyntax) {
-        return Optional.ofNullable(RDFLanguages.contentTypeToLang(rdfSyntax.mediaType));
+        return Optional.ofNullable(RDFLanguages.contentTypeToLang(rdfSyntax.mediaType()));
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/commons-rdf/blob/ee725a5a/simple/src/main/java/org/apache/commons/rdf/simple/experimental/AbstractRDFParser.java
----------------------------------------------------------------------
diff --git a/simple/src/main/java/org/apache/commons/rdf/simple/experimental/AbstractRDFParser.java
b/simple/src/main/java/org/apache/commons/rdf/simple/experimental/AbstractRDFParser.java
index 584bff3..62fe79c 100644
--- a/simple/src/main/java/org/apache/commons/rdf/simple/experimental/AbstractRDFParser.java
+++ b/simple/src/main/java/org/apache/commons/rdf/simple/experimental/AbstractRDFParser.java
@@ -240,7 +240,7 @@ public abstract class AbstractRDFParser<T extends AbstractRDFParser<T>>
implemen
     public T contentType(RDFSyntax rdfSyntax) throws IllegalArgumentException {
         AbstractRDFParser<T> c = clone();
         c.contentTypeSyntax = Optional.ofNullable(rdfSyntax);
-        c.contentType = c.contentTypeSyntax.map(syntax -> syntax.mediaType);
+        c.contentType = c.contentTypeSyntax.map(syntax -> syntax.mediaType());
         return c.asT();
     }
 


Mime
View raw message