SDFWriter extends DefaultChemObjectWriter { @Override public void write(IChemObject object) throws CDKException { try { if (object instanceof IAtomContainerSet) { writeMoleculeSet((IAtomContainerSet) object); return; } else if (object instanceof IChemFile) { writeChemFile((IChemFile) object); return; } else if (object instanceof IChemModel) { IChemFile file = object.getBuilder().newInstance(IChemFile.class); IChemSequence sequence = object.getBuilder().newInstance(IChemSequence.class); sequence.addChemModel((IChemModel) object); file.addChemSequence(sequence); writeChemFile((IChemFile) file); return; } else if (object instanceof IAtomContainer) { writeMolecule((IAtomContainer) object); return; } } catch (Exception ex) { logger.error(ex.getMessage()); logger.debug(ex); throw new CDKException("Exception while writing MDL file: " + ex.getMessage(), ex); } throw new CDKException( "Only supported is writing of ChemFile, MoleculeSet, AtomContainer and Molecule objects."); } SDFWriter(BufferedWriter wtr);  SDFWriter(Writer wtr);  SDFWriter(OutputStream output);  SDFWriter();  SDFWriter(Writer out, Set<String> propertiesToWrite);  SDFWriter(OutputStream output, Set<String> propertiesToWrite);  SDFWriter(Set<String> propertiesToWrite); @Override IResourceFormat getFormat(); @Override void setWriter(Writer out); @Override void setWriter(OutputStream output); @Override void close(); @Override boolean accepts(Class<? extends IChemObject> classObject); @Override void write(IChemObject object); void setAlwaysV3000(boolean val); void customizeJob(); static final String OptAlwaysV3000; static final String OptWriteData; static final String OptTruncateLongData; }
@Test public void setProgramName() { StringWriter sw = new StringWriter(); try (SDFWriter sdfw = new SDFWriter(sw)) { sdfw.getSetting(MDLV2000Writer.OptWriteDefaultProperties) .setSetting("false"); sdfw.getSetting(MDLV2000Writer.OptProgramName) .setSetting("Bioclipse"); sdfw.write(TestMoleculeFactory.make123Triazole()); sdfw.getSetting(SDFWriter.OptAlwaysV3000) .setSetting("true"); sdfw.write(TestMoleculeFactory.make123Triazole()); } catch (IOException | CDKException e) { e.printStackTrace(); } String sdf = sw.toString(); for (String mol : sdf.split("\\$\\$\\$\\$", 2)) { assertThat(mol, CoreMatchers.containsString("Bioclip")); } }
@Test public void optionallyTruncateLongProperties() { StringWriter sw = new StringWriter(); try (SDFWriter sdfw = new SDFWriter(sw)) { sdfw.getSetting(MDLV2000Writer.OptWriteDefaultProperties) .setSetting("false"); sdfw.getSetting(SDFWriter.OptTruncateLongData) .setSetting("true"); IAtomContainer mol = TestMoleculeFactory.make123Triazole(); mol.setProperty("MyLongField", "ThisIsAVeryLongFieldThatShouldBeWrapped" + "ThisIsAVeryLongFieldThatShouldBeWrapped" + "ThisIsAVeryLongFieldThatShouldBeWrapped" + "ThisIsAVeryLongFieldThatShouldBeWrapped" + "ThisIsAVeryLongFieldThatShouldBeWrapped" + "ThisIsAVeryLongFieldThatShouldBeWrapped" + "ThisIsAVeryLongFieldThatShouldBeWrapped" + "ThisIsAVeryLongFieldThatShouldBeWrapped" + "ThisIsAVeryLongFieldThatShouldBeWrapped" + "ThisIsAVeryLongFieldThatShouldBeWrapped"); sdfw.write(mol); } catch (IOException | CDKException e) { e.printStackTrace(); } String sdf = sw.toString(); assertThat(sdf, CoreMatchers.containsString("ThisIsAVeryLongFieldThatShouldBeWrappedThisIsAVeryLongFieldThatShouldBeWrappedThisIsAVeryLongFieldThatShouldBeWrappedThisIsAVeryLongFieldThatShouldBeWrappedThisIsAVeryLongFieldThatShouldBeWrappedThisI\n")); }