1 /******************************************************************************* 2 * Copyright (c) 2009, 2015 Mountainminds GmbH & Co. KG and Contributors 3 * All rights reserved. This program and the accompanying materials 4 * are made available under the terms of the Eclipse Public License v1.0 5 * which accompanies this distribution, and is available at 6 * http://www.eclipse.org/legal/epl-v10.html 7 * 8 * Contributors: 9 * Marc R. Hoffmann - initial API and implementation 10 * 11 *******************************************************************************/ 12 package org.jacoco.report.internal.xml; 13 14 import static org.junit.Assert.fail; 15 16 import java.io.ByteArrayInputStream; 17 import java.io.IOException; 18 import java.io.StringReader; 19 20 import javax.xml.parsers.DocumentBuilder; 21 import javax.xml.parsers.DocumentBuilderFactory; 22 import javax.xml.parsers.ParserConfigurationException; 23 import javax.xml.xpath.XPath; 24 import javax.xml.xpath.XPathConstants; 25 import javax.xml.xpath.XPathExpressionException; 26 import javax.xml.xpath.XPathFactory; 27 28 import org.w3c.dom.Document; 29 import org.xml.sax.ErrorHandler; 30 import org.xml.sax.InputSource; 31 import org.xml.sax.SAXException; 32 import org.xml.sax.SAXParseException; 33 34 /** 35 * Test utility to parse, validate and query XML documents. 36 */ 37 public class XMLSupport { 38 39 private final DocumentBuilder builder; 40 41 private XPath xpath; 42 XMLSupport(Class<?> resourceDelegate)43 public XMLSupport(Class<?> resourceDelegate) 44 throws ParserConfigurationException { 45 final DocumentBuilderFactory builderFactory = DocumentBuilderFactory 46 .newInstance(); 47 builderFactory.setValidating(true); 48 builder = builderFactory.newDocumentBuilder(); 49 builder.setEntityResolver(new LocalEntityResolver(resourceDelegate)); 50 builder.setErrorHandler(new ErrorHandler() { 51 public void error(SAXParseException exception) throws SAXException { 52 fail(exception.getMessage()); 53 } 54 55 public void fatalError(SAXParseException exception) 56 throws SAXException { 57 fail(exception.getMessage()); 58 } 59 60 public void warning(SAXParseException exception) 61 throws SAXException { 62 fail(exception.getMessage()); 63 } 64 }); 65 } 66 getXPath()67 private XPath getXPath() { 68 if (xpath == null) { 69 xpath = XPathFactory.newInstance().newXPath(); 70 } 71 return xpath; 72 } 73 parse(String document)74 public Document parse(String document) throws SAXException, IOException, 75 ParserConfigurationException { 76 return builder.parse(new InputSource(new StringReader(document))); 77 } 78 parse(byte[] document)79 public Document parse(byte[] document) throws SAXException, IOException, 80 ParserConfigurationException { 81 return builder 82 .parse(new InputSource(new ByteArrayInputStream(document))); 83 } 84 findStr(final Document doc, final String query)85 public String findStr(final Document doc, final String query) 86 throws XPathExpressionException { 87 return (String) getXPath().evaluate(query, doc, XPathConstants.STRING); 88 } 89 90 } 91