提交 bdf13dce 编写于 作者: K Kohsuke Kawaguchi

Added a hook to customize the XML parser behaviour.

上级 18496c54
......@@ -55,7 +55,9 @@ Upcoming changes</a>
<!-- Record your changes in the trunk here. -->
<div id="trunk" style="display:none"><!--=TRUNK-BEGIN=-->
<ul class=image>
<li class=>
<li class=rfe>
Added a mechanism to control the XML parser behaviour
(<a href="https://github.com/jenkinsci/jenkins/pull/67">pull request #67</a>)
</ul>
</div><!--=TRUNK-END=-->
......
......@@ -25,6 +25,7 @@ package hudson.tasks.junit;
import hudson.tasks.test.TestObject;
import hudson.util.IOException2;
import hudson.util.io.ParserConfigurator;
import org.apache.commons.io.FileUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
......@@ -84,6 +85,18 @@ public final class SuiteResult implements Serializable {
this.file = null;
}
/**
* Passed to {@link ParserConfigurator}.
* @since 1.416
*/
public static class SuiteResultParserConfigurationContext {
public final File xmlReport;
SuiteResultParserConfigurationContext(File xmlReport) {
this.xmlReport = xmlReport;
}
}
/**
* Parses the JUnit XML file into {@link SuiteResult}s.
* This method returns a collection, as a single XML may have multiple &lt;testsuite>
......@@ -94,10 +107,8 @@ public final class SuiteResult implements Serializable {
// parse into DOM
SAXReader saxReader = new SAXReader();
// install EntityResolver for resolving DTDs, which are in files created by TestNG.
// (see https://hudson.dev.java.net/servlets/ReadMsg?listName=users&msgNo=5530)
XMLEntityResolver resolver = new XMLEntityResolver();
saxReader.setEntityResolver(resolver);
ParserConfigurator.applyConfiguration(saxReader,new SuiteResultParserConfigurationContext(xmlReport));
Document result = saxReader.read(xmlReport);
Element root = result.getRootElement();
......
......@@ -23,6 +23,10 @@
*/
package hudson.tasks.junit;
import hudson.Extension;
import hudson.tasks.junit.SuiteResult.SuiteResultParserConfigurationContext;
import hudson.util.io.ParserConfigurator;
import org.dom4j.io.SAXReader;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
......@@ -43,7 +47,8 @@ import java.util.logging.Logger;
*
* @author Mikael Carneholm
*/
class XMLEntityResolver implements EntityResolver {
@Extension
public class XMLEntityResolver extends ParserConfigurator implements EntityResolver {
private static final String TESTNG_NAMESPACE = "http://testng.org/";
......@@ -67,5 +72,16 @@ class XMLEntityResolver implements EntityResolver {
return null;
}
/**
* Install EntityResolver for resolving DTDs, which are in files created by TestNG.
* (see https://hudson.dev.java.net/servlets/ReadMsg?listName=users&msgNo=5530)
*/
@Override
public void configure(SAXReader reader, Object context) {
if (context instanceof SuiteResultParserConfigurationContext) {
reader.setEntityResolver(this);
}
}
private static final Logger LOGGER = Logger.getLogger(XMLEntityResolver.class.getName());
}
\ No newline at end of file
/*
* The MIT License
*
* Copyright (c) 2011, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.util.io;
import hudson.ExtensionList;
import hudson.ExtensionPoint;
import hudson.model.Hudson;
import org.dom4j.io.SAXReader;
/**
* Configures XML parsers to be used in various context of Jenkins.
*
* @author Kohsuke Kawaguchi
* @since 1.416
*/
public abstract class ParserConfigurator implements ExtensionPoint {
/**
* Configures the given {@link SAXReader}
*
* @param context
* Object that represents the context in which the parser is used.
* It is up to the caller to decide what to pass in here.
*/
public void configure(SAXReader reader, Object context) {}
/**
* Returns all the registered {@link ParserConfigurator}s.
*/
public static ExtensionList<ParserConfigurator> all() {
return Hudson.getInstance().getExtensionList(ParserConfigurator.class);
}
public static void applyConfiguration(SAXReader reader, Object context) {
for (ParserConfigurator pc : all())
pc.configure(reader,context);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册