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 *******************************************************************************/ 12package org.jacoco.report; 13 14import java.io.File; 15import java.io.FileInputStream; 16import java.io.IOException; 17import java.io.InputStream; 18 19/** 20 * Locator for source files that picks source files from a given directory in 21 * the file system. 22 */ 23public class DirectorySourceFileLocator extends InputStreamSourceFileLocator { 24 25 private final File directory; 26 27 /** 28 * Creates a new locator that searches for source files in the given 29 * directory. 30 * 31 * @param directory 32 * directory to search for source file 33 * @param encoding 34 * encoding of the source files, <code>null</code> for platform 35 * default encoding 36 * @param tabWidth 37 * tab width in source files as number of blanks 38 * 39 */ 40 public DirectorySourceFileLocator(final File directory, 41 final String encoding, final int tabWidth) { 42 super(encoding, tabWidth); 43 this.directory = directory; 44 } 45 46 @Override 47 protected InputStream getSourceStream(final String path) throws IOException { 48 final File file = new File(directory, path); 49 if (file.exists()) { 50 return new FileInputStream(file); 51 } else { 52 return null; 53 } 54 } 55 56} 57