commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From st...@apache.org
Subject commons-rdf git commit: COMMONSRDF-47: Add plural fileExtensions() mediaTypes()
Date Fri, 10 Feb 2017 16:54:11 GMT
Repository: commons-rdf
Updated Branches:
  refs/heads/COMMONSRDF-47 701e47ec2 -> a574473c0


COMMONSRDF-47: Add plural fileExtensions() mediaTypes()

merged RDFa types for HTML and XHTML
byMediaType() and byFileExtension() now checks the set

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

Branch: refs/heads/COMMONSRDF-47
Commit: a574473c04746c7d1e67ddf4a3a77de6dee32f5f
Parents: 701e47e
Author: Stian Soiland-Reyes <stain@apache.org>
Authored: Fri Feb 10 16:53:58 2017 +0000
Committer: Stian Soiland-Reyes <stain@apache.org>
Committed: Fri Feb 10 16:53:58 2017 +0000

----------------------------------------------------------------------
 .../org/apache/commons/rdf/api/RDFSyntax.java   | 55 ++++++++++++++------
 .../apache/commons/rdf/api/W3CRDFSyntax.java    | 26 ++++++---
 .../apache/commons/rdf/api/RDFSyntaxTest.java   | 46 ++++++++++++----
 3 files changed, 92 insertions(+), 35 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-rdf/blob/a574473c/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 657fbc3..c82c0f5 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
@@ -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
@@ -134,6 +129,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.
      * <p>
@@ -142,6 +153,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
      * Datasets</a>.
@@ -211,9 +234,9 @@ public interface RDFSyntax {
     /**
      * 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 interface RDFSyntax {
      */
     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 interface RDFSyntax {
      */
     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();
     }
     

http://git-wip-us.apache.org/repos/asf/commons-rdf/blob/a574473c/api/src/main/java/org/apache/commons/rdf/api/W3CRDFSyntax.java
----------------------------------------------------------------------
diff --git a/api/src/main/java/org/apache/commons/rdf/api/W3CRDFSyntax.java b/api/src/main/java/org/apache/commons/rdf/api/W3CRDFSyntax.java
index 82b6692..6b88955 100644
--- a/api/src/main/java/org/apache/commons/rdf/api/W3CRDFSyntax.java
+++ b/api/src/main/java/org/apache/commons/rdf/api/W3CRDFSyntax.java
@@ -39,8 +39,7 @@ import java.util.Set;
  *      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 @@ final class W3CRDFSyntax implements RDFSyntax {
     }
 
     
-    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 @@ final class W3CRDFSyntax implements RDFSyntax {
         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;

http://git-wip-us.apache.org/repos/asf/commons-rdf/blob/a574473c/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 50b934f..5494147 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
@@ -30,8 +30,7 @@ public class RDFSyntaxTest {
         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 class RDFSyntaxTest {
         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 class RDFSyntaxTest {
         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());
+        }
     }
     
 }


Mime
View raw message