From 01507bcfcca4d7b0f6ee8ffa4cd7a0881b19b7f2 Mon Sep 17 00:00:00 2001
From: Stefan Kuhn <Stefan.Kuhn@ebi.ac.uk>
Date: Thu, 8 Jul 2010 11:50:00 +0100
Subject: [PATCH] The iteratingmdlreader now reads multiple fields with the same key into
 a list. Until now, values were overwritten. The specification does not
 say if it is allowed or not to have this, but in practice it exists.

---
 .../cdk/io/iterator/IteratingMDLReader.java        |   17 +++++++++++++++--
 src/test/data/mdl/test.sdf                         |    3 +++
 .../cdk/io/iterator/IteratingMDLReaderTest.java    |   13 ++++++++++++-
 3 files changed, 30 insertions(+), 3 deletions(-)

diff --git a/src/main/org/openscience/cdk/io/iterator/IteratingMDLReader.java b/src/main/org/openscience/cdk/io/iterator/IteratingMDLReader.java
index 1684d6c..55d42c3 100644
--- a/src/main/org/openscience/cdk/io/iterator/IteratingMDLReader.java
+++ b/src/main/org/openscience/cdk/io/iterator/IteratingMDLReader.java
@@ -30,13 +30,15 @@ import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.Reader;
 import java.io.StringReader;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.NoSuchElementException;
 
 import org.openscience.cdk.annotations.TestMethod;
 import org.openscience.cdk.exception.CDKException;
 import org.openscience.cdk.interfaces.IChemObject;
-import org.openscience.cdk.interfaces.IMolecule;
 import org.openscience.cdk.interfaces.IChemObjectBuilder;
+import org.openscience.cdk.interfaces.IMolecule;
 import org.openscience.cdk.io.ISimpleChemObjectReader;
 import org.openscience.cdk.io.MDLV2000Reader;
 import org.openscience.cdk.io.ReaderFactory;
@@ -199,7 +201,18 @@ public class IteratingMDLReader extends DefaultIteratingChemObjectReader impleme
                 String data = extractFieldData(str);
                 if (fieldName != null) {
                     logger.info("fieldName, data: ", fieldName, ", ", data);
-                    m.setProperty(fieldName, data);
+                    if(m.getProperty(fieldName)==null){
+                        m.setProperty(fieldName, data);
+                    }else{
+                        if(m.getProperty(fieldName) instanceof String){
+                            List<String> datas = new ArrayList<String>();
+                            datas.add((String) m.getProperty(fieldName));
+                            datas.add(data);
+                            m.setProperty(fieldName, datas);
+                        }else{
+                            ((List<String>)m.getProperty(fieldName)).add(data);
+                        }
+                    }
                 }
             }
             currentLine = input.readLine();
diff --git a/src/test/data/mdl/test.sdf b/src/test/data/mdl/test.sdf
index c74aa76..336927f 100644
--- a/src/test/data/mdl/test.sdf
+++ b/src/test/data/mdl/test.sdf
@@ -107,6 +107,9 @@ M  END
 > <E_NSC>
 2
 
+> <E_NSC>
+3
+
 > <E_CAS>
 120-78-5
 
diff --git a/src/test/org/openscience/cdk/io/iterator/IteratingMDLReaderTest.java b/src/test/org/openscience/cdk/io/iterator/IteratingMDLReaderTest.java
index 128f29c..f73279d 100644
--- a/src/test/org/openscience/cdk/io/iterator/IteratingMDLReaderTest.java
+++ b/src/test/org/openscience/cdk/io/iterator/IteratingMDLReaderTest.java
@@ -28,14 +28,15 @@
 package org.openscience.cdk.io.iterator;
 
 import java.io.InputStream;
+import java.util.List;
 import java.util.Properties;
 
 import org.junit.Assert;
 import org.junit.Test;
 import org.openscience.cdk.CDKConstants;
 import org.openscience.cdk.CDKTestCase;
-import org.openscience.cdk.Molecule;
 import org.openscience.cdk.DefaultChemObjectBuilder;
+import org.openscience.cdk.Molecule;
 import org.openscience.cdk.exception.CDKException;
 import org.openscience.cdk.geometry.GeometryTools;
 import org.openscience.cdk.interfaces.IMolecule;
@@ -111,6 +112,16 @@ public class IteratingMDLReaderTest extends CDKTestCase {
         Molecule m = (Molecule) object;
         Assert.assertEquals("1", m.getProperty("E_NSC"));
         Assert.assertEquals("553-97-9", m.getProperty("E_CAS"));
+        Assert.assertTrue(reader.hasNext());
+        object = reader.next();
+        Assert.assertNotNull(object);
+        Assert.assertTrue(object instanceof Molecule);
+        m = (Molecule) object;
+        Assert.assertTrue(m.getProperty("E_NSC") instanceof List);
+        List<String> e_nsc = (List<String>)m.getProperty("E_NSC");
+        Assert.assertEquals("2", e_nsc.get(0));
+        Assert.assertEquals("3", e_nsc.get(1));
+        Assert.assertEquals("120-78-5", m.getProperty("E_CAS"));
     }
 
     @Test public void testMultipleEntryFields() throws Exception {
-- 
1.5.4.3

