Author: mrglavas
Date: Mon Feb 13 22:22:02 2006
New Revision: 377643

URL: http://svn.apache.org/viewcvs?rev=377643&view=rev
Log:
JIRA Issue #1117:
http://issues.apache.org/jira/browse/XERCESJ-1117

When an LSException is created from another exception it should be set as
the cause on J2SE 1.4 and above. This improvement is based on the patch
from Matej Kraus. I've moved all of the reflection code into DOMUtil.
Instead of looking up the initCause() method each time, we only try once.
There were several other places in the code which create an LSException
from another exception which weren't covered by Matej's patch. I've applied
this change to all of those places.

Modified:
    xerces/java/trunk/src/org/apache/xerces/parsers/DOMParserImpl.java
    xerces/java/trunk/src/org/apache/xerces/util/DOMUtil.java
    xerces/java/trunk/src/org/apache/xml/serialize/DOMSerializerImpl.java

Modified: xerces/java/trunk/src/org/apache/xerces/parsers/DOMParserImpl.java
URL: 
http://svn.apache.org/viewcvs/xerces/java/trunk/src/org/apache/xerces/parsers/DOMParserImpl.java?rev=377643&r1=377642&r2=377643&view=diff
==============================================================================
--- xerces/java/trunk/src/org/apache/xerces/parsers/DOMParserImpl.java 
(original)
+++ xerces/java/trunk/src/org/apache/xerces/parsers/DOMParserImpl.java Mon Feb 
13 22:22:02 2006
@@ -1,5 +1,5 @@
 /*
- * Copyright 2000-2005 The Apache Software Foundation.
+ * Copyright 2000-2006 The Apache Software Foundation.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@
 import org.apache.xerces.impl.Constants;
 import org.apache.xerces.util.DOMEntityResolverWrapper;
 import org.apache.xerces.util.DOMErrorHandlerWrapper;
+import org.apache.xerces.util.DOMUtil;
 import org.apache.xerces.util.SymbolTable;
 import org.apache.xerces.util.XMLSymbols;
 import org.apache.xerces.xni.Augmentations;
@@ -895,7 +896,7 @@
                 if (DEBUG) {
                     e.printStackTrace ();
                 }
-                throw new LSException(LSException.PARSE_ERR, e.getMessage());
+                throw (LSException) 
DOMUtil.createLSException(LSException.PARSE_ERR, e).fillInStackTrace();
             }
         }
         return getDocument ();
@@ -950,7 +951,7 @@
                 if (DEBUG) {
                    e.printStackTrace ();
                 }
-                throw new LSException(LSException.PARSE_ERR, e.getMessage());
+                throw (LSException) 
DOMUtil.createLSException(LSException.PARSE_ERR, e).fillInStackTrace();
             }
         }
         return getDocument ();

Modified: xerces/java/trunk/src/org/apache/xerces/util/DOMUtil.java
URL: 
http://svn.apache.org/viewcvs/xerces/java/trunk/src/org/apache/xerces/util/DOMUtil.java?rev=377643&r1=377642&r2=377643&view=diff
==============================================================================
--- xerces/java/trunk/src/org/apache/xerces/util/DOMUtil.java (original)
+++ xerces/java/trunk/src/org/apache/xerces/util/DOMUtil.java Mon Feb 13 
22:22:02 2006
@@ -1,5 +1,5 @@
 /*
- * Copyright 1999-2002,2004,2005 The Apache Software Foundation.
+ * Copyright 1999-2002,2004-2006 The Apache Software Foundation.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -21,13 +21,13 @@
 import org.apache.xerces.dom.AttrImpl;
 import org.apache.xerces.dom.DocumentImpl;
 import org.apache.xerces.impl.xs.opti.ElementImpl;
-
 import org.w3c.dom.Attr;
-import org.w3c.dom.Document;
 import org.w3c.dom.DOMException;
+import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.NamedNodeMap;
 import org.w3c.dom.Node;
+import org.w3c.dom.ls.LSException;
 
 /**
  * Some useful utility methods.
@@ -836,4 +836,63 @@
         }
         return null;
     }
-} // class XUtil
+    
+    /**
+     * Creates a DOMException. On J2SE 1.4 and above the cause for the 
exception will be set.
+     */
+    public static DOMException createDOMException(short code, Throwable cause) 
{
+        DOMException de = new DOMException(code, cause != null ? 
cause.getMessage() : null);
+        if (cause != null && ThrowableMethods.fgThrowableMethodsAvailable) {
+            try {
+                ThrowableMethods.fgThrowableInitCauseMethod.invoke(de, new 
Object [] {cause});
+            }
+            // Something went wrong. There's not much we can do about it.
+            catch (Exception e) {}
+        }
+        return de;
+    }
+    
+    /**
+     * Creates an LSException. On J2SE 1.4 and above the cause for the 
exception will be set.
+     */
+    public static LSException createLSException(short code, Throwable cause) {
+        LSException lse = new LSException(code, cause != null ? 
cause.getMessage() : null);
+        if (cause != null && ThrowableMethods.fgThrowableMethodsAvailable) {
+            try {
+                ThrowableMethods.fgThrowableInitCauseMethod.invoke(lse, new 
Object [] {cause});
+            }
+            // Something went wrong. There's not much we can do about it.
+            catch (Exception e) {}
+        }
+        return lse;
+    }
+    
+    /**
+     * Holder of methods from java.lang.Throwable.
+     */
+    static class ThrowableMethods {
+        
+        // Method: java.lang.Throwable.initCause(java.lang.Throwable)
+        private static java.lang.reflect.Method fgThrowableInitCauseMethod = 
null;
+        
+        // Flag indicating whether or not Throwable methods available.
+        private static boolean fgThrowableMethodsAvailable = false;
+        
+        private ThrowableMethods() {}
+        
+        // Attempt to get methods for java.lang.Throwable on class 
initialization.
+        static {
+            try {
+                fgThrowableInitCauseMethod = 
Throwable.class.getMethod("initCause", new Class [] {Throwable.class});
+                fgThrowableMethodsAvailable = true;
+            }
+            // ClassNotFoundException, NoSuchMethodException or 
SecurityException
+            // Whatever the case, we cannot use java.nio.charset.*.
+            catch (Exception exc) {
+                fgThrowableInitCauseMethod = null;
+                fgThrowableMethodsAvailable = false;
+            }
+        }
+    }
+    
+} // class DOMUtil

