Skip to content

Commit

Permalink
api
Browse files Browse the repository at this point in the history
  • Loading branch information
ryaneberly committed Aug 26, 2017
1 parent 7d178ad commit 4330edd
Show file tree
Hide file tree
Showing 8 changed files with 669 additions and 317 deletions.
69 changes: 39 additions & 30 deletions src/main/java/com/cflint/CFLint.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.cflint.config.CFLintPluginInfo.PluginInfoRule;
import com.cflint.config.CFLintPluginInfo.PluginInfoRule.PluginMessage;
import com.cflint.config.ConfigUtils;
import com.cflint.exception.CFLintScanException;
import com.cflint.listeners.ScanProgressListener;
import com.cflint.plugins.CFLintScanner;
import com.cflint.plugins.CFLintSet;
Expand Down Expand Up @@ -197,8 +198,6 @@ public void setupConfigAncestry(File folder) {
if (verbose) {
System.out.println("read config " + file);
}
System.out.println(
"DEPRECATED: The uses of \"inheritPlugins\" and \"output\" have been marked as deprecated in CFLint 1.2.x and support for them will be fully removed in CFLint 1.3.0. Please remove the settings from your configuration file(s). Run CFLint in verbose mode for config file location details.");
try {
final CFLintConfig newConfig = file.getName().toLowerCase().endsWith(".xml")
? ConfigUtils.unmarshal(new FileInputStream(file), CFLintConfig.class)
Expand Down Expand Up @@ -236,8 +235,6 @@ public void scan(final File folderOrFile) {
if (verbose) {
System.out.println("read config " + file);
}
System.out.println(
"DEPRECATED: The use of \"inheritPlugins\" has been marked as deprecated in CFLint 1.2.x and support for it will be fully removed in CFLint 1.3.0. Please remove the setting from your configuration file(s). Run CFLint in verbose mode for config file location details.");
final CFLintConfiguration newConfig = file.getName().toLowerCase().endsWith(".xml")
? ConfigUtils.unmarshal(new FileInputStream(file), CFLintConfig.class)
: ConfigUtils.unmarshalJson(new FileInputStream(file), CFLintConfig.class);
Expand Down Expand Up @@ -284,29 +281,32 @@ protected void printException(final Exception e, final Element... elem) {
}
}

public void process(final String src, final String filename) throws IOException {

fireStartedProcessing(filename);
if (src == null || src.trim().length() == 0) {
final Context context = new Context(filename, null, null, false, handler);
reportRule(null, null, context, null, new ContextMessage(AVOID_EMPTY_FILES, null));
} else {
final CFMLSource cfmlSource = new CFMLSource(src.contains("<!---") ? CommentReformatting.wrap(src) : src);
final ParserTag firstTag = getFirstTagQuietly(cfmlSource);
final List<Element> elements = new ArrayList<>();
if (firstTag != null) {
elements.addAll(cfmlSource.getChildElements());
}
if (isComponentOrInterfaceScript(src, elements)) {
// Check if pure cfscript
final CFScriptStatement scriptStatement = cfmlParser.parseScript(src);
final Context context = new Context(filename, null, null, false, handler, scriptStatement.getTokens());
process(scriptStatement, context);
public void process(final String src, final String filename) throws CFLintScanException {
try{
fireStartedProcessing(filename);
if (src == null || src.trim().length() == 0) {
final Context context = new Context(filename, null, null, false, handler);
reportRule(null, null, context, null, new ContextMessage(AVOID_EMPTY_FILES, null));
} else {
processStack(elements, " ", filename, null);
final CFMLSource cfmlSource = new CFMLSource(src.contains("<!---") ? CommentReformatting.wrap(src) : src);
final ParserTag firstTag = getFirstTagQuietly(cfmlSource);
final List<Element> elements = new ArrayList<>();
if (firstTag != null) {
elements.addAll(cfmlSource.getChildElements());
}
if (isComponentOrInterfaceScript(src, elements)) {
// Check if pure cfscript
final CFScriptStatement scriptStatement = cfmlParser.parseScript(src);
final Context context = new Context(filename, null, null, false, handler, scriptStatement.getTokens());
process(scriptStatement, context);
} else {
processStack(elements, " ", filename, null);
}
}
fireFinishedProcessing(filename);
}catch(final Exception e){
throw new CFLintScanException(e);
}
fireFinishedProcessing(filename);
}

/**
Expand Down Expand Up @@ -336,7 +336,7 @@ protected ParserTag getFirstTagQuietly(final CFMLSource cfmlSource) {
}

public void processStack(final List<Element> elements, final String space, final String filename,
final CFIdentifier functionName) throws IOException {
final CFIdentifier functionName) throws IOException, ParseException, CFLintScanException {
Element commentElement = null;
for (final Element elem : elements) {
if (elem.getName().equals(CF.COMMENT)) {
Expand All @@ -354,7 +354,7 @@ public void processStack(final List<Element> elements, final String space, final
}

public void processStack(final List<Element> elements, final String space, final Context context)
throws ParseException, IOException {
throws CFLintScanException {
Element commentElement = null;
for (final Element elem : elements) {
if (elem.getName().equals(CF.COMMENT)) {
Expand All @@ -371,7 +371,7 @@ public void processStack(final List<Element> elements, final String space, final
}

private void process(final Element elem, final String space, final Context context)
throws ParseException, IOException {
throws CFLintScanException {
if (skipToPosition > elem.getBegin()) {
return;
} else {
Expand Down Expand Up @@ -451,7 +451,14 @@ private void process(final Element elem, final String space, final Context conte
skipToPosition = nextTag.getEnd();
}
}
final CFScriptStatement scriptStatement = cfmlParser.parseScript(cfscript);
CFScriptStatement scriptStatement;
try {
scriptStatement = cfmlParser.parseScript(cfscript);
} catch (ParseException e) {
throw new CFLintScanException(e);
} catch (IOException e) {
throw new CFLintScanException(e);
}

final Context subcontext = context.subContext(elem);
process(scriptStatement, subcontext);
Expand Down Expand Up @@ -868,7 +875,7 @@ private void process(final CFScriptStatement expression, final Context context)
process(FileUtil.loadFile(include), context.getFilename());
includeFileStack.pop();
}
} catch (final IOException ex) {
} catch (final CFLintScanException ex) {
System.err.println("Invalid include file " + context.getFilename());
final int line = context.startLine();
final ContextMessage cm = new ContextMessage(PARSE_ERROR, null, null, line);
Expand Down Expand Up @@ -1305,7 +1312,9 @@ public List<String> getAllowedExtensions() {

public void setAllowedExtensions(final List<String> allowedExtensions) {
this.allowedExtensions.clear();
this.allowedExtensions.addAll(allowedExtensions);
if(allowedExtensions != null){
this.allowedExtensions.addAll(allowedExtensions);
}
}

@Override
Expand Down
239 changes: 239 additions & 0 deletions src/main/java/com/cflint/api/CFLintAPI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
package com.cflint.api;

import java.io.File;
import java.io.FileInputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.io.IOUtils;

import com.cflint.BugInfo;
import com.cflint.BugList;
import com.cflint.CFLint;
import com.cflint.Version;
import com.cflint.config.CFLintConfiguration;
import com.cflint.config.CFLintPluginInfo;
import com.cflint.config.CFLintPluginInfo.RuleGroup;
import com.cflint.config.ConfigBuilder;
import com.cflint.config.ConfigUtils;
import com.cflint.exception.CFLintConfigurationException;
import com.cflint.exception.CFLintScanException;
import com.cflint.tools.CFLintFilter;

/**
* Provides a public API for integrating CFLint directly into another JVM environment.
*
*/
public class CFLintAPI {

PrintStream printStreamOut = System.out;
PrintStream printStreamErr = System.err;
final CFLintPluginInfo pluginInfo = ConfigUtils.loadDefaultPluginInfo();

String filterFile = null;
boolean verbose = false;
boolean logError = false;
boolean quiet = false;

/**
* List of file extensions to scan. Default to *.cfc and *.cfm
*/
private List<String> extensions = new ArrayList<>(Arrays.asList("cfc","cfm"));

private boolean strictInclude;

final CFLintConfiguration configuration;
CFLint cflint;

public CFLintAPI(final CFLintConfiguration configuration) {
super();
this.configuration = configuration;
}

public CFLintAPI() {
this(new ConfigBuilder().build());
}


public CFLintResult scan(final List<String> fileOrFolder) throws CFLintScanException, CFLintConfigurationException {
cflint = createCFLintInstance();

for (final String scanfolder : fileOrFolder) {
cflint.scan(scanfolder);
}
for (final BugInfo bug : cflint.getBugs()) {
cflint.getStats().getCounts().add(bug.getMessageCode(), bug.getSeverity());
}
return new CFLintResult(cflint);
}

public CFLintResult scan(final String source) throws CFLintScanException, CFLintConfigurationException {
return scan(source, "source.cfc");
}

public CFLintResult scan(final String source, final String filename)
throws CFLintScanException, CFLintConfigurationException {
cflint = createCFLintInstance();
final File starterFile = new File(filename);
if (starterFile.exists() && starterFile.getParentFile().exists()) {
cflint.setupConfigAncestry(starterFile.getParentFile());
}
cflint.process(source, filename);
for (final BugInfo bug : cflint.getBugs()) {
cflint.getStats().getCounts().add(bug.getMessageCode(), bug.getSeverity());
}
return new CFLintResult(cflint);
}

public BugList getResults() {
return cflint.getBugs();
}

private CFLint createCFLintInstance() throws CFLintConfigurationException {
try {
final CFLint cflint = new CFLint(configuration);
cflint.setVerbose(verbose);
cflint.setLogError(logError);
cflint.setQuiet(quiet);
cflint.setStrictIncludes(strictInclude);
cflint.setAllowedExtensions(extensions);
cflint.getBugs().setFilter(createFilter());
return cflint;
} catch (final Exception e) {
throw new CFLintConfigurationException(e);
}
}

protected CFLintFilter createFilter() throws CFLintConfigurationException {
try {
if (filterFile != null) {
final File ffile = new File(filterFile);
if (ffile.exists()) {
final FileInputStream fis = new FileInputStream(ffile);
final byte b[] = new byte[fis.available()];
IOUtils.read(fis, b);
fis.close();
return CFLintFilter.createFilter(new String(b), verbose);
}
}
return CFLintFilter.createFilter(verbose);
} catch (final Exception e) {
throw new CFLintConfigurationException(e);
}
}

/**
* Return the current version of CFLint
*
* @return
*/
public String getVersion() {
return Version.getVersion();
}

/**
* Return the version of CFParser used by the current CFLint
*
* @return
*/
public String getCFParserVersion() {
return cfml.parsing.Version.getVersion();
}

/**
* List the rule groups
*
* @param pluginInfo
* @return
*/
public List<RuleGroup> getRuleGroups() {
return pluginInfo.getRuleGroups();
}

/**
* Limit file extensions to this list
*
* @param extensions
*/
public void setExtensions(final List<String> extensions) {
this.extensions = extensions;
}

/**
* Verbose output
*
* @param verbose
*/
public void setVerbose(final boolean verbose) {
this.verbose = verbose;
}

/**
* Log errors to output
*
* @param logerror
*/
public void setLogError(final boolean logerror) {
this.logError = logerror;
}

/**
* Run quietly
*
* @param quiet
*/
public void setQuiet(final boolean quiet) {
this.quiet = quiet;
}

/**
* Follow include paths and report an error if the included file cannot be
* found
*
* @param strictInclude
*/
public void setStrictInclude(final boolean strictInclude) {
this.strictInclude = strictInclude;
}

/**
* Get the configuration object
*
* @return
*/
public CFLintConfiguration getConfiguration() {
return configuration;
}

/**
* Set filter file
*
* @param filterFile
*/
public void setFilterFile(final String filterFile) {
this.filterFile = filterFile;
}

/**
* Provide the stream to use for standard output, by default System.out is
* used.
*
* @param printStreamOut
*/
public void setPrintStreamOut(final PrintStream printStreamOut) {
this.printStreamOut = printStreamOut;
}

/**
* Provide the stream to use for error output, by default System.err is
* used.
*
* @param printStreamErr
*/
public void setPrintStreamErr(final PrintStream printStreamErr) {
this.printStreamErr = printStreamErr;
}

}
Loading

0 comments on commit 4330edd

Please sign in to comment.