it.tidalwave.bluebill.taxonomy.elmo.impl.ElmoExporter Maven / Gradle / Ivy
/***********************************************************************************************************************
*
* blueBill Core - open source birding
* Copyright (C) 2009-2011 by Tidalwave s.a.s. (http://www.tidalwave.it)
*
***********************************************************************************************************************
*
* Licensed 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.
*
***********************************************************************************************************************
*
* WWW: http://bluebill.tidalwave.it
* SCM: https://kenai.com/hg/bluebill~core-src
*
**********************************************************************************************************************/
package it.tidalwave.bluebill.taxonomy.elmo.impl;
import java.io.OutputStream;
import javax.annotation.Nonnull;
import java.io.IOException;
import java.io.FileOutputStream;
import java.io.File;
import java.io.Writer;
import java.io.OutputStreamWriter;
import it.tidalwave.util.logging.Logger;
import it.tidalwave.bluebill.taxonomy.io.Exporter;
import java.io.FilterOutputStream;
import org.openrdf.repository.Repository;
import org.openrdf.repository.RepositoryException;
import org.openrdf.repository.RepositoryConnection;
import org.openrdf.rio.RDFHandlerException;
import org.openrdf.rio.RDFWriter;
import org.openrdf.rio.n3.N3Writer;
import org.openrdf.rio.rdfxml.util.OrganizedRDFXMLWriter;
import static it.tidalwave.bluebill.taxonomy.elmo.ElmoTaxonomyVocabulary.*;
/***********************************************************************************************************************
*
* @author Fabrizio Giudici
* @version $Id$
*
**********************************************************************************************************************/
public class ElmoExporter implements Exporter
{
private static final String CLASS = ElmoExporter.class.getName();
private static final Logger logger = Logger.getLogger(CLASS);
@Nonnull
private final Repository repository;
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public ElmoExporter (final @Nonnull Repository repository)
{
this.repository = repository;
}
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
public void export (final @Nonnull File file)
throws IOException
{
logger.info("Exporting repository to %s", file);
final String fileName = file.getName().toLowerCase();
export(new FileOutputStream(file), fileName);
}
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
public void export (@Nonnull OutputStream os, final @Nonnull String fileName)
throws IOException
{
os = new FilterOutputStream(os)
{
@Override
public void close() throws IOException
{
flush();
}
};
try
{
final RepositoryConnection connection = repository.getConnection();
final Writer w1 = new OutputStreamWriter(os, "UTF-8");
if (fileName.endsWith(".rdf"))
{
final OrganizedRDFXMLWriter rdfWriter = new OrganizedRDFXMLWriter(w1);
setupNamespaces(rdfWriter);
connection.export(rdfWriter);
rdfWriter.close();
}
else if (fileName.endsWith(".n3"))
{
final N3Writer n3Writer = new N3Writer(w1);
setupNamespaces(n3Writer);
connection.export(n3Writer);
w1.close();
}
connection.close();
}
catch (RDFHandlerException e)
{
throw new IOException(e.toString()); // Java 5/Android compatibility
}
catch (RepositoryException e)
{
throw new IOException(e.toString()); // Java 5/Android compatibility
}
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
private void setupNamespaces (final @Nonnull RDFWriter rdfWriter)
throws RDFHandlerException
{
rdfWriter.handleNamespace("rdfs", "http://www.w3.org/2000/01/rdf-schema#");
rdfWriter.handleNamespace("foaf", "http://xmlns.com/foaf/0.1/");
rdfWriter.handleNamespace("dcmi", "http://purl.org/dc/dcmitype/");
rdfWriter.handleNamespace("dc", "http://purl.org/dc/elements/1.1/");
rdfWriter.handleNamespace("dcterms", "http://purl.org/dc/terms/");
rdfWriter.handleNamespace("dwc", NS_DARWIN_CORE);
rdfWriter.handleNamespace("owl", "http://www.w3.org/2002/07/owl#");
rdfWriter.handleNamespace("skos", "http://www.w3.org/2004/02/skos/core#");
rdfWriter.handleNamespace("txn", "http://rdf.geospecies.org/ont/txn.owl#");
rdfWriter.handleNamespace("taxo", NS_TAXO_BASE);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy