All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.tidalwave.bluebill.taxonomy.birds.GeneratorSupport Maven / Gradle / Ivy

There is a newer version: 1.0.18
Show newest version
/***********************************************************************************************************************
 *
 * blueBill Mobile - open source birdwatching
 * ==========================================
 *
 * Copyright (C) 2009, 2010 by Tidalwave s.a.s. (http://www.tidalwave.it)
 * http://bluebill.tidalwave.it/mobile/
 *
 ***********************************************************************************************************************
 *
 * 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.
 *
 ***********************************************************************************************************************
 *
 * $Id: GeneratorSupport.java,v 71523a7b745b 2010/08/02 12:20:06 fabrizio $
 *
 **********************************************************************************************************************/
package it.tidalwave.bluebill.taxonomy.birds;

import it.tidalwave.util.logging.Logger;
import java.util.logging.Handler;
import java.util.logging.LogManager;
import it.tidalwave.util.logging.SingleLineLogFormatter;
import it.tidalwave.bluebill.taxonomy.TaxonomyManager;
import it.tidalwave.bluebill.taxonomy.Taxonomy;
import java.io.Writer;
import java.io.OutputStreamWriter;
import org.openrdf.elmo.ElmoManager;
import org.openrdf.elmo.ElmoModule;
import org.openrdf.elmo.sesame.SesameManagerFactory;
import org.openrdf.repository.Repository;
import org.openrdf.repository.RepositoryConnection;
import org.openrdf.repository.RepositoryException;
import org.openrdf.repository.sail.SailRepository;
import org.openrdf.rio.RDFHandlerException;
import org.openrdf.rio.RDFParseException;
import org.openrdf.rio.n3.N3Writer;
import org.openrdf.rio.rdfxml.util.OrganizedRDFXMLWriter;
import org.openrdf.sail.memory.MemoryStore;
import it.tidalwave.openrdf.elmo.ElmoManagerThreadLocal;
import java.io.InputStreamReader;
import java.io.Reader;
import org.openrdf.rio.RDFFormat;
import org.openrdf.rio.RDFWriter;
import javax.annotation.Nonnull;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.zip.GZIPInputStream;
import static it.tidalwave.bluebill.taxonomy.elmo.ElmoTaxonomyVocabulary.*;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id: $
 *
 **********************************************************************************************************************/
public abstract class GeneratorSupport
  {
    private static final String CLASS = GeneratorSupport.class.getName();
    private static final Logger logger = Logger.getLogger(CLASS);

    protected final File resourcesFolder;

    protected final File targetFolder;

    protected SesameManagerFactory smf;

    protected Repository repository;

    @Nonnull
    protected final String taxonomyName;

    @Nonnull
    protected final String taxonomyFileName;

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public GeneratorSupport (final @Nonnull String baseDir,
                             final @Nonnull String taxonomyName,
                             final @Nonnull String taxonomyFileName)
      {
        this.taxonomyName = taxonomyName;
        this.taxonomyFileName = taxonomyFileName;
        resourcesFolder = new File(baseDir + "/src/test/resources/");
        targetFolder = new File(baseDir + "/target/test-artifacts/");
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public void execute()
      throws Exception
      {
        setupLogging(GeneratorSupport.class);
        initialize();
        final BirdTaxonomyImporter importer = run();

        final File file = new File(targetFolder, taxonomyFileName + "-importer.ser");
        final ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
        oos.writeObject(importer);
        oos.close();
        
        exportRepository(targetFolder, taxonomyFileName, taxonomyName);
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public abstract BirdTaxonomyImporter run()
      throws Exception;

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    protected void initialize()
      throws Exception
      {
        repository = new SailRepository(new MemoryStore());
        repository.initialize();
        final ElmoModule elmoModule = new ElmoModule();
        smf = new SesameManagerFactory(elmoModule, repository);
        final ElmoManager em = smf.createElmoManager();
        ElmoManagerThreadLocal.set(em);
        targetFolder.mkdirs();
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    protected void close()
      {
        ElmoManagerThreadLocal.get().close();
        ElmoManagerThreadLocal.set(null);
        smf.close();
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    protected void importRepository (final @Nonnull File file)
      throws IOException, RDFParseException, RepositoryException
      {
        InputStream is = new FileInputStream(file);

        if (file.getName().endsWith(".gz"))
          {
            is = new GZIPInputStream(is);
          }

        final Reader r = new InputStreamReader(is);
        repository.getConnection().add(r, "http://foo.bar", RDFFormat.RDFXML); // FIXME
        r.close();
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    protected void exportRepository (final @Nonnull File folder,
                                     final @Nonnull String file,
                                     final @Nonnull String taxonomyName)
      throws Exception
      {
        final RepositoryConnection connection = repository.getConnection();

        final File rdfXmlFile = new File(folder, file + ".rdf");
        logger.info("Exporting repository to %s", rdfXmlFile);
        final Writer w1 = new OutputStreamWriter(new FileOutputStream(rdfXmlFile), "UTF-8");
        final OrganizedRDFXMLWriter rdfWriter = new OrganizedRDFXMLWriter(w1);
        setupNamespaces(rdfWriter);
        connection.export(rdfWriter);
        rdfWriter.close();

        final File rdfN3File = new File(folder, file + ".n3");
        logger.info("Exporting repository to %s", rdfN3File);
        final Writer w2 = new OutputStreamWriter(new FileOutputStream(rdfN3File), "UTF-8");
        final N3Writer n3Writer = new N3Writer(w2);
        setupNamespaces(n3Writer);
        connection.export(n3Writer);
        w2.close();

        connection.close();

        if ((taxonomyName != null) && !taxonomyName.startsWith("Clements")) // Clements not needed now, and it's slow to create
          {
            final Taxonomy taxonomy = TaxonomyManager.Locator.findTaxonomyManager().findTaxonomyByName(taxonomyName, repository);
            final File jsonFile = new File(folder, file + ".json");
            final Writer w3 = new OutputStreamWriter(new FileOutputStream(jsonFile), "UTF-8");
            final TaxonomyJSONExporter exporter = new TaxonomyJSONExporter();
            exporter.export(taxonomy, w3);
            w3.close();
          }
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    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("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_BASE);
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public static void setupLogging (final @Nonnull Class clazz)
      {
        try
          {
            new File("target/logs").mkdirs();
            final InputStream is = clazz.getResourceAsStream("log.properties");

            if (is == null)
              {
                System.err.println("NO LOGGING CONFIGURATION");
              }
            else
              {
                LogManager.getLogManager().readConfiguration(is);
                is.close();
              }
            //
            // The formatter must be set programmatically as the property in the log.properties won't
            // be honored. I suspect it is related with NetBeans module classloaders as the formatter
            // is loaded inside LogManager by using the SystemClassLoader, which only sees the classpath.
            //
            final SingleLineLogFormatter formatter = new SingleLineLogFormatter();
            java.util.logging.Logger rootLogger = java.util.logging.Logger.getLogger(clazz.getName());

            while (rootLogger.getParent() != null)
              {
                rootLogger = rootLogger.getParent();
              }

            for (final Handler handler : rootLogger.getHandlers())
              {
                handler.setFormatter(formatter);
              }
          }
        catch (Exception e)
          {
            e.printStackTrace();
          }
      }
  }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy