vtd-xml: Autopilot: declareVariableExpr - vtd-xml

Java VTD-XML has the following API
class AutoPilot{
declareVariableExpr(java.lang.String varName, java.lang.String varExpr);
}
Register the binding between a variableExpr name and variableExpr expression
I have successfully used it for simple variable bindings like:
abc => "some value"
and Autopilot can run expressions like
ap.selectXpath("concat(/a/b/text(), $abc)");
ap.evalXPathToString()
My question is:
The API says it binds a variable to a variable expression.
how to bind to a variable expression? and what is the usage of binding to an 'expression'?

Try this.
ap.declareVariableExpr("my_expr","/a/b/c");

Related

Declaration of dynamic dataweave variable in Mule 4 like we have done with using in Mule 3

I have a requirement of creating a runtime variable in Dataweave like we have done in Mule 3 with the using keyword. Can someone let me know how can it be achieved in Mule 4
You can still use using keyword in Mule 4/Dataweave 2.
Local variables are initialized in the body of the DataWeave script and can be referenced by name only from within the scope of the expression where they are initialized.
The syntax for initializing a local variable looks like this: using ( = )
You can combine several local variable definitions as a comma separated list inside the using function. For example: using (firstName='Annie', lastName='Point')
%dw 2.0
output application/json
---
using (x = 2) 3 + x
Here is an example of defining a local variable within an object:
%dw 2.0
output application/xml
---
{
person: using (user='Greg', gender='male') {
name: user,
gender: gender
}
}
Note thise variables are only scoped to the 'person' object. Accessing them outside of person will throw an error.
Full documentation on this here: https://docs.mulesoft.com/mule-runtime/4.1/dataweave-variables

Conditional expression is not working in Mule 4

I have this expression when setting the value of a variable in Mule:
#[(message.inboundProperties['message-id'] != null) ? message.inboundProperties['message-id'] : java.util.UUID.randomUUID().toString().replace('-', '')]
Basically, if the message does not already have an id allocated to it then it will have one created.
I have moved onto Mule 4 and Anypoint 7 and this expression no longer works. I know the inboundProperties has changed to attributes so have made the following changes:
#[(attributes.headers.'message-id' != null) ? attributes.headers.'message-id' : java.util.UUID.randomUUID().toString().replace('-', '')]
For both expressions I get the error "No viable alternative at input '('.
How can I fix this statement to work for Mule 4?
Thanks
#[attributes.headers.'message-id' default (uuid() replace '-' with '')]
expression use Dataweave 2.0 as default in mule 4, not MEL. So you can no longer use java method invocation. Instead use the uuid() dataweave function and the replace dataweave function
You can use default instead of the if else check

How to pass variable to groovy code in Intellij IDEA live templates groovy script?

