Is there is possibility of perform xpath eval with custom value instead of having xml.
Example:
count(/departmemt/employees) > 10
Here, i will provide the values for /department/employee and i want to use xpath libraray in java to take care of doing the evaluation.
It is something like the user exposed method, String getValue (String operand)...
here getValue method should get called from xpathEngine and i will take care of providing the value for each operand.
Please help me if there is any possibility of doing this.
Thanks
Durai
For use XPath-like structures in java can be used commons-jxpath
http://commons.apache.org/proper/commons-jxpath/
Related
I have a json template as below
{
"Account_Number" : "${accNo}"
}
I want to use a user-defined directive which is basically a Java code to check if accNo is greater than 0. If not, I want to set the value as 0000.
I was reading here ( https://freemarker.apache.org/docs/pgui_datamodel_directive.html) that it is possible to write Java code by implementing the TemplateDirectiveModel interface. However, I was unable to retrieve the value of accNo.
Is it possible to achieve the above using user-defined directive? If yes, how?
You want something like <#accNoJson value=accNo />, then you can get it from params parameter of TemplateDirectiveModel.execute. If you want something like <#accNoJson/> (which is a bit odd), then you can get it with env.getVariable("accNo"), where env is the 1st parameter of TemplateDirectiveModel.execute.
The most typical approach would be ${accNoJson(accNo)}, in which case you should implement TemplateMethodModelEx.
I'm using Jayway JSONPath.
Given a JSON document having nodes with the same name at different structure levels, how would I select only those nodes that are terminal nodes, i.e. having only text or no content?
XPath would allow not(child::*) as a predicate, but I can't see a JSONPath equivalent.
Unfortunately, no JSONPath implementation (as of now) offers such an operation. However, some of the more advanced implementations that expanded on Goessner's reference have operations that get close to this.
One workaround is to use check the type of a node, if possible. For instance this is possible in the JavaScript JSONPath-Plus implementation using Type selectors for JSON types: e.g. #null(), #boolean(), #number(), #string(), #array(), #object()
#integer() and others. This allow us for instance to get only numeric values:
$..*#number()
Combined with a more meaningful path selection we might get close. Nonetheless, this will not yield terminal values only but at least avoids array and object type properties.
Another workaround that is should work with basic data types is to use the regex matcher available in quite a few implementations (like JayWays, many JavaScript implementations, etc) to interpret the type of a node, e.g. again let's say numeric values
$..[?(#.price =~ /[0-9]+\.?[0-9]*/)]
Again, this will not give you terminal values only but avoids array and object type properties.
I want get to the variables(${colour}) list and then pass the variables dynamically.Is it possible to get the variables list.
Thanks
You'll have to write code to do that yourself.
Three approaches:
Use TextUtils to get just the text, and parse for ${
Use XPath
Use Traverse utils
All 3 of these are documented further in docx4j's Getting Started.
Method 1 is the simplest, I guess.
Could your variables be in headers/footers/footnotes/endnotes? If so, note these are separate "parts", and would also need to be handled.
Assuming some xml like
<foo>
<bar>test</bar>
</foo>
Evaluating an expression with returnType = String like
/foo/bar
will return "test". However, I'd like to get the serialized xml instead, so something like
<bar>test</bar>
should be returned instead. As I can not check for the returnType in java's xpath implementation (xerces), I cannot simply get an object as result and if it indeed is a node, convert it to serialized xml.
Note: I don't know whether the expression will actually return a node, a string, a number or whatever so I cannot provide an appropriate return type to the eval function except string which, as my problem states, returns the text content and not the serialized xml.
So I am curious -> is there either a java- or (preferred) a xpath-way (function?) to get serialized xml for type string instead of the text children of the selected node?
thanks!
Alex
use the xpath return type XPathConstants.NODE and then you can serialize the returned Node yourself.
Now, you are right to observe that it's difficult to discover the return type of the result; this is a real design weakness of JAXP.
If it's a problem to you, consider using Saxon's s9api interface, which returns XdmValue objects whose type you can interrogate; you also get XPath 2.0 access as a bonus.
As Michael Kay answered, this is difficult in JAXP (the native Java interface).
In Mr Kay's Saxon library's s9api API (see Evaluating XPath Expressions using s9api), once you've called XPathSelector.evaluate() or XPathSelector.evaluateSingle() you can get the XML serialisation by calling XdmValue.toString().
However, if the XPath selected an attribute (e.g. //#name) you will still get the XML serialisation, e.g. name="value". You can call XdmItem.getStringValue(), but for elements that method will return the same values you're already seeing - the textual content of the element, not the serialisation. I've posted separately about how to distinguish between attributes and elements returned from Saxon s9api.
I have a form with (at the moment) two fields and submit the following:
capture.id = 213
capture.description = DescriptionText
The target object 'capture' is immutable and I would like to provide a type converter to take both values and call the constructor. What I cannot seem to do is get by TypeConverter to be invoked.
If the input is simply:
capture = foo
Then the type converter is called, but obviously this isn't much use, is there away to make a ognl delegate the rest of the rest of the type conversation to me, perhaps passing in a Map of the parameters?
Any ideas? Is this even possible in struts2
versions: struts 2.0.14 & ognl 2.6.11
EDIT: I've done a bit of reading on this and my next attempt seemed to me to be a good plan. My theory was that using the Map syntax would make Ognl convert the values to a map and then call my converter with that map to convert it to my value.
capture[id] = 213
capture[description] = DescriptionText
Nope that doesn't seem make any difference at all.
The way I did this was to have the following in the JSP:
<s:textfield name="capture" value="capture.id" />
<s:textfield name="capture" value="capture.description" />
In the type converter, the String[] values parameter of the convertFromString method will contain both values needed to construct a new immutable capture. Provided that you are consistent with the text field ordering (or better yet, encapsulate it in a tag file), you can use the indexes of the values array to reliably get the appropriate field of the capture object.
The one weird part about this approach is that the convertToString method doesn't really do anything for you. You can return either id or description (or concatenate them together), but since you are using the values attribute in the JSP, it doesn't matter.
It seems the that the answer is no you can't do that with struts2.
I've posted this question on the struts2 mailing list and it seems that it just isn't possible to have multiple fields be presented to a TypeConverter.
The alternative solution suggested is to have mutable object with setters and then have some form of 'petify' method to prevent any future changes.
For my project I've actually implemented another struts Interceptor to implement my custom parameter binding behaviour.