Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
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.marmotta.platform.core.services.exporter;
 
 
 import java.io.Writer;
 import java.util.Set;

Methods for writing triple data into different targets using different exporters.

Author(s):
Sebastian Schaffert
 
 public class ExporterServiceImpl implements ExportService {
 
     @Inject
     private Logger log;
 
     @Inject
     private MarmottaIOService ioService;
 
 
     @Inject
     private SesameService sesameService;



    
Get a collection of all mime types accepted by this exporter. Used for automatically selecting the appropriate exporter in ExportService.

Returns:
a set of strings representing the mime types accepted by this exporter
 
     @Override
     public Set<StringgetProducedTypes() {
         return ImmutableSet.copyOf(.getProducedTypes());
     }

    
Export the triple data contained in the named graph passed as argument "context" and return it as a Java string using the serialisation format specified by "mimeType".

The context parameter limits the exported triples to the named graph represented by this resource. If it is set to null, all named graphs will be exported.

The mime type must be supported by at least one of the registered exporters, otherwise an UnsupportedExporterException. Available mime types can be retrieved using the getProducedTypes() method.

Parameters:
context the named graph to export; if null, all named graphs will be exported
mimeType a mime type registered by an exporter
Throws:
org.apache.marmotta.platform.core.exception.io.UnsupportedExporterException in case there is no matching exporter for the given mime type
 
     @Override
     public String exportData(URI contextString mimeTypethrows UnsupportedExporterException {
         RDFFormat serializer = .getSerializer(mimeType);
         if(serializer == null) {
             .warn("could not find serializer for MIME type {}",mimeType);
             throw new UnsupportedExporterException("No serializer for mime type "+mimeType);
         }
         try {
             StringWriter writer = new StringWriter();
            exportData(writer,context,mimeType);
            return writer.toString();
        } catch (IOException e) {
            .error("impossible I/O error while writing to string buffer",e);
            return null;
        }
    }

    
Export the triple data contained in the named graph passed as argument "context" and write it to the writer given as first argument using the serialisation format specified by "mimeType".

The context parameter limits the exported triples to the named graph represented by this resource. If it is set to null, all named graphs will be exported.

The mime type must be supported by at least one of the registered exporters, otherwise an UnsupportedExporterException. Available mime types can be retrieved using the getProducedTypes() method.

Parameters:
writer the writer to write the triples to; will be closed when the triples are written
context the named graph to export; if null, all named graphs will be exported
mimeType a mime type registered by an exporter
Throws:
org.apache.marmotta.platform.core.exception.io.UnsupportedExporterException in case there is no matching exporter for the given mime type
java.io.IOException in case there is an error writing to the output
    @Override
    public void exportData(Writer writerURI contextString mimeTypethrows UnsupportedExporterExceptionIOException {
        RDFFormat serializer = .getSerializer(mimeType);
        if(serializer == null) {
            .warn("could not find serializer for MIME type {}",mimeType);
            throw new UnsupportedExporterException("No serializer for mime type "+mimeType);
        }
        // HINT: This method might be executed outside a transaction!
        RDFWriter handler = Rio.createWriter(serializer,writer);
        try {
            RepositoryConnection connection = .getConnection();
            connection.begin();
            try {
                if(context == null) {
                    connection.exportStatements(null,null,null,true,handler);
                } else {
                    connection.exportStatements(null,null,null,true,handler,context);
                }
            } finally {
                connection.commit();
                connection.close();
            }
        } catch (RepositoryException e) {
            throw new IOException("error while getting repository connection");
        } catch (RDFHandlerException e) {
            throw new IOException("error while writing RDF data to stream");
        }
    }

    
Export the triple data contained in the named graph passed as argument "context" and write it to the output stream given as first argument using the serialisation format specified by "mimeType".

The context parameter limits the exported triples to the named graph represented by this resource. If it is set to null, all named graphs will be exported.

The mime type must be supported by at least one of the registered exporters, otherwise an UnsupportedExporterException. Available mime types can be retrieved using the getProducedTypes() method.

Parameters:
outputStream the OutputStream to write the triples to; data will be written using UTF-8 encoding; will be closed when the triples are written
context the named graph to export; if null, all named graphs will be exported
mimeType a mime type registered by an exporter
Throws:
org.apache.marmotta.platform.core.exception.io.UnsupportedExporterException in case there is no matching exporter for the given mime type
java.io.IOException in case there is an error writing to the output
    @Override
    public void exportData(OutputStream outputStreamURI contextString mimeTypethrows UnsupportedExporterExceptionIOException {
        RDFFormat serializer = .getSerializer(mimeType);
        if(serializer == null) {
            .warn("could not find serializer for MIME type {}",mimeType);
            throw new UnsupportedExporterException("No serializer for mime type "+mimeType);
        }
        // HINT: This method might be executed outside a transaction!
        RDFWriter handler = Rio.createWriter(serializer,outputStream);
        try {
            RepositoryConnection connection = .getConnection();
            try {
                if(context == null) {
                    connection.exportStatements(null,null,null,true,handler);
                } else {
                    connection.exportStatements(null,null,null,true,handler,context);
                }
            } finally {
                connection.commit();
                connection.close();
            }
        } catch (RepositoryException e) {
            throw new IOException("error while getting repository connection");
        } catch (RDFHandlerException e) {
            throw new IOException("error while writing RDF data to stream");
        }
    }


    
Export the triple data for the given resource contained in the named graph passed as argument "context" and return it as a Java string using the serialisation format specified by "mimeType".

The context parameter limits the exported triples to the named graph represented by this resource. If it is set to null, all named graphs will be exported.

The mime type must be supported by at least one of the registered exporters, otherwise an UnsupportedExporterException. Available mime types can be retrieved using the getProducedTypes() method.

Parameters:
context the named graph to export; if null, all named graphs will be exported
mimeType a mime type registered by an exporter
Throws:
org.apache.marmotta.platform.core.exception.io.UnsupportedExporterException in case there is no matching exporter for the given mime type
    @Override
    public String exportData(URI resourceURI contextString mimeTypethrows UnsupportedExporterException {
        RDFFormat serializer = .getSerializer(mimeType);
        if(serializer == null) {
            .warn("could not find serializer for MIME type {}",mimeType);
            throw new UnsupportedExporterException("No serializer for mime type "+mimeType);
        }
        try {
            StringWriter writer = new StringWriter();
            exportData(writer,resource,context,mimeType);
            return writer.toString();
        } catch (IOException e) {
            .error("impossible I/O error while writing to string buffer",e);
            return null;
        }
    }

    
Export the triple data for the given resource contained in the named graph passed as argument "context" and write it to the writer given as first argument using the serialisation format specified by "mimeType".

The context parameter limits the exported triples to the named graph represented by this resource. If it is set to null, all named graphs will be exported.

The mime type must be supported by at least one of the registered exporters, otherwise an UnsupportedExporterException. Available mime types can be retrieved using the getProducedTypes() method.

Parameters:
writer the writer to write the triples to; will be closed when the triples are written
context the named graph to export; if null, all named graphs will be exported
mimeType a mime type registered by an exporter
Throws:
org.apache.marmotta.platform.core.exception.io.UnsupportedExporterException in case there is no matching exporter for the given mime type
java.io.IOException in case there is an error writing to the output
    @Override
    public void exportData(Writer writerURI resourceURI contextString mimeTypethrows UnsupportedExporterExceptionIOException {
        RDFFormat serializer = .getSerializer(mimeType);
        if(serializer == null) {
            .warn("could not find serializer for MIME type {}",mimeType);
            throw new UnsupportedExporterException("No serializer for mime type "+mimeType);
        }
        // HINT: This method might be executed outside a transaction!
        RDFWriter handler = Rio.createWriter(serializer,writer);
        try {
            RepositoryConnection connection = .getConnection();
            try {
                if(context == null) {
                    connection.exportStatements(resource,null,null,true,handler);
                } else {
                    connection.exportStatements(resource,null,null,true,handler,context);
                }
            } finally {
                connection.commit();
                connection.close();
            }
        } catch (RepositoryException e) {
            throw new IOException("error while getting repository connection");
        } catch (RDFHandlerException e) {
            throw new IOException("error while writing RDF data to stream");
        }
    }

    
Export the triple data for the given resource contained in the named graph passed as argument "context" and write it to the output stream given as first argument using the serialisation format specified by "mimeType".

The context parameter limits the exported triples to the named graph represented by this resource. If it is set to null, all named graphs will be exported.

The mime type must be supported by at least one of the registered exporters, otherwise an UnsupportedExporterException. Available mime types can be retrieved using the getProducedTypes() method.

Parameters:
outputStream the OutputStream to write the triples to; data will be written using UTF-8 encoding; will be closed when the triples are written
context the named graph to export; if null, all named graphs will be exported
mimeType a mime type registered by an exporter
Throws:
org.apache.marmotta.platform.core.exception.io.UnsupportedExporterException in case there is no matching exporter for the given mime type
java.io.IOException in case there is an error writing to the output
    @Override
    public void exportData(OutputStream outputStreamURI resourceURI contextString mimeTypethrows UnsupportedExporterExceptionIOException {
        RDFFormat serializer = .getSerializer(mimeType);
        if(serializer == null) {
            .warn("could not find serializer for MIME type {}",mimeType);
            throw new UnsupportedExporterException("No serializer for mime type "+mimeType);
        }
        // HINT: This method might be executed outside a transaction!
        RDFWriter handler = Rio.createWriter(serializer,outputStream);
        try {
            RepositoryConnection connection = .getConnection();
            try {
                if(context == null) {
                    connection.exportStatements(resource,null,null,true,handler);
                } else {
                    connection.exportStatements(resource,null,null,true,handler,context);
                }
            } finally {
                connection.commit();
                connection.close();
            }
        } catch (RepositoryException e) {
            throw new IOException("error while getting repository connection");
        } catch (RDFHandlerException e) {
            throw new IOException("error while writing RDF data to stream");
        }
    }
New to GrepCode? Check out our FAQ X