Search in sources :

Example 1 with CyFileFilter

use of org.cytoscape.io.CyFileFilter in project cytoscape-impl by cytoscape.

the class GenericReaderManager method getReader.

public R getReader(InputStream stream, String inputName) {
    try {
        if (!stream.markSupported()) {
            stream = new BufferedInputStream(stream);
            stream.mark(1025);
        }
        for (T factory : factories) {
            CyFileFilter cff = factory.getFileFilter();
            // of the first 2KB rather than the stream itself.
            if (cff.accepts(CopyInputStream.copyKBytes(stream, 1), category)) {
                // logger.debug("successfully matched READER " + factory);
                return (R) factory.createTaskIterator(stream, inputName).next();
            }
        }
    } catch (IOException ioe) {
        userLogger.warn("Error setting input stream", ioe);
    }
    userLogger.warn("No reader found for input stream");
    return null;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) CyFileFilter(org.cytoscape.io.CyFileFilter) IOException(java.io.IOException)

Example 2 with CyFileFilter

use of org.cytoscape.io.CyFileFilter in project cytoscape-impl by cytoscape.

the class GenericReaderManager method getReader.

/**
 * Gets the GraphReader that is capable of reading the specified file.
 *
 * @param uri
 *            URI of file to be read.
 * @return GraphReader capable of reading the specified file. Null if file
 *         cannot be read.
 */
public R getReader(final URI uri, final String inputName) {
    // Data location is always required.
    if (uri == null) {
        throw new NullPointerException("Data source URI is null");
    }
    // This is the default reader, which accepts files with no extension.
    // Usually, this is ImportNetworkTableReaderFactory (Manual table
    // import)
    T defaultFactory = null;
    final List<T> factoryList = new ArrayList<T>();
    final Map<String, T> factoryTable = new HashMap<String, T>();
    // Pick compatible reader factories.
    for (final T factory : factories) {
        final CyFileFilter cff = factory.getFileFilter();
        // Got "Accepted" flag. Need to check it's default or not.
        if (cff.accepts(uri, category)) {
            logger.info("Filter returns Accepted.  Need to check priority: " + factory);
            if (factory.getClass().toString().contains(DEFAULT_READER_FACTORY_CLASS)) {
                defaultFactory = factory;
            } else {
                factoryList.add(factory);
                for (final String extension : cff.getExtensions()) {
                    factoryTable.put(extension, factory);
                }
            }
        }
    }
    T chosenFactory = null;
    // No compatible factory is available.
    if (factoryTable.isEmpty() && defaultFactory == null) {
        userLogger.warn("No reader found for uri: " + uri.toString());
        throw new IllegalStateException("Don't know how to read " + uri.toString());
    } else if (factoryList.size() == 1) {
        // There is only one compatible reader factory.  Use it:
        chosenFactory = factoryList.get(0);
    } else {
        if (factoryList.isEmpty() && defaultFactory != null) {
            // There is only one factory
            chosenFactory = defaultFactory;
        } else {
            // Well, we cannot decide which one is correct.  Try to use ext...
            String extension = FilenameUtils.getExtension(uri.toString());
            if (factoryTable.containsKey(extension))
                chosenFactory = factoryTable.get(extension);
            else {
                if (factoryTable.containsKey(""))
                    chosenFactory = factoryTable.get("");
                else {
                    userLogger.warn("Can't figure out how to read: " + uri.toString() + " from extension");
                    throw new IllegalStateException("Can't figure out how to read " + uri.toString() + " from extension");
                }
            }
        }
    }
    try {
        logger.info("Successfully found compatible ReaderFactory " + chosenFactory);
        // This returns strean using proxy if it exists.
        final InputStream stream = streamUtil.getInputStream(uri.toURL());
        return (R) chosenFactory.createTaskIterator(stream, inputName).next();
    } catch (FileNotFoundException e) {
        throw new IllegalStateException("File '" + inputName + "' not found:", e);
    } catch (IOException e) {
        userLogger.warn("Error opening stream to URI: " + uri.toString(), e);
        throw new IllegalStateException("Could not open stream for reader.", e);
    }
}
Also used : HashMap(java.util.HashMap) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) CyFileFilter(org.cytoscape.io.CyFileFilter)

Example 3 with CyFileFilter

use of org.cytoscape.io.CyFileFilter in project cytoscape-impl by cytoscape.

the class CyNetworkViewWriterTest method setUp.

@Before
public void setUp() throws Exception {
    writerManager = mock(CyNetworkViewWriterManager.class);
    cyApplicationManager = mock(CyApplicationManager.class);
    view = viewSupport.getNetworkView();
    final List<CyFileFilter> filters = new ArrayList<CyFileFilter>();
    final CyFileFilter dummyFilter = mock(CyFileFilter.class);
    filters.add(dummyFilter);
    when(dummyFilter.getDescription()).thenReturn("dummy description");
    when(dummyFilter.getExtensions()).thenReturn(Collections.singleton("dummy"));
    when(writerManager.getAvailableWriterFilters()).thenReturn(filters);
    final CyNetworkViewWriter writer = new CyNetworkViewWriter(writerManager, cyApplicationManager, view);
    this.cyWriter = writer;
    when(writerManager.getWriter(eq(view), eq(dummyFilter), any(File.class))).thenReturn(writer, writer);
}
Also used : CyApplicationManager(org.cytoscape.application.CyApplicationManager) CyFileFilter(org.cytoscape.io.CyFileFilter) ArrayList(java.util.ArrayList) CyNetworkViewWriterManager(org.cytoscape.io.write.CyNetworkViewWriterManager) File(java.io.File) Before(org.junit.Before)

