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