Modified: xerces/java/trunk/src/org/apache/xml/serialize/DOMSerializerImpl.java
URL: 
http://svn.apache.org/viewcvs/xerces/java/trunk/src/org/apache/xml/serialize/DOMSerializerImpl.java?rev=377643&r1=377642&r2=377643&view=diff
==============================================================================
--- xerces/java/trunk/src/org/apache/xml/serialize/DOMSerializerImpl.java 
(original)
+++ xerces/java/trunk/src/org/apache/xml/serialize/DOMSerializerImpl.java Mon 
Feb 13 22:22:02 2006
@@ -1,5 +1,5 @@
 /*
- * Copyright 1999-2005 The Apache Software Foundation.
+ * Copyright 1999-2006 The Apache Software Foundation.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -41,6 +41,7 @@
 import org.w3c.dom.DOMStringList;
 import org.apache.xerces.impl.Constants;
 import org.apache.xerces.impl.XMLEntityManager;
+import org.apache.xerces.util.DOMUtil;
 import org.apache.xerces.util.NamespaceSupport;
 import org.apache.xerces.util.SymbolTable;
 import org.apache.xerces.util.XML11Char;
@@ -521,7 +522,7 @@
                 // stopped at user request
                 return null;
             }
-            throw new LSException(LSException.SERIALIZE_ERR, e.toString());    
        
+            throw (LSException) 
DOMUtil.createLSException(LSException.SERIALIZE_ERR, e).fillInStackTrace();     
  
         } catch (IOException ioe) {
             // REVISIT: A generic IOException doesn't provide enough 
information
             // to determine that the serialized document is too large to fit
@@ -810,7 +811,7 @@
                 // stopped at user request
                 return false;
             }
-            throw new LSException(LSException.SERIALIZE_ERR, e.toString());    
        
+            throw (LSException) 
DOMUtil.createLSException(LSException.SERIALIZE_ERR, e).fillInStackTrace();
         } catch (Exception e) {
             if (ser.fDOMErrorHandler != null) {
                 DOMErrorImpl error = new DOMErrorImpl();
@@ -819,9 +820,8 @@
                 error.fSeverity = DOMError.SEVERITY_ERROR;
                 ser.fDOMErrorHandler.handleError(error);
 
-            }   
-            e.printStackTrace();       
-            throw new LSException(LSException.SERIALIZE_ERR, e.toString());
+            }
+            throw (LSException) 
DOMUtil.createLSException(LSException.SERIALIZE_ERR, e).fillInStackTrace();
         }
         return true;
 
@@ -960,7 +960,7 @@
                 // stopped at user request
                 return false;
             }
-            throw new LSException(LSException.SERIALIZE_ERR, e.toString());    
        
+            throw (LSException) 
DOMUtil.createLSException(LSException.SERIALIZE_ERR, e).fillInStackTrace();
         } catch (Exception e) {
             if (ser.fDOMErrorHandler != null) {
                 DOMErrorImpl error = new DOMErrorImpl();
@@ -969,7 +969,7 @@
                 error.fSeverity = DOMError.SEVERITY_ERROR;
                 ser.fDOMErrorHandler.handleError(error);
             }
-            throw new LSException(LSException.SERIALIZE_ERR, e.toString());
+            throw (LSException) 
DOMUtil.createLSException(LSException.SERIALIZE_ERR, e).fillInStackTrace();
         }
         return true;
     } //writeURI



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to