@Override public boolean accepts(Class<? extends IChemObject> classObject) { Class<?>[] interfaces = classObject.getInterfaces(); for (int i = 0; i < interfaces.length; i++) { if (IAtomContainer.class.equals(interfaces[i])) return true; } if (IAtomContainer.class.equals(classObject)) return true; Class superClass = classObject.getSuperclass(); if (superClass != null) return this.accepts(superClass); return false; }
private void customizeJob() { fireIOSettingQuestion(write2DCoordinates); fireIOSettingQuestion(write3DCoordinates); fireIOSettingQuestion(builder); }
/** * Constructs a new CDKSourceCodeWriter. * * @param out The Writer to write to */ public CDKSourceCodeWriter(Writer out) { initIOSettings(); try { setWriter(out); } catch (Exception exc) { } }
@Test public void testOutput() throws Exception { StringWriter writer = new StringWriter(); IAtomContainer molecule = new AtomContainer(); Atom atom = new Atom("C"); atom.setMassNumber(14); molecule.addAtom(atom); CDKSourceCodeWriter sourceWriter = new CDKSourceCodeWriter(writer); sourceWriter.write(molecule); sourceWriter.close(); String output = writer.toString(); String newline = "\n"; assertThat(output, is("{" + newline + " IChemObjectBuilder builder = DefaultChemObjectBuilder.getInstance();" + newline + " IAtomContainer mol = builder.newInstance(IAtomContainer.class);" + newline + " IAtom a1 = builder.newInstance(IAtom.class,\"C\");" + newline + " a1.setFormalCharge(0);" + newline + " mol.addAtom(a1);" + newline + "}" + newline)); } }
protected File saveAsCDKSourceCode(IChemModel model, File outFile) throws Exception { logger.info("Saving the contents as a CDK source code file..."); String fileName = outFile.toString(); if (!fileName.endsWith(".cdk")) { fileName += ".cdk"; outFile = new File(fileName); } cow = new CDKSourceCodeWriter(new FileWriter(outFile)); if (cow != null && askIOSettings()) { cow.addChemObjectIOListener(new SwingGUIListener(jcpPanel, IOSetting.Importance.HIGH)); } Iterator<IAtomContainer> containers = ChemModelManipulator.getAllAtomContainers(model).iterator(); while (containers.hasNext()) { IAtomContainer ac = (IAtomContainer)containers.next(); if (ac != null) { cow.write(ac); } else { System.err.println("AC == null!"); } } cow.close(); if(jcpPanel instanceof JChemPaintPanel) ((JChemPaintPanel)jcpPanel).setTitle(jcpPanel.getChemModel().getID()); return outFile; }
@Override public void write(IChemObject object) throws CDKException { customizeJob(); if (object instanceof IAtomContainer) { try { writeAtomContainer((IAtomContainer) object); writer.flush(); } catch (Exception ex) { logger.error(ex.getMessage()); logger.debug(ex); throw new CDKException("Exception while writing to CDK source code: " + ex.getMessage(), ex); } } else { throw new CDKException("Only supported is writing of IAtomContainer objects."); } }
private void initIOSettings() { write2DCoordinates = addSetting(new BooleanIOSetting("write2DCoordinates", IOSetting.Importance.LOW, "Should 2D coordinates be added?", "true")); write3DCoordinates = addSetting(new BooleanIOSetting("write3DCoordinates", IOSetting.Importance.LOW, "Should 3D coordinates be added?", "true")); builder = addSetting(new StringIOSetting("builder", IOSetting.Importance.LOW, "Which IChemObjectBuilder should be used?", "DefaultChemObjectBuilder")); }
private void writeAtoms(IAtomContainer molecule) throws Exception { Iterator<IAtom> atoms = molecule.atoms().iterator(); while (atoms.hasNext()) { IAtom atom = (IAtom) atoms.next(); writeAtom(atom); writer.write(" mol.addAtom(" + atom.getID() + ");"); writer.write('\n'); } }
@Override public void setWriter(OutputStream output) throws CDKException { setWriter(new OutputStreamWriter(output)); }
@BeforeClass public static void setup() { setChemObjectIO(new CDKSourceCodeWriter()); }
@Override public void write(IChemObject object) throws CDKException { customizeJob(); if (object instanceof IAtomContainer) { try { writeAtomContainer((IAtomContainer) object); writer.flush(); } catch (Exception ex) { logger.error(ex.getMessage()); logger.debug(ex); throw new CDKException("Exception while writing to CDK source code: " + ex.getMessage(), ex); } } else { throw new CDKException("Only supported is writing of IAtomContainer objects."); } }
private void initIOSettings() { write2DCoordinates = addSetting(new BooleanIOSetting("write2DCoordinates", IOSetting.Importance.LOW, "Should 2D coordinates be added?", "true")); write3DCoordinates = addSetting(new BooleanIOSetting("write3DCoordinates", IOSetting.Importance.LOW, "Should 3D coordinates be added?", "true")); builder = addSetting(new StringIOSetting("builder", IOSetting.Importance.LOW, "Which IChemObjectBuilder should be used?", "DefaultChemObjectBuilder")); }
private void writeAtoms(IAtomContainer molecule) throws Exception { Iterator<IAtom> atoms = molecule.atoms().iterator(); while (atoms.hasNext()) { IAtom atom = (IAtom) atoms.next(); writeAtom(atom); writer.write(" mol.addAtom(" + atom.getID() + ");"); writer.write('\n'); } }
@Override public void setWriter(OutputStream output) throws CDKException { setWriter(new OutputStreamWriter(output)); }
/** * Constructs a new CDKSourceCodeWriter. * * @param out The Writer to write to */ public CDKSourceCodeWriter(Writer out) { initIOSettings(); try { setWriter(out); } catch (Exception exc) { } }
@Override public boolean accepts(Class<? extends IChemObject> classObject) { Class<?>[] interfaces = classObject.getInterfaces(); for (int i = 0; i < interfaces.length; i++) { if (IAtomContainer.class.equals(interfaces[i])) return true; } if (IAtomContainer.class.equals(classObject)) return true; Class superClass = classObject.getSuperclass(); if (superClass != null) return this.accepts(superClass); return false; }
private void customizeJob() { fireIOSettingQuestion(write2DCoordinates); fireIOSettingQuestion(write3DCoordinates); fireIOSettingQuestion(builder); }