Skip to content

Instantly share code, notes, and snippets.

@egonw
Created January 30, 2010 18:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save egonw/290659 to your computer and use it in GitHub Desktop.
Save egonw/290659 to your computer and use it in GitHub Desktop.
From 3a765e0dfd10ed9b02ed6b864748c7ebe83148cb Mon Sep 17 00:00:00 2001
From: Egon Willighagen <egonw@users.sourceforge.net>
Date: Sat, 30 Jan 2010 11:54:31 +0100
Subject: [PATCH 1/2] Added a new IO API for reporting file format errors.
* introducing a IChemObjectReaderErrorHandler
* handleError() methods for IChemObjectReader's to report problems
* these handleErrors() method throw an CDKException when
in STRICT mode
* line and column number support, useful for MDL V2000 molfile text
editors in, e.g. Bioclipse
---
.../cdk/io/DefaultChemObjectReader.java | 46 ++++++++++++++++--
.../org/openscience/cdk/io/IChemObjectReader.java | 52 ++++++++++++++++++-
.../iterator/DefaultIteratingChemObjectReader.java | 49 ++++++++++++++++--
3 files changed, 134 insertions(+), 13 deletions(-)
diff --git a/src/main/org/openscience/cdk/io/DefaultChemObjectReader.java b/src/main/org/openscience/cdk/io/DefaultChemObjectReader.java
index 1807005..1015c49 100644
--- a/src/main/org/openscience/cdk/io/DefaultChemObjectReader.java
+++ b/src/main/org/openscience/cdk/io/DefaultChemObjectReader.java
@@ -1,6 +1,5 @@
-/* $Revision$ $Author$ $Date$
- *
- * Copyright (C) 2002-2007 The Jmol Development Team
+/* Copyright (C) 2002-2007 The Jmol Development Team
+ * 2010 Egon Willighagen <egonw@users.sf.net>
*
* Contact: cdk-devel@lists.sourceforge.net
*
@@ -23,6 +22,7 @@ package org.openscience.cdk.io;
import java.util.ArrayList;
import java.util.List;
+import org.openscience.cdk.exception.CDKException;
import org.openscience.cdk.io.listener.IChemObjectIOListener;
import org.openscience.cdk.io.listener.IReaderListener;
import org.openscience.cdk.io.setting.IOSetting;
@@ -42,6 +42,7 @@ public abstract class DefaultChemObjectReader implements ISimpleChemObjectReader
private ReaderEvent frameReadEvent = null;
protected IChemObjectReader.Mode mode = IChemObjectReader.Mode.RELAXED;
+ protected IChemObjectReaderErrorHandler errorHandler = null;
/**
* Holder of reader event listeners.
@@ -88,5 +89,42 @@ public abstract class DefaultChemObjectReader implements ISimpleChemObjectReader
public void setReaderMode(ISimpleChemObjectReader.Mode mode) {
this.mode = mode;
}
-
+
+ /** {@inheritDoc} */
+ public void setErrorHandler(IChemObjectReaderErrorHandler handler) {
+ this.errorHandler = handler;
+ }
+
+ /** {@inheritDoc} */
+ public void handleError(String message) throws CDKException {
+ if (this.errorHandler != null) this.errorHandler.handleError(message);
+ if (this.mode == Mode.STRICT) throw new CDKException(message);
+ }
+
+ /** {@inheritDoc} */
+ public void handleError(String message, Exception exception)
+ throws CDKException {
+ if (this.errorHandler != null)
+ this.errorHandler.handleError(message, exception);
+ if (this.mode == Mode.STRICT) {
+ throw new CDKException(message, exception);
+ }
+ }
+
+ /** {@inheritDoc} */
+ public void handleError(String message, int row, int col) throws CDKException {
+ if (this.errorHandler != null)
+ this.errorHandler.handleError(message, row, col);
+ if (this.mode == Mode.STRICT) throw new CDKException(message);
+ }
+
+ /** {@inheritDoc} */
+ public void handleError(String message, int row, int col, Exception exception)
+ throws CDKException {
+ if (this.errorHandler != null)
+ this.errorHandler.handleError(message, row, col, exception);
+ if (this.mode == Mode.STRICT) {
+ throw new CDKException(message, exception);
+ }
+ }
}
diff --git a/src/main/org/openscience/cdk/io/IChemObjectReader.java b/src/main/org/openscience/cdk/io/IChemObjectReader.java
index d9a29bc..8709fa4 100644
--- a/src/main/org/openscience/cdk/io/IChemObjectReader.java
+++ b/src/main/org/openscience/cdk/io/IChemObjectReader.java
@@ -1,6 +1,4 @@
-/* $Revision$ $Author$ $Date$
- *
- * Copyright (C) 2000-2007 Egon Willighagen <egonw@users.sf.net>
+/* Copyright (C) 2000-2007,2010 Egon Willighagen <egonw@users.sf.net>
*
* Contact: cdk-devel@lists.sourceforge.net
*
@@ -66,5 +64,53 @@ public interface IChemObjectReader extends IChemObjectIO {
* @param mode
*/
public void setReaderMode(Mode mode);
+
+ /**
+ * Sets an error handler that is sent events when file format issues occur.
+ *
+ * @param handler {@link IChemObjectReaderErrorHandler} to send error
+ * messages to.
+ */
+ public void setErrorHandler(IChemObjectReaderErrorHandler handler);
+
+ /**
+ * Redirects an error message to the {@link IChemObjectReaderErrorHandler}.
+ * Throws an {@link CDKException} when in STRICT {@link Mode}.
+ *
+ * @param message the error message.
+ */
+ public void handleError(String message) throws CDKException;
+
+ /**
+ * Redirects an error message to the {@link IChemObjectReaderErrorHandler}.
+ * Throws an {@link CDKException} when in STRICT {@link Mode}.
+ *
+ * @param message the error message.
+ * @param exception the corresponding {@link Exception}.
+ */
+ public void handleError(String message, Exception exception)
+ throws CDKException;
+
+ /**
+ * Redirects an error message to the {@link IChemObjectReaderErrorHandler}.
+ * Throws an {@link CDKException} when in STRICT {@link Mode}.
+ *
+ * @param message the error message.
+ * @param row Row in the file where the error is found.
+ * @param col Column in the file where the error is found.
+ */
+ public void handleError(String message, int row, int col) throws CDKException;
+
+ /**
+ * Redirects an error message to the {@link IChemObjectReaderErrorHandler}.
+ * Throws an {@link CDKException} when in STRICT {@link Mode}.
+ *
+ * @param message the error message.
+ * @param exception the corresponding {@link Exception}.
+ * @param row Row in the file where the error is found.
+ * @param col Column in the file where the error is found.
+ */
+ public void handleError(String message, int row, int col, Exception exception)
+ throws CDKException;
}
diff --git a/src/main/org/openscience/cdk/io/iterator/DefaultIteratingChemObjectReader.java b/src/main/org/openscience/cdk/io/iterator/DefaultIteratingChemObject
Reader.java
index 8b79efd..6131481 100644
--- a/src/main/org/openscience/cdk/io/iterator/DefaultIteratingChemObjectReader.java
+++ b/src/main/org/openscience/cdk/io/iterator/DefaultIteratingChemObjectReader.java
@@ -1,9 +1,5 @@
-/* $RCSfile$
- * $Author$
- * $Date$
- * $Revision$
- *
- * Copyright (C) 2003-2007 The Jmol Development Team
+/* Copyright (C) 2003-2007 The Jmol Development Team
+ * 2010 Egon Willighagen <egonw@users.sf.net>
*
* Contact: cdk-devel@lists.sourceforge.net
*
@@ -26,8 +22,11 @@ package org.openscience.cdk.io.iterator;
import java.util.ArrayList;
import java.util.List;
+import org.openscience.cdk.exception.CDKException;
import org.openscience.cdk.io.IChemObjectReader;
+import org.openscience.cdk.io.IChemObjectReaderErrorHandler;
import org.openscience.cdk.io.ISimpleChemObjectReader;
+import org.openscience.cdk.io.IChemObjectReader.Mode;
import org.openscience.cdk.io.listener.IChemObjectIOListener;
import org.openscience.cdk.io.setting.IOSetting;
@@ -41,6 +40,7 @@ import org.openscience.cdk.io.setting.IOSetting;
public abstract class DefaultIteratingChemObjectReader implements IIteratingChemObjectReader {
protected IChemObjectReader.Mode mode = IChemObjectReader.Mode.RELAXED;
+ protected IChemObjectReaderErrorHandler errorHandler = null;
/**
* Holder of reader event listeners.
@@ -83,4 +83,41 @@ public abstract class DefaultIteratingChemObjectReader implements IIteratingChem
this.mode = mode;
}
+ /** {@inheritDoc} */
+ public void setErrorHandler(IChemObjectReaderErrorHandler handler) {
+ this.errorHandler = handler;
+ }
+
+ /** {@inheritDoc} */
+ public void handleError(String message) throws CDKException {
+ if (this.errorHandler != null) this.errorHandler.handleError(message);
+ if (this.mode == Mode.STRICT) throw new CDKException(message);
+ }
+
+ /** {@inheritDoc} */
+ public void handleError(String message, Exception exception)
+ throws CDKException {
+ if (this.errorHandler != null)
+ this.errorHandler.handleError(message, exception);
+ if (this.mode == Mode.STRICT) {
+ throw new CDKException(message, exception);
+ }
+ }
+
+ /** {@inheritDoc} */
+ public void handleError(String message, int row, int col) throws CDKException {
+ if (this.errorHandler != null)
+ this.errorHandler.handleError(message, row, col);
+ if (this.mode == Mode.STRICT) throw new CDKException(message);
+ }
+
+ /** {@inheritDoc} */
+ public void handleError(String message, int row, int col, Exception exception)
+ throws CDKException {
+ if (this.errorHandler != null)
+ this.errorHandler.handleError(message, row, col, exception);
+ if (this.mode == Mode.STRICT) {
+ throw new CDKException(message, exception);
+ }
+ }
}
--
1.6.6.1
From 362e7926b7b4c4e6ba8f93a98eda639e829c180b Mon Sep 17 00:00:00 2001
From: Egon Willighagen <egonw@users.sourceforge.net>
Date: Sat, 30 Jan 2010 12:11:49 +0100
Subject: [PATCH 2/2] Use the new error reporting IO API
---
.../org/openscience/cdk/io/MDLV2000Reader.java | 69 ++++++++++++++------
1 files changed, 48 insertions(+), 21 deletions(-)
diff --git a/src/main/org/openscience/cdk/io/MDLV2000Reader.java b/src/main/org/openscience/cdk/io/MDLV2000Reader.java
index bffaeda..06e6f30 100644
--- a/src/main/org/openscience/cdk/io/MDLV2000Reader.java
+++ b/src/main/org/openscience/cdk/io/MDLV2000Reader.java
@@ -1,6 +1,5 @@
-/* $Revision$ $Author$ $Date$
- *
- * Copyright (C) 1997-2007 Christoph Steinbeck <steinbeck@users.sourceforge.net>
+/* Copyright (C) 1997-2007 Christoph Steinbeck <steinbeck@users.sourceforge.net>
+ * 2010 Egon Willighagen <egonw@users.sourceforge.net>
*
* Contact: cdk-devel@lists.sourceforge.net
*
@@ -386,14 +385,14 @@ public class MDLV2000Reader extends DefaultChemObjectReader {
}
}
}
-
- if (mode == Mode.STRICT) {
- if (line.contains("V3000") || line.contains("v3000")) {
- throw new CDKException("This file must be read with the MDLV3000Reader.");
- } else if (!line.contains("V2000") && !line.contains("v2000")) {
- throw new CDKException("This file must be read with the MDLReader.");
- }
- }
+
+ // check the CT block version
+ if (line.contains("V3000") || line.contains("v3000")) {
+ handleError("This file must be read with the MDLV3000Reader.");
+ } else if (!line.contains("V2000") && !line.contains("v2000")) {
+ handleError("This file must be read with the MDLReader.");
+ }
+
atoms = Integer.parseInt(line.substring(0, 3).trim());
logger.debug("Atomcount: " + atoms);
bonds = Integer.parseInt(line.substring(3, 6).trim());
@@ -442,9 +441,11 @@ public class MDLV2000Reader extends DefaultChemObjectReader {
}
atom = molecule.getBuilder().newPseudoAtom(element);
} else {
- if (mode == ISimpleChemObjectReader.Mode.STRICT) {
- throw new CDKException("Invalid element type. Must be an existing element, or one in: A, Q, L, LP, *.");
- }
+ handleError(
+ "Invalid element type. Must be an existing " +
+ "element, or one in: A, Q, L, LP, *.",
+ linecount, 32
+ );
atom = molecule.getBuilder().newPseudoAtom(element);
}
@@ -462,7 +463,11 @@ public class MDLV2000Reader extends DefaultChemObjectReader {
atom.setMassNumber(major.getMassNumber() + massDiff);
}
} catch (Exception exception) {
- logger.error("Could not parse mass difference field");
+ handleError(
+ "Could not parse mass difference field.",
+ linecount, 35,
+ exception
+ );
}
} else {
logger.error("Cannot set mass difference for a non-element!");
@@ -572,7 +577,10 @@ public class MDLV2000Reader extends DefaultChemObjectReader {
stereo = (IBond.Stereo)CDKConstants.UNSET;
}
} else {
- logger.warn("Missing expected stereo field at line: " + line);
+ handleError(
+ "Missing expected stereo field at line: ",
+ linecount, 12
+ );
}
if (logger.isDebugEnabled()) {
logger.debug("Bond: " + atom1 + " - " + atom2 + "; order " + order);
@@ -609,7 +617,10 @@ public class MDLV2000Reader extends DefaultChemObjectReader {
while (true) {
line = input.readLine(); linecount++;
if (line == null) {
- throw new CDKException("The expected property block is missing!");
+ handleError(
+ "The expected property block is missing!",
+ linecount, 0
+ );
}
if (line.startsWith("M END")) break;
@@ -678,7 +689,11 @@ public class MDLV2000Reader extends DefaultChemObjectReader {
String error = "Error (" + exception.getMessage() + ") while parsing line "
+ linecount + ": " + line + " in property block.";
logger.error(error);
- throw new CDKException("NumberFormatException in isotope information on line: " + line, exception);
+ handleError(
+ "NumberFormatException in isotope information.",
+ linecount, 7,
+ exception
+ );
}
} else if (line.startsWith("M RAD")) {
try {
@@ -701,7 +716,11 @@ public class MDLV2000Reader extends DefaultChemObjectReader {
String error = "Error (" + exception.getMessage() + ") while parsing line "
+ linecount + ": " + line + " in property block.";
logger.error(error);
- throw new CDKException("NumberFormatException in radical information on line: " + line, exception);
+ handleError(
+ "NumberFormatException in radical information",
+ linecount, 7,
+ exception
+ );
}
} else if (line.startsWith("G ")) {
try {
@@ -725,7 +744,11 @@ public class MDLV2000Reader extends DefaultChemObjectReader {
String error = "Error (" + exception.toString() + ") while parsing line "
+ linecount + ": " + line + " in property block.";
logger.error(error);
- throw new CDKException("NumberFormatException in group information on line: " + line, exception);
+ handleError(
+ "NumberFormatException in group information",
+ linecount, 4,
+ exception
+ );
}
}
if (!lineRead) {
@@ -742,7 +765,11 @@ public class MDLV2000Reader extends DefaultChemObjectReader {
String error = "Error while parsing line " + linecount + ": " + line + " -> " + exception.getMessage();
logger.error(error);
logger.debug(exception);
- throw new CDKException(error, exception);
+ handleError(
+ "Error while parsing line: " + line,
+ linecount, 7,
+ exception
+ );
}
if (interpretHydrogenIsotopes.isSet()) {
fixHydrogenIsotopes(molecule);
--
1.6.6.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment