File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\tasks\junit\XMLEntityResolver.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Jorg Heymans
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.tasks.junit;
25
26 import hudson.Extension;
27 import hudson.tasks.junit.SuiteResult.SuiteResultParserConfigurationContext;
28 import hudson.util.io.ParserConfigurator;
29 import org.dom4j.io.SAXReader;
30 import org.xml.sax.EntityResolver;
31 import org.xml.sax.InputSource;
32 import org.xml.sax.SAXException;
33
34 import java.io.IOException;
35 import java.net.URL;
36 import java.util.logging.Level;
37 import java.util.logging.Logger;
38
39 /**
40 * As the name suggest: a resolver for XML entities.
41 *
42 * <p>
43 * Basically, it provides the possibility to intercept online DTD lookups
44 * and instead do offline lookup by redirecting to a local directory where
45 * .dtd's are stored
46 *
47 * (useful when parsing testng-results.xml - which points to testng.org)
48 *
49 * @author Mikael Carneholm
50 */
51 @Extension
52 public class XMLEntityResolver extends ParserConfigurator implements EntityResolver {
53
54 private static final String TESTNG_NAMESPACE = "http://testng.org/";
55
56 /**
57 * Intercepts the lookup of publicId, systemId
58 */
59 public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
60 if (systemId != null) {
61 if (LOGGER.isLoggable(Level.FINE)) {
62 LOGGER.fine("Will try to resolve systemId [" + systemId + "]");
63 }
64 // TestNG system-ids
65 if (systemId.startsWith(TESTNG_NAMESPACE)) {
66 LOGGER.fine("It's a TestNG document, will try to lookup DTD in classpath");
67 String dtdFileName = systemId.substring(TESTNG_NAMESPACE.length());
68
69 URL url = getClass().getClassLoader().getResource(dtdFileName);
70 if (url != null)
71 return new InputSource(url.toString());
72 }
73 }
74 // Default fallback
75 return null;
76 }
77
78 /**
79 * Install EntityResolver for resolving DTDs, which are in files created by TestNG.
80 */
81 @Override
82 public void configure(SAXReader reader, Object context) {
83 if (context instanceof SuiteResultParserConfigurationContext) {
84 reader.setEntityResolver(this);
85 }
86 }
87
88 private static final Logger LOGGER = Logger.getLogger(XMLEntityResolver.class.getName());
89 }
90