How to read variables from the word document using docx4j? - java

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.

Related

Apache Freemarker - How to use Java code to compare value of a parameter

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.

Is there a Parameter Tree implementation in Java?

Java program takes a long list of inputs(parameters), churns a bit and spits some output.
I need a way to organize these parameters in a sane way so in the input txt file I want to write them like this:
parameter1 = 12
parameter2 = 10
strategy1.parameter1 = "goofy"
strategy2.parameter4 = 100.0
Then read this txt file, turn it into a Java object I can pass around to objects when I instantiate them.
I now pyqtgraph has ParameterTree which is handy to use; is there something similar in Java? I am sure others must have had the same need so I don't want to reinvent the wheel.
(other tree structures would also be fine, of course, I just wanted something easy to read)
One way is to turn input.txt into input.json:
{
"parameter1": 12,
"parameter2": 10,
"strategy1": {
"parameter1": "goofy"
},
"strategy2": {
"parameter4": 100.0
}
}
Then use Jackson to deserialize input.json into one of these:
A Map<String, Object> instance, which you could navigate in depth to get all your parameters
An instance of some class of your own that mimics input.json's structure, where your parameters would reside
A JsonNode instance that would be the root of the tree
(1) has the advantage that it's easy and you don't have to create any class to read the parameters, however you'd need to traverse the map, downcast the values you get from it, and you'd need to know the keys in advance (keys match json object's attribute names).
(2) has the advantage that everything would be correctly typed upon deserialization; no need to downcast anything, since every type would be a field of your own classes which represent the structure of the parameters. However, if the structure of your input.json file changed, you would need to change the structure of your classes as well.
(3) is the most flexible of all, and I believe it's the option that is closest to what you have in mind, nonetheless is the most tedious to work with, since it's too low-level. Please refer to this article for further details.

Plugin for changing declaration to be java collection interfaces rather than specific implementation

I would like to know whether there are any plugins available to change the Declarations into Java collection Interfaces such as "List" rather than specific implementation classes such as "LinkedList". This is because I need to change for a very huge number of files!!!
Any help is appreciated. Thanks in advance!
Search and replace with regular expressions should do this for you. Use the Search->File menu option and set the file name pattern to *.java. The containing text can be something like:
ArrayList (\w+) \=
Click the Replace button and in the next dialog use:
List $1 =
That will replace ArrayList with List but maintain the field name. You would have to do the same for other collection types, and possibly use another search and replace to add missing imports.

XPath with custom value for operand instead of xml

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/

sphinx facet search: 'with_all' analogue for php/java

I have a doc set with MVA and i need to filer docs that have all required attrs (let's say, i need all 'news' doc having both 'java' and 'oracle' tags, assume i have tags ids).
at ThinkingSphinx (http://pat.github.com/ts/en/searching.html#filters) i found a usefull notaion:
For matching multiple values in a multi-value attribute, :with doesn’t
quite do what you want. Give :with_all a try instead:
Article.search 'pancakes', :with_all => {:tag_ids => #tags.collect(&:id)}
that, as far as i guess, allows to filter docs having ALL the provided attributes, instead of getting docs, having ANY of provided attr list for SetFilterRange usage.
Can anyone suggest soluion at least in terms of standard PHP interface? hope i'll be able to transform it to java.
Multiple calls to setFilter are ANDed. Where as you note specifing multiple ids to one call are ORed.
$cl->setFilter('tag_ids',array($tag_id1));
$cl->setFilter('tag_ids',array($tag_id2));
$cl->setFilter('tag_ids',array($tag_id3));
Sorry cant help with the java syntax.

Categories

Resources