Re: Apache Velocity : Verify if all substitutions are made

Claude Brisson <[email protected]> Sat, 17 Jun 2023 12:13:02 +0200
Newsgroups gmane.comp.jakarta.velocity.user
Message-ID <[email protected]>
The strict mode is set globally, you would need two instances of Velocity.

Another direction is disabling the strict mode, and using an 
InvalidReferenceEventHandler, which lets you decide what to do on a case 
by case basis.

   Claude

On 17/06/2023 10:53, Debraj Manna wrote:
> 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 PM Debraj Manna <[email protected]>
> 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 = 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 throw an error as cardNumber is not present in the card.vm
>>
>>
>> VelocityContext context = new VelocityContext();
>> context.put("firstName", "tuk");
>> context.put("lastName", "man");
>> *context.put("cardNumber", "1234");*
>> StringWriter writer = new StringWriter();
>> t.merge( context, writer );
>>
>>
>> On Wed, Jun 14, 2023 at 11:57 AM Stefan Großhauser <
>> [email protected]> wrote:
>>
>>> Hello,
>>>
>>> isn't the "strict mode" already what you want?
>>>
>>>
>>> https://velocity.apache.org/engine/devel/user-guide.html#strict-rendering-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 = new VelocityEngine();
>>> velocityEngine.init();
>>>
>>> Template t = velocityEngine.getTemplate("card.vm");
>>>
>>> VelocityContext context = new VelocityContext();
>>> context.put("firstName", "tuk");
>>> StringWriter writer = 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ürnberg HRB 10908*
>>> *Geschäftsführer Bernhard Hammerbacher, Ursula Hammerbacher, Christoph
>>> Hammerbacher, Andreas Hammerbacher*
>>> Hausanschrift
>>> Daimlerstraße 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ßlich und allein für den Empfänger bestimmt. Die Verwendung durch
>>> Dritte ist untersagt. Die Firma Hammerbacher GmbH ist nur für die von ihr
>>> eingegeben Informationen verantwortlich, jedoch nicht für die einwandfreie
>>> Übertragung oder im Zusammenhang mit der Übertragung oder dem Empfang
>>> eingetretene Veränderungen oder Verzögerungen. Diese E-Mail enthält
>>> vetrtrauliche und/oder rechtlich geschützte Informationen. Wenn Sie nicht
>>> der richtige Adressat sind oder diese E-Mail irrtümlich erhalten haben,
>>> informieren Sie bitte sofort den Absender und vernichten Sie diese E-Mail.
>>> Das unerlaubte Kopieren sowie die unbefugte Weitergabe dieser Mail ist
>>> nicht gestattet.
>>>
>>>