How to migration JXLS from version 1 to 2 - java

JXLS 2 is not backward compatible with version 1. There are no upgrade instructions and while I can get things working with version two I'm having two issues.
Version 1 did not require use of comment tags, but now I cant get the output to generate without them.
Java code must specify which worksheet location to write the results, in version 1, output was written on top of the template which did not require duplication of template settings in Excel and Java code.
If I cannot find a workaround to these two issues, I'll have to go change every single template to use comments. But worse, I'll also have to make my java code aware of the template worksheet layout. I dont understand why this is the default behavior. Is there any way to get version 2 to behave more like version 1?

Version 1 did not require use of comment tags, but now I cant get the output to generate without them.
This is not completely true. You can choose not to use XlsCommentAreaBuilder. In this case you have 3 options
Use XmlAreaBuilder (probably not the way you want)
Use Java API to build the commands (probably not the way you want)
Create your own Jxls1TagCommandAreaBuilder which will build the commands from the Jxls-1 tag notation (and contribute it to back to jxls core)
The 3 option is probably the best because you will be able to use Jxls-1 templates with Jxls-2 without changes. Sure it is not trivial but should be possible.
Java code must specify which worksheet location to write the results, in version 1, output was written on top of the template which did not require duplication of template settings in Excel and Java code.
To get the Jxls-1 behaviour here you can just do something like this
for (Area xlsArea : xlsAreaList) {
xlsArea.applyAt(
new CellRef(xlsArea.getStartCellRef().getCellName()), context);
}
So you simply apply an area at its starting cell.
If you wish this to be a default behavior you can raise an improvement in jxls issue tracker

Related

Java: Parse JavaScript & Flag Errors