I have a groovyScript in my Intellij IDEA live template, like this :
groovyScript("D:/test.groovy","", v1)
on my D:/test.groovy i have a code like this:
if ( v1 == 'abc') {
'abc'
}
Now I want to pass v1 variable into test.groovy ,can any one help me how can I do this?
For exemplification purposes I made a live template which is printing a comment with the current class and current method.
This is how my live template is defined:
And here is how I edited the variableResolvedWithGroovyScript variable:
The Expression for the given variable has the follwing value:
groovyScript("return \"// Current Class:\" + _1 + \". Current Method:\"+ _2 ", className(),methodName())
As you can see, in this case the _1(which acts like a variable in the groovy script) takes the value of the first parameter which is the class name, and the _2 takes the value of the
second parameter which is the method name. If another parameter is needed the _3 will be used in the groovy script to reference the given parameter.
The arguments to groovyScript macro are bound to script variables named _1, _2 etc. This is also described at groovyScript help at Edit Template Variables Dialog / Live Template Variables.
I found a solution.
I was needing to calculate a CRC32 of the qualified class name using live models
I used it like this:
groovyScript("
def crc = new java.util.zip.CRC32().with { update _1.bytes; value };
return Long.toHexString(crc);
", qualifiedClassName())
then the result is
Based on the documentation, your variables are available as _1, _2, etc. Please note that variables are passed without dollar signs (so only v1 instead of $v1$)
So your test script should look like
if ( _2 == 'abc') {
'abc'
}

Using a filter in part of a string

I have a filter that formats a number based on what stat type it is (percent, integer, currency, etc). I want to use this filter on a value as part of a string. See below:
<js-widget
v-on:click="this.selectedReportId = key"
:selected="this.selectedReportId == key"
:icon="report.icon"
:stat="report.current | stat report.statType"
:title="report.title"
:tooltip="report.tooltip"
:percent="report.percent"
:description="'Previous value: '+(report.past | stat report.statType)">
</js-widget>
As you can see in the :stat parameter, I can use the filter if it is the only thing in the expression. But when it tries to evaluate the :description parameter, I get Error when evaluating expression. Is it possible to use a filter in this way?
It's been very long since the question was asked, but if someone searches, I happen to know the solution.
First there is a way to call a filter from inside the vue component. Actually all filters are stored in the
this.$options.filters
collection. So if you want to call a filter from inside the component you should do
...
this.$options.filters.stat(report.past, report.statType)
...
And if you want to do the same from the markup, you should also call filter not like filter (| syntax), but like the component method
<js-widget
v-on:click="this.selectedReportId = key"
:selected="this.selectedReportId == key"
:icon="report.icon"
:stat="report.current | stat report.statType"
:title="report.title"
:tooltip="report.tooltip"
:percent="report.percent"
:description="'Previous value: '+ $options.filters.stat(report.past, report.statType)">
</js-widget>
Or, better, with template string
:description = "`Previous value: ${$options.filters.stat(report.past, report.statType)}`"
The filter is something available outside of the expression you are evaluating – so that's not going to work syntactially
I would use a computed property instead. For more information, see http://vuejs.org/guide/computed.html.

How to access a stored value in PHPUnit_Extensions_SeleniumTestCase

How can I store a value within Selenium-RC (through PHPUnit) and then retrieve/access it later using PHPUnit?
Suppose I run a command like the following in a test:
$this->storeExpression( "foo", "bar" );
If I understand the Selenium API documentation correctly, I could access this data using javascript{storedVars['foo']} using good 'ol fashioned Selenese. It should contain the value "bar".
My question is this: how can I access this javascript{storedVars['test']} expression (or, more generally, javascript{storedVars} in PHPUnit?
For example, here's a simple test I've run:
public function testStorage()
{
$this->open('http://www.google.com/'); // for example
$this->storeExpression( 'foo', 'bar' );
$foo = $this->getExpression('foo');
echo $foo;
}
The output of which is "foo" (among the other standard PHPUnit output), while I expect it should be "bar". It's just giving me back the name of the expression, not its value.
Can anyone with experience in this give me some guidance?
Good posts in this thread, but looks like no 100% working answer so far.
Based on the Selenium reference here
http://release.seleniumhq.org/selenium-core/1.0/reference.html#storedVars
It would seem the correct code syntax would be:
$this->storeExpression( 'bar', 'foo' );
$foo = $this->getExpression("\${foo}");
I haven't tested that exactly, but doing something similar with
$this->storeHtmlSource('srcTxt');
$val = $this->getExpression('\${srcTxt}');
print $val;
did the trick for me.
The PHPUnit Selenium Testcase driver actually understands storeExpression and getExpression; have a look at its source code. You can do
$this->storeExpression('foo', 'bar');
and
$this->getExpression('foo');
As Selenium Stores the expression result in second argument it stores value in "bar" and when u need to call it you should call the stored name to get the expression.
$this->storeExpression( 'foo', 'bar' );
$foo = $this->getExpression("bar");
May this helps you it worked for me.
EDIT :
$evaluated = $this->getEval("regex:3+3");
$expressed = $this->getExpression("regex:3+3");
The First Evaluated will give the evaluated output for expression
and the second will show the expressed output.
The secound is used to verify that the specified expression is genrated or not by the alert.