Is there a native way to encode/escape HTML in Java? I specifically ask for native, as using the Apache Commons is not an option. My application uses modules/is compiled to a runnable image, so using the non-module compatible Apache Commons libraries is not an option. It just seems wild to be that something this basic wouldn't be included, but I have not been able to find anything so far. Any insight appreciated.
"privilege" comes from the Latin words for "private" and "law" (legal) and dates to feudal times. To "claim privilege" meant that you were above the laws that applied to the common people.
Norm Radder wrote:The URLEncoder class does something like that.
I found this one, but it unfortunately only has encoding specifically for URLs. It does not take HTML unsafe characters and re-encode them for display on web pages. The functionality that I'm looking for would be the same as is offered by StringEscapeUtils.escapeHtml() from apache-commons. Like I said in the original post though, using the apache commons class is not an option for my project.
Ron McLeod wrote:Why isn't Apache Commons Text an option?
My project requires that libraries have compatibility with the module system. I use jpackage to create platform specific images/installers for, and in that particular case, using the automatic module feature is unfortunately not an option.
I think I'm probably just going to go with the option of keeping track of HTML unsafe characters and replacing them myself, as suggested by Carey for the moment.
I have used module info injection for a few libraries that I absolutely cannot do without, but I am limiting that to cases where there is no other work around. It seems like most commonly used libraries are slowly coming fully into Java9+ land, so I’m just trying to be patient there rather than use a method that requires later refactoring, or makes the maven dependencies less standard.
Ron McLeod wrote:Could you use a Maven plugin like ModiTect to create the module-info.java descriptors that you may need?
ModiTect works fine for your own libraries, but commons-text isn't ours to update.
However, I think that commons-text should work just fine. It's an automatic module, yes, but it defines its module name in the Manifest. It has no dependencies that I know of, it doesn't need to hide any package, and doesn't use or provide any service that I know of.