Re: Apache Velocity : Verify if all substitutions are made
Debraj Manna <[email protected]> Sat, 17 Jun 2023 14:23:28 +0530
| Newsgroups | gmane.comp.jakarta.velocity.user |
|---|---|
| Message-ID | <CAF6DVKOVgpDONJYKfo-LK9RUikBfxr0VUw4LnJ3PeJ6afaj5Dw@mail.gmail.com> |
--000000000000f3cdaa05fe4f7005
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Any suggestions on my last email? The same has been asked in stackoverflow
also.
https://stackoverflow.com/q/76487506/785523
On Wed, Jun 14, 2023 at 9:51=E2=80=AFPM Debraj Manna <subharaj.manna@gmail.=
com>
wrote:
> Strict mode will also suffice for my needs. I am new to Velocity. I am
> setting the runtime.reference.strict to true before init() like below
>
> final var engine =3D new VelocityEngine();
> engine.setProperty("runtime.references.strict", "true");
> engine.init();
>
> I want the Strict mode to be enabled only for some templates. So can this
> be overwritten per context or I have to create two VelocityEngine
> instances one with strict mode enabled and another without strict mode?
>
> Another follow-up question on the same topic, is it possible to also
> check if all keys specified in VelocityContext are valid for a template.
> For example, let's say I have a template, card.vm
>
> card {
> type: CREDIT
> company: VISA
> name: "${firstName} ${lastName}"
> }
>
> If I am then doing something like the below then the above code should th=
row an error as cardNumber is not present in the card.vm
>
>
> VelocityContext context =3D new VelocityContext();
> context.put("firstName", "tuk");
> context.put("lastName", "man");
> *context.put("cardNumber", "1234");*
> StringWriter writer =3D new StringWriter();
> t.merge( context, writer );
>
>
> On Wed, Jun 14, 2023 at 11:57=E2=80=AFAM Stefan Gro=C3=9Fhauser <
> [email protected]> wrote:
>
>>
>> Hello,
>>
>> isn't the "strict mode" already what you want?
>>
>>
>> https://velocity.apache.org/engine/devel/user-guide.html#strict-renderin=
g-mode
>>
>> best regards
>>
>> Stefan
>>
>>
>>
>> * Von: * Debraj Manna <[email protected]>
>> * An: * <[email protected]>
>> * Gesendet: * 13.06.2023 11:38
>> * Betreff: * Apache Velocity : Verify if all substitutions are made
>>
>> Can someone let me know how I can verify a velocity context against a
>> template? Basically, I want to throw some error if all variables are not
>> substituted in the velocity template.
>>
>> For example, let's say I have a velocity template, card.vm
>>
>> card {
>> type: CREDIT
>> company: VISA
>> name: "${firstName} ${lastName}"
>> }
>>
>> The substitution is done like below
>>
>> VelocityEngine velocityEngine =3D new VelocityEngine();
>> velocityEngine.init();
>>
>> Template t =3D velocityEngine.getTemplate("card.vm");
>>
>> VelocityContext context =3D new VelocityContext();
>> context.put("firstName", "tuk");
>> StringWriter writer =3D new StringWriter();
>> t.merge( context, writer );
>>
>> In this case, I want to throw some error specifying that lastName is not
>> replaced in template.
>>
>> Does velocity provide anything for this?
>>
>>
>>
>>
>>
>>
>>
>> *JETZT NEWSLETTER ABONNIEREN:
>> https://hammerbacher.com/newsletter-anmeldung/
>> <https://hammerbacher.com/newsletter-anmeldung/>*
>> *Hammerbacher GmbH Registergericht N=C3=BCrnberg HRB 10908*
>> *Gesch=C3=A4ftsf=C3=BChrer Bernhard Hammerbacher, Ursula Hammerbacher, C=
hristoph
>> Hammerbacher, Andreas Hammerbacher*
>> Hausanschrift
>> Daimlerstra=C3=9Fe 4-6
>> D 92318 Neumarkt Telefon
>> +49(0)9181
>> 2592-0 Telefax
>> +49(0)9181
>> 2592-28 E-Mail
>> [email protected]
>> www.hammerbacher.com
>>
>>
>> Haftungsausschluss / Disclaimer
>> Die Informationen, die in dieser Kommunikation enthalten sind, sind
>> ausschlie=C3=9Flich und allein f=C3=BCr den Empf=C3=A4nger bestimmt. Die=
Verwendung durch
>> Dritte ist untersagt. Die Firma Hammerbacher GmbH ist nur f=C3=BCr die v=
on ihr
>> eingegeben Informationen verantwortlich, jedoch nicht f=C3=BCr die einwa=
ndfreie
>> =C3=9Cbertragung oder im Zusammenhang mit der =C3=9Cbertragung oder dem =
Empfang
>> eingetretene Ver=C3=A4nderungen oder Verz=C3=B6gerungen. Diese E-Mail en=
th=C3=A4lt
>> vetrtrauliche und/oder rechtlich gesch=C3=BCtzte Informationen. Wenn Sie=
nicht
>> der richtige Adressat sind oder diese E-Mail irrt=C3=BCmlich erhalten ha=
ben,
>> informieren Sie bitte sofort den Absender und vernichten Sie diese E-Mai=
l.
>> Das unerlaubte Kopieren sowie die unbefugte Weitergabe dieser Mail ist
>> nicht gestattet.
>>
>>
--000000000000f3cdaa05fe4f7005--