
it.tidalwave.bluebill.factsheet.impl.DefaultGraphProvider Maven / Gradle / Ivy
The newest version!
/***********************************************************************************************************************
*
* blueBill Mobile - Android - 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/mobile
* SCM: https://java.net/hg/bluebill-mobile~android-src
*
**********************************************************************************************************************/
package it.tidalwave.bluebill.factsheet.impl;
import javax.annotation.Nonnull;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import javax.inject.Provider;
import org.openrdf.model.Graph;
import it.tidalwave.netbeans.util.Locator;
import it.tidalwave.semantic.io.GraphDeserializer;
import it.tidalwave.semantic.io.MimeTypes;
import it.tidalwave.semantic.io.impl.DefaultGraphDeserializer;
import it.tidalwave.bluebill.factsheet.GraphProvider;
import it.tidalwave.bluebill.taxonomy.mobile.Taxon;
import it.tidalwave.mobile.asset.SmartAssetManager;
import lombok.Cleanup;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
/***********************************************************************************************************************
*
* @author Fabrizio Giudici
* @version $Id$
*
**********************************************************************************************************************/
@RequiredArgsConstructor @Slf4j
public class DefaultGraphProvider implements GraphProvider
{
@Nonnull
private final Taxon taxon;
private final Provider createProviderFor = Locator.createProviderFor(SmartAssetManager.class);
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
public Graph getGraph()
throws IOException
{
log.info("getGraph()");
final String resourceName = taxon.getId().stringValue().replaceAll(":", "_") + ".n3";
log.info(">>>> resourceName: {}", resourceName);
final File file = createProviderFor.get().findAsset("factsheets.zip");
log.info(">>>> zip file: {}", file);
final @Cleanup ZipFile zipFile = new ZipFile(file);
final ZipEntry entry = zipFile.getEntry(resourceName);
final @Cleanup InputStream is = zipFile.getInputStream(entry);
return loadGraph(is, MimeTypes.MIME_N3);
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
protected Graph loadGraph (final @Nonnull InputStream is, final @Nonnull String mimeType)
throws IOException
{
try
{
final GraphDeserializer deserializer = new DefaultGraphDeserializer();
return deserializer.read(is, mimeType);
}
catch (Exception e)
{
throw new IOException(e.toString()); // Java 5 / Android compatibility
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy