Re: Custom encoding a string value
"martijn.list" <[email protected]>
| Newsgroups | gmane.comp.web.freemarker.user |
|---|---|
| Message-ID | <[email protected]> |
Hi Daniel,
Thanks for the help. I have implemented TemplateMethodModel and used
Apache commons/codec to do the quoted-printable encoding. As I require
the functionality with the current release I have not added it to the
upcoming version. I'll try to find some time to see if I can add it.
Thanks,
Martijn Brinkers
The QP function for Freemarker:
import java.util.List;
import mitm.common.locale.CharacterEncoding;
import org.apache.commons.codec.EncoderException;
import org.apache.commons.codec.net.QuotedPrintableCodec;
import freemarker.template.SimpleScalar;
import freemarker.template.TemplateMethodModel;
import freemarker.template.TemplateModelException;
/**
* A 'function' for Freemarker that allows you to encode a string to
quoted-printable
*
*/
public class QuotedPrintableEncodeMethod implements TemplateMethodModel
{
public static String METHOD_NAME = "qp";
@SuppressWarnings("unchecked")
public Object exec(List arguments)
throws TemplateModelException
{
if (arguments.size() != 1 && arguments.size() != 2) {
throw new TemplateModelException(METHOD_NAME + " needs 1 or
two arguments.");
}
try {
String input = (String) arguments.get(0);
String charset = arguments.size() == 2 ? (String)
arguments.get(1) : CharacterEncoding.UTF_8;
QuotedPrintableCodec codec = new QuotedPrintableCodec(charset);
return new SimpleScalar(codec.encode(input));
}
catch (EncoderException e) {
throw new TemplateModelException("Quoted Printable encoding
failed", e);
}
}
}
Daniel Dekany wrote:
> Tuesday, December 1, 2009, 2:18:23 PM, martijn.list wrote:
>
>> Hi,
>>
>> I would like to add my own string encoding to Freemarker.
>>
>> I would like to encode a string to quoted-printable format. There are
>> built-in encodings like ?html. Is it possible to add for example ?QP
>> (for quoted printable)?
>>
>> What is the best approach for me?
>
> Write a #function or a TemplateMethodModelEx for it, and put that in a
> file that you #import (or #include) everywhere. Or, write a
> TemplateMethodModelEx and set it as shared variable in the
> Configuration object. In any case, you will be able to do something
> like this: qp(theExpression), but not theExpression?qp, since ?stuff
> is reserved for core stuff (mostly that's why it exist at all).
>
> Another possibility is that you contribute and then ?qp can be part of
> the next FreeMarker release (and immediately of he nightly build).
> Note that QP is a charset-aware algorithm, so it need a charset
> parameter that defaults to .output_encoding, just like in the case of
> ?url.
>
>> Kind regards,
>>
>> Martijn Brinkers
>>
>>
>> ------------------------------------------------------------------------------
>> Join us December 9, 2009 for the Red Hat Virtual Experience,
>> a free event focused on virtualization and cloud computing.
>> Attend in-depth sessions from your desk. Your couch. Anywhere.
>> http://p.sf.net/sfu/redhat-sfdev2dev
>> _______________________________________________
>> FreeMarker-user mailing list
>> [email protected]
>> https://lists.sourceforge.net/lists/listinfo/freemarker-user
>>
>
--
Djigzo open source email encryption
------------------------------------------------------------------------------
Join us December 9, 2009 for the Red Hat Virtual Experience,
a free event focused on virtualization and cloud computing.
Attend in-depth sessions from your desk. Your couch. Anywhere.
http://p.sf.net/sfu/redhat-sfdev2dev