This chapter describes the classes and methods available in the C++test API so that you can add them to your rule definitions in RuleWizard. Some properties are node-type dependent and not all of these properties are accessible. If a property is not accessible, a -1
will be returned if the return type is a number. An empty string will be returned if the return type is a string.
Modules
nodeprovider
Classes:
NodeProvider
Methods:
getProperty
string getProperty(string propertyName)
Returns property as string.
Dictionaries: C,C++ and C++Text
Return type: string
Parameters:
string propertyName | Name of property Available values:
|
---|
Example:
def test(node, context): context.report(node.getProperty("name")) return 1
getColumn
int getColumn()
Returns the start column for a node.
Dictionaries: C,C++ and C++Text
Return type: int
Example:
def test(node, context): context.report(str(node.getColumn())) return 1
getLine
int getLine()
Returns the line for a node.
Dictionaries: C,C++ and C++Text
Return type: int
Example:
def test(node, context): context.report(str(node.getLine())) return 1
report
report ()
Reports text in C++test style.
Dictionaries: C,C++ and C++Text
Return type: none
Example:
def test(node, context): node.report("hit") return 1
getElements
Enumeration getElements(string elementName)
Returns enumerations.
Dictionaries: C++Text
Return type: enumeration
Parameters:
string elementName | Name of element Available values:
|
---|
Example:
def test(node, context): bodyenumeration = node.getElements("body") c = 0 elem = bodyenumeration.next() while (elem != None): c = c + 1 elem = bodyenumeration.next() context.report("The number of elements in the body: " + str(c)) return 1
getEnforcer
RuleEnforcerContext getEnforcer()
Returns the enforcer context - RuleEnforcerContext.
Dictionaries: C,C++ and C++Text
Return type: RuleEnforcerContext
Example:
def test(node, context): ruleenforcercontext = node.getEnforcer() context.report(ruleenforcercontext.getContextName()) return 1
rulecontext
Classes:
RuleContext
Methods:
getContextName
string getContextName()
Returns the context name.
Dictionaries: C,C++ and C++Text
Return type: string
Example:
def test(node, context): context.report(context.getContextName()) return 1
getParentContext
RuleEnforcerContext getParentContext()
Returns the parent context - RuleEnforcerContext.
Dictionaries:C,C++ and C++Text
Return type: RuleEnforcerContext
Example:
def test(node, context): ruleenforcercontext = context.getParentContext() context.report(ruleenforcercontext.getContextName()) return 1
getList
List getList(string collectorName)
Returns a list of collected objects.
Dictionaries: C,C++ and C++Text
Return type: list
Parameters:
string collectorName | name of collector |
---|
Example:
# first we have coleced nodes in colector A def test(node, context): list = context.getList("A") output = "" for it in list: output = output + it.getProperty("name") + " " context.report("Elements in the list: " + output) return 1
report
report ()
Reports text in C++test style.
Dictionaries: C,C++ and C++Text
Return type: none
Example:
def test(node, context): context.report("hit") return 1
put
put(string key, string value)
Saves a key value inside the context.
Dictionaries: C,C++ and C++Text
Return type: none
Parameters:
string key | name of key |
---|---|
string value | key value |
Example:
def test(node, context): context.put("color","red") return 1
get
string get(string key)
Gets the key value from the context.
Dictionaries: C,C++ and C++Text
Return type: string
Parameters:
string key | name of key |
---|
Example:
def test(node, context): context.put("color","red") context.report(context.get("color")) return 1
getKeys
List getKeys()
Gets a list of keys stored in the context.
Dictionaries: C,C++ and C++Text
Return type: list
Example:
def test(node, context): context.put("color","red") context.put("size","big") context.report(context.getKeys()) return 1
clear
clear()
Removes all keys stored in the context.
Dictionaries: C,C++ and C++Text
Return type: none
Example:
def test(node, context): context.put("color","red") txt1 = context.get("color") context.clear() txt2 = context.get("color") context.report("Before clear: " + txt1 + " After clear: " + str(txt2)) return 1
enforcercontext
Classes:
RuleEnforcerContext
Methods:
getContextName
string getContextName()
Returns context name.
Dictionaries: C,C++ and C++Text
Return type: string
Example:
def test(node, context): enforcercontext = context.getParentContext() context.report(enforcercontext.getContextName()) return 1
executeRule
int executeRule(string textRuleName, NodeProvider node)
Returns 1
if the called text rule returns true.
Return type: int
Parameters:
string textRuleName | Path and name of text rule |
---|---|
NodeProvider node | Node determines the point where text rule will be invoked |
Example:
def test(node, context): ruleenforcercontext = node.getEnforcer() #call text rule in the line from node and in default: first column ret = ruleenforcercontext.executeRule("./my_text_rule.rule", node) return ret
executeRuleEx
int executeRuleEx(string textRuleName, NodeProvider node, string fileName, int line, int column)
Returns 1
if the called text rule returns true.
Dictionaries: C,C++ and C++Text
Return type: int
Parameters:
string textRuleName | Path and name of text rule |
---|---|
NodeProvider node | Node determines the point where text rule will be invoked |
string fileName | Name of source file |
int line | Starting line number from which the text rule will be invoked. If -1 , the call child rule will be skipped. |
int column | Starting column number from which the text rule will be invoked. If -1 , the call child rule will be skipped. |
Example:
def test(node, context): ruleenforcercontext = node.getEnforcer() file = node.getProperty("pathname") line = node.getLine() colum = node.getColumn() ret = ruleenforcercontext.executeRuleEx("./txt.rule", node, filename, line, colum) return ret
executeRegexp
int executeRegexp(string regExp, NodeProvider node)
Calls a regExp on the current source file in the line specified in the node.
Dictionaries: C,C++ and C++Text
Return type: int
Parameters:
string regExp | Regular expression string |
---|---|
NodeProvider node | Node is used to determine the line for which the regExp is called |
Example:
def test(node, context): ruleenforcercontext = node.getEnforcer() #return 1 if there is cpp style comment in the line from node ret = ruleenforcercontext.executeRegexp("\/\/",node) return ret
executeRegexpEx
int executeRegexpEx(string regExp, string filename, int start_line, int op_start_column, int op_end_line, int op_end_column)
Calls a regExp on source file $filename
in the region defined by $start_line $op_start_column $op_end_lin $op_end_column
Dictionaries: C,C++ and C++Text
Return type: int
Parameters:
string regExp | Regular expression string |
---|---|
string fileName | Name of source file |
int start_line | Starting line number. |
int op_start_column | Starting column number (optional). Available value(s):
|
int op_end_line | Defined end line (optional). Available value(s):
|
int op_end_column | Defined end column (optional). Available value(s):
|
Example:
def test(node, context): ruleenforcercontext = node.getEnforcer() line = node.getLine() file = node.getProperty("pathname") #return 1 if there is cpp style comment in next line given from node ret = ruleenforcercontext.executeRegexpEx("\/\/", file, line+1) return ret
put
put(string key, string value)
Saves the key value inside the context.
Dictionaries: C,C++ and C++Text
Return type: none
Parameters:
string key | name of the key |
---|---|
string value | value of the key |
Example:
def test(node, context): ruleenforcercontext = context.getParentContext() ruleenforcercontext.put("color","red") return 1
get
string get(string key)
Gets the key value from the context.
Dictionaries: C,C++ and C++Text
Return type: string
Parameters:
string key | name of the key |
---|
Example:
def test(node, context): ruleenforcercontext = context.getParentContext() ruleenforcercontext.put("color","red") context.report(ruleenforcercontext.get("color")) return 1
getKeys
List getKeys()
Gets a list of keys stored in the context.
Dictionaries: C,C++ and C++Text
Return type: list
Example:
def test(node, context): ruleenforcercontext = context.getParentContext() ruleenforcercontext.put("color","red") ruleenforcercontext.put("size","big") context.report(ruleenforcercontext.getKeys()) return 1
clear
clear ()
Removes all keys stored in the context.
Dictionaries: C,C++ and C++Text
Return type: none
Example:
def test(node, context): ruleenforcercontext = context.getParentContext() ruleenforcercontext.put("color","red") txt1 = ruleenforcercontext.get("color") ruleenforcercontext.clear() txt2 = ruleenforcercontext.get("color") context.report("Before clear: " + txt1 + " After clear: " + str(txt2)) return 1
enumeration
Classes:
Enumeration
Methods:
next
NodeProvider nerxt()
Returns the next object in the enumeration.
Dictionaries: C++Text
Return type: NodeProvider
Example:
def test(node, context): bodyenumeration = node.getElements("body") #call next() in for: lines ="" for el in bodyenumeration: lines = lines +str(el.getLine()) + " " context.report("Lines with elements: " + lines) return 1
Examples of Python Functions
def reportMsg(node, context): node.report(repr(node.getLine())) context.report(repr(node.getLine())) return 1
def cleaning( node, context ): #cleans the mark context.put( "a_mark","" ) def firstOnly( node, context ): #checks if mark has been already set; if not sets it if (context.get( "a_mark" ) != "a"): context.put( "a_mark", "a" ) return 1 else: return 0
def compare( node, context ): list = context.getList( 'A' ) if (list.size( ) == 0): return 0 else: return 1