Home | History | Annotate | Download | only in report
      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;
     13 
     14 import java.io.IOException;
     15 import java.io.InputStream;
     16 import java.io.InputStreamReader;
     17 import java.io.Reader;
     18 
     19 /**
     20  * Abstract base class for {@link ISourceFileLocator} locator implementations
     21  * based on {@link InputStream}s. It handles the encoding and tab width.
     22  */
     23 public abstract class InputStreamSourceFileLocator implements
     24 		ISourceFileLocator {
     25 
     26 	private final String encoding;
     27 	private final int tabWidth;
     28 
     29 	/**
     30 	 * Creates a new locator with the given specification.
     31 	 *
     32 	 * @param encoding
     33 	 *            encoding of the source files, <code>null</code> for platform
     34 	 *            default encoding
     35 	 * @param tabWidth
     36 	 *            tab width in source files as number of blanks
     37 	 *
     38 	 */
     39 	protected InputStreamSourceFileLocator(final String encoding,
     40 			final int tabWidth) {
     41 		this.encoding = encoding;
     42 		this.tabWidth = tabWidth;
     43 	}
     44 
     45 	public Reader getSourceFile(final String packageName, final String fileName)
     46 			throws IOException {
     47 		final InputStream in;
     48 		if (packageName.length() > 0) {
     49 			in = getSourceStream(packageName + "/" + fileName);
     50 		} else {
     51 			in = getSourceStream(fileName);
     52 		}
     53 
     54 		if (in == null) {
     55 			return null;
     56 		}
     57 
     58 		if (encoding == null) {
     59 			return new InputStreamReader(in);
     60 		} else {
     61 			return new InputStreamReader(in, encoding);
     62 		}
     63 	}
     64 
     65 	public int getTabWidth() {
     66 		return tabWidth;
     67 	}
     68 
     69 	/**
     70 	 * Tries to locate the given source file and opens its binary content.
     71 	 *
     72 	 * @param path
     73 	 *            local path to the resource
     74 	 * @return stream if the file could be located, <code>null</code> otherwise
     75 	 * @throws IOException
     76 	 *             in case of problems while opening the stream
     77 	 */
     78 	protected abstract InputStream getSourceStream(String path)
     79 			throws IOException;
     80 
     81 }
     82