Github user FSchumacher commented on a diff in the pull request: https://github.com/apache/jmeter/pull/432#discussion_r235125815 --- Diff: src/core/org/apache/jmeter/gui/action/template/TemplateManager.java --- @@ -149,19 +109,107 @@ public TemplateManager reset() { } else { if (log.isWarnEnabled()) { log.warn("Ignoring template file:'{}' as it does not exist or is not readable", - f.getAbsolutePath()); + file.getAbsolutePath()); } } } catch(Exception ex) { if (log.isWarnEnabled()) { - log.warn("Ignoring template file:'{}', an error occurred parsing the file", f.getAbsolutePath(), + log.warn("Ignoring template file:'{}', an error occurred parsing the file", file.getAbsolutePath(), ex); } } } } return temps; } + + public final class LoggingErrorHandler implements ErrorHandler { + private Logger logger; + + public LoggingErrorHandler(Logger logger) { + this.logger = logger; + } + @Override + public void error(SAXParseException ex) throws SAXException { + throw ex; + } + + @Override + public void fatalError(SAXParseException ex) throws SAXException { + throw ex; + } + + @Override + public void warning(SAXParseException ex) throws SAXException { + logger.warn("Warning", ex); + } + } + + public static class DefaultEntityResolver implements EntityResolver { + public DefaultEntityResolver() { + super(); + } + + public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { + if(systemId.endsWith("templates.dtd")) { + return new InputSource(TemplateManager.class.getResourceAsStream("/org/apache/jmeter/gui/action/template/templates.dtd")); + } else { + return null; + } + } + } + + public Map<String, Template> parseTemplateFile(File file) throws IOException, SAXException, ParserConfigurationException{ + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + dbf.setValidating(true); + dbf.setNamespaceAware(true); + dbf.setFeature("http://xml.org/sax/features/external-general-entities", false); + dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false); + DocumentBuilder bd = dbf.newDocumentBuilder(); + bd.setEntityResolver(new DefaultEntityResolver()); + LoggingErrorHandler errorHandler = new LoggingErrorHandler(log); + bd.setErrorHandler(errorHandler); + Document document = bd.parse(file.getAbsolutePath()); + document.getDocumentElement().normalize(); + Map<String, Template> templates = new TreeMap<>(); + NodeList templateNodes = document.getElementsByTagName("template"); + for (int i = 0; i < templateNodes.getLength(); i++) { + Node node = templateNodes.item(i); + parseTemplateNode(templates, node); + } + return templates; + } + + /** + * @param templates --- End diff -- either fill in the javadoc, or leave it out completely.
---