carnold 2005/07/14 14:23:55 Modified: src/java/org/apache/log4j/xml Tag: v1_2-branch DOMConfigurator.java tests/src/java/org/apache/log4j/xml Tag: v1_2-branch DOMTestCase.java Added: tests/input/xml Tag: v1_2-branch DOMTest4.xml DOMTest4_A1.xml DOMTest4_A2.xml tests/witness Tag: v1_2-branch dom.A1.4 dom.A2.4 Log: Bug 23705: Parse gets confused with external entities Revision Changes Path No revision No revision 1.49.2.7 +73 -36 logging-log4j/src/java/org/apache/log4j/xml/DOMConfigurator.java Index: DOMConfigurator.java =================================================================== RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/xml/DOMConfigurator.java,v retrieving revision 1.49.2.6 retrieving revision 1.49.2.7 diff -u -r1.49.2.6 -r1.49.2.7 --- DOMConfigurator.java 24 May 2005 05:06:23 -0000 1.49.2.6 +++ DOMConfigurator.java 14 Jul 2005 21:23:48 -0000 1.49.2.7 @@ -29,7 +29,8 @@ import org.apache.log4j.config.PropertySetter; import org.xml.sax.InputSource; -import java.io.FileInputStream; +import org.xml.sax.SAXException; +import java.io.File; import java.io.InputStream; import java.io.Reader; import java.io.IOException; @@ -584,34 +585,37 @@ xdog.setDelay(delay); xdog.start(); } + + private interface ParseAction { + Document parse(final DocumentBuilder parser) throws SAXException, IOException; + } + public - void doConfigure(String filename, LoggerRepository repository) { - FileInputStream fis = null; - try { - fis = new FileInputStream(filename); - doConfigure(fis, repository); - } catch(IOException e) { - LogLog.error("Could not open ["+filename+"].", e); - } finally { - if (fis != null) { - try { - fis.close(); - } catch(java.io.IOException e) { - LogLog.error("Could not close ["+filename+"].", e); - } - } - } + void doConfigure(final String filename, LoggerRepository repository) { + ParseAction action = new ParseAction() { + public Document parse(final DocumentBuilder parser) throws SAXException, IOException { + return parser.parse(new File(filename)); + } + public String toString() { + return "file [" + filename + "]"; + } + }; + doConfigure(action, repository); } public - void doConfigure(URL url, LoggerRepository repository) { - try { - doConfigure(url.openStream(), repository); - } catch(IOException e) { - LogLog.error("Could not open ["+url+"].", e); - } + void doConfigure(final URL url, LoggerRepository repository) { + ParseAction action = new ParseAction() { + public Document parse(final DocumentBuilder parser) throws SAXException, IOException { + return parser.parse(url.toString()); + } + public String toString() { + return "url [" + url.toString() + "]"; + } + }; + doConfigure(action, repository); } /** @@ -620,9 +624,19 @@ */ public - void doConfigure(InputStream inputStream, LoggerRepository repository) + void doConfigure(final InputStream inputStream, LoggerRepository repository) throws FactoryConfigurationError { - doConfigure(new InputSource(inputStream), repository); + ParseAction action = new ParseAction() { + public Document parse(final DocumentBuilder parser) throws SAXException, IOException { + InputSource inputSource = new InputSource(inputStream); + inputSource.setSystemId("dummy://log4j.dtd"); + return parser.parse(inputSource); + } + public String toString() { + return "input stream [" + inputStream.toString() + "]"; + } + }; + doConfigure(action, repository); } /** @@ -631,9 +645,19 @@ */ public - void doConfigure(Reader reader, LoggerRepository repository) + void doConfigure(final Reader reader, LoggerRepository repository) throws FactoryConfigurationError { - doConfigure(new InputSource(reader), repository); + ParseAction action = new ParseAction() { + public Document parse(final DocumentBuilder parser) throws SAXException, IOException { + InputSource inputSource = new InputSource(reader); + inputSource.setSystemId("dummy://log4j.dtd"); + return parser.parse(inputSource); + } + public String toString() { + return "reader [" + reader.toString() + "]"; + } + }; + doConfigure(action, repository); } /** @@ -642,8 +666,25 @@ */ protected - void doConfigure(InputSource inputSource, LoggerRepository repository) + void doConfigure(final InputSource inputSource, LoggerRepository repository) throws FactoryConfigurationError { + if (inputSource.getSystemId() == null) { + inputSource.setSystemId("dummy://log4j.dtd"); + } + ParseAction action = new ParseAction() { + public Document parse(final DocumentBuilder parser) throws SAXException, IOException { + return parser.parse(inputSource); + } + public String toString() { + return "input source [" + inputSource.toString() + "]"; + } + }; + doConfigure(action, repository); + } + + + private final void doConfigure(final ParseAction action, final LoggerRepository repository) + throws FactoryConfigurationError { DocumentBuilderFactory dbf = null; this.repository = repository; try { @@ -665,17 +706,13 @@ DocumentBuilder docBuilder = dbf.newDocumentBuilder(); docBuilder.setErrorHandler(new SAXErrorHandler()); - docBuilder.setEntityResolver(new Log4jEntityResolver()); - // we change the system ID to a valid URI so that Crimson won't - // complain. Indeed, "log4j.dtd" alone is not a valid URI which - // causes Crimson to barf. The Log4jEntityResolver only cares - // about the "log4j.dtd" ending. - inputSource.setSystemId("dummy://log4j.dtd"); - Document doc = docBuilder.parse(inputSource); + docBuilder.setEntityResolver(new Log4jEntityResolver()); + + Document doc = action.parse(docBuilder); parse(doc.getDocumentElement()); } catch (Exception e) { // I know this is miserable... - LogLog.error("Could not parse input source ["+inputSource+"].", e); + LogLog.error("Could not parse "+ action.toString() + ".", e); } } No revision No revision 1.1.2.1 +29 -0 logging-log4j/tests/input/xml/Attic/DOMTest4.xml 1.1.2.1 +1 -0 logging-log4j/tests/input/xml/Attic/DOMTest4_A1.xml 1.1.2.1 +1 -0 logging-log4j/tests/input/xml/Attic/DOMTest4_A2.xml No revision No revision 1.5.2.3 +31 -7 logging-log4j/tests/src/java/org/apache/log4j/xml/Attic/DOMTestCase.java Index: DOMTestCase.java =================================================================== RCS file: /home/cvs/logging-log4j/tests/src/java/org/apache/log4j/xml/Attic/DOMTestCase.java,v retrieving revision 1.5.2.2 retrieving revision 1.5.2.3 diff -u -r1.5.2.2 -r1.5.2.3 --- DOMTestCase.java 27 Jun 2005 19:40:18 -0000 1.5.2.2 +++ DOMTestCase.java 14 Jul 2005 21:23:54 -0000 1.5.2.3 @@ -96,6 +96,37 @@ assertTrue(Compare.compare(FILTERED_A1, "witness/dom.A1.1")); assertTrue(Compare.compare(FILTERED_A2, "witness/dom.A2.1")); } + + /** + * Tests processing of external entities in XML file. + */ + public void test4() throws Exception { + DOMConfigurator.configure("input/xml/DOMTest4.xml"); + common(); + + ControlFilter cf1 = new ControlFilter(new String[]{TEST1_1A_PAT, TEST1_1B_PAT, + EXCEPTION1, EXCEPTION2, EXCEPTION3}); + + ControlFilter cf2 = new ControlFilter(new String[]{TEST1_2_PAT, + EXCEPTION1, EXCEPTION2, EXCEPTION3}); + + Transformer.transform( + TEMP_A1 + ".4", FILTERED_A1 + ".4", + new Filter[] { + cf1, new LineNumberFilter(), new SunReflectFilter(), + new JunitTestRunnerFilter() + }); + + Transformer.transform( + TEMP_A2 + ".4", FILTERED_A2 + ".4", + new Filter[] { + cf2, new LineNumberFilter(), new ISO8601Filter(), + new SunReflectFilter(), new JunitTestRunnerFilter() + }); + + assertTrue(Compare.compare(FILTERED_A1 + ".4", "witness/dom.A1.4")); + assertTrue(Compare.compare(FILTERED_A2 + ".4", "witness/dom.A2.4")); + } void common() { int i = -1; @@ -123,11 +154,4 @@ root.error("Message " + i, e); } - - public static Test suite() { - TestSuite suite = new TestSuite(); - suite.addTest(new DOMTestCase("test1")); - return suite; - } - } No revision No revision 1.1.2.1 +93 -0 logging-log4j/tests/witness/Attic/dom.A1.4 1.1.2.1 +62 -0 logging-log4j/tests/witness/Attic/dom.A2.4
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]