[jdom-interest] Adapters exception patch

Elliotte Rusty Harold elharo at metalab.unc.edu
Mon Apr 8 11:45:16 PDT 2002


I've attached a large patch that changes virtually every file in the 
adapters package. As we discussed the prupose of this is to rationalize 
the exception handling. Please check the patch for sanity before 
applying. I'm still a little shaky with CVS. This is the first time I've 
tried to do a diff against an entire package.

What this patch does is limit all the adapters to throwing either 
JDOMExceptions or IOExceptions. IOExceptions are thrown only on an 
actual I/O error such as a broken socket. They are no longer thrown due 
to malformed XML or missing classes. These now throw JDOMException instead.

These changes do not break any existing tests that weren't broken 
before. They should not break any existing code because the adapter 
classes were previously declared to throw Exception, and I've made the 
throws clauses more specific. Still, I haven't really testes these at 
all yet so if anyone has any existing code that actually uses this I'd 
appreciate hearing your experiences.

-- 
+-----------------------+------------------------+-------------------+
| Elliotte Rusty Harold | elharo at metalab.unc.edu | Writer/Programmer |
+-----------------------+------------------------+-------------------+ 
|           The XML Bible, 2nd Edition (IDG Books, 2001)             |
|             http://www.cafeconleche.org/books/bible2/              |
|   http://www.amazon.com/exec/obidos/ISBN=0764547607/cafeaulaitA/   |
+----------------------------------+---------------------------------+
|  Read Cafe au Lait for Java News:   http://www.cafeaulait.org/     | 
|  Read Cafe con Leche for XML News:  http://www.cafeconleche.org/   |
+----------------------------------+---------------------------------+

-------------- next part --------------
? src/java/org/jdom/adapters/GNUDOMAdapter.java
Index: src/java/org/jdom/adapters/AbstractDOMAdapter.java
===================================================================
RCS file: /home/cvspublic/jdom/src/java/org/jdom/adapters/AbstractDOMAdapter.java,v
retrieving revision 1.13
diff -d -u -r1.13 AbstractDOMAdapter.java
--- src/java/org/jdom/adapters/AbstractDOMAdapter.java	2002/02/14 09:16:38	1.13
+++ src/java/org/jdom/adapters/AbstractDOMAdapter.java	2002/04/09 02:43:14
@@ -56,6 +56,7 @@
 
 package org.jdom.adapters;
 
+import java.io.IOException;
 import java.io.FileInputStream;
 import java.io.File;
 import java.io.InputStream;
@@ -93,10 +94,11 @@
      * @param filename file to parse.
      * @param validate <code>boolean</code> to indicate if validation should occur.
      * @return <code>Document</code> - instance ready for use.
-     * @throws Exception when errors occur in parsing.
+     * @throws IOException when I/O error occurs.
+     * @throws JDOMException when errors occur in parsing.
      */
     public Document getDocument(File filename, boolean validate)
