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;
}
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);
}
}
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);
}
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);
}
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);
}
Aggregations