Guest User

Untitled

a guest
Apr 7th, 2018
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 26.25 KB | None | 0 0
  1. From 3a765e0dfd10ed9b02ed6b864748c7ebe83148cb Mon Sep 17 00:00:00 2001
  2. From: Egon Willighagen <egonw@users.sourceforge.net>
  3. Date: Sat, 30 Jan 2010 11:54:31 +0100
  4. Subject: [PATCH 1/2] Added a new IO API for reporting file format errors.
  5.  
  6. * introducing a IChemObjectReaderErrorHandler
  7. * handleError() methods for IChemObjectReader's to report problems
  8. * these handleErrors() method throw an CDKException when
  9. in STRICT mode
  10. * line and column number support, useful for MDL V2000 molfile text
  11. editors in, e.g. Bioclipse
  12. ---
  13. .../cdk/io/DefaultChemObjectReader.java | 46 ++++++++++++++++--
  14. .../org/openscience/cdk/io/IChemObjectReader.java | 52 ++++++++++++++++++-
  15. .../iterator/DefaultIteratingChemObjectReader.java | 49 ++++++++++++++++--
  16. 3 files changed, 134 insertions(+), 13 deletions(-)
  17.  
  18. diff --git a/src/main/org/openscience/cdk/io/DefaultChemObjectReader.java b/src/main/org/openscience/cdk/io/DefaultChemObjectReader.java
  19. index 1807005..1015c49 100644
  20. --- a/src/main/org/openscience/cdk/io/DefaultChemObjectReader.java
  21. +++ b/src/main/org/openscience/cdk/io/DefaultChemObjectReader.java
  22. @@ -1,6 +1,5 @@
  23. -/* $Revision$ $Author$ $Date$
  24. - *
  25. - * Copyright (C) 2002-2007 The Jmol Development Team
  26. +/* Copyright (C) 2002-2007 The Jmol Development Team
  27. + * 2010 Egon Willighagen <egonw@users.sf.net>
  28. *
  29. * Contact: cdk-devel@lists.sourceforge.net
  30. *
  31. @@ -23,6 +22,7 @@ package org.openscience.cdk.io;
  32. import java.util.ArrayList;
  33. import java.util.List;
  34.  
  35. +import org.openscience.cdk.exception.CDKException;
  36. import org.openscience.cdk.io.listener.IChemObjectIOListener;
  37. import org.openscience.cdk.io.listener.IReaderListener;
  38. import org.openscience.cdk.io.setting.IOSetting;
  39. @@ -42,6 +42,7 @@ public abstract class DefaultChemObjectReader implements ISimpleChemObjectReader
  40. private ReaderEvent frameReadEvent = null;
  41.  
  42. protected IChemObjectReader.Mode mode = IChemObjectReader.Mode.RELAXED;
  43. + protected IChemObjectReaderErrorHandler errorHandler = null;
  44.  
  45. /**
  46. * Holder of reader event listeners.
  47. @@ -88,5 +89,42 @@ public abstract class DefaultChemObjectReader implements ISimpleChemObjectReader
  48. public void setReaderMode(ISimpleChemObjectReader.Mode mode) {
  49. this.mode = mode;
  50. }
  51. -
  52. +
  53. + /** {@inheritDoc} */
  54. + public void setErrorHandler(IChemObjectReaderErrorHandler handler) {
  55. + this.errorHandler = handler;
  56. + }
  57. +
  58. + /** {@inheritDoc} */
  59. + public void handleError(String message) throws CDKException {
  60. + if (this.errorHandler != null) this.errorHandler.handleError(message);
  61. + if (this.mode == Mode.STRICT) throw new CDKException(message);
  62. + }
  63. +
  64. + /** {@inheritDoc} */
  65. + public void handleError(String message, Exception exception)
  66. + throws CDKException {
  67. + if (this.errorHandler != null)
  68. + this.errorHandler.handleError(message, exception);
  69. + if (this.mode == Mode.STRICT) {
  70. + throw new CDKException(message, exception);
  71. + }
  72. + }
  73. +
  74. + /** {@inheritDoc} */
  75. + public void handleError(String message, int row, int col) throws CDKException {
  76. + if (this.errorHandler != null)
  77. + this.errorHandler.handleError(message, row, col);
  78. + if (this.mode == Mode.STRICT) throw new CDKException(message);
  79. + }
  80. +
  81. + /** {@inheritDoc} */
  82. + public void handleError(String message, int row, int col, Exception exception)
  83. + throws CDKException {
  84. + if (this.errorHandler != null)
  85. + this.errorHandler.handleError(message, row, col, exception);
  86. + if (this.mode == Mode.STRICT) {
  87. + throw new CDKException(message, exception);
  88. + }
  89. + }
  90. }
  91. diff --git a/src/main/org/openscience/cdk/io/IChemObjectReader.java b/src/main/org/openscience/cdk/io/IChemObjectReader.java
  92. index d9a29bc..8709fa4 100644
  93. --- a/src/main/org/openscience/cdk/io/IChemObjectReader.java
  94. +++ b/src/main/org/openscience/cdk/io/IChemObjectReader.java
  95. @@ -1,6 +1,4 @@
  96. -/* $Revision$ $Author$ $Date$
  97. - *
  98. - * Copyright (C) 2000-2007 Egon Willighagen <egonw@users.sf.net>
  99. +/* Copyright (C) 2000-2007,2010 Egon Willighagen <egonw@users.sf.net>
  100. *
  101. * Contact: cdk-devel@lists.sourceforge.net
  102. *
  103. @@ -66,5 +64,53 @@ public interface IChemObjectReader extends IChemObjectIO {
  104. * @param mode
  105. */
  106. public void setReaderMode(Mode mode);
  107. +
  108. + /**
  109. + * Sets an error handler that is sent events when file format issues occur.
  110. + *
  111. + * @param handler {@link IChemObjectReaderErrorHandler} to send error
  112. + * messages to.
  113. + */
  114. + public void setErrorHandler(IChemObjectReaderErrorHandler handler);
  115. +
  116. + /**
  117. + * Redirects an error message to the {@link IChemObjectReaderErrorHandler}.
  118. + * Throws an {@link CDKException} when in STRICT {@link Mode}.
  119. + *
  120. + * @param message the error message.
  121. + */
  122. + public void handleError(String message) throws CDKException;
  123. +
  124. + /**
  125. + * Redirects an error message to the {@link IChemObjectReaderErrorHandler}.
  126. + * Throws an {@link CDKException} when in STRICT {@link Mode}.
  127. + *
  128. + * @param message the error message.
  129. + * @param exception the corresponding {@link Exception}.
  130. + */
  131. + public void handleError(String message, Exception exception)
  132. + throws CDKException;
  133. +
  134. + /**
  135. + * Redirects an error message to the {@link IChemObjectReaderErrorHandler}.
  136. + * Throws an {@link CDKException} when in STRICT {@link Mode}.
  137. + *
  138. + * @param message the error message.
  139. + * @param row Row in the file where the error is found.
  140. + * @param col Column in the file where the error is found.
  141. + */
  142. + public void handleError(String message, int row, int col) throws CDKException;
  143. +
  144. + /**
  145. + * Redirects an error message to the {@link IChemObjectReaderErrorHandler}.
  146. + * Throws an {@link CDKException} when in STRICT {@link Mode}.
  147. + *
  148. + * @param message the error message.
  149. + * @param exception the corresponding {@link Exception}.
  150. + * @param row Row in the file where the error is found.
  151. + * @param col Column in the file where the error is found.
  152. + */
  153. + public void handleError(String message, int row, int col, Exception exception)
  154. + throws CDKException;
  155. }
  156.  
  157. diff --git a/src/main/org/openscience/cdk/io/iterator/DefaultIteratingChemObjectReader.java b/src/main/org/openscience/cdk/io/iterator/DefaultIteratingChemObject
  158. Reader.java
  159. index 8b79efd..6131481 100644
  160. --- a/src/main/org/openscience/cdk/io/iterator/DefaultIteratingChemObjectReader.java
  161. +++ b/src/main/org/openscience/cdk/io/iterator/DefaultIteratingChemObjectReader.java
  162. @@ -1,9 +1,5 @@
  163. -/* $RCSfile$
  164. - * $Author$
  165. - * $Date$
  166. - * $Revision$
  167. - *
  168. - * Copyright (C) 2003-2007 The Jmol Development Team
  169. +/* Copyright (C) 2003-2007 The Jmol Development Team
  170. + * 2010 Egon Willighagen <egonw@users.sf.net>
  171. *
  172. * Contact: cdk-devel@lists.sourceforge.net
  173. *
  174. @@ -26,8 +22,11 @@ package org.openscience.cdk.io.iterator;
  175. import java.util.ArrayList;
  176. import java.util.List;
  177.  
  178. +import org.openscience.cdk.exception.CDKException;
  179. import org.openscience.cdk.io.IChemObjectReader;
  180. +import org.openscience.cdk.io.IChemObjectReaderErrorHandler;
  181. import org.openscience.cdk.io.ISimpleChemObjectReader;
  182. +import org.openscience.cdk.io.IChemObjectReader.Mode;
  183. import org.openscience.cdk.io.listener.IChemObjectIOListener;
  184. import org.openscience.cdk.io.setting.IOSetting;
  185.  
  186. @@ -41,6 +40,7 @@ import org.openscience.cdk.io.setting.IOSetting;
  187. public abstract class DefaultIteratingChemObjectReader implements IIteratingChemObjectReader {
  188.  
  189. protected IChemObjectReader.Mode mode = IChemObjectReader.Mode.RELAXED;
  190. + protected IChemObjectReaderErrorHandler errorHandler = null;
  191.  
  192. /**
  193. * Holder of reader event listeners.
  194. @@ -83,4 +83,41 @@ public abstract class DefaultIteratingChemObjectReader implements IIteratingChem
  195. this.mode = mode;
  196. }
  197.  
  198. + /** {@inheritDoc} */
  199. + public void setErrorHandler(IChemObjectReaderErrorHandler handler) {
  200. + this.errorHandler = handler;
  201. + }
  202. +
  203. + /** {@inheritDoc} */
  204. + public void handleError(String message) throws CDKException {
  205. + if (this.errorHandler != null) this.errorHandler.handleError(message);
  206. + if (this.mode == Mode.STRICT) throw new CDKException(message);
  207. + }
  208. +
  209. + /** {@inheritDoc} */
  210. + public void handleError(String message, Exception exception)
  211. + throws CDKException {
  212. + if (this.errorHandler != null)
  213. + this.errorHandler.handleError(message, exception);
  214. + if (this.mode == Mode.STRICT) {
  215. + throw new CDKException(message, exception);
  216. + }
  217. + }
  218. +
  219. + /** {@inheritDoc} */
  220. + public void handleError(String message, int row, int col) throws CDKException {
  221. + if (this.errorHandler != null)
  222. + this.errorHandler.handleError(message, row, col);
  223. + if (this.mode == Mode.STRICT) throw new CDKException(message);
  224. + }
  225. +
  226. + /** {@inheritDoc} */
  227. + public void handleError(String message, int row, int col, Exception exception)
  228. + throws CDKException {
  229. + if (this.errorHandler != null)
  230. + this.errorHandler.handleError(message, row, col, exception);
  231. + if (this.mode == Mode.STRICT) {
  232. + throw new CDKException(message, exception);
  233. + }
  234. + }
  235. }
  236. --
  237. 1.6.6.1
Add Comment
Please, Sign In to add comment