/** * Constructs a ReaderFactory which tries to detect the format in the * first given number of chars. * * @param headerLength length of the header in number of chars */ public ReaderFactory(int headerLength) { formatFactory = new FormatFactory(headerLength); this.headerLength = headerLength; }
public List<IChemFormatMatcher> getFormats() { return formatFactory.getFormats(); }
/** * Registers a format for detection. */ public void registerFormat(IChemFormatMatcher format) { formatFactory.registerFormat(format); }
/** * Test that the format factory guesses the correct IChemFormat * based on the file content. * * @throws Exception */ @Test public void testRGFileFormat() throws Exception { String filename = "data/mdl/rgfile.1.mol"; InputStream ins = this.getClass().getClassLoader().getResourceAsStream(filename); IChemFormat format = new FormatFactory().guessFormat(new BufferedInputStream(ins)); Assert.assertEquals(format.getClass(), RGroupQueryFormat.class); }
ISimpleChemObjectReader reader = null; if (input instanceof GZIPInputStream) { format = formatFactory.guessFormat(input); reader = createReader(format); if (reader != null) { format = formatFactory.guessFormat(istreamToRead); reader = createReader(format); if (reader != null) {
private void expectFormat(String filename, IResourceFormat expectedFormat) throws Exception { InputStream ins = this.getClass().getClassLoader().getResourceAsStream(filename); Assert.assertNotNull("Cannot find file: " + filename, ins); if (expectedFormat instanceof IChemFormatMatcher) { factory.registerFormat((IChemFormatMatcher) expectedFormat); } ins = new BufferedInputStream(ins); IChemFormat format = factory.guessFormat(ins); Assert.assertNotNull(format); Assert.assertEquals(expectedFormat.getFormatName(), format.getFormatName()); }
public IChemFormat guessFormat(InputStream input) throws IOException { if (input == null) { throw new IllegalArgumentException("input cannot be null"); } if (!input.markSupported()) { throw new IllegalArgumentException("input must support mark"); } // make a copy of the header byte[] header = new byte[this.headerLength]; input.mark(this.headerLength); input.read(header, 0, this.headerLength); input.reset(); BufferedReader buffer = new BufferedReader(new StringReader(new String(header))); IChemFormat chemFormat = getMatchResult(buffer); if(chemFormat == null){ buffer = new BufferedReader(new StringReader(new String(header))); chemFormat = getXYZFormat(buffer); } return chemFormat; }
/** * Constructs a ReaderFactory which tries to detect the format in the * first given number of chars. * * @param headerLength length of the header in number of chars */ public FormatFactory(int headerLength) { this.headerLength = headerLength; loadFormats(); }
FormatFactory formatFactory = new FormatFactory(8192); IChemFormat format=formatFactory.guessFormat(input);
ISimpleChemObjectReader reader = null; if (input instanceof GZIPInputStream) { format = formatFactory.guessFormat(input); reader = createReader(format); if (reader != null) { format = formatFactory.guessFormat(istreamToRead); reader = createReader(format); if (reader != null) {
@Test public void testRegisterFormat() throws IOException { factory.registerFormat(new DummyFormat()); StringReader reader = new StringReader("DummyFormat:"); IChemFormat format = factory.guessFormat(reader); Assert.assertNotNull(format); Assert.assertTrue(format instanceof DummyFormat); } }
IChemFormat chemFormat = getMatchResult(buffer); chemFormat = getXYZFormat(buffer);
/** * Detects the format of the Reader input, and if known, it will return * a CDK Reader to read the format. This method is not able to detect the * format of gziped files. Use createReader(InputStream) instead for such * files. * * @see #createReader(InputStream) */ public ISimpleChemObjectReader createReader(Reader input) throws IOException { if (!(input instanceof BufferedReader)) { input = new BufferedReader(input); } IChemFormat chemFormat = formatFactory.guessFormat((BufferedReader) input); ISimpleChemObjectReader coReader = createReader(chemFormat); try { coReader.setReader(input); } catch (Exception exception) { logger.error("Could not set the Reader source: ", exception.getMessage()); logger.debug(exception); } return coReader; }
/** * Constructs a ReaderFactory which tries to detect the format in the * first given number of chars. * * @param headerLength length of the header in number of chars */ public ReaderFactory(int headerLength) { formatFactory = new FormatFactory(headerLength); this.headerLength = headerLength; }
public List<IChemFormatMatcher> getFormats() { return formatFactory.getFormats(); }
/** * Registers a format for detection. */ public void registerFormat(IChemFormatMatcher format) { formatFactory.registerFormat(format); }
/** * Detects the format of the Reader input, and if known, it will return * a CDK Reader to read the format. This method is not able to detect the * format of gziped files. Use createReader(InputStream) instead for such * files. * * @see #createReader(InputStream) */ public ISimpleChemObjectReader createReader(Reader input) throws IOException { if (!(input instanceof BufferedReader)) { input = new BufferedReader(input); } IChemFormat chemFormat = formatFactory.guessFormat((BufferedReader) input); ISimpleChemObjectReader coReader = createReader(chemFormat); try { coReader.setReader(input); } catch (Exception exception) { logger.error("Could not set the Reader source: ", exception.getMessage()); logger.debug(exception); } return coReader; }
@BeforeClass public static void setup() { factory = new FormatFactory(); }
@Test public void testGetFormats() { List<IChemFormatMatcher> formats = factory.getFormats(); Assert.assertNotNull(formats); Assert.assertNotSame(0, formats.size()); for (IChemFormatMatcher matcher : formats) { Assert.assertNotNull(matcher); } }
@Test public void testGuessFormat_Gz() throws Exception { String filename = "data/xyz/bf3.xyz.gz"; InputStream input = new BufferedInputStream(new GZIPInputStream(this.getClass().getClassLoader() .getResourceAsStream(filename))); IChemFormat format = factory.guessFormat(input); Assert.assertNotNull(format); // make sure the InputStream is properly reset BufferedReader reader = new BufferedReader(new InputStreamReader(input)); String line = reader.readLine(); Assert.assertNotNull(line); Assert.assertEquals("4", line); line = reader.readLine(); Assert.assertNotNull(line); Assert.assertEquals("Bortrifluorid", line); }