You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2018/07/31 09:44:00 UTC

[camel] branch camel-12691 created (now d9cff3d)

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a change to branch camel-12691
in repository https://gitbox.apache.org/repos/asf/camel.git.


      at d9cff3d  CAMEL-12691: XmlConverter should use logger api when reporting parsing errors.

This branch includes the following new commits:

     new d9cff3d  CAMEL-12691: XmlConverter should use logger api when reporting parsing errors.

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[camel] 01/01: CAMEL-12691: XmlConverter should use logger api when reporting parsing errors.

Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch camel-12691
in repository https://gitbox.apache.org/repos/asf/camel.git

commit d9cff3daf6119e27ee3f5feb3c65433b5370946f
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue Jul 31 11:43:47 2018 +0200

    CAMEL-12691: XmlConverter should use logger api when reporting parsing errors.
---
 .../apache/camel/converter/jaxp/XmlConverter.java  | 46 ++++++++++++++++------
 1 file changed, 34 insertions(+), 12 deletions(-)

diff --git a/camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java b/camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java
index fa19414..6d0e20a 100644
--- a/camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java
+++ b/camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java
@@ -59,8 +59,10 @@ import org.w3c.dom.Element;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 
+import org.xml.sax.ErrorHandler;
 import org.xml.sax.InputSource;
 import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
 import org.xml.sax.XMLReader;
 
 import org.apache.camel.BytesSource;
@@ -90,6 +92,7 @@ public class XmlConverter {
     private static final String JDK_FALLBACK_TRANSFORMER_FACTORY = "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl";
     private static final String XALAN_TRANSFORMER_FACTORY = "org.apache.xalan.processor.TransformerFactoryImpl";
     private static final Logger LOG = LoggerFactory.getLogger(XmlConverter.class);
+    private static final ErrorHandler domLoggingErrorHandler = new DomLoggingErrorHandler();
 
     private volatile DocumentBuilderFactory documentBuilderFactory;
     private volatile TransformerFactory transformerFactory;
@@ -175,8 +178,6 @@ public class XmlConverter {
 
     /**
      * Converts the given Node to a Source
-     * @throws TransformerException
-     * @throws ParserConfigurationException
      * @deprecated  use toDOMSource instead
      */
     @Deprecated
@@ -186,8 +187,6 @@ public class XmlConverter {
 
     /**
      * Converts the given Node to a Source
-     * @throws TransformerException
-     * @throws ParserConfigurationException
      */
     @Converter
     public DOMSource toDOMSource(Node node) throws ParserConfigurationException, TransformerException {
@@ -662,7 +661,7 @@ public class XmlConverter {
     public DOMSource toDOMSource(InputStream is, Exchange exchange) throws ParserConfigurationException, IOException, SAXException {
         InputSource source = new InputSource(is);
         String systemId = source.getSystemId();
-        DocumentBuilder builder = getDocumentBuilderFactory(exchange).newDocumentBuilder();
+        DocumentBuilder builder = createDocumentBuilder(getDocumentBuilderFactory(exchange));
         Document document = builder.parse(source);
         return new DOMSource(document, systemId);
     }
@@ -694,7 +693,7 @@ public class XmlConverter {
         Document document;
         String systemId = source.getSystemId();
 
-        DocumentBuilder builder = getDocumentBuilderFactory(exchange).newDocumentBuilder();
+        DocumentBuilder builder = createDocumentBuilder(getDocumentBuilderFactory(exchange));
         Reader reader = source.getReader();
         if (reader != null) {
             document = builder.parse(new InputSource(reader));
@@ -844,7 +843,7 @@ public class XmlConverter {
      */
     @Converter
     public Document toDOMDocument(byte[] data, Exchange exchange) throws IOException, SAXException, ParserConfigurationException {
-        DocumentBuilder documentBuilder = getDocumentBuilderFactory(exchange).newDocumentBuilder();
+        DocumentBuilder documentBuilder = createDocumentBuilder(getDocumentBuilderFactory(exchange));
         return documentBuilder.parse(new ByteArrayInputStream(data));
     }
 
@@ -869,7 +868,7 @@ public class XmlConverter {
      */
     @Converter
     public Document toDOMDocument(InputStream in, Exchange exchange) throws IOException, SAXException, ParserConfigurationException {
-        DocumentBuilder documentBuilder = getDocumentBuilderFactory(exchange).newDocumentBuilder();
+        DocumentBuilder documentBuilder = createDocumentBuilder(getDocumentBuilderFactory(exchange));
         return documentBuilder.parse(in);
     }
 
@@ -918,7 +917,7 @@ public class XmlConverter {
      */
     @Converter
     public Document toDOMDocument(InputSource in, Exchange exchange) throws IOException, SAXException, ParserConfigurationException {
-        DocumentBuilder documentBuilder = getDocumentBuilderFactory(exchange).newDocumentBuilder();
+        DocumentBuilder documentBuilder = createDocumentBuilder(getDocumentBuilderFactory(exchange));
         return documentBuilder.parse(in);
     }
 
@@ -967,7 +966,7 @@ public class XmlConverter {
      */
     @Converter
     public Document toDOMDocument(File file, Exchange exchange) throws IOException, SAXException, ParserConfigurationException {
-        DocumentBuilder documentBuilder = getDocumentBuilderFactory(exchange).newDocumentBuilder();
+        DocumentBuilder documentBuilder = createDocumentBuilder(getDocumentBuilderFactory(exchange));
         return documentBuilder.parse(file);
     }
 
@@ -1155,8 +1154,13 @@ public class XmlConverter {
     }
 
     public DocumentBuilder createDocumentBuilder() throws ParserConfigurationException {
-        DocumentBuilderFactory factory = getDocumentBuilderFactory();
-        return factory.newDocumentBuilder();
+        return createDocumentBuilder(getDocumentBuilderFactory());
+    }
+
+    public DocumentBuilder createDocumentBuilder(DocumentBuilderFactory factory) throws ParserConfigurationException {
+        DocumentBuilder builder = factory.newDocumentBuilder();
+        builder.setErrorHandler(domLoggingErrorHandler);
+        return builder;
     }
 
     public Document createDocument() throws ParserConfigurationException {
@@ -1263,4 +1267,22 @@ public class XmlConverter {
         sfactory.setNamespaceAware(true);
         return sfactory;
     }
+
+    private static class DomLoggingErrorHandler implements ErrorHandler {
+
+        @Override
+        public void warning(SAXParseException exception) throws SAXException {
+            LOG.warn(exception.getMessage(), exception);
+        }
+
+        @Override
+        public void error(SAXParseException exception) throws SAXException {
+            LOG.error(exception.getMessage(), exception);
+        }
+
+        @Override
+        public void fatalError(SAXParseException exception) throws SAXException {
+            LOG.error(exception.getMessage(), exception);
+        }
+    }
 }