Xtend Template Notation - code-generation

i have a Problem with the Xtend Template Notation.
I want to do «i=i+1» in a template method
def generateSomething() '''
...
«i=i+1»
...
'''
The «i=i+1» is obviously only to count i higher but it also shows the value of i in the generated file too. But i dont want that. Is there a way to solve this problem without changing the method?

You could use a block expression :
def generateSomething() '''
...
«{i=i+1 ''}»
...
'''

If it is possible to used AtomicInteger instead of an int for your code then this should work.
val i = new AtomicInteger
val generated = '''
...
«i.andIncrement»
...
'''

«FOR i : 0..iMaxInclusive»
some text «somelist.get(i)» more text
«ENDFOR»

Related

Finding partial attribute value in Katalon

Trying to find a partial attribute value. Full value is no problem.
I have h1 class="a b c" and want to find out, whether this h1 has a as a class attribute.
Trying WebUI.verifyMatch(findTestObject('mytest/mytest-h1'),'a', 'a.*', false, FailureHandling.STOP_ON_FAILURE) and fails on finding.
Also answered via this Katalon Forum post (Apologies if the link is broken, in the future).
As per Mate Mrše's answer you can also try the following:
def attribute = WebUI.getAttribute(findTestObject('mytest/mytest-h1'), 'class')
boolean doesAttributeExist = attribute.contains('a')
if (!doesAttributeExist) {
// Add some logic/interaction
}
Since you added FailureHandling.STOP_ON_FAILURE the test will fail regardless of the condition.
Should you want the test to continue rather use FailureHandling.OPTIONAL
Try this:
def attribute = WebUI.getAttribute(findTestObject('mytest/mytest-h1'), 'class')
assert attribute.contains('a ')
Alternatively, create an object using the CSS class as the locator and verify the element exists:
assert WebUI.verifyElementClickable(findTestObject('mytest/mytest-h1-a')) == true

Using variables in js.eval() function in scala js

