DirectorySourceFileLocator.java

    1/*******************************************************************************
    2 * Copyright (c) 2009, 2010 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.InputStreamReader;
   18import java.io.Reader;
   19
   20/**
   21 * Locator for source files that picks source files from a given directory in
   22 * the file system.
   23 * 
   24 * @author Marc R. Hoffmann
   25 * @version 0.4.1.20101007204400
   26 */
   27public class DirectorySourceFileLocator implements ISourceFileLocator {
   28
   29    private final File directory;
   30
   31    private final String encoding;
   32
   33    /**
   34     * Creates a new locator that searches for source files in the given
   35     * directory.
   36     * 
   37     * @param directory
   38     *            directory to search for source file
   39     * @param encoding
   40     *            encoding of the source files
   41     */
   42    public DirectorySourceFileLocator(final File directory, final String encoding) {
   43        this.directory = directory;
   44        this.encoding = encoding;
   45    }
   46
   47    public Reader getSourceFile(final String packageName, final String fileName)
   48            throws IOException {
   49        final File dir = new File(directory, packageName);
   50        final File file = new File(dir, fileName);
   51        if (file.exists()) {
   52            return new InputStreamReader(new FileInputStream(file), encoding);
   53        }
   54        return null;
   55    }
   56
   57}