Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
white-listing for export properties
Only properties explicitly inputed into SdfWriter should be
exported. This new behavior could be added with an extra constructor
that takes a Set of properties. Using existing constructors keeps
the old behavior.

Change-Id: I04d42b0d245f5b7479436396efecbcb6daf13669
  • Loading branch information
Joos Kiener authored and johnmay committed Aug 2, 2013
1 parent 084fd88 commit 72eb921
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/main/org/openscience/cdk/io/SDFWriter.java
Expand Up @@ -35,6 +35,7 @@
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.openscience.cdk.annotations.TestClass;
import org.openscience.cdk.annotations.TestMethod;
Expand Down Expand Up @@ -74,6 +75,7 @@ public class SDFWriter extends DefaultChemObjectWriter {
private BufferedWriter writer;
private BooleanIOSetting writerProperties;
private Map<String,IOSetting> mdlWriterSettings;
private Set<String> propertiesToWrite;

/**
* Constructs a new SDFWriter that writes to the given {@link Writer}.
Expand Down Expand Up @@ -103,6 +105,38 @@ public SDFWriter() {
this(new StringWriter());
}

/**
* Constructs a new SDFWriter that writes to the given {@link Writer}.
*
* @param out The {@link Writer} to write to
*/
public SDFWriter(Writer out, Set<String> propertiesToWrite) {
if (out instanceof BufferedWriter) {
writer = (BufferedWriter) out;
} else {
writer = new BufferedWriter(out);
}
initIOSettings();
this.propertiesToWrite = propertiesToWrite;
}

/**
* Constructs a new SdfWriter that can write to a given
* {@link OutputStream}.
*
* @param output The {@link OutputStream} to write to
*/
public SDFWriter(OutputStream output, Set<String> propertiesToWrite) {
this(new OutputStreamWriter(output), propertiesToWrite);
}

/**
* Writes SD-File to a String including the given properties
*/
public SDFWriter(Set<String> propertiesToWrite) {
this(new StringWriter(), propertiesToWrite);
}

@TestMethod("testGetFormat")
public IResourceFormat getFormat() {
return SDFFormat.getInstance();
Expand Down Expand Up @@ -217,9 +251,12 @@ private void writeMolecule(IAtomContainer container) throws CDKException {

// write the properties
Map<Object,Object> sdFields = container.getProperties();
boolean writeAllProperties = propertiesToWrite == null;
if(sdFields != null){
for (Object propKey : sdFields.keySet()) {
if (!isCDKInternalProperty(propKey)) {
if (writeAllProperties ||
propertiesToWrite.contains(propKey)) {
writer.write("> <" + propKey + ">");
writer.newLine();
writer.write("" + sdFields.get(propKey));
Expand All @@ -228,6 +265,7 @@ private void writeMolecule(IAtomContainer container) throws CDKException {
}
}
}
}
writer.write("$$$$\n");
} catch (IOException exception) {
throw new CDKException(
Expand Down

0 comments on commit 72eb921

Please sign in to comment.