I am using javascript to fetch data from a text box using js.eval() function like :
val amount=js.eval("document.getElementById('amount').value;")
It is working fine when I am writing the id directly i.e. 'amount' in this case. But I want to create a function which will receive the id and then use the variable id.
I tried using '$' sign also like :
def getAmount(amount_id:String):Unit={
println("Amount is : "+js.eval(s"document.getElementById(${id}).value;"))
But it is not working. Any suggestions ?
Do not use js.eval. Use the interoperability features of Scala.js instead:
import org.scalajs.dom
import dom.html
def getAmount(amount_id:String): Unit = {
val input = dom.document.getElementById(amount_id).asInstanceOf[html.Input]
println(s"Amount is : ${input.value}")
}

itextsharp: unable to cast from FilteredRenderListener to ITextExtractionStrategy

I want to extract text from a specified pdf area with itextsharp. I know there is an example http://sourceforge.net/p/itextsharp/code/HEAD/tree/book/iTextExamplesWeb/iTextExamplesWeb/iTextInAction2Ed/Chapter15/ExtractPageContentArea.cs#l35. The core code is like this:
RenderFilter[] filter = {new RegionTextRenderFilter(rect)};
ITextExtractionStrategy strategy = new FilteredRenderListener(new LocationTextExtractionStrategy(), filter);
string text = PdfTextExtractor.GetTextFromPage(reader, i, strategy);
However, vs2012 shows me that "cannot convert implicitly from FilteredRenderListener to ITextExtractionStrategy ". I tried to do explicit conversion. But failed. Could anyone help me? Do I use a wrong itextsharp version? Thanks so much!
Your line looks like this:
ITextExtractionStrategy strategy = new FilteredRenderListener(new LocationTextExtractionStrategy(), filter);
but the corresponding lines from the sample you reference look like this:
ITextExtractionStrategy strategy;
...
strategy = new FilteredTextRenderListener(
new LocationTextExtractionStrategy(), filter
);
I.e. the sample uses a FilteredTextRenderListener while you only use a FilteredRenderListener. The former actually extends the latter to also implement ITextExtractionStrategy.
Thus, simply use FilteredTextRenderListener instead of FilteredRenderListener.

Drools/Scala - Create var/val inside DRL

I'm trying to use Drools with Scala and i would like to know if it is possible to call a chain of events and create var/val when the function has a return.
Here is what i'm trying but i'm stuck:
import com.models.*
import com.service.*
rule "First Fule"
when
person:Person(name == 'aa')
then
//Here should return a string
//and i should set this string
//something like:
//var x = new Person(ServiceLongDong.sayHello(), person.age, person.name)
//or var y = ServiceLongDong.sayHello();
ServiceLongDong.sayHello();
ServiceLongDong.finish(x);
end
Is possible to create varl/vals and pass it to another function?
Thank in advance.
Rules aren't functions (or methods) and don't "return" values or objects. The right hand side is just Java code. You can call static methods, but stick to correct Java syntax:
Person p = new Person(ServiceLongDong.sayHello(),
person.age, person.name);
ServiceLongDong.sayHello();
ServiceLongDong.finish(x);
This can't be correct Java if ServiceLongDong is a class:
... = ServiceLongDong().sayHello();

Cannot access the parameter of a Menu.param from a Lift Snippet

I'm trying to extract the parameter from a Lift Menu.param within a snippet so that I can use it to create a named Comet. However, I get a NullPointerException when I try to pass the parameter to the snippet using SnippetDisptach in my Boot.scala, as suggested here:
http://comments.gmane.org/gmane.comp.web.lift/44299
I've created the Menu item as follows:
object AnItemPage {
// create a parameterized page
def menu = Menu.param[Item]("Item", "Item",
s => fetchItem(s), item => item._id.toString) / "item"
private def fetchItem(s:String) : Box[Item] = synchronized {
ItemDAO.findById(ObjectId.massageToObjectId(s))
}
}
I've added the menu to SiteMap. I've also created a Snippet which I would like to pick up the Item parameter. (I'm using fmpwizard's InsertNamedComet library here):
class AddCometItemPage(boxedItem: Box[Item]) extends InsertNamedComet with DispatchSnippet{
val item : Item = boxedItem.openOr(null)
override lazy val name= "comet_item_" + item._id.toString
override lazy val cometClass= "UserItemCometActor"
def dispatch = null
}
My next step is to crate an instance of this class as demonstrated by David Pollak here:
http://comments.gmane.org/gmane.comp.web.lift/44299
This is what I have added to my Boot.scala:
LiftRules.snippetDispatch.append {
case "item_page" => new AddCometItemPage(AnItemPage.menu.currentValue)
}
My item.html references this snippet:
<div class="lift:item_page">
I get the following null pointer exception when I compile and run this:
Exception occurred while processing /item/5114eb4044ae953cf863b786
Message: java.lang.NullPointerException
net.liftweb.sitemap.Loc$class.siteMap(Loc.scala:147)
net.liftweb.sitemap.Menu$ParamMenuable$$anon$9.siteMap(Menu.scala:170)
net.liftweb.sitemap.Loc$class.allParams(Loc.scala:123)
net.liftweb.sitemap.Menu$ParamMenuable$$anon$9.allParams(Menu.scala:170)
net.liftweb.sitemap.Loc$class.net$liftweb$sitemap$Loc$$staticValue(Loc.scala:87)
net.liftweb.sitemap.Menu$ParamMenuable$$anon$9.net$liftweb$sitemap$Loc$$staticValue(Menu.scala:170)
net.liftweb.sitemap.Loc$$anonfun$paramValue$2.apply(Loc.scala:85)
net.liftweb.sitemap.Loc$$anonfun$paramValue$2.apply(Loc.scala:85)
net.liftweb.common.EmptyBox.or(Box.scala:646)
net.liftweb.sitemap.Loc$class.paramValue(Loc.scala:85)
net.liftweb.sitemap.Menu$ParamMenuable$$anon$9.paramValue(Menu.scala:170)
net.liftweb.sitemap.Loc$$anonfun$currentValue$3.apply(Loc.scala:114)
net.liftweb.sitemap.Loc$$anonfun$currentValue$3.apply(Loc.scala:114)
net.liftweb.common.EmptyBox.or(Box.scala:646)
net.liftweb.sitemap.Loc$class.currentValue(Loc.scala:114)
net.liftweb.sitemap.Menu$ParamMenuable$$anon$9.currentValue(Menu.scala:170)
bootstrap.liftweb.Boot$$anonfun$lift$8.apply(Boot.scala:107)
bootstrap.liftweb.Boot$$anonfun$lift$8.apply(Boot.scala:106)
net.liftweb.util.NamedPF$$anonfun$applyBox$1.apply(NamedPartialFunction.scala:97)
net.liftweb.util.NamedPF$$anonfun$applyBox$1.apply(NamedPartialFunction.scala:97)
net.liftweb.common.Full.map(Box.scala:553)
net.liftweb.util.NamedPF$.applyBox(NamedPartialFunction.scala:97)
net.liftweb.http.LiftRules.snippet(LiftRules.scala:711)
net.liftweb.http.LiftSession$$anonfun$net$liftweb$http$LiftSession$$findSnippetInstance$1.apply(LiftSession.scala:1506)
net.liftweb.http.LiftSession$$anonfun$net$liftweb$http$LiftSession$$findSnippetInstance$1.apply(LiftSession.scala:1506)
net.liftweb.common.EmptyBox.or(Box.scala:646)
net.liftweb.http.LiftSession.net$liftweb$http$LiftSession$$findSnippetInstance(LiftSession.scala:1505)
net.liftweb.http.LiftSession$$anonfun$locateAndCacheSnippet$1$1$$anonfun$apply$88.apply(LiftSession.scala:1670)
net.liftweb.http.LiftSession$$anonfun$locateAndCacheSnippet$1$1$$anonfun$apply$88.apply(LiftSession.scala:1669)
Has anybody any idea where I'm going wrong? I've not been able to find a lot of information on Menu.param.
Thank you very much for your help.
f
I have never tried what you are doing, so I am not sure the best way to accomplish it. The way you are using the Loc Param, you are extracting a variable from a URL pattern. In your case, http://server/item/ITEMID where ITEMID is the string representation of an Item, and which is the value that gets passed to the fetchItem function. The function call will not have a value if you just arbitrarily call it, and from what I can see you are requesting a value that is not initialized.
I would think there are two possible solutions. The first would be to use S.location instead of AnItemPage.menu.currentValue. It will return a Box[Loc[Any]] representing the Loc that is currently being accessed (with the parameters set). You can use that Loc to retrive currentValue and set your parameter.
The other option would be to instantiate the actor in your snippet. Something like this:
item.html
<div data-lift="AnItemPage">
<div id="mycomet"></div>
</div>
And then in your AnItemPage snippet, something like this:
class AnItemPage(item: Item) {
def render = "#mycomet" #> new AddCometItemPage(item).render
}
I haven't tested either of those, so they'll probably need some tweaking. Hopefully it will give you a general idea.