/*
 *  eXist Open Source Native XML Database
 *  Copyright (C) 2009 The eXist Project
 *  http://exist-db.org
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *  $Id$
 */
package org.exist.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javax.xml.parsers.SAXParserFactory;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * Helper class for creating an instance of javax.xml.parsers.SAXParserFactory
 *
 * @author dizzzz@exist-db.org
 */
public class ExistSAXParserFactory {

    private final static Logger LOG = LogManager.getLogger(ExistSAXParserFactory.class);

    public final static String ORG_EXIST_SAXPARSERFACTORY = "org.exist.SAXParserFactory";

    /**
     * Get SAXParserFactory instance specified by factory class name.
     *
     * @param className Full class name of factory
     * @return A Sax parser factory or NULL when not available.
     */
    public static SAXParserFactory getSAXParserFactory(final String className) {

        Class<?> clazz = null;
        try {
            clazz = Class.forName(className);

        } catch (final ClassNotFoundException ex) {
            // quick escape
            LOG.debug(className + ": " + ex.getMessage(), ex);
            return null;
        }

        // Get specific method
        Method method = null;
        try {
            method = clazz.getMethod("newInstance", (Class[]) null);
        } catch (final SecurityException | NoSuchMethodException ex) {
            // quick escape
            LOG.debug("Method " + className + ".newInstance not found.", ex);
            return null;
        }

        // Invoke method
        Object result = null;
        try {
            result = method.invoke(null, (Object[]) null);

        } catch (final IllegalAccessException | InvocationTargetException ex) {
            // quick escape
            LOG.debug("Could not invoke method " + className + ".newInstance.", ex);
            return null;
        }

        if (!(result instanceof SAXParserFactory)) {
            LOG.debug("Could not create instance of SAXParserFactory: " + result.toString());
            return null;
        }

        return (SAXParserFactory) result;
    }

    /**
     * Get instance of a SAXParserFactory. Return factory specified by
     * system property org.exist.SAXParserFactory (if available) otherwise
     * return system default.
     *
     * @return A sax parser factory.
     */
    public static SAXParserFactory getSAXParserFactory() {

        SAXParserFactory factory = null;

        // Get SAXParser configuratin from system
        final String config = System.getProperty(ORG_EXIST_SAXPARSERFACTORY);

        // Get SAXparser factory specified by system property
        if (config != null) {
            factory = getSAXParserFactory(config);
        }

        // If no factory could be retrieved, create system default property.
        if (factory == null) {
            factory = SAXParserFactory.newInstance();
            LOG.info(String.format("Using default SAXParserFactory '%s'", factory.getClass().getCanonicalName()));
        }

        return factory;
    }
}

@@ -1901,32 +1901,76 @@
 // quick escape%0A
+            if (LOG.isDebugEnabled()) %7B%0A    
             LOG.
@@ -2012,24 +2012,38 @@
 age(), ex);%0A
+            %7D%0A
             
@@ -2301,32 +2301,76 @@
 // quick escape%0A
+            if (LOG.isDebugEnabled()) %7B%0A    
             LOG.
@@ -2423,32 +2423,46 @@
 t found.%22, ex);%0A
+            %7D%0A
             retu
@@ -2710,32 +2710,76 @@
 // quick escape%0A
+            if (LOG.isDebugEnabled()) %7B%0A    
             LOG.
@@ -2839,32 +2839,46 @@
 nstance.%22, ex);%0A
+            %7D%0A
             retu
@@ -2946,24 +2946,68 @@
 Factory)) %7B%0A
+            if (LOG.isDebugEnabled()) %7B%0A    
             
@@ -3084,24 +3084,38 @@
 oString());%0A
+            %7D%0A
             
@@ -3986,16 +3986,61 @@
     
+if (
 LOG.i
-nfo
+sDebugEnabled()) %7B%0A                LOG.debug
 (Str
@@ -4126,24 +4126,38 @@
 alName()));%0A
+            %7D%0A
         %7D%0A%0A 

Fix a regression with verbose logging in ExistSAXParserFactory introduced by 1c3f0ae