I've been having terrible luck trying to get this to work, so I'm hopeful someone can help here.
In Java, I need to be able to take an HTML page with JavaScript within it and detect any JavaScript errors without, preferably without executing the JavaScript code.
I found this article:
Javascript parser for Java
And I've attempted to figure out how I'm supposed to use Caja to do this, but I'm having a difficult time finding any documentation with working examples of anything close to what I'm doing.
As a result I took a look at Nashorn also referenced in that article. I found a few examples which show how to execute JavaScript code from Java, but this doesn't process the whole HTML page. Even then, the execution doesn't seem to include the ability to validate common JavaScript functions (e.g. It hadn't heard of "alert").
Can anyone recommend something that might be able to do what I want, and point me in the right direction for their documentation or give me an example?
jshint as a standalone product seems to be a good fit for this:
it can run in java inside rhino (see https://github.com/jshint/jshint/)
a nodejs package exists (see https://www.npmjs.com/package/jshint)
it works with nashorn but it's quite tricky
I will only cover the technical difficulties around 3rd solution as I finally managed to make it work too...
Spoiler alert: "alert()" is not detected yet... Solution nb 2 will help there...
You first need to grab this specific release of jshint: https://github.com/jshint/jshint/releases/tag/2.4.4
Anything later than v2.7.0 will fail for now and I personally gave up patching intensively prototypes and namespaces... Releases from v2.4.4 until v2.6.3 work without modification but are limited in functionalities.
In the release notes, it's specifically written that "support for the Nashorn JavaScript engine" is working on this release. I'm using JDK8 nashorn 1.8.0_45 for this test.
Next step is to extract from this release this single file jshint-2.4.4/dist/jshint-rhino.js
Now you need to run nashorn/jjs in scripting mode and you need to be specific about the single file you wish to verify. In solution 2 (nodejs based) you can do multiple files or a complete hierarchy below a folder...
Create a simple file file.js:
function(){}
Now run the following command (please note the presence of -- ):
jjs -scripting jshint-rhino.js -- file.js
This will give you the following output:
Missing name in function declaration. (file.js:1:9)
> function(){}
So this covers the how to run jshint in a simple manner with nashorn... With the 3rd solution, at least you can find missing semicolons and several typical errors. But it's not a silver bullet and to me it's not a real alternative.
My personal preference would be to stick to solution 2 only. If you've the possibility to install either nodejs or iojs on your dev platform, go and grab https://www.npmjs.com/package/jshint. Not only will you be able to do more than the 3rd solution, you'll also be able to configure a jshintrc file as described at http://jshint.com/docs/

Check first implementation of a feature in java

I wanted to use a particular option -DentityExpansionLimit in java using a jdk and I was unsuccessful on an old version. I there anyway I can see which was the version this option was first implemented on? I was unable to find any answers and was hoping there is like a guide where an option was first implemented or even all the new features implemented for each version. Please let me know.
Seems like it was first introduced in Java 1.4.
Reference Links:
http://docs.oracle.com/javase/7/docs/technotes/guides/xml/jaxp/JAXP-Compatibility_160.html
http://www.oracle.com/technetwork/java/javase/relnotes-139182.html
As per the link:
New system property to limit entity expansion
The entityExpansionLimit system property lets existing applications constrain the total number of entity expansions without recompiling the code. The parser throws a fatal error once it has reached the entity expansion limit. (By default, the limit is set to 64000.)
To set the entity expansion limit using the system property, use an option like the following on the java command line: -DentityExpansionLimit=100000
I think your best option is to use google and check the release notes of each version.
Example: http://www.oracle.com/technetwork/java/javase/jdk7-relnotes-429209.html
In your case, seems like your flag comes from JSDK v1.4.2

Override existing Eclipse plugin extension

I have an existing Eclipse plugin that provides an extension point. The plugin uses standard Eclipse mechanism to find the extensions. In this plugin's code, following code is used to get the extension.
IConfigurationElement[] config = Platform.getExtensionRegistry()
.getConfigurationElementsFor(extensionPoint);
if (config.length > 0) {
return config[0];
}
As you can see in the code, only the first found extension is used. This plugin already provides an extension and this extension is used in the default case.
Now I need to override the behavior of the default extension, so I created a new plugin and extends the same extension point. But it turns out that the default extension is always the first one in the IConfigurationElement array, so it's always picked up.
How can I make my own plugin appear first in the found IConfigurationElement array, then my own plugin is used instead of the default one?
The existing plugin is written by others and I don't want to make changes to it until it's absolutely necessary.
I'd say this is a bad way to get extensions from an extension point, either way. If they just want the use the pluginsystem to load a specific extension they have created, they could use the getConfigurationElementsFor(String namespace, String extensionPointName, String extensionId) method instead and close off the possibility for others to use the extension point. As of now there is no sure way of knowing which extension they will get. Chances are, there are instances in the code later on that assumes they will get their extension and when they don't get the extension they expect, Mr ClassCastException comes knocking on the door. (Had a bug like this in a system once)
Of course the best way is to change the code to handle many extensions!
But to your question; I dont know how the ExtensionRegistry fills the array, the API doesnt say. Perhaps there is a way to perhaps set a specific version of your extension that will allow it to be placed first in the array. You would have to look in the code of the ExtensionRegistry to know exactly how the extensions are found. I think it may be in alphabetical order, but im not sure.
Another way is to overload the existing plugin with your plugin and replace functionality. A very dirty approach, but in some cases it is doable. See one of my questions regarding this

Templated correspondence generation using Java

I am looking for a way to create pixel perfect, paginated, styled correspondence in a Java web application.
The requirements for this functionality are
- Ability to define outer/ master template with header/ footer/ logo etc...
- Ability to define inner content template where specific variables/ fields can be substituted with instance values at run time - e.g. employeeName. employeeAddress. The content template would be defined online using rich text editor or something similar
- Supports tables/ list
I believe iText does most/ if not all. Other options that I have considered is BIRT. Are there other/ better choices for this purpose?
Thanks
I have done something like with iText, although our case did not use an online text editor (we had a small number of templates and they were defined as part of the development cycle)
[Note: We did this under an older version of iText when it was still LGPL rather than Affero GPL]
The results were excellent, but in order to get it pixel-perfect we had to do a lot of work ourselves. We did manual layout and pagination - including manual text wrapping, etc. The layout tools that iText provides looked good, but they didn't give us the pixel-perfect control that we needed for the use case we had.
Have a look at iReport.

How to apply .diff patch with Eclipse to HtmlUnit

I'm trying to patch HtmlUnit with the patches at the bottom of this page. I can see how to apply patches from Eclipse, but I can't figure out how to apply these particular patches. I had the JARs initially, but then I realised I'd need to source, so I obtained those, but still no luck.
In the patch I see:
--- a/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/WebClient.java
and etc. but what is up with the 'a/htmlunit'? I have the packages and "src/main/java/com/gargoylesoftware/htmlunit/WebClient.java" (for example) is easily locatable, but not a/htmlunit. I can't really figure out how to apply this.
Can anyone help?
Usually, patches come with prefixes, kind of like putting the previous version in directory a, and the new version in directory b and just diffing them to produce the patch, only with version control systems, you don't really need to do that anymore. Most still do I suppose (at least git does by default).
Anyway, patch-tools have an option to skip a number of directory levels as prefix, in your case you'd have to skip two (a and htmlunit). Unfortunately, I don't know how this is done in eclipse, with vanilla unix patch you would need to add a -p2 parameter. Poke around the UI, it ought to be there somewhere.

Categories