1 /******************************************************************************* 2 * Copyright (c) 2009, 2021 Mountainminds GmbH & Co. KG and Contributors 3 * This program and the accompanying materials are made available under 4 * the terms of the Eclipse Public License 2.0 which is available at 5 * http://www.eclipse.org/legal/epl-2.0 6 * 7 * SPDX-License-Identifier: EPL-2.0 8 * 9 * Contributors: 10 * Marc R. Hoffmann - initial API and implementation 11 * 12 *******************************************************************************/ 13 package org.jacoco.ant; 14 15 import java.io.File; 16 import java.io.IOException; 17 import java.io.InputStream; 18 import java.util.HashMap; 19 import java.util.Map; 20 21 import org.apache.tools.ant.types.Resource; 22 import org.jacoco.report.InputStreamSourceFileLocator; 23 24 /** 25 * Source locator based on Ant file resources. 26 */ 27 class AntFilesLocator extends InputStreamSourceFileLocator { 28 29 private final Map<String, Resource> resources; 30 AntFilesLocator(final String encoding, final int tabWidth)31 public AntFilesLocator(final String encoding, final int tabWidth) { 32 super(encoding, tabWidth); 33 this.resources = new HashMap<String, Resource>(); 34 } 35 36 /** 37 * Adds the given file resource as a potential source file. 38 * 39 * @param file 40 * file resource to add 41 */ add(final Resource file)42 void add(final Resource file) { 43 resources.put(file.getName().replace(File.separatorChar, '/'), file); 44 } 45 46 @Override getSourceStream(final String path)47 protected InputStream getSourceStream(final String path) 48 throws IOException { 49 final Resource file = resources.get(path); 50 if (file == null) { 51 return null; 52 } else { 53 return file.getInputStream(); 54 } 55 } 56 57 } 58