Example 4 with CyFileFilter

use of org.cytoscape.io.CyFileFilter in project cytoscape-impl by cytoscape.

the class ViewWriterTest method setUp.

@Before
@SuppressWarnings("rawtypes")
public void setUp() throws Exception {
    final PresentationWriterManager viewWriterMgr = mock(PresentationWriterManager.class);
    final CyApplicationManager applicationManager = mock(CyApplicationManager.class);
    final RenderingEngineManager engineManager = mock(RenderingEngineManager.class);
    final CyNetworkView view = mock(CyNetworkView.class);
    final CyNetwork network = mock(CyNetwork.class);
    final CyRow row = mock(CyRow.class);
    final RenderingEngine re = mock(RenderingEngine.class);
    final CyServiceRegistrar serviceRegistrar = mock(CyServiceRegistrar.class);
    when(serviceRegistrar.getService(CyApplicationManager.class)).thenReturn(applicationManager);
    when(serviceRegistrar.getService(RenderingEngineManager.class)).thenReturn(engineManager);
    when(serviceRegistrar.getService(PresentationWriterManager.class)).thenReturn(viewWriterMgr);
    fileFilter = mock(CyFileFilter.class);
    when(fileFilter.getDescription()).thenReturn("A dummy filter");
    when(fileFilter.getExtensions()).thenReturn(Collections.singleton("dummy"));
    final List<CyFileFilter> filters = new ArrayList<>();
    filters.add(fileFilter);
    when(viewWriterMgr.getAvailableWriterFilters()).thenReturn(filters);
    when(view.getModel()).thenReturn(network);
    when(network.getRow(network)).thenReturn(row);
    cyWriter = new ViewWriter(view, re, serviceRegistrar);
    final CyWriter aWriter = mock(CyWriter.class);
    when(viewWriterMgr.getWriter(eq(view), eq(re), eq(fileFilter), argThat(new AnyOutputStreamMatcher()))).thenReturn(aWriter);
}
Also used : RenderingEngineManager(org.cytoscape.view.presentation.RenderingEngineManager) PresentationWriterManager(org.cytoscape.io.write.PresentationWriterManager) CyWriter(org.cytoscape.io.write.CyWriter) ArrayList(java.util.ArrayList) CyNetwork(org.cytoscape.model.CyNetwork) CyRow(org.cytoscape.model.CyRow) CyServiceRegistrar(org.cytoscape.service.util.CyServiceRegistrar) CyApplicationManager(org.cytoscape.application.CyApplicationManager) RenderingEngine(org.cytoscape.view.presentation.RenderingEngine) CyFileFilter(org.cytoscape.io.CyFileFilter) CyNetworkView(org.cytoscape.view.model.CyNetworkView) ViewWriter(org.cytoscape.task.internal.export.ViewWriter) Before(org.junit.Before)

Example 5 with CyFileFilter

use of org.cytoscape.io.CyFileFilter in project cytoscape-impl by cytoscape.

the class ExportNetworkImageTaskFactoryTest method setUp.

@Before
@SuppressWarnings({ "rawtypes", "unchecked" })
public void setUp() throws Exception {
    final CyFileFilter dummyFilter = mock(CyFileFilter.class);
    final RenderingEngine engine = mock(RenderingEngine.class);
    final List<CyFileFilter> filters = new ArrayList<>();
    filters.add(dummyFilter);
    when(dummyFilter.getDescription()).thenReturn("dummy description");
    when(dummyFilter.getExtensions()).thenReturn(Collections.singleton("dummy"));
    when(viewWriterMgr.getAvailableWriterFilters()).thenReturn(filters);
    when(applicationManager.getCurrentRenderingEngine()).thenReturn(engine);
    when(serviceRegistrar.getService(CyApplicationManager.class)).thenReturn(applicationManager);
    when(serviceRegistrar.getService(RenderingEngineManager.class)).thenReturn(engineManager);
    when(serviceRegistrar.getService(PresentationWriterManager.class)).thenReturn(viewWriterMgr);
}
Also used : RenderingEngine(org.cytoscape.view.presentation.RenderingEngine) CyFileFilter(org.cytoscape.io.CyFileFilter) ArrayList(java.util.ArrayList) Before(org.junit.Before)

Aggregations

CyFileFilter (org.cytoscape.io.CyFileFilter)10 ArrayList (java.util.ArrayList)5 File (java.io.File)3 IOException (java.io.IOException)3 Before (org.junit.Before)3 BufferedInputStream (java.io.BufferedInputStream)2 CyApplicationManager (org.cytoscape.application.CyApplicationManager)2 CyWriter (org.cytoscape.io.write.CyWriter)2 RenderingEngine (org.cytoscape.view.presentation.RenderingEngine)2 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Properties (java.util.Properties)1 ZipEntry (java.util.zip.ZipEntry)1 CyFileFilterProvider (org.cytoscape.io.CyFileFilterProvider)1 CyNetworkViewWriterManager (org.cytoscape.io.write.CyNetworkViewWriterManager)1 CySessionWriterManager (org.cytoscape.io.write.CySessionWriterManager)1 PresentationWriterManager (org.cytoscape.io.write.PresentationWriterManager)1 CyNetwork (org.cytoscape.model.CyNetwork)1