db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bpendle...@apache.org
Subject svn commit: r1688297 - /db/derby/code/trunk/java/engine/org/apache/derby/vti/XmlVTI.java
Date Mon, 29 Jun 2015 20:11:49 GMT
Author: bpendleton
Date: Mon Jun 29 20:11:49 2015
New Revision: 1688297

URL: http://svn.apache.org/r1688297
Log:
DERBY-6820: Improve error handling in XmlVTI

This patch refines some of the error-handling behavior in the XmlVTI class:
- next() no longer calls printStackTrace if an exception is encountered.
- the SQLException thrown by next() now chains the original exception as cause
- an XMLErrorHandler is installed to process any internal parsing errors
- the XMLErrorHandler closes the input source, so resources can be freed

No new tests are included in this change. Additional new tests which exercise
these code paths are being developed as part of DERBY-6810, however, and
will be submitted in the near future.


Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/vti/XmlVTI.java

Modified: db/derby/code/trunk/java/engine/org/apache/derby/vti/XmlVTI.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/vti/XmlVTI.java?rev=1688297&r1=1688296&r2=1688297&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/vti/XmlVTI.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/vti/XmlVTI.java Mon Jun 29 20:11:49 2015
@@ -39,7 +39,9 @@ import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
+import org.xml.sax.ErrorHandler;
 import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
 
 /**
  * <p>
@@ -345,8 +347,7 @@ public  class   XmlVTI  extends StringCo
             else { return false; }
         } catch (Throwable t)
         {
-            t.printStackTrace( System.out );
-            throw new SQLException( t.getMessage() );
+            throw new SQLException( t.getMessage(), t );
         }
     }
 
@@ -372,6 +373,7 @@ public  class   XmlVTI  extends StringCo
         DocumentBuilderFactory  factory = DocumentBuilderFactory.newInstance();
         
         _builder = factory.newDocumentBuilder();
+        _builder.setErrorHandler(new XMLErrorHandler());
 
         Document        doc = _builder.parse( _xmlResource );
         Element             root = doc.getDocumentElement();
@@ -468,5 +470,46 @@ public  class   XmlVTI  extends StringCo
     }
 
 
+    /*
+     ** The XMLErrorHandler class is just a generic implementation
+     ** of the ErrorHandler interface.  It allows us to catch
+     ** and process XML parsing errors in a graceful manner.
+     */
+    private class XMLErrorHandler implements ErrorHandler
+    {
+        private void closeInput()
+        {
+            try
+            {
+                if( _xmlResource != null )
+                    _xmlResource.close();
+            }
+            catch (Exception ex)
+            {
+            }
+        }
+
+        public void error (SAXParseException exception)
+            throws SAXException
+        {
+            closeInput();
+            throw new SAXException (exception);
+        }
+
+        public void fatalError (SAXParseException exception)
+            throws SAXException
+        {
+            closeInput();
+            throw new SAXException (exception);
+        }
+
+        public void warning (SAXParseException exception)
+            throws SAXException
+        {
+            closeInput();
+            throw new SAXException (exception);
+        }
+    }
+
 }
 



Mime
View raw message