Example usage for org.apache.commons.rdf.api RDFSyntax mediaType

List of usage examples for org.apache.commons.rdf.api RDFSyntax mediaType

Introduction

In this page you can find the example usage for org.apache.commons.rdf.api RDFSyntax mediaType.

Prototype

String mediaType

To view the source code for org.apache.commons.rdf.api RDFSyntax mediaType.

Click Source Link

Document

The <a href="https://tools.ietf.org/html/rfc2046">IANA media type</a> for the RDF syntax.

Usage

From source file:org.trellisldp.io.JenaIOService.java

@Override
public void write(final Stream<Triple> triples, final OutputStream output, final RDFSyntax syntax,
        final IRI... profiles) {
    requireNonNull(triples, "The triples stream may not be null!");
    requireNonNull(output, "The output stream may not be null!");
    requireNonNull(syntax, "The RDF syntax value may not be null!");

    try {// w w w  .j  a  v  a2 s .  co  m
        if (RDFA.equals(syntax)) {
            writeHTML(triples, output, profiles.length > 0 ? profiles[0].getIRIString() : null);
        } else {
            final Lang lang = rdf.asJenaLang(syntax).orElseThrow(
                    () -> new RuntimeTrellisException("Invalid content type: " + syntax.mediaType()));

            final RDFFormat format = defaultSerialization(lang);

            if (nonNull(format)) {
                LOGGER.debug("Writing stream-based RDF: {}", format);
                final StreamRDF stream = getWriterStream(output, format);
                stream.start();
                ofNullable(nsService).ifPresent(svc -> svc.getNamespaces().forEach(stream::prefix));
                triples.map(rdf::asJenaTriple).forEachOrdered(stream::triple);
                stream.finish();
            } else {
                LOGGER.debug("Writing buffered RDF: {}", lang);
                final org.apache.jena.graph.Graph graph = createDefaultGraph();
                ofNullable(nsService).map(NamespaceService::getNamespaces)
                        .ifPresent(graph.getPrefixMapping()::setNsPrefixes);
                triples.map(rdf::asJenaTriple).forEachOrdered(graph::add);
                if (JSONLD.equals(lang)) {
                    writeJsonLd(output, DatasetGraphFactory.create(graph), profiles);
                } else {
                    RDFDataMgr.write(output, graph, lang);
                }
            }
        }
    } catch (final AtlasException ex) {
        throw new RuntimeTrellisException(ex);
    }
}

From source file:org.trellisldp.io.JenaIOService.java

@Override
public Stream<Triple> read(final InputStream input, final RDFSyntax syntax, final String base) {
    requireNonNull(input, "The input stream may not be null!");
    requireNonNull(syntax, "The syntax value may not be null!");

    try {/*from ww  w  . ja  v  a 2s  .  co m*/
        final org.apache.jena.graph.Graph graph = createDefaultGraph();
        final Lang lang = rdf.asJenaLang(syntax).orElseThrow(
                () -> new RuntimeTrellisException("Unsupported RDF Syntax: " + syntax.mediaType()));

        RDFParser.source(input).lang(lang).base(base).parse(graph);

        // Check the graph for any new namespace definitions
        final Set<String> namespaces = nsService.getNamespaces().entrySet().stream().map(Map.Entry::getValue)
                .collect(toSet());
        graph.getPrefixMapping().getNsPrefixMap().forEach((prefix, namespace) -> {
            if (!namespaces.contains(namespace)) {
                LOGGER.debug("Setting prefix ({}) for namespace {}", prefix, namespace);
                nsService.setPrefix(prefix, namespace);
            }
        });
        return rdf.asGraph(graph).stream().map(Triple.class::cast);
    } catch (final RiotException | AtlasException | IllegalArgumentException ex) {
        throw new RuntimeTrellisException(ex);
    }
}