Add a url option for ImportControl check. Thanks to Benjamin Lerman for
the patch 1724683.
This commit is contained in:
parent
02da493e64
commit
7a6bf01c4d
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -355,6 +355,14 @@
|
|||
<td><a href="property_types.html#string">string</a></td>
|
||||
<td><span class="default">null</span></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>url</td>
|
||||
<td>
|
||||
url of the file containing the import control configuration.
|
||||
</td>
|
||||
<td><a href="property_types.html#string">string</a></td>
|
||||
<td><span class="default">null</span></td>
|
||||
</tr>
|
||||
</table>
|
||||
</subsection>
|
||||
|
||||
|
|
|
|||
|
|
@ -54,6 +54,10 @@
|
|||
Set the <tt>failureProperty</tt> with a meaningful message
|
||||
(feature request 1725475).
|
||||
</li>
|
||||
<li>
|
||||
Add a url option for ImportControl check. Thanks to Benjamin
|
||||
Lerman for the patch 1724683.
|
||||
</li>
|
||||
</ul>
|
||||
</section>
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue