diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/imports/ImportControlCheck.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/imports/ImportControlCheck.java index 753ffe377..02060dd2d 100755 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/imports/ImportControlCheck.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/imports/ImportControlCheck.java @@ -18,6 +18,9 @@ //////////////////////////////////////////////////////////////////////////////// package com.puppycrawl.tools.checkstyle.checks.imports; +import java.io.File; +import java.net.URI; + import com.puppycrawl.tools.checkstyle.api.Check; import com.puppycrawl.tools.checkstyle.api.CheckstyleException; import com.puppycrawl.tools.checkstyle.api.DetailAST; @@ -97,6 +100,33 @@ public class ImportControlCheck extends Check } } + /** + * Set the parameter for the url containing the import control + * configuration. It will cause the url to be loaded. + * @param aUrl the url of the file to load. + * @throws ConversionException on error loading the file. + */ + public void setUrl(final String aUrl) + { + // Handle empty param + if ((aUrl == null) || (aUrl.trim().length() == 0)) { + return; + } + final URI uri; + try { + uri = URI.create(aUrl); + } + catch (final IllegalArgumentException ex) { + throw new ConversionException("syntax error in url " + aUrl, ex); + } + try { + mRoot = ImportControlLoader.load(uri); + } + catch (final CheckstyleException ex) { + throw new ConversionException("Unable to load " + aUrl, ex); + } + } + /** * Set the parameter for the file containing the import control * configuration. It will cause the file to be loaded. @@ -111,7 +141,7 @@ public class ImportControlCheck extends Check } try { - mRoot = ImportControlLoader.load(aName); + mRoot = ImportControlLoader.load(new File(aName).toURI()); } catch (final CheckstyleException ex) { throw new ConversionException("Unable to load " + aName, ex); diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/imports/ImportControlLoader.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/imports/ImportControlLoader.java index 83798fdb6..386c948a1 100755 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/imports/ImportControlLoader.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/imports/ImportControlLoader.java @@ -20,9 +20,10 @@ package com.puppycrawl.tools.checkstyle.checks.imports; import com.puppycrawl.tools.checkstyle.api.AbstractLoader; import com.puppycrawl.tools.checkstyle.api.CheckstyleException; -import java.io.FileInputStream; -import java.io.FileNotFoundException; import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URI; import java.util.Stack; import javax.xml.parsers.ParserConfigurationException; import org.xml.sax.Attributes; @@ -109,32 +110,35 @@ final class ImportControlLoader extends AbstractLoader /** * Loads the import control file from a file. - * @param aFilename the name of the file to load. + * @param aUri the uri of the file to load. * @return the root {@link PkgControl} object. * @throws CheckstyleException if an error occurs. */ - static PkgControl load(final String aFilename) throws CheckstyleException + static PkgControl load(final URI aUri) throws CheckstyleException { - FileInputStream fis = null; + InputStream is = null; try { - fis = new FileInputStream(aFilename); + is = aUri.toURL().openStream(); } - catch (final FileNotFoundException e) { - throw new CheckstyleException("unable to find " + aFilename, e); + catch (final MalformedURLException e) { + throw new CheckstyleException("syntax error in url " + aUri, e); } - final InputSource source = new InputSource(fis); - return load(source, aFilename); + catch (final IOException e) { + throw new CheckstyleException("unable to find " + aUri, e); + } + final InputSource source = new InputSource(is); + return load(source, aUri); } /** * Loads the import control file from a {@link InputSource}. * @param aSource the source to load from. - * @param aSourceName name of the source being loaded. + * @param aUri uri of the source being loaded. * @return the root {@link PkgControl} object. * @throws CheckstyleException if an error occurs. */ private static PkgControl load(final InputSource aSource, - final String aSourceName) throws CheckstyleException + final URI aUri) throws CheckstyleException { try { final ImportControlLoader loader = new ImportControlLoader(); @@ -142,14 +146,14 @@ final class ImportControlLoader extends AbstractLoader return loader.getRoot(); } catch (final ParserConfigurationException e) { - throw new CheckstyleException("unable to parse " + aSourceName, e); + throw new CheckstyleException("unable to parse " + aUri, e); } catch (final SAXException e) { - throw new CheckstyleException("unable to parse " + aSourceName + throw new CheckstyleException("unable to parse " + aUri + " - " + e.getMessage(), e); } catch (final IOException e) { - throw new CheckstyleException("unable to read " + aSourceName, e); + throw new CheckstyleException("unable to read " + aUri, e); } } @@ -170,7 +174,7 @@ final class ImportControlLoader extends AbstractLoader * @return the value of the attribute. * @throws SAXException if the attribute does not exist. */ - private String safeGet(final Attributes aAtts, String aName) + private String safeGet(final Attributes aAtts, final String aName) throws SAXException { final String retVal = aAtts.getValue(aName); diff --git a/src/tests/com/puppycrawl/tools/checkstyle/checks/imports/ImportControlLoaderTest.java b/src/tests/com/puppycrawl/tools/checkstyle/checks/imports/ImportControlLoaderTest.java index 82aa50556..3eddc231c 100755 --- a/src/tests/com/puppycrawl/tools/checkstyle/checks/imports/ImportControlLoaderTest.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/checks/imports/ImportControlLoaderTest.java @@ -1,5 +1,7 @@ package com.puppycrawl.tools.checkstyle.checks.imports; +import java.io.File; + import com.puppycrawl.tools.checkstyle.api.CheckstyleException; import junit.framework.TestCase; @@ -7,9 +9,10 @@ public class ImportControlLoaderTest extends TestCase { public void testLoad() throws CheckstyleException { - final PkgControl root = ImportControlLoader.load(System - .getProperty("testinputs.dir") - + "/import-control_complete.xml"); + final PkgControl root = + ImportControlLoader.load(new File(System + .getProperty("testinputs.dir") + + "/import-control_complete.xml").toURI()); assertNotNull(root); } } diff --git a/src/xdocs/config_imports.xml b/src/xdocs/config_imports.xml index 7bf24759e..3760bac91 100755 --- a/src/xdocs/config_imports.xml +++ b/src/xdocs/config_imports.xml @@ -355,6 +355,14 @@