aboutsummaryrefslogtreecommitdiff
path: root/org.jacoco.report
diff options
context:
space:
mode:
authorMarc R. Hoffmann <hoffmann@mountainminds.com>2012-01-18 20:24:45 +0000
committerMarc R. Hoffmann <hoffmann@mountainminds.com>2012-01-18 20:24:45 +0000
commit8b5d6a399c8927362780dd9426b85c2432fd8d7a (patch)
tree33fd6472de9fb08f77ff570418f95821a99acb9d /org.jacoco.report
parentf6898eece6c8ba3a4fee48189ab0a714b8d55cf2 (diff)
downloadjacoco-8b5d6a399c8927362780dd9426b85c2432fd8d7a.tar.gz
Trac #119: Support for source folders in Ant report task.
Diffstat (limited to 'org.jacoco.report')
-rw-r--r--org.jacoco.report/src/org/jacoco/report/DirectorySourceFileLocator.java31
-rw-r--r--org.jacoco.report/src/org/jacoco/report/InputStreamSourceFileLocator.java81
-rw-r--r--org.jacoco.report/src/org/jacoco/report/MultiSourceFileLocator.java68
3 files changed, 160 insertions, 20 deletions
diff --git a/org.jacoco.report/src/org/jacoco/report/DirectorySourceFileLocator.java b/org.jacoco.report/src/org/jacoco/report/DirectorySourceFileLocator.java
index 5cec1f0c..6ab9dab8 100644
--- a/org.jacoco.report/src/org/jacoco/report/DirectorySourceFileLocator.java
+++ b/org.jacoco.report/src/org/jacoco/report/DirectorySourceFileLocator.java
@@ -14,21 +14,16 @@ package org.jacoco.report;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
-import java.io.InputStreamReader;
-import java.io.Reader;
+import java.io.InputStream;
/**
* Locator for source files that picks source files from a given directory in
* the file system.
*/
-public class DirectorySourceFileLocator implements ISourceFileLocator {
+public class DirectorySourceFileLocator extends InputStreamSourceFileLocator {
private final File directory;
- private final String encoding;
-
- private final int tabWidth;
-
/**
* Creates a new locator that searches for source files in the given
* directory.
@@ -36,30 +31,26 @@ public class DirectorySourceFileLocator implements ISourceFileLocator {
* @param directory
* directory to search for source file
* @param encoding
- * encoding of the source files
+ * encoding of the source files, <code>null</code> for platform
+ * default encoding
* @param tabWidth
* tab width in source files as number of blanks
*
*/
public DirectorySourceFileLocator(final File directory,
final String encoding, final int tabWidth) {
+ super(encoding, tabWidth);
this.directory = directory;
- this.encoding = encoding;
- this.tabWidth = tabWidth;
}
- public Reader getSourceFile(final String packageName, final String fileName)
- throws IOException {
- final File dir = new File(directory, packageName);
- final File file = new File(dir, fileName);
+ @Override
+ protected InputStream getSourceStream(final String path) throws IOException {
+ final File file = new File(directory, path);
if (file.exists()) {
- return new InputStreamReader(new FileInputStream(file), encoding);
+ return new FileInputStream(file);
+ } else {
+ return null;
}
- return null;
- }
-
- public int getTabWidth() {
- return tabWidth;
}
}
diff --git a/org.jacoco.report/src/org/jacoco/report/InputStreamSourceFileLocator.java b/org.jacoco.report/src/org/jacoco/report/InputStreamSourceFileLocator.java
new file mode 100644
index 00000000..8dea3611
--- /dev/null
+++ b/org.jacoco.report/src/org/jacoco/report/InputStreamSourceFileLocator.java
@@ -0,0 +1,81 @@
+/*******************************************************************************
+ * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Marc R. Hoffmann - initial API and implementation
+ *
+ *******************************************************************************/
+package org.jacoco.report;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+
+/**
+ * Abstract base class for {@link ISourceFileLocator} locator implementations
+ * based on {@link InputStream}s. It handles the encoding and tab width.
+ */
+public abstract class InputStreamSourceFileLocator implements
+ ISourceFileLocator {
+
+ private final String encoding;
+ private final int tabWidth;
+
+ /**
+ * Creates a new locator with the given specification.
+ *
+ * @param encoding
+ * encoding of the source files, <code>null</code> for platform
+ * default encoding
+ * @param tabWidth
+ * tab width in source files as number of blanks
+ *
+ */
+ protected InputStreamSourceFileLocator(final String encoding,
+ final int tabWidth) {
+ this.encoding = encoding;
+ this.tabWidth = tabWidth;
+ }
+
+ public Reader getSourceFile(final String packageName, final String fileName)
+ throws IOException {
+ final InputStream in;
+ if (packageName.length() > 0) {
+ in = getSourceStream(packageName + "/" + fileName);
+ } else {
+ in = getSourceStream(fileName);
+ }
+
+ if (in == null) {
+ return null;
+ }
+
+ if (encoding == null) {
+ return new InputStreamReader(in);
+ } else {
+ return new InputStreamReader(in, encoding);
+ }
+ }
+
+ public int getTabWidth() {
+ return tabWidth;
+ }
+
+ /**
+ * Tries to locate the given source file and opens its binary content.
+ *
+ * @param path
+ * local path to the resource
+ * @return stream if the file could be located, <code>null</code> otherwise
+ * @throws IOException
+ * in case of problems while opening the stream
+ */
+ protected abstract InputStream getSourceStream(String path)
+ throws IOException;
+
+}
diff --git a/org.jacoco.report/src/org/jacoco/report/MultiSourceFileLocator.java b/org.jacoco.report/src/org/jacoco/report/MultiSourceFileLocator.java
new file mode 100644
index 00000000..cd70fd0d
--- /dev/null
+++ b/org.jacoco.report/src/org/jacoco/report/MultiSourceFileLocator.java
@@ -0,0 +1,68 @@
+/*******************************************************************************
+ * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Marc R. Hoffmann - initial API and implementation
+ *
+ *******************************************************************************/
+package org.jacoco.report;
+
+import java.io.IOException;
+import java.io.Reader;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Locator that searches source files in multiple {@link ISourceFileLocator}
+ * instances. For each lookup request the first locator that returns a
+ * {@link Reader} for source content is selected.
+ */
+public class MultiSourceFileLocator implements ISourceFileLocator {
+
+ private final int tabWidth;
+
+ private final List<ISourceFileLocator> delegates;
+
+ /**
+ * Creates a new empty locator.
+ *
+ * @param tabWidth
+ * tab width in source files as number of blanks used for all
+ * source files
+ */
+ public MultiSourceFileLocator(final int tabWidth) {
+ this.tabWidth = tabWidth;
+ this.delegates = new ArrayList<ISourceFileLocator>();
+ }
+
+ /**
+ * Adds the given locator. Locators are queried in the sequence they have
+ * been added.
+ *
+ * @param locator
+ * Additional locator to query
+ */
+ public void add(final ISourceFileLocator locator) {
+ delegates.add(locator);
+ }
+
+ public Reader getSourceFile(final String packageName, final String fileName)
+ throws IOException {
+ for (final ISourceFileLocator d : delegates) {
+ final Reader reader = d.getSourceFile(packageName, fileName);
+ if (reader != null) {
+ return reader;
+ }
+ }
+ return null;
+ }
+
+ public int getTabWidth() {
+ return tabWidth;
+ }
+
+}