use of cfml.parsing.cfscript.script.CFScriptStatement in project CFLint by cflint.
the class CFLint method process.
private void process(final CFScriptStatement expression, Context context) {
if (expression == null) {
return;
}
if (expression != null && expression.getToken() != null) {
List<Object> checkItem = Arrays.asList(expression, expression.getToken());
if (processed.contains(checkItem)) {
System.err.println("Attempt to process expression twice aborted. This may be a parsing bug in " + context.getFilename() + " : " + (expression.getToken() != null ? expression.getToken().getLine() : ""));
return;
}
processed.add(checkItem);
}
final Element elem = context.getElement();
try {
if (expression instanceof CFCompoundStatement) {
scanExpression(expression, context, elem);
for (final CFScriptStatement statement : ((CFCompoundStatement) expression).getStatements()) {
process(statement, context);
}
} else if (expression instanceof CFExpressionStatement) {
scanExpression(expression, context, elem);
process(((CFExpressionStatement) expression).getExpression(), elem, context);
} else if (expression instanceof CFPropertyStatement) {
try {
//TODO fix this to use getPropertyName() when it is available and not null.
Field field = CFPropertyStatement.class.getDeclaredField("propertyName");
field.setAccessible(true);
CFExpression value = (CFExpression) field.get(expression);
if (value == null) {
for (Entry<CFIdentifier, CFExpression> entry : ((CFPropertyStatement) expression).getAttributes().entrySet()) {
if ("name".equals(entry.getKey().getName())) {
value = entry.getValue();
}
}
}
String name = value.Decompile(0);
handler.addVariable(name.substring(1, name.length() - 1));
} catch (Exception e) {
e.printStackTrace();
}
scanExpression(expression, context, elem);
// for(CFExpression expr: ((CFPropertyStatement) expression).decomposeExpression()){
// process(expr, elem, context);
// }
} else if (expression instanceof CFCompDeclStatement) {
CFCompDeclStatement compDeclStatement = (CFCompDeclStatement) expression;
final Context componentContext = context.subContext(null);
componentContext.setInComponent(true);
componentContext.setContextType(ContextType.Component);
for (Entry<CFExpression, CFExpression> entry : compDeclStatement.getAttributes().entrySet()) {
if (entry.getKey() != null && entry.getKey().Decompile(0).equalsIgnoreCase("name")) {
componentContext.setComponentName(entry.getValue().Decompile(0));
}
}
// componentContext.setComponentName(compDeclStatement.get); //
// TODO
// Register any overrides from multi-line comments.
registerRuleOverrides(componentContext, expression.getToken());
// do startComponent notifications
doStructureStart(elem, componentContext, expression.getClass());
scanExpression(compDeclStatement, componentContext, elem);
// process the component declaration
if (compDeclStatement.getBody() instanceof CFCompoundStatement) {
//Process property expressions first
for (CFScriptStatement subscript : compDeclStatement.getBody().decomposeScript()) {
if (subscript instanceof CFPropertyStatement) {
process(subscript, componentContext);
}
}
for (CFScriptStatement subscript : compDeclStatement.getBody().decomposeScript()) {
if (!(subscript instanceof CFPropertyStatement)) {
process(subscript, componentContext);
}
}
} else {
process(compDeclStatement.getBody(), componentContext);
}
// do endComponent notifications
for (final CFLintStructureListener structurePlugin : getStructureListeners(extensions)) {
try {
structurePlugin.endComponent(componentContext, bugs);
for (final ContextMessage message : componentContext.getMessages()) {
reportRule(elem, null, componentContext, (CFLintScanner) structurePlugin, message);
}
componentContext.getMessages().clear();
} catch (final Exception e) {
printException(e);
fireCFLintException(e, PARSE_ERROR, context.getFilename(), null, null, null, null);
}
}
} else if (expression instanceof CFForStatement) {
scanExpression(expression, context, elem);
process(((CFForStatement) expression).getInit(), elem, context);
process(((CFForStatement) expression).getCond(), elem, context);
process(((CFForStatement) expression).getNext(), elem, context);
process(((CFForStatement) expression).getBody(), context);
} else if (expression instanceof CFForInStatement) {
scanExpression(expression, context, elem);
process(((CFForInStatement) expression).getVariable(), elem, context);
process(((CFForInStatement) expression).getStructure(), elem, context);
process(((CFForInStatement) expression).getBody(), context);
} else if (expression instanceof CFIfStatement) {
scanExpression(expression, context, elem);
final CFIfStatement cfif = (CFIfStatement) expression;
process(cfif.getCond(), elem, context);
process(cfif.getThenStatement(), context);
process(cfif.getElseStatement(), context);
} else if (expression instanceof CFSwitchStatement) {
scanExpression(expression, context, elem);
final CFSwitchStatement cfswitch = (CFSwitchStatement) expression;
process(cfswitch.getVariable(), elem, context);
for (CFCase _case : cfswitch.getCases()) {
process(_case, context);
}
} else if (expression instanceof CFCase) {
scanExpression(expression, context, elem);
final CFCase cfcase = (CFCase) expression;
for (CFScriptStatement cfstatement : cfcase.getStatements()) {
process(cfstatement, context);
}
} else if (expression instanceof CFTryCatchStatement) {
scanExpression(expression, context, elem);
final CFTryCatchStatement cftry = (CFTryCatchStatement) expression;
process(cftry.getBody(), context);
for (CFCatchStatement stmt : cftry.getCatchStatements()) {
process(stmt.getCatchBody(), context);
}
process(cftry.getFinallyStatement(), context);
} else if (expression instanceof CFReturnStatement) {
scanExpression(expression, context, elem);
final CFReturnStatement cfreturn = (CFReturnStatement) expression;
final CFExpression subExpression = cfreturn.getExpression();
process(subExpression, elem, context);
} else if (expression instanceof CFFuncDeclStatement) {
final CFFuncDeclStatement function = (CFFuncDeclStatement) expression;
final Context functionContext = context.subContext(null);
functionContext.setContextType(ContextType.Function);
functionContext.setFunctionInfo(function);
registerRuleOverrides(functionContext, function.getToken());
handler.push("function");
for (final CFFunctionParameter param : function.getFormals()) {
handler.addArgument(param.getName());
}
doStructureStart(elem, functionContext, CFFuncDeclStatement.class);
scanExpression(expression, functionContext, elem);
Context functionBodyContext = functionContext.subContext(null);
process(function.getBody(), functionBodyContext);
for (final CFLintStructureListener structurePlugin : getStructureListeners(extensions)) {
try {
structurePlugin.endFunction(functionContext, bugs);
for (final ContextMessage message : functionContext.getMessages()) {
reportRule(elem, null, functionContext, (CFLintScanner) structurePlugin, message);
}
functionContext.getMessages().clear();
} catch (final Exception e) {
printException(e);
fireCFLintException(e, PARSE_ERROR, context.getFilename(), null, null, null, null);
}
}
handler.pop();
} else {
scanExpression(expression, context, elem);
}
} catch (final StackOverflowError soe) {
System.err.println("Stack overflow in " + context.getFilename());
final int line = context.startLine();
fireCFLintException(soe, PARSE_ERROR, context.getFilename(), line, 1, "", "Stack overflow on " + expression.getClass());
}
// Process any messages added by downstream parsing.
for (final ContextMessage message : context.getMessages()) {
reportRule(elem, null, context, message.getSource(), message);
}
context.getMessages().clear();
}
use of cfml.parsing.cfscript.script.CFScriptStatement in project CFLint by cflint.
the class CFLint method process.
private void process(final Element elem, final String space, final Context context) throws ParseException, IOException {
currentElement = elem;
if (elem.getName().equalsIgnoreCase("cfcomponent")) {
final Context componentContext = context.subContext(elem);
componentContext.setInComponent(true);
componentContext.setComponentName(elem.getAttributeValue("displayname"));
componentContext.setContextType(ContextType.Component);
handler.push("component");
doStructureStart(elem, componentContext, CFCompDeclStatement.class);
} else if (elem.getName().equalsIgnoreCase("cffunction")) {
final Context functionContext = context.subContext(elem);
functionContext.setFunctionName(elem.getAttributeValue("name"));
functionContext.setContextType(ContextType.Function);
handler.push("function");
doStructureStart(elem, functionContext, CFFuncDeclStatement.class);
}
if (elem.getName().equalsIgnoreCase("cfset") || elem.getName().equalsIgnoreCase("cfif") || elem.getName().equalsIgnoreCase("cfelseif") || elem.getName().equalsIgnoreCase("cfreturn")) {
scanElement(elem, context);
final Pattern p = Pattern.compile("<\\w+\\s(.*[^/])/?>", Pattern.MULTILINE | Pattern.DOTALL);
final String expr = elem.getFirstStartTag().toString();
final Matcher m = p.matcher(expr);
if (m.matches()) {
// TODO if LUCEE?
// final int uglyNotPos = elem.toString().lastIndexOf("<>");
// int endPos = elem.getStartTag().getEnd() - 1;
//
// if (uglyNotPos > 0) {
// final int nextPos = elem.toString().indexOf(">", uglyNotPos +
// 2);
// if (nextPos > 0 && nextPos < elem.getEndTag().getBegin()) {
// endPos = nextPos;
// }
// }
// final String cfscript =
// elem.toString().substring(elem.getName().length() + 1,
// Math.min(endPos,elem.toString().length()-1));
final String cfscript = m.group(1);
try {
final CFExpression expression = cfmlParser.parseCFExpression(cfscript, this);
if (expression != null) {
process(expression, elem, context);
}
} catch (final Exception npe) {
printException(npe, elem);
fireCFLintException(npe, PARSE_ERROR, context.getFilename(), null, null, null, null);
}
}
processStack(elem.getChildElements(), space + " ", context);
} else if (elem.getName().equalsIgnoreCase("cfargument")) {
scanElement(elem, context);
final String name = elem.getAttributeValue("name");
if (name != null) {
handler.addArgument(name);
}
processStack(elem.getChildElements(), space + " ", context);
} else if (elem.getName().equalsIgnoreCase("cfscript")) {
scanElement(elem, context);
final String cfscript = elem.getContent().toString();
final CFScriptStatement scriptStatement = cfmlParser.parseScript(cfscript);
Context subcontext = context.subContext(elem);
process(scriptStatement, subcontext);
processStack(elem.getChildElements(), space + " ", context);
} else if (elem.getName().equalsIgnoreCase("cffunction")) {
final Context functionContext = context.subContext(elem);
functionContext.setFunctionName(elem.getAttributeValue("name"));
functionContext.setContextType(ContextType.Function);
scanElement(elem, functionContext);
processStack(elem.getChildElements(), space + " ", functionContext);
// Process any messages added by downstream parsing.
for (final ContextMessage message : functionContext.getMessages()) {
reportRule(elem, null, functionContext, message.getSource(), message);
}
functionContext.getMessages().clear();
for (final CFLintStructureListener structurePlugin : getStructureListeners(extensions)) {
try {
structurePlugin.endFunction(functionContext, bugs);
for (final ContextMessage message : functionContext.getMessages()) {
reportRule(elem, null, functionContext, (CFLintScanner) structurePlugin, message);
}
functionContext.getMessages().clear();
} catch (final Exception e) {
printException(e);
fireCFLintException(e, PARSE_ERROR, context.getFilename(), null, null, null, null);
}
}
handler.pop();
} else if (elem.getName().equalsIgnoreCase("cfcomponent")) {
final Context componentContext = context.subContext(elem);
componentContext.setInComponent(true);
componentContext.setComponentName(elem.getAttributeValue("displayname"));
componentContext.setContextType(ContextType.Component);
scanElement(elem, componentContext);
processStack(elem.getChildElements(), space + " ", componentContext);
for (final CFLintStructureListener structurePlugin : getStructureListeners(extensions)) {
try {
structurePlugin.endComponent(componentContext, bugs);
for (final ContextMessage message : componentContext.getMessages()) {
reportRule(elem, null, componentContext, (CFLintScanner) structurePlugin, message);
}
componentContext.getMessages().clear();
} catch (final Exception e) {
printException(e);
fireCFLintException(e, PARSE_ERROR, context.getFilename(), null, null, null, null);
}
}
handler.pop();
} else if (elem.getName().equalsIgnoreCase("cfquery")) {
scanElement(elem, context);
final List<Element> list = elem.getAllElements();
processStack(list.subList(1, list.size()), space + " ", context);
} else if (elem.getName().equalsIgnoreCase("cfqueryparam")) {
scanElement(elem, context);
if (elem.getAttributeValue("value") != null) {
}
} else {
scanElement(elem, context);
processStack(elem.getChildElements(), space + " ", context);
}
// Process any messages added by downstream parsing.
for (final ContextMessage message : context.getMessages()) {
reportRule(elem, null, context, message.getSource(), message);
}
context.getMessages().clear();
}
use of cfml.parsing.cfscript.script.CFScriptStatement in project CFLint by cflint.
the class CFLint method process.
public void process(final String src, final String filename) throws ParseException, IOException {
fireStartedProcessing(filename);
final CFMLSource cfmlSource = new CFMLSource(src != null && src.contains("<!---") ? CommentReformatting.wrap(src) : src);
final ParserTag firstTag = getFirstTagQuietly(cfmlSource);
final List<Element> elements = new ArrayList<Element>();
if (firstTag != null) {
elements.addAll(cfmlSource.getChildElements());
}
if (src.contains("component") && (elements.isEmpty() || elements.get(0).getBegin() > src.indexOf("component"))) {
// Check if pure cfscript
final CFScriptStatement scriptStatement = cfmlParser.parseScript(src);
Context context = new Context(filename, null, null, false, handler, scriptStatement.getTokens());
process(scriptStatement, context);
} else {
processStack(elements, " ", filename, null);
}
fireFinishedProcessing(filename);
}
use of cfml.parsing.cfscript.script.CFScriptStatement in project CFLint by cflint.
the class CFLint method reportRule.
protected void reportRule(final Element elem, final Object expression, final Context context, final CFLintScanner pluginParm, final ContextMessage msg) {
final String msgcode = msg.getMessageCode();
final String nameVar = msg.getVariable();
final CFLintScanner plugin = msg.getSource() == null ? pluginParm : msg.getSource();
if (checkForDisabled(elem, msgcode)) {
return;
}
if (configuration == null) {
throw new NullPointerException("Configuration is null");
}
PluginInfoRule ruleInfo;
if ("PLUGIN_ERROR".equals(msgcode)) {
ruleInfo = new PluginInfoRule();
final PluginMessage msgInfo = new PluginMessage("PLUGIN_ERROR");
msgInfo.setMessageText("Error in plugin: ${variable}");
msgInfo.setSeverity("ERROR");
ruleInfo.getMessages().add(msgInfo);
} else {
if (plugin == null) {
throw new NullPointerException("Plugin not set. Plugin should be using addMessage(messageCode,variable,source) to report messages in parent contexts");
}
ruleInfo = configuration.getRuleForPlugin(plugin);
}
if (ruleInfo == null) {
throw new NullPointerException("Rule not found for " + plugin.getClass().getSimpleName());
}
final PluginMessage msgInfo = ruleInfo.getMessageByCode(msgcode);
if (configuration == null) {
throw new NullPointerException("Message definition not found for [" + msgcode + "] in " + plugin.getClass().getSimpleName());
}
final BugInfoBuilder bldr = new BugInfo.BugInfoBuilder().setMessageCode(msgcode).setVariable(nameVar).setFunction(context.getFunctionName()).setFilename(context.getFilename()).setComponent(context.getComponentName());
if (msgInfo != null) {
bldr.setSeverity(msgInfo.getSeverity());
bldr.setMessage(msgInfo.getMessageText());
} else {
String errMessage = "Message code: " + msgcode + " is not configured correctly.";
fireCFLintException(new NullPointerException(errMessage), PLUGIN_ERROR, "", null, null, null, null);
bldr.setSeverity("WARNING");
bldr.setMessage(msgcode);
}
if (expression instanceof CFStatement) {
bldr.setExpression(((CFStatement) expression).Decompile(0));
} else if (expression instanceof CFScriptStatement) {
bldr.setExpression(((CFScriptStatement) expression).Decompile(0));
} else if (elem != null) {
bldr.setExpression(elem.toString());
}
bldr.setRuleParameters(ruleInfo.getParameters());
if (configuration.includes(ruleInfo.getMessageByCode(msgcode)) && !configuration.excludes(ruleInfo.getMessageByCode(msgcode))) {
if (expression instanceof CFExpression) {
BugInfo bugInfo = bldr.build((CFExpression) expression, elem);
final Token token = ((CFExpression) expression).getToken();
if (!suppressed(bugInfo, token, context)) {
bugs.add(bugInfo);
}
} else {
final BugInfo bug = bldr.build((CFParsedStatement) expression, elem);
if (msg.getLine() != null) {
bug.setLine(msg.getLine());
bug.setColumn(0);
}
bugs.add(bug);
}
}
}
Aggregations