Home | History | Annotate | Download | only in xml
      1 /*******************************************************************************
      2  * Copyright (c) 2009, 2018 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 java.io.IOException;
     15 import java.io.InputStream;
     16 
     17 import org.xml.sax.EntityResolver;
     18 import org.xml.sax.InputSource;
     19 import org.xml.sax.SAXException;
     20 
     21 /**
     22  * Loader for local DTD definitions to avoid network access.
     23  */
     24 class LocalEntityResolver implements EntityResolver {
     25 
     26 	private final Class<?> resourceDelegate;
     27 
     28 	public LocalEntityResolver(Class<?> resourceDelegate) {
     29 		this.resourceDelegate = resourceDelegate;
     30 	}
     31 
     32 	public InputSource resolveEntity(final String publicId, String systemId)
     33 			throws SAXException, IOException {
     34 		final int sep = systemId.lastIndexOf('/');
     35 		if (sep != -1) {
     36 			systemId = systemId.substring(sep + 1);
     37 		}
     38 		final InputStream in = resourceDelegate.getResourceAsStream(systemId);
     39 		if (in == null) {
     40 			throw new IOException("No local copy for " + systemId);
     41 		}
     42 		return new InputSource(in);
     43 	}
     44 
     45 }
     46