-        throws Exception {
+        throws IOException, JDOMException {
 
         return getDocument(new FileInputStream(filename), validate);
     }
@@ -111,10 +113,11 @@
      * @param in <code>InputStream</code> to parse.
      * @param validate <code>boolean</code> to indicate if validation should occur.
      * @return <code>Document</code> - instance ready for use.
-     * @throws Exception when errors occur in parsing.
+     * @throws IOException when I/O error occurs.
+     * @throws JDOMException when errors occur in parsing.
      */
     public abstract Document getDocument(InputStream in, boolean validate)
-        throws Exception;
+        throws IOException, JDOMException;
 
     /**
      * <p>
@@ -123,9 +126,9 @@
      * </p>
      *
      * @return <code>Document</code> - created DOM Document.
-     * @throws Exception when errors occur.
+     * @throws JDOMException when errors occur.
      */
-    public abstract Document createDocument() throws Exception;
+    public abstract Document createDocument() throws JDOMException;
 
     /**
      * <p>
@@ -137,9 +140,9 @@
      *
      * @param doctype Initial <code>DocType</code> of the document.
      * @return <code>Document</code> - created DOM Document.
-     * @throws Exception when errors occur.
+     * @throws JDOMException when errors occur.
      */
-    public Document createDocument(DocType doctype) throws Exception {
+    public Document createDocument(DocType doctype) throws JDOMException {
         if (doctype == null) {
             return createDocument();
         }
Index: src/java/org/jdom/adapters/CrimsonDOMAdapter.java
===================================================================
RCS file: /home/cvspublic/jdom/src/java/org/jdom/adapters/CrimsonDOMAdapter.java,v
retrieving revision 1.8
diff -d -u -r1.8 CrimsonDOMAdapter.java
--- src/java/org/jdom/adapters/CrimsonDOMAdapter.java	2002/01/08 09:17:10	1.8
+++ src/java/org/jdom/adapters/CrimsonDOMAdapter.java	2002/04/09 02:43:15
@@ -66,6 +66,8 @@
 import org.xml.sax.InputSource;
 import org.xml.sax.SAXParseException;
 
+import org.jdom.JDOMException;
+
 /**
  * <b><code>CrimsonDOMAdapater</code></b>
  * <p>
@@ -91,10 +93,11 @@
      * @param in <code>InputStream</code> to parse.
      * @param validate <code>boolean</code> to indicate if validation should occur.
      * @return <code>Document</code> - instance ready for use.
-     * @throws IOException when errors occur in parsing.
+     * @throws IOException when I/O error occurs.
+     * @throws JDOMException when errors occur in parsing.
      */
     public Document getDocument(InputStream in, boolean validate)
-        throws IOException  {
+        throws IOException, JDOMException  {
 
         try {
             Class[] parameterTypes = new Class[2];
@@ -122,14 +125,17 @@
             Throwable targetException = e.getTargetException();
             if (targetException instanceof org.xml.sax.SAXParseException) {
                 SAXParseException parseException = (SAXParseException)targetException;
-                throw new IOException("Error on line " + parseException.getLineNumber() +
-                                      " of XML document: " + parseException.getMessage());
+                throw new JDOMException("Error on line " + parseException.getLineNumber() +
+                                      " of XML document: " + parseException.getMessage(), parseException);
+            } else if (targetException instanceof IOException) {
+                IOException ioException = (IOException) targetException;
+                throw ioException;
             } else {
-                throw new IOException(targetException.getMessage());
+                throw new JDOMException(targetException.getMessage(), targetException);
             }
         } catch (Exception e) {
-            throw new IOException(e.getClass().getName() + ": " +
-                                  e.getMessage());
+            throw new JDOMException(e.getClass().getName() + ": " +
+                                  e.getMessage(), e);
         }
     }
 
@@ -140,9 +146,9 @@
      * </p>
      *
      * @return <code>Document</code> - created DOM Document.
-     * @throws IOException when errors occur.
+     * @throws JDOMException when errors occur.
      */
-    public Document createDocument() throws IOException {
+    public Document createDocument() throws JDOMException {
         try {
             return
                 (Document)Class.forName(
@@ -150,8 +156,8 @@
                     .newInstance();
 
         } catch (Exception e) {
-            throw new IOException(e.getClass().getName() + ": " +
-                                  e.getMessage());
+            throw new JDOMException(e.getClass().getName() + ": " +
+                                  e.getMessage() + " when creating document", e);
         }
     }
 }
Index: src/java/org/jdom/adapters/DOMAdapter.java
===================================================================
RCS file: /home/cvspublic/jdom/src/java/org/jdom/adapters/DOMAdapter.java,v
retrieving revision 1.13
diff -d -u -r1.13 DOMAdapter.java
--- src/java/org/jdom/adapters/DOMAdapter.java	2002/01/08 09:17:10	1.13
+++ src/java/org/jdom/adapters/DOMAdapter.java	2002/04/09 02:43:15
@@ -56,6 +56,7 @@
 
 package org.jdom.adapters;
 
+import java.io.IOException;
 import java.io.FileInputStream;
 import java.io.File;
 import java.io.InputStream;
@@ -63,6 +64,7 @@
 import org.w3c.dom.Document;
 
 import org.jdom.DocType;
+import org.jdom.JDOMException;
 
 /**
  * <b><code>DOMAdapter</code></b>
@@ -87,10 +89,11 @@
      * @param validate <code>boolean</code> to indicate if validation 
      * should occur.
      * @return <code>Document</code> - instance ready for use.
-     * @throws Exception when errors occur in parsing.
+     * @throws IOException when I/O error occurs.
+     * @throws JDOMException when errors occur in parsing.
      */
     public Document getDocument(File filename, boolean validate)
-        throws Exception;
+        throws IOException, JDOMException;
 
     /**
      * <p>
@@ -103,10 +106,11 @@
      * @param validate <code>boolean</code> to indicate if validation 
      * should occur.
      * @return <code>Document</code> - instance ready for use.
-     * @throws Exception when errors occur in parsing.
+     * @throws IOException when I/O error occurs.
+     * @throws JDOMException when errors occur in parsing.
      */
     public Document getDocument(InputStream in, boolean validate)
-        throws Exception;
+        throws IOException, JDOMException;
 
     /**
      * <p>
@@ -117,7 +121,7 @@
      * @return <code>Document</code> - created DOM Document.
      * @throws Exception when errors occur.
      */
-    public Document createDocument() throws Exception;
+    public Document createDocument() throws JDOMException;
 
     /**
      * <p>
@@ -129,5 +133,5 @@
      * @return <code>Document</code> - created DOM Document.
      * @throws Exception when errors occur.
      */
-    public Document createDocument(DocType doctype) throws Exception;
+    public Document createDocument(DocType doctype) throws JDOMException;
 }
Index: src/java/org/jdom/adapters/JAXPDOMAdapter.java
===================================================================
RCS file: /home/cvspublic/jdom/src/java/org/jdom/adapters/JAXPDOMAdapter.java,v
retrieving revision 1.5
diff -d -u -r1.5 JAXPDOMAdapter.java
--- src/java/org/jdom/adapters/JAXPDOMAdapter.java	2002/01/08 09:17:10	1.5
+++ src/java/org/jdom/adapters/JAXPDOMAdapter.java	2002/04/09 02:43:15
@@ -66,6 +66,7 @@
 import org.xml.sax.InputSource;
 import org.xml.sax.SAXParseException;
 
+import org.jdom.JDOMException;
 import org.jdom.input.BuilderErrorHandler;
 
 /**
@@ -94,60 +95,70 @@
      * @param validate <code>boolean</code> to indicate if validation 
      *        should occur.
      * @return <code>Document</code> - instance ready for use.
-     */
+     * @throws IOException when I/O error occurs.
+     * @throws JDOMException when errors occur in parsing.
+      */
     public Document getDocument(InputStream in, boolean validate)
-        throws ClassNotFoundException, NoSuchMethodException, 
-               IllegalAccessException, InvocationTargetException {
-
-        // Try using JAXP...
-        // Note we need DOM Level 2 and thus JAXP 1.1.
-        Class.forName("javax.xml.transform.Transformer");
+        throws IOException, JDOMException {
 
-        // Try JAXP 1.1 calls to build the document
-        Class factoryClass =
-            Class.forName("javax.xml.parsers.DocumentBuilderFactory");
+        try {
+            // Try using JAXP...
+            // Note we need DOM Level 2 and thus JAXP 1.1.
+            Class.forName("javax.xml.transform.Transformer");
 
-        // factory = DocumentBuilderFactory.newInstance();
-        Method newParserInstance =
-            factoryClass.getMethod("newInstance", null);
-        Object factory = newParserInstance.invoke(null, null);
+            // Try JAXP 1.1 calls to build the document
+            Class factoryClass =
+                Class.forName("javax.xml.parsers.DocumentBuilderFactory");
 
-        // factory.setValidating(validate);
-        Method setValidating =
-            factoryClass.getMethod("setValidating",
-                                   new Class[]{boolean.class});
-        setValidating.invoke(factory,
-                             new Object[]{new Boolean(validate)});
+            // factory = DocumentBuilderFactory.newInstance();
+            Method newParserInstance =
+                factoryClass.getMethod("newInstance", null);
+            Object factory = newParserInstance.invoke(null, null);
 
-        // factory.setNamespaceAware(true);
-        Method setNamespaceAware =
-            factoryClass.getMethod("setNamespaceAware",
+            // factory.setValidating(validate);
+            Method setValidating =
+                factoryClass.getMethod("setValidating",
                                    new Class[]{boolean.class});
-        setNamespaceAware.invoke(factory,
-                             new Object[]{Boolean.TRUE});
+            setValidating.invoke(factory,
+                                 new Object[]{new Boolean(validate)});
 
-        // jaxpParser = factory.newDocumentBuilder();
-        Method newDocBuilder =
-            factoryClass.getMethod("newDocumentBuilder", null);
-        Object jaxpParser  = newDocBuilder.invoke(factory, null);
+            // factory.setNamespaceAware(true);
+            Method setNamespaceAware =
+                factoryClass.getMethod("setNamespaceAware",
+                                       new Class[]{boolean.class});
+            setNamespaceAware.invoke(factory,
+                                 new Object[]{Boolean.TRUE});
+    
+            // jaxpParser = factory.newDocumentBuilder();
+            Method newDocBuilder =
+                factoryClass.getMethod("newDocumentBuilder", null);
+            Object jaxpParser  = newDocBuilder.invoke(factory, null);
 
-        // jaxpParser.setErrorHandler(null);
-        Class parserClass = jaxpParser.getClass();
-        Method setErrorHandler =
-            parserClass.getMethod("setErrorHandler",
-                             new Class[]{org.xml.sax.ErrorHandler.class});
-        setErrorHandler.invoke(jaxpParser,
-                             new Object[]{new BuilderErrorHandler()});
+            // jaxpParser.setErrorHandler(null);
+            Class parserClass = jaxpParser.getClass();
+            Method setErrorHandler =
+                parserClass.getMethod("setErrorHandler",
+                                 new Class[]{org.xml.sax.ErrorHandler.class});
+            setErrorHandler.invoke(jaxpParser,
+                                 new Object[]{new BuilderErrorHandler()});
 
-        // domDoc = jaxpParser.parse(in);
-        Method parse = parserClass.getMethod(
-            "parse", new Class[]{InputStream.class});
-        org.w3c.dom.Document domDoc = (org.w3c.dom.Document)
-            parse.invoke(jaxpParser, new Object[]{in});
+            // domDoc = jaxpParser.parse(in);
+            Method parse = parserClass.getMethod(
+                "parse", new Class[]{InputStream.class});
+            org.w3c.dom.Document domDoc = (org.w3c.dom.Document)
+                parse.invoke(jaxpParser, new Object[]{in});
 
-        // System.out.println("Using jaxp " +
-        //   domDoc.getClass().getName());
-        return domDoc;
+            return domDoc;
+        } catch (InvocationTargetException e) {
+            Throwable targetException = e.getTargetException();
+            if (targetException instanceof IOException) {
+                throw (IOException) targetException;
+            } else {
+                throw new JDOMException(targetException.getMessage(), targetException);
+            }
+        } catch (Exception e) {
+            throw new JDOMException("Reflection failed while parsing a document with JAXP", e); 
+        }
 
         // Allow all exceptions to pass through
     }
@@ -159,39 +170,40 @@
      * </p>
      *
      * @return <code>Document</code> - created DOM Document.
-     */
+     * @throws JDOMException when errors occur in parsing.
+      */
     public Document createDocument() 
-        throws ClassNotFoundException, NoSuchMethodException, 
-               IllegalAccessException, InvocationTargetException {
+        throws JDOMException {
 
-        // We need DOM Level 2 and thus JAXP 1.1.
-        // If JAXP 1.0 is all that's available then we error out.
-        Class.forName("javax.xml.transform.Transformer");
+        try {
+            // We need DOM Level 2 and thus JAXP 1.1.
+            // If JAXP 1.0 is all that's available then we error out.
+            Class.forName("javax.xml.transform.Transformer");
 
-        // Try JAXP 1.1 calls to build the document
-        Class factoryClass =
-            Class.forName("javax.xml.parsers.DocumentBuilderFactory");
+            // Try JAXP 1.1 calls to build the document
+            Class factoryClass =
+                Class.forName("javax.xml.parsers.DocumentBuilderFactory");
 
-        // factory = DocumentBuilderFactory.newInstance();
-        Method newParserInstance =
-            factoryClass.getMethod("newInstance", null);
-        Object factory = newParserInstance.invoke(null, null);
+            // factory = DocumentBuilderFactory.newInstance();
+            Method newParserInstance =
+                factoryClass.getMethod("newInstance", null);
+            Object factory = newParserInstance.invoke(null, null);
 
-        // jaxpParser = factory.newDocumentBuilder();
-        Method newDocBuilder =
-            factoryClass.getMethod("newDocumentBuilder", null);
-        Object jaxpParser  = newDocBuilder.invoke(factory, null);
+            // jaxpParser = factory.newDocumentBuilder();
+            Method newDocBuilder =
+                factoryClass.getMethod("newDocumentBuilder", null);
+            Object jaxpParser  = newDocBuilder.invoke(factory, null);
 
-        // domDoc = jaxpParser.newDocument();
-        Class parserClass = jaxpParser.getClass();
-        Method newDoc = parserClass.getMethod("newDocument", null);
-        org.w3c.dom.Document domDoc =
-            (org.w3c.dom.Document) newDoc.invoke(jaxpParser, null);
+            // domDoc = jaxpParser.newDocument();
+            Class parserClass = jaxpParser.getClass();
+            Method newDoc = parserClass.getMethod("newDocument", null);
+            org.w3c.dom.Document domDoc =
+                (org.w3c.dom.Document) newDoc.invoke(jaxpParser, null);
 
-        return domDoc;
-        // System.out.println("Using jaxp " +
-        //   domDoc.getClass().getName());
+            return domDoc;
+        } catch (Exception e) {
+            throw new JDOMException("Reflection failed while creating new JAXP document", e); 
+        }
 
-        // Allow all exceptions to pass through
     }
 }
Index: src/java/org/jdom/adapters/OracleV1DOMAdapter.java
===================================================================
RCS file: /home/cvspublic/jdom/src/java/org/jdom/adapters/OracleV1DOMAdapter.java,v
retrieving revision 1.11
diff -d -u -r1.11 OracleV1DOMAdapter.java
--- src/java/org/jdom/adapters/OracleV1DOMAdapter.java	2002/01/08 09:17:10	1.11
+++ src/java/org/jdom/adapters/OracleV1DOMAdapter.java	2002/04/09 02:43:15
@@ -66,6 +66,8 @@
 import org.xml.sax.InputSource;
 import org.xml.sax.SAXParseException;
 
+import org.jdom.JDOMException;
+
 /**
  * <b><code>OracleV1DOMAdapter</code></b>
  * <p>
@@ -92,10 +94,11 @@
      * @param in <code>InputStream</code> to parse.
      * @param validate <code>boolean</code> to indicate if validation should occur.
      * @return <code>Document</code> - instance ready for use.
-     * @throws IOException when errors occur in parsing.
+     * @throws IOException when I/O error occurs.
+     * @throws JDOMException when errors occur in parsing.
      */
     public Document getDocument(InputStream in, boolean validate)
-        throws IOException  {
+        throws IOException, JDOMException  {
 
         try {
             // Load the parser class
@@ -120,15 +123,18 @@
         } catch (InvocationTargetException e) {
             Throwable targetException = e.getTargetException();
             if (targetException instanceof org.xml.sax.SAXParseException) {
-                SAXParseException parseException = (SAXParseException)targetException;
-                throw new IOException("Error on line " + parseException.getLineNumber() +
-                                      " of XML document: " + parseException.getMessage());
+                SAXParseException parseException = (SAXParseException) targetException;
+                throw new JDOMException("Error on line " + parseException.getLineNumber() +
+                                      " of XML document: " + parseException.getMessage(), parseException);
+            } else if (targetException instanceof IOException) {
+                IOException ioException = (IOException) targetException;
+                throw ioException;
             } else {
-                throw new IOException(targetException.getMessage());
+                throw new JDOMException(targetException.getMessage(), targetException);
             }
         } catch (Exception e) {
-            throw new IOException(e.getClass().getName() + ": " +
-                                  e.getMessage());
+            throw new JDOMException(e.getClass().getName() + ": " +
+                                  e.getMessage(), e);
         }
     }
 
@@ -139,9 +145,9 @@
      * </p>
      *
      * @return <code>Document</code> - created DOM Document.
-     * @throws IOException when errors occur.
+     * @throws JDOMException when errors occur.
      */
-    public Document createDocument() throws IOException {
+    public Document createDocument() throws JDOMException {
         try {
             return
                 (Document)Class.forName(
@@ -149,8 +155,8 @@
                     .newInstance();
 
         } catch (Exception e) {
-            throw new IOException(e.getClass().getName() + ": " +
-                                  e.getMessage());
+            throw new JDOMException(e.getClass().getName() + ": " +
+                                  e.getMessage() + " when creating document", e);
         }
     }
 }
Index: src/java/org/jdom/adapters/OracleV2DOMAdapter.java
===================================================================
RCS file: /home/cvspublic/jdom/src/java/org/jdom/adapters/OracleV2DOMAdapter.java,v
retrieving revision 1.9
diff -d -u -r1.9 OracleV2DOMAdapter.java
--- src/java/org/jdom/adapters/OracleV2DOMAdapter.java	2002/01/08 09:17:10	1.9
+++ src/java/org/jdom/adapters/OracleV2DOMAdapter.java	2002/04/09 02:43:15
@@ -66,6 +66,8 @@
 import org.xml.sax.InputSource;
 import org.xml.sax.SAXParseException;
 
+import org.jdom.JDOMException;
+
 /**
  * <b><code>OracleV2DOMAdapter</code></b>
  * <p>
@@ -92,10 +94,11 @@
      * @param in <code>InputStream</code> to parse.
      * @param validate <code>boolean</code> to indicate if validation should occur.
      * @return <code>Document</code> - instance ready for use.
-     * @throws IOException when errors occur in parsing.
+     * @throws IOException when I/O error occurs.
+     * @throws JDOMException when errors occur in parsing.
      */
     public Document getDocument(InputStream in, boolean validate)
-        throws IOException  {
+        throws IOException, JDOMException {
 
         try {
             // Load the parser class
@@ -121,14 +124,17 @@
             Throwable targetException = e.getTargetException();
             if (targetException instanceof org.xml.sax.SAXParseException) {
                 SAXParseException parseException = (SAXParseException)targetException;
-                throw new IOException("Error on line " + parseException.getLineNumber() +
-                                      " of XML document: " + parseException.getMessage());
+                throw new JDOMException("Error on line " + parseException.getLineNumber() +
+                                      " of XML document: " + parseException.getMessage(), parseException);
+            } else if (targetException instanceof IOException) {
+                IOException ioException = (IOException) targetException;
+                throw ioException;
             } else {
-                throw new IOException(targetException.getMessage());
+                throw new JDOMException(targetException.getMessage(), targetException);
             }
         } catch (Exception e) {
-            throw new IOException(e.getClass().getName() + ": " +
-                                  e.getMessage());
+            throw new JDOMException(e.getClass().getName() + ": " +
+                                  e.getMessage(), e);
         }
     }
 
@@ -139,9 +145,9 @@
      * </p>
      *
      * @return <code>Document</code> - created DOM Document.
-     * @throws IOException when errors occur.
+     * @throws JDOMException when errors occur.
      */
-    public Document createDocument() throws IOException {
+    public Document createDocument() throws JDOMException {
         try {
             return
                 (Document)Class.forName(
@@ -149,8 +155,8 @@
                     .newInstance();
 
         } catch (Exception e) {
-            throw new IOException(e.getClass().getName() + ": " +
-                                  e.getMessage());
+            throw new JDOMException(e.getClass().getName() + ": " +
+                                  e.getMessage() + " when creating document", e);
         }
     }
 }
Index: src/java/org/jdom/adapters/XML4JDOMAdapter.java
===================================================================
RCS file: /home/cvspublic/jdom/src/java/org/jdom/adapters/XML4JDOMAdapter.java,v
retrieving revision 1.9
diff -d -u -r1.9 XML4JDOMAdapter.java
--- src/java/org/jdom/adapters/XML4JDOMAdapter.java	2002/01/08 09:17:10	1.9
+++ src/java/org/jdom/adapters/XML4JDOMAdapter.java	2002/04/09 02:43:15
@@ -68,6 +68,7 @@
 import org.xml.sax.SAXException;
 import org.xml.sax.SAXParseException;
 
+import org.jdom.JDOMException;
 import org.jdom.input.BuilderErrorHandler;
 
 /**
@@ -96,10 +97,11 @@
      * @param in <code>InputStream</code> to parse.
      * @param validate <code>boolean</code> to indicate if validation should occur.
      * @return <code>Document</code> - instance ready for use.
-     * @throws IOException when errors occur in parsing.
+     * @throws IOException when I/O error occurs.
+     * @throws JDOMException when errors occur in parsing.
      */
     public Document getDocument(InputStream in, boolean validate)
-        throws IOException  {
+        throws IOException, JDOMException  {
 
         try {
             /*
@@ -146,14 +148,17 @@
             Throwable targetException = e.getTargetException();
             if (targetException instanceof org.xml.sax.SAXParseException) {
                 SAXParseException parseException = (SAXParseException)targetException;
-                throw new IOException("Error on line " + parseException.getLineNumber() +
-                                      " of XML document: " + parseException.getMessage());
+                throw new JDOMException("Error on line " + parseException.getLineNumber() +
+                                      " of XML document: " + parseException.getMessage(), parseException);
+            } else if (targetException instanceof IOException) {
+                IOException ioException = (IOException) targetException;
+                throw ioException;
             } else {
-                throw new IOException(targetException.getMessage());
+                throw new JDOMException(targetException.getMessage(), targetException);
             }
         } catch (Exception e) {
-            throw new IOException(e.getClass().getName() + ": " +
-                                  e.getMessage());
+            throw new JDOMException(e.getClass().getName() + ": " +
+                                  e.getMessage(), e);
         }
     }
 
@@ -164,9 +169,9 @@
      * </p>
      *
      * @return <code>Document</code> - created DOM Document.
-     * @throws IOException when errors occur.
+     * @throws JDOMException when errors occur.
      */
-    public Document createDocument() throws IOException {
+    public Document createDocument() throws JDOMException {
         try {
             return
                 (Document)Class.forName(
@@ -174,8 +179,8 @@
                     .newInstance();
 
         } catch (Exception e) {
-            throw new IOException(e.getClass().getName() + ": " +
-                                  e.getMessage());
+            throw new JDOMException(e.getClass().getName() + ": " +
+                                  e.getMessage() + " while creating document", e);
         }
     }
 }
Index: src/java/org/jdom/adapters/XercesDOMAdapter.java
===================================================================
RCS file: /home/cvspublic/jdom/src/java/org/jdom/adapters/XercesDOMAdapter.java,v
retrieving revision 1.10
diff -d -u -r1.10 XercesDOMAdapter.java
--- src/java/org/jdom/adapters/XercesDOMAdapter.java	2002/01/08 09:17:10	1.10
+++ src/java/org/jdom/adapters/XercesDOMAdapter.java	2002/04/09 02:43:15
@@ -68,6 +68,7 @@
 import org.xml.sax.SAXException;
 import org.xml.sax.SAXParseException;
 
+import org.jdom.JDOMException;
 import org.jdom.input.BuilderErrorHandler;
 
 /**
@@ -97,10 +98,11 @@
      * @param validate <code>boolean</code> to indicate if validation 
      * should occur.
      * @return <code>Document</code> - instance ready for use.
-     * @throws IOException when errors occur in parsing.
+     * @throws IOException when I/O error occurs.
+     * @throws JDOMException when errors occur in parsing.
      */
     public Document getDocument(InputStream in, boolean validate)
-        throws IOException  {
+        throws IOException, JDOMException  {
 
         try {
             // Load the parser class
@@ -146,16 +148,19 @@
             if (targetException instanceof org.xml.sax.SAXParseException) {
                 SAXParseException parseException =
                     (SAXParseException)targetException;
-                throw new IOException("Error on line " +
+                throw new JDOMException("Error on line " +
                                       parseException.getLineNumber() +
                                       " of XML document: " +
-                                      parseException.getMessage());
+                                      parseException.getMessage(), e);
+            } else if (targetException instanceof IOException) {
+                IOException ioException = (IOException) targetException;
+                throw ioException;
             } else {
-                throw new IOException(targetException.getMessage());
+                throw new JDOMException(targetException.getMessage(), e);
             }
         } catch (Exception e) {
-            throw new IOException(e.getClass().getName() + ": " +
-                                  e.getMessage());
+            throw new JDOMException(e.getClass().getName() + ": " +
+                                  e.getMessage(), e);
         }
     }
 
@@ -166,15 +171,15 @@
      * </p>
      *
      * @return <code>Document</code> - created DOM Document.
-     * @throws IOException when errors occur.
+     * @throws JDOMException when errors occur.
      */
-    public Document createDocument() throws IOException {
+    public Document createDocument() throws JDOMException {
         try {
             return (Document)Class.forName(
                 "org.apache.xerces.dom.DocumentImpl").newInstance();
         } catch (Exception e) {
-            throw new IOException(e.getClass().getName() + ": " +
-                                  e.getMessage());
+            throw new JDOMException(e.getClass().getName() + ": " +
+                                  e.getMessage() + " when creating document", e);
         }
     }
 }


More information about the jdom-interest mailing list