diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/header/AbstractHeaderCheck.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/header/AbstractHeaderCheck.java index 1500366c3..3665696f6 100755 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/header/AbstractHeaderCheck.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/header/AbstractHeaderCheck.java @@ -18,26 +18,30 @@ //////////////////////////////////////////////////////////////////////////////// package com.puppycrawl.tools.checkstyle.checks.header; -import com.google.common.collect.ImmutableList; - import java.io.BufferedInputStream; -import java.io.FileInputStream; +import java.io.File; +import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.io.LineNumberReader; import java.io.Reader; import java.io.StringReader; import java.io.UnsupportedEncodingException; +import java.net.MalformedURLException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; import java.nio.charset.Charset; import java.util.List; +import org.apache.commons.beanutils.ConversionException; + +import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck; import com.puppycrawl.tools.checkstyle.api.CheckstyleException; import com.puppycrawl.tools.checkstyle.api.Utils; -import org.apache.commons.beanutils.ConversionException; - /** * Abstract super class for header checks. * Provides support for header and headerFile properties. @@ -101,8 +105,9 @@ public abstract class AbstractHeaderCheck extends AbstractFileSetCheck checkHeaderNotInitialized(); Reader headerReader = null; try { + final URI uri = resolveHeaderFile(); headerReader = new InputStreamReader(new BufferedInputStream( - new FileInputStream(mFilename)), mCharset); + uri.toURL().openStream()), mCharset); loadHeader(headerReader); } catch (final IOException ex) { @@ -114,6 +119,49 @@ public abstract class AbstractHeaderCheck extends AbstractFileSetCheck } } + /** + * Resolve the specified filename param to a URI. + * @return resolved header file URI + * @throws IOException on failure + */ + private URI resolveHeaderFile() throws IOException + { + // figure out if this is a File or a URL + URI uri; + try { + final URL url = new URL(mFilename); + uri = url.toURI(); + } + catch (final MalformedURLException ex) { + uri = null; + } + catch (final URISyntaxException ex) { + // URL violating RFC 2396 + uri = null; + } + if (uri == null) { + final File file = new File(mFilename); + if (file.exists()) { + uri = file.toURI(); + } + else { + // check to see if the file is in the classpath + try { + final URL configUrl = AbstractHeaderCheck.class + .getResource(mFilename); + if (configUrl == null) { + throw new FileNotFoundException(mFilename); + } + uri = configUrl.toURI(); + } + catch (final URISyntaxException e) { + throw new FileNotFoundException(mFilename); + } + } + } + return uri; + } + /** * Called before initializing the header. * @throws ConversionException if header has already been set diff --git a/src/tests/com/puppycrawl/tools/checkstyle/checks/header/HeaderCheckTest.java b/src/tests/com/puppycrawl/tools/checkstyle/checks/header/HeaderCheckTest.java index 2c174f962..9b78e2ddb 100755 --- a/src/tests/com/puppycrawl/tools/checkstyle/checks/header/HeaderCheckTest.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/checks/header/HeaderCheckTest.java @@ -20,10 +20,14 @@ package com.puppycrawl.tools.checkstyle.checks.header; import static org.junit.Assert.fail; +import java.io.File; +import java.net.URI; + +import org.junit.Test; + import com.puppycrawl.tools.checkstyle.BaseFileSetCheckTestSupport; import com.puppycrawl.tools.checkstyle.DefaultConfiguration; import com.puppycrawl.tools.checkstyle.api.CheckstyleException; -import org.junit.Test; public class HeaderCheckTest extends BaseFileSetCheckTestSupport { @@ -52,6 +56,19 @@ public class HeaderCheckTest extends BaseFileSetCheckTestSupport verify(checkConfig, getPath("InputScopeAnonInner.java"), expected); } + @Test + public void testRegexpHeaderURL() throws Exception + { + final DefaultConfiguration checkConfig = + createCheckConfig(RegexpHeaderCheck.class); + URI uri = (new File(getPath("regexp.header"))).toURI(); + checkConfig.addAttribute("headerFile", uri.toString()); + final String[] expected = { + "3: Line does not match expected header line of '// Created: 2002'.", + }; + verify(checkConfig, getPath("InputScopeAnonInner.java"), expected); + } + @Test public void testInlineRegexpHeader() throws Exception {