logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject svn commit: r1514013 - in /logging/log4j/log4j2/trunk/core/src: main/java/org/apache/logging/log4j/core/config/XMLConfiguration.java test/java/org/apache/logging/log4j/core/config/XMLConfigurationTest.java
Date Wed, 14 Aug 2013 19:28:18 GMT
Author: ggregory
Date: Wed Aug 14 19:28:17 2013
New Revision: 1514013

URL: http://svn.apache.org/r1514013
Log:
[LOG4J2-341] Enable XInclude for XML configurations. Start work on getting XML configs using
XInclude to be validated. Refactor the creation of the document builder in a method; refactor
enablement of XInclude in a method. Tests must process the XML document to allow XInclude
to kick in before validation is performed.

Modified:
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/XMLConfiguration.java
    logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/config/XMLConfigurationTest.java

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/XMLConfiguration.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/XMLConfiguration.java?rev=1514013&r1=1514012&r2=1514013&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/XMLConfiguration.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/XMLConfiguration.java
Wed Aug 14 19:28:17 2013
@@ -67,6 +67,55 @@ import org.xml.sax.SAXException;
  */
 public class XMLConfiguration extends BaseConfiguration implements Reconfigurable {
 
+    private static final String XINCLUDE_FIXUP_LANGUAGE = "http://apache.org/xml/features/xinclude/fixup-language";
+
+    private static final String XINCLUDE_FIXUP_BASE_URIS = "http://apache.org/xml/features/xinclude/fixup-base-uris";
+
+    /**
+     * Creates a new DocumentBuilder suitable for parsing a configuration file.
+     * 
+     * @return a new DocumentBuilder
+     * @throws ParserConfigurationException
+     */
+    static DocumentBuilder newDocumentBuilder() throws ParserConfigurationException {
+        final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+        factory.setNamespaceAware(true);
+        enableXInclude(factory);
+        final DocumentBuilder builder = factory.newDocumentBuilder();
+        return builder;
+    }
+
+    /**
+     * Enables XInclude for the given DocumentBuilderFactory
+     * 
+     * @param factory
+     *            a DocumentBuilderFactory
+     * @throws ParserConfigurationException
+     */
+    private static void enableXInclude(final DocumentBuilderFactory factory) {
+        try {
+            // Alternative: We set if a system property on the command line is set, for example:
+            // -DLog4j.XInclude=true
+            factory.setXIncludeAware(true);
+        } catch (UnsupportedOperationException e) {
+            LOGGER.warn("The DocumentBuilderFactory does not support XInclude: " + factory,
e);
+        }
+        try {
+            // Alternative: We could specify all features and values with system properties
like:
+            // -DLog4j.DocumentBuilderFactory.Feature="http://apache.org/xml/features/xinclude/fixup-base-uris
true"
+            factory.setFeature(XINCLUDE_FIXUP_BASE_URIS, true);
+        } catch (ParserConfigurationException e) {
+            LOGGER.warn("The DocumentBuilderFactory [" + factory + "] does not support the
feature ["
+                    + XINCLUDE_FIXUP_BASE_URIS + "]", e);
+        }
+        try {
+            factory.setFeature(XINCLUDE_FIXUP_LANGUAGE, true);
+        } catch (ParserConfigurationException e) {
+            LOGGER.warn("The DocumentBuilderFactory [" + factory + "] does not support the
feature ["
+                    + XINCLUDE_FIXUP_LANGUAGE + "]", e);
+        }
+    }
+
     private static final String[] VERBOSE_CLASSES = new String[] {ResolverUtil.class.getName()};
 
     private static final String LOG4J_XSD = "Log4j-config.xsd";
@@ -100,15 +149,7 @@ public class XMLConfiguration extends Ba
             buffer = toByteArray(configStream);
             configStream.close();
             final InputSource source = new InputSource(new ByteArrayInputStream(buffer));
-            final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
-            factory.setNamespaceAware(true);
-            try {
-                factory.setXIncludeAware(true);
-            } catch (UnsupportedOperationException e) {
-                LOGGER.warn("This DocumentBuilderFactory does not support XInclude: " + factory,
e);
-            }
-            final DocumentBuilder builder = factory.newDocumentBuilder();
-            final Document document = builder.parse(source);
+            final Document document = newDocumentBuilder().parse(source);
             rootElement = document.getDocumentElement();
             final Map<String, String> attrs = processAttributes(rootNode, rootElement);
             Level status = getDefaultStatus();

Modified: logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/config/XMLConfigurationTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/config/XMLConfigurationTest.java?rev=1514013&r1=1514012&r2=1514013&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/config/XMLConfigurationTest.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/config/XMLConfigurationTest.java
Wed Aug 14 19:28:17 2013
@@ -32,8 +32,9 @@ import java.util.Iterator;
 import java.util.Map;
 
 import javax.xml.XMLConstants;
+import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.transform.Source;
-import javax.xml.transform.stream.StreamSource;
+import javax.xml.transform.dom.DOMSource;
 import javax.xml.validation.Schema;
 import javax.xml.validation.SchemaFactory;
 import javax.xml.validation.Validator;
@@ -48,12 +49,14 @@ import org.apache.logging.log4j.core.fil
 import org.apache.logging.log4j.status.StatusLogger;
 import org.junit.After;
 import org.junit.Assert;
+import org.junit.Assume;
 import org.junit.Before;
 import org.junit.Ignore;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.junit.runners.Parameterized;
 import org.junit.runners.Parameterized.Parameters;
+import org.w3c.dom.Document;
 import org.xml.sax.SAXException;
 
 /**
@@ -64,17 +67,19 @@ public class XMLConfigurationTest {
 
     @Parameters
     public static Collection<Object[]> data() {
-        return Arrays.asList(new Object[][] { { "log4j-test1.xml", "target/test.log" },
-                { "log4j-xinclude.xml", "target/test.log" } });
+        return Arrays.asList(new Object[][] { { "log4j-test1.xml", "target/test.log", false
},
+                { "log4j-xinclude.xml", "target/test.log", true } });
     }
 
     private final String configFile;
     private final String logFile;
+    private final boolean xinclude;
 
-    public XMLConfigurationTest(String configFile, String logFile) {
+    public XMLConfigurationTest(String configFile, String logFile, boolean validate) {
         super();
         this.configFile = configFile;
         this.logFile = logFile;
+        this.xinclude = validate;
     }
 
     @Test
@@ -152,11 +157,14 @@ public class XMLConfigurationTest {
 
     @Test
     @Ignore
-    public void testValidation() throws SAXException, IOException {
+    public void testValidation() throws SAXException, IOException, ParserConfigurationException
{
+        // For now, XInclude and validation do not work together.
+        Assume.assumeFalse(this.xinclude);
         URL schemaFile = ClassLoader.getSystemResource("Log4j-config.xsd");
         URL xmlFile = ClassLoader.getSystemResource(configFile);
         Assert.assertNotNull(schemaFile);
-        Source xmlSource = new StreamSource(xmlFile.toString());
+        Document doc = XMLConfiguration.newDocumentBuilder().parse(xmlFile.toString());
+        Source xmlSource = new DOMSource(doc);
         SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
         Schema schema = schemaFactory.newSchema(schemaFile);
         Validator validator = schema.newValidator();



Mime
View raw message