Freemarker has the ability to do text escaping using something like this:
<#escape x as x?html>
Foo: ${someVal}
Bar: ${someOtherVal}
</#escape>
xml, xhtml, and html are all built in escapers. Is there a way to register a custom written escaper? I want to generate CSV and have each individual element escaped and that seems like a good mechanism.
I'm trying to do this in Struts 2 if that matters as well.
You seem to be confusing two concepts here. ?xml, ?xhtml and ?html are string built-ins.
<#escape> OTOH is syntax sugar to save you from typing the same expression over and over again. It can be used with any expression, it's not limited to built-ins.
That said, there's unfortunately no built-in for csv string escaping and there's no way to write your own without modifying FreeMarker source (though if you do want to go this way it's pretty straightforward - take a look at freemarker.core.BuiltIn). Perhaps you can by with ?replace using regex or just write / expose an appropriate method and invoke it in your template.
The Javadoc for HtmlEscaper indicates how to instantiate/register that in code (see the header), so I suspect if you implement your own TemplateTransformModel, and register it in a similar fashion then that should work.
Related
I have a requirement where I need to traverse a hierarchy of Java beans and the hierarchy is different based on the starting point. What would be ideal is if I would be able to use the "dot operator" from JSTL in my Java class.
Then I can have a static map of Strings to describe my hierarchy..something like:
clazz1=attribute1.attribute2
clazz2=attribute3.attribute4
I look up which class and which attributes I need to drill down and go to the root object.
I am coding for it anyway, just checking if BeanUtils etc had such a facility already since seems to me like it can be an useful feature.
You could write some of your code in groovy - it compiles to java bytecode so interoperates perfectly.
I'm looking for Java framework like libconfig. I like its simple human readable configuration files and also that I can create complex setting values composed from arrays, lists, groups.
Is there anything similar for Java (not XML) ?
Looks like a simple JSON-esque format; why not just use JSON?
Commons Configuration supports a similarly-formatted config using PropertyListConfiguration.
A Groovy-based ConfigSlurper might be suitable if you're considering JVM, rather than Java-the-language, solutions. JRuby and JavaScript etc. has similar implementations.
How to customize the freemarker expression, the syntax like the following
<#if name?myExpr>
You can't do that with ?. The point of the ? operator is exactly that the function names come after it are reserved for the FreeMarker Template Language, so new functions can be added to new FreeMarker releases without breaking backward-compatibility.
You can, however, add functions that are called as foo(param). I understand that people would prefer the postfix-call style (param?foo), but that has the compatibility issue mentioned. (I have also recommended param?#foo for this, but AFAIR the idea wasn't popular on the list.)
This question has almost certainly been asked before, but I ask it anyway because I couldn't find an answer.
Generally, is there a utility class of some sort that assists in common String manipulations associated with URL/URIs?
I'm thinking something like Java SE's URL Class, but maybe a little beefier. I'm looking for something that will let you do simple things, like:
Get a List of query string parameters
An "addParameter" method to add a
query string parameter, and it will
take care of adding "&", "?", and "="
where necessary
Also, encoding
parameter values would be ideal...
Let me know, thanks!
There isn't really (oddly enough) any standard that does it all. There are some bits and pieces, usually buried in various util packages:
I've used http://java.net/projects/urlencodedquerystring/pages/Home to decent effect (for extraction of parameters).
Atlassian's JIRA has http://docs.atlassian.com/jira/4.2/index.html?com/atlassian/jira/util/UrlBuilder.html, which I've actually extracted from the jar and used.
On Android, http://developer.android.com/reference/android/net/Uri.Builder.html is a Uri builder that works pretty well as far as building a url with ease.
And finally, in a classic case of history repeating itself: A good library to do URL Query String manipulation in Java.
I'd really just rip out the android.net.Uri.Builder class and pair that with the urlencodedquerystring class and then carry those around with you, but this does seem like a good candidate for an Apache commons package.
I personnaly like UriBuilder from jax-rs
This does not answer OP's question directly (i.e. it's not a generic, all-around library for URL manipulation), but: if you're going to be using Spring anyway, you might as well consider the ServletUriComponentsBuilder and UriComponentsBuilder classes (see here and here for javadocs).
I believe they are bundled with the spring-web dependency. IMHO, these offer quite a few convenient utility methods for working with URIs, URLs and query parameters.
I'm writing a custom flex file to generate a lexer for use with JSyntaxpane.
The custom language I need to lex has different states that can be embedded into each other in a kind of stack.
I.E you could be writing an expression that has a single quoted string in it and then embed another expression within the string using a special token eval(). But you can also embed the expression within a double quoted string.
eg:
someExpressionFunction('a single-quoted string with an eval(expression) embedded in it', "a double-quoted string with an eval(expression) embedded in it")
This is a simplification, there are more states than this, but assuming I need to have different states for DOUBLE_STRING and SINGLE_STRING it adequately describes my situation.
What's the best way to ensure I return to the correct state upon closing the eval expression (i.e return to DOUBLE_STRING if I was in double quotes, SINGLE_STRING if I was in single quotes)
The solution I've come up with, which works, is to keep track of state using a Stack and some custom methods to use in lieu of using yybegin to start a different state.
private Stack<Integer> stack = new Stack<Integer>();
public void yypushState(int newState) {
stack.push(yystate());
yybegin(newState);
}
public void yypopState() {
yybegin(stack.pop());
}
Is this the best way to achieve this? Is there a simpler built-in function of JFlex I can leverage or a best practice I should know about?
I think that's one very good way of doing it. I actually needed some similar feature to add Groovy GString, Python like String and some HTML to JavaDocs.
What I would also like to add is a Lexer calling a Lexer to parse sub sections. Something like JavaScript embedded in HTML. But I could not get the time to do it.
I like StackOverflow, but just wondering why didn't you post this on JSyntaxPane's issues?