
org.apache.commons.rdf.api.RDFSyntax Maven / Gradle / Ivy
Show all versions of commons-rdf-api Show documentation
/**
* 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.Arrays;
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", ".jsonld", true),
/**
* RDF 1.1 Turtle
*
* @see https://www.w3.org/TR/turtle/
*
*/
TURTLE("RDF 1.1 Turtle", "text/turtle", ".ttl", false),
/**
* RDF 1.1 N-Quads
*
* @see https://www.w3.org/TR/n-quads/
*/
NQUADS("RDF 1.1 N-Quads", "application/n-quads", ".nq", true),
/**
* RDF 1.1 N-Triples
*
* @see https://www.w3.org/TR/n-triples/
*/
NTRIPLES("RDF 1.1 N-Triples", "application/n-triples", ".nt", false),
/**
* HTML+RDFa 1.1
*
* @see https://www.w3.org/TR/html-rdfa/
*/
RDFA_HTML("HTML+RDFa 1.1", "text/html", ".html", false),
/**
* XHTML+RDFa 1.1
*
* @see https://www.w3.org/TR/xhtml-rdfa/
*/
RDFA_XHTML("XHTML+RDFa 1.1", "application/xhtml+xml", ".xhtml", false),
/**
* RDF 1.1 XML Syntax
*
* @see https://www.w3.org/TR/rdf-syntax-grammar/
*/
RDFXML("RDF 1.1 XML Syntax", "application/rdf+xml", ".rdf", false),
/**
* RDF 1.1 TriG
*
* @see https://www.w3.org/TR/trig/
*/
TRIG("RDF 1.1 TriG", "application/trig", ".trig", true);
/**
* 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;
/**
* The IANA-registered file extension.
*
* The file extension includes the leading period, e.g. .jsonld
*/
public final String fileExtension;
/**
* Indicate if this RDF syntax supports RDF Datasets.
*/
public final boolean supportsDataset;
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, String fileExtension, boolean supportsDataset) {
this.name = name;
this.mediaType = mediaType;
this.fileExtension = fileExtension;
this.supportsDataset = supportsDataset;
}
/**
* 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) {
final String type = mediaType.toLowerCase(Locale.ENGLISH).
split("\\s*[;,]", 2)[0];
return Arrays.stream(RDFSyntax.values()).filter(
t -> t.mediaType.equals(type)).findAny();
}
/**
* Return the RDFSyntax with the specified file extension.
*
* The fileExtension
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 .
* @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 byFileExtension(String fileExtension) {
final String ext = fileExtension.toLowerCase(Locale.ENGLISH);
return Arrays.stream(RDFSyntax.values()).filter(
t -> t.fileExtension.equals(ext)).findAny();
}
}