View Javadoc
1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.commons.rdf.api;
19  
20  import java.util.Arrays;
21  import java.util.Locale;
22  import java.util.Optional;
23  
24  /**
25   * Enumeration of the RDF 1.1 serialization syntaxes.
26   * <p>
27   * This enumeration lists the W3C standardized RDF 1.1 syntaxes like
28   * {@link #TURTLE} and {@link #JSONLD}. Note the existence of other RDF syntaxes
29   * that are not included here, e.g.
30   * <a href="http://www.w3.org/TeamSubmission/n3/">N3</a> and
31   * <a href="https://en.wikipedia.org/wiki/TriX_%28syntax%29">TriX</a>.
32   * 
33   * @see <a href="https://www.w3.org/TR/rdf11-primer/#section-graph-syntax">RDF
34   *      1.1 Primer</a>
35   * @see org.apache.commons.rdf.experimental.RDFParser
36   */
37  public enum RDFSyntax {
38  
39      /**
40       * JSON-LD 1.0
41       * 
42       * @see <a href=
43       *      "https://www.w3.org/TR/json-ld/">https://www.w3.org/TR/json-ld/</a>
44       * 
45       */
46      JSONLD("JSON-LD 1.0", "application/ld+json", ".jsonld", true),
47  
48      /**
49       * RDF 1.1 Turtle
50       * 
51       * @see <a href=
52       *      "https://www.w3.org/TR/turtle/">https://www.w3.org/TR/turtle/</a>
53       *
54       */
55      TURTLE("RDF 1.1 Turtle", "text/turtle", ".ttl", false),
56  
57      /**
58       * RDF 1.1 N-Quads
59       * 
60       * @see <a href=
61       *      "https://www.w3.org/TR/n-quads/">https://www.w3.org/TR/n-quads/</a>
62       */
63      NQUADS("RDF 1.1 N-Quads", "application/n-quads", ".nq", true),
64  
65      /**
66       * RDF 1.1 N-Triples
67       * 
68       * @see <a href=
69       *      "https://www.w3.org/TR/n-triples/">https://www.w3.org/TR/n-triples/</a>
70       */
71      NTRIPLES("RDF 1.1 N-Triples", "application/n-triples", ".nt", false),
72  
73      /**
74       * HTML+RDFa 1.1
75       * 
76       * @see <a href=
77       *      "https://www.w3.org/TR/html-rdfa/">https://www.w3.org/TR/html-rdfa/</a>
78       */
79      RDFA_HTML("HTML+RDFa 1.1", "text/html", ".html", false),
80  
81      /**
82       * XHTML+RDFa 1.1
83       * 
84       * @see <a href=
85       *      "https://www.w3.org/TR/xhtml-rdfa/">https://www.w3.org/TR/xhtml-rdfa/</a>
86       */
87      RDFA_XHTML("XHTML+RDFa 1.1", "application/xhtml+xml", ".xhtml", false),
88  
89      /**
90       * RDF 1.1 XML Syntax
91       * 
92       * @see <a href=
93       *      "https://www.w3.org/TR/rdf-syntax-grammar/">https://www.w3.org/TR/rdf-syntax-grammar/</a>
94       */
95      RDFXML("RDF 1.1 XML Syntax", "application/rdf+xml", ".rdf", false),
96  
97      /**
98       * RDF 1.1 TriG
99       * 
100      * @see <a href=
101      *      "https://www.w3.org/TR/trig/">https://www.w3.org/TR/trig/</a>
102      */
103     TRIG("RDF 1.1 TriG", "application/trig", ".trig", true);
104 
105     /**
106      * The <a href="https://tools.ietf.org/html/rfc2046">IANA media type</a> for
107      * the RDF syntax.
108      * <p>
109      * The media type can be used as part of <code>Content-Type</code> and
110      * <code>Accept</code> for <em>content negotiation</em> in the
111      * <a href="https://tools.ietf.org/html/rfc7231#section-3.1.1.1">HTTP
112      * protocol</a>.
113      */
114     public final String mediaType;
115 
116     /**
117      * The <a href="https://tools.ietf.org/html/rfc2046">IANA-registered</a>
118      * file extension.
119      * <p>
120      * The file extension includes the leading period, e.g. <code>.jsonld</code>
121      */
122     public final String fileExtension;
123 
124     /**
125      * Indicate if this RDF syntax supports
126      * <a href="https://www.w3.org/TR/rdf11-concepts/#section-dataset">RDF
127      * Datasets</a>.
128      */
129     public final boolean supportsDataset;
130 
131     private final String name;
132 
133     /**
134      * A human-readable name for the RDF syntax.
135      * <p>
136      * The name is equivalent to the the title of the corresponding W3C
137      * Specification.
138      */
139     @Override
140     public String toString() {
141         return name;
142     }
143 
144     private RDFSyntax(final String name, final String mediaType, final String fileExtension, final boolean supportsDataset) {
145         this.name = name;
146         this.mediaType = mediaType;
147         this.fileExtension = fileExtension;
148         this.supportsDataset = supportsDataset;
149     }
150 
151     /**
152      * Return the RDFSyntax with the specified media type.
153      * <p>
154      * The <code>mediaType</code> is compared in lower case, therefore it might
155      * not be equal to the {@link RDFSyntax#mediaType} of the returned
156      * RDFSyntax.
157      * <p>
158      * For convenience matching of media types used in a
159      * <code>Content-Type</code> header, if the <code>mediaType</code> contains
160      * the characters <code>;</code>, <code>,</code> or white space, only the
161      * part of the string to the left of those characters are considered.
162      * 
163      * @param mediaType
164      *            The media type to match
165      * @return If {@link Optional#isPresent()}, the {@link RDFSyntax} which has
166      *         a matching {@link RDFSyntax#mediaType}, otherwise
167      *         {@link Optional#empty()} indicating that no matching syntax was
168      *         found.
169      */
170     public static Optional<RDFSyntax> byMediaType(final String mediaType) {
171         final String type = mediaType.toLowerCase(Locale.ENGLISH).split("\\s*[;,]", 2)[0];
172         return Arrays.stream(RDFSyntax.values()).filter(t -> t.mediaType.equals(type)).findAny();
173     }
174 
175     /**
176      * Return the RDFSyntax with the specified file extension.
177      * <p>
178      * The <code>fileExtension</code> is compared in lower case, therefore it
179      * might not be equal to the {@link RDFSyntax#fileExtension} of the returned
180      * RDFSyntax.
181      * 
182      * @param fileExtension
183      *            The fileExtension to match, starting with <code>.</code>
184      * @return If {@link Optional#isPresent()}, the {@link RDFSyntax} which has
185      *         a matching {@link RDFSyntax#fileExtension}, otherwise
186      *         {@link Optional#empty()} indicating that no matching file
187      *         extension was found.
188      */
189     public static Optional<RDFSyntax> byFileExtension(final String fileExtension) {
190         final String ext = fileExtension.toLowerCase(Locale.ENGLISH);
191         return Arrays.stream(RDFSyntax.values()).filter(t -> t.fileExtension.equals(ext)).findAny();
192     }
193 
194 }