[camel] branch camel-2.21.x updated: CAMEL-12691: XmlConverter should use logger api when reporting parsin… (#2445)

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[camel] branch camel-2.21.x updated: CAMEL-12691: XmlConverter should use logger api when reporting parsin… (#2445)

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

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


The following commit(s) were added to refs/heads/camel-2.21.x by this push:
     new 5737328  CAMEL-12691: XmlConverter should use logger api when reporting parsin… (#2445)
5737328 is described below

commit 57373287a0f4a5c3656773ca720358582ef1e8dd
Author: Claus Ibsen <[hidden email]>
AuthorDate: Tue Jul 31 15:52:32 2018 +0200

    CAMEL-12691: XmlConverter should use logger api when reporting parsin… (#2445)
   
    * 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 88440b5..bfc5945 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 DOCUMENT_BUILDER_LOGGING_ERROR_HANDLER = new DocumentBuilderLoggingErrorHandler();
 
     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(DOCUMENT_BUILDER_LOGGING_ERROR_HANDLER);
+        return builder;
     }
 
     public Document createDocument() throws ParserConfigurationException {
@@ -1263,4 +1267,22 @@ public class XmlConverter {
         sfactory.setNamespaceAware(true);
         return sfactory;
     }
+
+    private static class DocumentBuilderLoggingErrorHandler 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);
+        }
+    }
 }