How to generate the output for a given java collection object

classic Classic list List threaded Threaded
15 messages Options
Reply | Threaded
Open this post in threaded view
|

How to generate the output for a given java collection object

Rajvinder Pal
Hi ,
 
Following is my java object 'itemMap'.And i want to generate the output as given below
 
Map<Integer, Set<String>> itemMap = new HashMap<Integer, Set<String>>();
//id 1
Integer i1 = new Integer(1);
Set<String> itemSet1 = new HashSet<String>();
itemSet1.add("A");
itemMap.put(i1, itemSet1);
//id 2
Integer i2 = new Integer(2);
Set<String> itemSet2 = new HashSet<String>();
itemSet2.add("B");
itemSet2.add("C");
itemMap.put(i2, itemSet2);
//id 3
Integer i3 = new Integer(3);
Set<String> itemSet3 = new HashSet<String>();
itemSet3.add("D");
itemSet3.add("E");
itemMap.put(i3, itemSet3);
System.out.println(itemMap);
 
Expected output:
 
1 A
2 B C
3 D E
 
Could anyone let me know how can i generate this using freemarker template?
 
Thanks,
Rajvinder

------------------------------------------------------------------------------
All the data continuously generated in your IT infrastructure contains a
definitive record of customers, application performance, security
threats, fraudulent activity and more. Splunk takes this data and makes
sense of it. Business sense. IT sense. Common sense.
http://p.sf.net/sfu/splunk-d2dcopy1
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: How to generate the output for a given java collection object

Daniel Henrique Alves Lima
    I would try something like

    [#list (itemMap?keys) as key]
       ${key} [#list (itemMap[key]) as item]${item}[/#list]
    [/#list]

   But I'm not sure if FM supports integers as hash keys.

Rajvinder Pal wrote:
Hi ,
 
Following is my java object 'itemMap'.And i want to generate the output as given below
 
Map<Integer, Set<String>> itemMap = new HashMap<Integer, Set<String>>();
//id 1
Integer i1 = new Integer(1);
Set<String> itemSet1 = new HashSet<String>();
itemSet1.add("A");
itemMap.put(i1, itemSet1);
//id 2
Integer i2 = new Integer(2);
Set<String> itemSet2 = new HashSet<String>();
itemSet2.add("B");
itemSet2.add("C");
itemMap.put(i2, itemSet2);
//id 3
Integer i3 = new Integer(3);
Set<String> itemSet3 = new HashSet<String>();
itemSet3.add("D");
itemSet3.add("E");
itemMap.put(i3, itemSet3);
System.out.println(itemMap);
 
Expected output:
 
1 A
2 B C
3 D E
 
Could anyone let me know how can i generate this using freemarker template?
 
Thanks,
Rajvinder



------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security
threats, fraudulent activity, and more. Splunk takes this data and makes
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2dcopy2
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: How to generate the output for a given java collection object

Rajvinder Pal
I tried the below one. but i am getting error:-
 
<#list itemMap?keys as key>
${key}<#list itemMap[key] as item>${item}</#list>
</#list>
 
error:-
Java backtrace for programmers:
----------
freemarker.template.TemplateException: Expected number, sequence, or string. itemMap evaluated instead to freemarker.template.SimpleHash on line 2, column 14 in complexMap.ftl.
at freemarker.core.TemplateObject.invalidTypeException(TemplateObject.java:135)
at freemarker.core.DynamicKeyName.dealWithNumericalKey(DynamicKeyName.java:125)
at freemarker.core.DynamicKeyName._getAsTemplateModel(DynamicKeyName.java:90)
at freemarker.core.Expression.getAsTemplateModel(Expression.java:89)
at freemarker.core.IteratorBlock.accept(IteratorBlock.java:94)
at freemarker.core.Environment.visit(Environment.java:210)
at freemarker.core.MixedContent.accept(MixedContent.java:92)
at freemarker.core.Environment.visit(Environment.java:210)
at freemarker.core.IteratorBlock$Context.runLoop(IteratorBlock.java:167)
at freemarker.core.Environment.visit(Environment.java:417)
at freemarker.core.IteratorBlock.accept(IteratorBlock.java:102)
at freemarker.core.Environment.visit(Environment.java:210)
at freemarker.core.Environment.process(Environment.java:190)
at freemarker.template.Template.process(Template.java:237)
at org.raj.ComplexMap.main(ComplexMap.java:56)

From: Daniel Henrique Alves Lima <[hidden email]>
To: Rajvinder Pal <[hidden email]>; FreeMarker-user <[hidden email]>
Sent: Friday, September 23, 2011 7:28 AM
Subject: Re: [FreeMarker-user] How to generate the output for a given java collection object

    I would try something like

    [#list (itemMap?keys) as key]
       ${key} [#list (itemMap[key]) as item]${item}[/#list]
    [/#list]

   But I'm not sure if FM supports integers as hash keys.

Rajvinder Pal wrote:
Hi ,
 
Following is my java object 'itemMap'.And i want to generate the output as given below
 
Map<Integer, Set<String>> itemMap = new HashMap<Integer, Set<String>>();
//id 1
Integer i1 = new Integer(1);
Set<String> itemSet1 = new HashSet<String>();
itemSet1.add("A");
itemMap.put(i1, itemSet1);
//id 2
Integer i2 = new Integer(2);
Set<String> itemSet2 = new HashSet<String>();
itemSet2.add("B");
itemSet2.add("C");
itemMap.put(i2, itemSet2);
//id 3
Integer i3 = new Integer(3);
Set<String> itemSet3 = new HashSet<String>();
itemSet3.add("D");
itemSet3.add("E");
itemMap.put(i3, itemSet3);
System.out.println(itemMap);
 
Expected output:
 
1 A
2 B C
3 D E
 
Could anyone let me know how can i generate this using freemarker template?
 
Thanks,
Rajvinder




------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security
threats, fraudulent activity, and more. Splunk takes this data and makes
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2dcopy2
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: How to generate the output for a given java collection object

Daniel Henrique Alves Lima
    Have you tried without removing the parenthesis? I'm just guessing...

   [#list (itemMap?keys) as key]
       ${key} [#list (itemMap[key]) as item]${item}[/#list]
    [/#list]

Or
 
   [#assign keys = itemMap?keys /]
   [#list keys as key][#assign items = itemMap[key] /]
       ${key} [#list items as item]${item}[/#list]
    [/#list]



Rajvinder Pal wrote:
I tried the below one. but i am getting error:-
 
<#list itemMap?keys as key>
${key}<#list itemMap[key] as item>${item}</#list>
</#list>
 
error:-
Java backtrace for programmers:
----------
freemarker.template.TemplateException: Expected number, sequence, or string. itemMap evaluated instead to freemarker.template.SimpleHash on line 2, column 14 in complexMap.ftl.
at freemarker.core.TemplateObject.invalidTypeException(TemplateObject.java:135)
at freemarker.core.DynamicKeyName.dealWithNumericalKey(DynamicKeyName.java:125)
at freemarker.core.DynamicKeyName._getAsTemplateModel(DynamicKeyName.java:90)
at freemarker.core.Expression.getAsTemplateModel(Expression.java:89)
at freemarker.core.IteratorBlock.accept(IteratorBlock.java:94)
at freemarker.core.Environment.visit(Environment.java:210)
at freemarker.core.MixedContent.accept(MixedContent.java:92)
at freemarker.core.Environment.visit(Environment.java:210)
at freemarker.core.IteratorBlock$Context.runLoop(IteratorBlock.java:167)
at freemarker.core.Environment.visit(Environment.java:417)
at freemarker.core.IteratorBlock.accept(IteratorBlock.java:102)
at freemarker.core.Environment.visit(Environment.java:210)
at freemarker.core.Environment.process(Environment.java:190)
at freemarker.template.Template.process(Template.java:237)
at org.raj.ComplexMap.main(ComplexMap.java:56)

From: Daniel Henrique Alves Lima [hidden email]
To: Rajvinder Pal [hidden email]; FreeMarker-user [hidden email]
Sent: Friday, September 23, 2011 7:28 AM
Subject: Re: [FreeMarker-user] How to generate the output for a given java collection object

    I would try something like

    [#list (itemMap?keys) as key]
       ${key} [#list (itemMap[key]) as item]${item}[/#list]
    [/#list]

   But I'm not sure if FM supports integers as hash keys.

Rajvinder Pal wrote:
Hi ,
 
Following is my java object 'itemMap'.And i want to generate the output as given below
 
Map<Integer, Set<String>> itemMap = new HashMap<Integer, Set<String>>();
//id 1
Integer i1 = new Integer(1);
Set<String> itemSet1 = new HashSet<String>();
itemSet1.add("A");
itemMap.put(i1, itemSet1);
//id 2
Integer i2 = new Integer(2);
Set<String> itemSet2 = new HashSet<String>();
itemSet2.add("B");
itemSet2.add("C");
itemMap.put(i2, itemSet2);
//id 3
Integer i3 = new Integer(3);
Set<String> itemSet3 = new HashSet<String>();
itemSet3.add("D");
itemSet3.add("E");
itemMap.put(i3, itemSet3);
System.out.println(itemMap);
 
Expected output:
 
1 A
2 B C
3 D E
 
Could anyone let me know how can i generate this using freemarker template?
 
Thanks,
Rajvinder





------------------------------------------------------------------------------ All of the data generated in your IT infrastructure is seriously valuable. Why? It contains a definitive record of application performance, security threats, fraudulent activity, and more. Splunk takes this data and makes sense of it. IT sense. And common sense. http://p.sf.net/sfu/splunk-d2dcopy2


_______________________________________________ FreeMarker-user mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/freemarker-user

------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security
threats, fraudulent activity, and more. Splunk takes this data and makes
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2dcopy2
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: How to generate the output for a given java collection object

Daniel Dekany
In reply to this post by Rajvinder Pal
The problem is that FTL, the template language, doesn't really support
maps... It only has a type that is like a Map<String, Object>, i.e.,
the keys are String-s. It's horrible. Regardless, ?keys *happens* to
work even if the map keys are non-String. But the [] operator won't.

What I always did is using BeansWraper. Where you configure FreeMarker
do this:

        Configuration cfg ...
        ...
        BeansWrapper wrapper = new BeansWrapper();
        wrapper.setSimpleMapWrapper(true);
        cfg.setObjectWrapper(wrapper);

Now you can use itemMap(key) to get items with non-String keys, so
your example will work. (Or, if you are using
wrapper.setSimpleMapWrapper(false), then you could just use the Java
API of the map, like itemMap.keySet() and itemMap.get(key), but then
?keys and such will give you a mess of true keys and method names, so
I don't recommend that.)

Note that wrapping XML nodes, Jython object and such won't be
automatic with this wrapper. Can be fixed with a custom one if you
care.

An beware, HashSet is unsorted. It doesn't give back its items in the
order as you have added them. (It's a Java thing, nothing to do with
FreeMarker.) Use LinkedHashSet if you need to keep the order.

--
Best regards,
 Daniel Dekany

Friday, September 23, 2011, 12:54:06 PM, Rajvinder Pal wrote:

> I tried the below one. but i am getting error:-
>  
> <#list itemMap?keys as key>
> ${key}
> <#list itemMap[key] as item>${item}</#list>
> </#list>
>  
> error:-
> Java backtrace for programmers:
> ----------
> freemarker.template.TemplateException
> : Expected number, sequence, or string. itemMap evaluated instead
> to freemarker.template.SimpleHash on line 2, column 14 in complexMap.ftl.
> at freemarker.core.TemplateObject.invalidTypeException(
> TemplateObject.java:135)
> at freemarker.core.DynamicKeyName.dealWithNumericalKey(
> DynamicKeyName.java:125)
> at freemarker.core.DynamicKeyName._getAsTemplateModel(
> DynamicKeyName.java:90)
> at freemarker.core.Expression.getAsTemplateModel(
> Expression.java:89)
> at freemarker.core.IteratorBlock.accept(
> IteratorBlock.java:94)
> at freemarker.core.Environment.visit(
> Environment.java:210)
> at freemarker.core.MixedContent.accept(
> MixedContent.java:92)
> at freemarker.core.Environment.visit(
> Environment.java:210)
> at freemarker.core.IteratorBlock$Context.runLoop(
> IteratorBlock.java:167)
> at freemarker.core.Environment.visit(
> Environment.java:417)
> at freemarker.core.IteratorBlock.accept(
> IteratorBlock.java:102)
> at freemarker.core.Environment.visit(
> Environment.java:210)
> at freemarker.core.Environment.process(
> Environment.java:190)
> at freemarker.template.Template.process(
> Template.java:237)
> at org.raj.ComplexMap.main(
> ComplexMap.java:56)
>
>
> From: Daniel Henrique Alves Lima <[hidden email]>
> To: Rajvinder Pal <[hidden email]>; FreeMarker-user
> <[hidden email]>
> Sent: Friday, September 23, 2011 7:28 AM
> Subject: Re: [FreeMarker-user] How to generate the output for a given java collection object
>
>     I would try something like
>
>     [#list (itemMap?keys) as key]
>        ${key} [#list (itemMap[key]) as item]${item}[/#list]
>     [/#list]
>
>    But I'm not sure if FM supports integers as hash keys.
>
> Rajvinder Pal wrote:
>
> Hi ,
>  
> Following is my java object 'itemMap'.And i want to generate the output as given below
>  
> Map<Integer, Set<String>> itemMap = new HashMap<Integer, Set<String>>();
> //id 1
> Integer i1 = new Integer(1);
> Set<String> itemSet1 = new HashSet<String>();
> itemSet1.add("A");
> itemMap.put(i1, itemSet1);
> //id 2
> Integer i2 = new Integer(2);
> Set<String> itemSet2 = new HashSet<String>();
> itemSet2.add("B");
> itemSet2.add("C");
> itemMap.put(i2, itemSet2);
> //id 3
> Integer i3 = new Integer(3);
> Set<String> itemSet3 = new HashSet<String>();
> itemSet3.add("D");
> itemSet3.add("E");
> itemMap.put(i3, itemSet3);
> System.out.println(itemMap);
>  
> Expected output:
>  
> 1 A
> 2 B C
> 3 D E
>  
> Could anyone let me know how can i generate this using freemarker template?
>  
> Thanks,
> Rajvinder
>
>
>





------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security
threats, fraudulent activity, and more. Splunk takes this data and makes
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2dcopy2
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: How to generate the output for a given java collection object

Rajvinder Pal
Thanks Daniel! finally i am able to get the expected output  with your suggesstion:-
 
<#list itemMap?keys as key>
${key} <#list itemMap(key) as item>${item} </#list>
</#list>
 
I was trying to evaluate the FreeMarker for a requirement to generate the html table dynamically.I felt FreeMarker is bit tricky to solve the problem. But the mailing list response is amazing.te
with respect to generating output i am facing one more challenge it may be very silly to ask but since i am new to FreeMarker i want to know, how can we write  template with proper indention but still able to get the get the output html  in proper indention.
 
for example suppose my template logic looks like:-
 
for each bean
    print <beean.....
    <# if condition.....>
        <#if condition .... >
                <#if condition >
                        print <inner tag.......
 
 
 
The above logic will print something similar to this:-
 
            <bean.....
                               <inner
 
 
 
IN the above output, indention is too much for second .
How can we have template as well generated output in proper xml type format? i mean with proper indention
 
Thanks,
Rajvinder

From: Daniel Dekany <[hidden email]>
To: Rajvinder Pal <[hidden email]>
Cc: FreeMarker-user <[hidden email]>
Sent: Saturday, September 24, 2011 1:22 PM
Subject: Re: [FreeMarker-user] How to generate the output for a given java collection object

The problem is that FTL, the template language, doesn't really support
maps... It only has a type that is like a Map<String, Object>, i.e.,
the keys are String-s. It's horrible. Regardless, ?keys *happens* to
work even if the map keys are non-String. But the [] operator won't.

What I always did is using BeansWraper. Where you configure FreeMarker
do this:

        Configuration cfg ...
        ...
        BeansWrapper wrapper = new BeansWrapper();
        wrapper.setSimpleMapWrapper(true);
        cfg.setObjectWrapper(wrapper);

Now you can use itemMap(key) to get items with non-String keys, so
your example will work. (Or, if you are using
wrapper.setSimpleMapWrapper(false), then you could just use the Java
API of the map, like itemMap.keySet() and itemMap.get(key), but then
?keys and such will give you a mess of true keys and method names, so
I don't recommend that.)

Note that wrapping XML nodes, Jython object and such won't be
automatic with this wrapper. Can be fixed with a custom one if you
care.

An beware, HashSet is unsorted. It doesn't give back its items in the
order as you have added them. (It's a Java thing, nothing to do with
FreeMarker.) Use LinkedHashSet if you need to keep the order.

--
Best regards,
Daniel Dekany

Friday, September 23, 2011, 12:54:06 PM, Rajvinder Pal wrote:

> I tried the below one. but i am getting error:-

> <#list itemMap?keys as key>
> ${key}
> <#list itemMap[key] as item>${item}</#list>
> </#list>

> error:-
> Java backtrace for programmers:
> ----------
> freemarker.template.TemplateException
> : Expected number, sequence, or string. itemMap evaluated instead
> to freemarker.template.SimpleHash on line 2, column 14 in complexMap.ftl.
> at freemarker.core.TemplateObject.invalidTypeException(
> TemplateObject.java:135)
> at freemarker.core.DynamicKeyName.dealWithNumericalKey(
> DynamicKeyName.java:125)
> at freemarker.core.DynamicKeyName._getAsTemplateModel(
> DynamicKeyName.java:90)
> at freemarker.core.Expression.getAsTemplateModel(
> Expression.java:89)
> at freemarker.core.IteratorBlock.accept(
> IteratorBlock.java:94)
> at freemarker.core.Environment.visit(
> Environment.java:210)
> at freemarker.core.MixedContent.accept(
> MixedContent.java:92)
> at freemarker.core.Environment.visit(
> Environment.java:210)
> at freemarker.core.IteratorBlock$Context.runLoop(
> IteratorBlock.java:167)
> at freemarker.core.Environment.visit(
> Environment.java:417)
> at freemarker.core.IteratorBlock.accept(
> IteratorBlock.java:102)
> at freemarker.core.Environment.visit(
> Environment.java:210)
> at freemarker.core.Environment.process(
> Environment.java:190)
> at freemarker.template.Template.process(
> Template.java:237)
> at org.raj.ComplexMap.main(
> ComplexMap.java:56)
>
>
> From: Daniel Henrique Alves Lima <[hidden email]>
> To: Rajvinder Pal <[hidden email]>; FreeMarker-user
> <[hidden email]>
> Sent: Friday, September 23, 2011 7:28 AM
> Subject: Re: [FreeMarker-user] How to generate the output for a given java collection object
>
>    I would try something like
>
>    [#list (itemMap?keys) as key]
>        ${key} [#list (itemMap[key]) as item]${item}[/#list]
>    [/#list]
>
>    But I'm not sure if FM supports integers as hash keys.
>
> Rajvinder Pal wrote:
>
> Hi ,

> Following is my java object 'itemMap'.And i want to generate the output as given below

> Map<Integer, Set<String>> itemMap = new HashMap<Integer, Set<String>>();
> //id 1
> Integer i1 = new Integer(1);
> Set<String> itemSet1 = new HashSet<String>();
> itemSet1.add("A");
> itemMap.put(i1, itemSet1);
> //id 2
> Integer i2 = new Integer(2);
> Set<String> itemSet2 = new HashSet<String>();
> itemSet2.add("B");
> itemSet2.add("C");
> itemMap.put(i2, itemSet2);
> //id 3
> Integer i3 = new Integer(3);
> Set<String> itemSet3 = new HashSet<String>();
> itemSet3.add("D");
> itemSet3.add("E");
> itemMap.put(i3, itemSet3);
> System.out.println(itemMap);

> Expected output:

> 1 A
> 2 B C
> 3 D E

> Could anyone let me know how can i generate this using freemarker template?

> Thanks,
> Rajvinder
>
>
>







------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security
threats, fraudulent activity, and more. Splunk takes this data and makes
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2dcopy2
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: How to generate the output for a given java collection object

Daniel Dekany
Saturday, September 24, 2011, 5:00:00 PM, Rajvinder Pal wrote:

> Thanks Daniel! finally i am able to get the expected output  with your suggesstion:-
>  
> <#list itemMap?keys as key>
> ${key}
> <#list itemMap(key) as item>${item} </#list>
> </#list>
>  
> I was trying to evaluate the FreeMarker for a requirement to
> generate the html table dynamically.I felt FreeMarker is bit tricky
> to solve the problem.

Well, there are some old warts (legacies...) that users has to get
over... the two main warts are that FTL has no map type (only a
string-keys-only one) and that FTL doesn't have null (but treats
null-s coming from Java as missing variables).

> But the mailing list response is amazing.te
> with respect to generating output i am facing one more challenge it
> may be very silly to ask but since i am new to FreeMarker i want to
> know, how can we write  template with proper indention but still
> able to get the get the output html  in proper indention.

Sadly, you can't... nor in any other template language that I know of.
It's actually a rather tricky thing to do properly (much easier to do
almost properly), unless the template language understands HTML/XML
(FreeMarker doesn't), AND you are generating HTML/XML, and even then
it would mean some runtime overhead. You may find some tricks here
regarding white-space handling:

  http://freemarker.org/docs/dgui_misc_whitespace.html

but they won't give you proper indentation.

> for example suppose my template logic looks like:-
>  
> for each bean
>     print <beean.....
>     <# if condition.....>
>         <#if condition .... >
>                 <#if condition >
>                         print <inner tag.......
>  
>  
>  
> The above logic will print something similar to this:-
>  
>             <bean.....
>                                <inner
>  
>  
>  
> IN the above output, indention is too much for second .
> How can we have template as well generated output in proper xml
> type format? i mean with proper indention

You had to apply something on the output Writer that understands the
output format and reformats it... Of course, that will have its cost
in resource usage.

--
Best regards,
 Daniel Dekany

>
> Thanks,
> Rajvinder
>
>
> From: Daniel Dekany <[hidden email]>
> To: Rajvinder Pal <[hidden email]>
> Cc: FreeMarker-user <[hidden email]>
> Sent: Saturday, September 24, 2011 1:22 PM
> Subject: Re: [FreeMarker-user] How to generate the output for a given java collection object
>
> The problem is that FTL, the template language, doesn't really support
> maps... It only has a type that is like a Map<String, Object>, i.e.,
> the keys are String-s. It's horrible. Regardless, ?keys *happens* to
> work even if the map keys are non-String. But the [] operator won't.
>
> What I always did is using BeansWraper. Where you configure FreeMarker
> do this:
>
>         Configuration cfg ...
>         ...
>         BeansWrapper wrapper = new BeansWrapper();
>         wrapper.setSimpleMapWrapper(true);
>         cfg.setObjectWrapper(wrapper);
>
> Now you can use itemMap(key) to get items with non-String keys, so
> your example will work. (Or, if you are using
> wrapper.setSimpleMapWrapper(false), then you could just use the Java
> API of the map, like itemMap.keySet() and itemMap.get(key), but then
> ?keys and such will give you a mess of true keys and method names, so
> I don't recommend that.)
>
> Note that wrapping XML nodes, Jython object and such won't be
> automatic with this wrapper. Can be fixed with a custom one if you
> care.
>
> An beware, HashSet is unsorted. It doesn't give back its items in the
> order as you have added them. (It's a Java thing, nothing to do with
> FreeMarker.) Use LinkedHashSet if you need to keep the order.
>


------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security
threats, fraudulent activity, and more. Splunk takes this data and makes
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2dcopy2
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: How to generate the output for a given java collection object

Rajvinder Pal
Thanks, in that case i will prefer to write template with proper formating.I will use compress directive in template and let browser format the html display for me.
Regards,
Rajvinder
 

From: Daniel Dekany <[hidden email]>
To: Rajvinder Pal <[hidden email]>
Cc: FreeMarker-user <[hidden email]>
Sent: Saturday, September 24, 2011 10:13 PM
Subject: Re: [FreeMarker-user] How to generate the output for a given java collection object

Saturday, September 24, 2011, 5:00:00 PM, Rajvinder Pal wrote:

> Thanks Daniel! finally i am able to get the expected output  with your suggesstion:-

> <#list itemMap?keys as key>
> ${key}
> <#list itemMap(key) as item>${item} </#list>
> </#list>

> I was trying to evaluate the FreeMarker for a requirement to
> generate the html table dynamically.I felt FreeMarker is bit tricky
> to solve the problem.

Well, there are some old warts (legacies...) that users has to get
over... the two main warts are that FTL has no map type (only a
string-keys-only one) and that FTL doesn't have null (but treats
null-s coming from Java as missing variables).

> But the mailing list response is amazing.te
> with respect to generating output i am facing one more challenge it
> may be very silly to ask but since i am new to FreeMarker i want to
> know, how can we write  template with proper indention but still
> able to get the get the output html  in proper indention.

Sadly, you can't... nor in any other template language that I know of.
It's actually a rather tricky thing to do properly (much easier to do
almost properly), unless the template language understands HTML/XML
(FreeMarker doesn't), AND you are generating HTML/XML, and even then
it would mean some runtime overhead. You may find some tricks here
regarding white-space handling:

  http://freemarker.org/docs/dgui_misc_whitespace.html

but they won't give you proper indentation.

> for example suppose my template logic looks like:-

> for each bean
>    print <beean.....
>    <# if condition.....>
>        <#if condition .... >
>                <#if condition >
>                        print <inner tag.......



> The above logic will print something similar to this:-

>            <bean.....
>                                <inner



> IN the above output, indention is too much for second .
> How can we have template as well generated output in proper xml
> type format? i mean with proper indention

You had to apply something on the output Writer that understands the
output format and reformats it... Of course, that will have its cost
in resource usage.

--
Best regards,
Daniel Dekany

>
> Thanks,
> Rajvinder
>
>
> From: Daniel Dekany <[hidden email]>
> To: Rajvinder Pal <[hidden email]>
> Cc: FreeMarker-user <[hidden email]>
> Sent: Saturday, September 24, 2011 1:22 PM
> Subject: Re: [FreeMarker-user] How to generate the output for a given java collection object
>
> The problem is that FTL, the template language, doesn't really support
> maps... It only has a type that is like a Map<String, Object>, i.e.,
> the keys are String-s. It's horrible. Regardless, ?keys *happens* to
> work even if the map keys are non-String. But the [] operator won't.
>
> What I always did is using BeansWraper. Where you configure FreeMarker
> do this:
>
>        Configuration cfg ...
>        ...
>        BeansWrapper wrapper = new BeansWrapper();
>        wrapper.setSimpleMapWrapper(true);
>        cfg.setObjectWrapper(wrapper);
>
> Now you can use itemMap(key) to get items with non-String keys, so
> your example will work. (Or, if you are using
> wrapper.setSimpleMapWrapper(false), then you could just use the Java
> API of the map, like itemMap.keySet() and itemMap.get(key), but then
> ?keys and such will give you a mess of true keys and method names, so
> I don't recommend that.)
>
> Note that wrapping XML nodes, Jython object and such won't be
> automatic with this wrapper. Can be fixed with a custom one if you
> care.
>
> An beware, HashSet is unsorted. It doesn't give back its items in the
> order as you have added them. (It's a Java thing, nothing to do with
> FreeMarker.) Use LinkedHashSet if you need to keep the order.
>




------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security
threats, fraudulent activity, and more. Splunk takes this data and makes
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2dcopy2
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: How to generate the output for a given java collection object

Daniel Dekany
Saturday, September 24, 2011, 7:28:40 PM, Rajvinder Pal wrote:

> Thanks, in that case i will prefer to write template with proper
> formating.I will use compress directive in template and let browser format the html display for me.

I would not use compress, unless the bandwidth you save indeed
matters. It's a *runtime* compression, and is actually yet another
legacy, from FM 1.x I believe.

--
Best regards,
 Daniel Dekany

> Regards,
> Rajvinder
>  
>
>
> From: Daniel Dekany <[hidden email]>
> To: Rajvinder Pal <[hidden email]>
> Cc: FreeMarker-user <[hidden email]>
> Sent: Saturday, September 24, 2011 10:13 PM
> Subject: Re: [FreeMarker-user] How to generate the output for a given java collection object
>
> Saturday, September 24, 2011, 5:00:00 PM, Rajvinder Pal wrote:
>
>> Thanks Daniel! finally i am able to get the expected output  with your suggesstion:-
>>  
>> <#list itemMap?keys as key>
>> ${key}
>> <#list itemMap(key) as item>${item} </#list>
>> </#list>
>>  
>> I was trying to evaluate the FreeMarker for a requirement to
>> generate the html table dynamically.I felt FreeMarker is bit tricky
>> to solve the problem.
>
> Well, there are some old warts (legacies...) that users has to get
> over... the two main warts are that FTL has no map type (only a
> string-keys-only one) and that FTL doesn't have null (but treats
> null-s coming from Java as missing variables).
>
>> But the mailing list response is amazing.te
>> with respect to generating output i am facing one more challenge it
>> may be very silly to ask but since i am new to FreeMarker i want to
>> know, how can we write  template with proper indention but still
>> able to get the get the output html  in proper indention.
>
> Sadly, you can't... nor in any other template language that I know of.
> It's actually a rather tricky thing to do properly (much easier to do
> almost properly), unless the template language understands HTML/XML
> (FreeMarker doesn't), AND you are generating HTML/XML, and even then
> it would mean some runtime overhead. You may find some tricks here
> regarding white-space handling:
>
>   http://freemarker.org/docs/dgui_misc_whitespace.html
>
> but they won't give you proper indentation.
>
>> for example suppose my template logic looks like:-
>>  
>> for each bean
>>    print <beean.....
>>    <# if condition.....>
>>        <#if condition .... >
>>                <#if condition >
>>                        print <inner tag.......
>>  
>>  
>>  
>> The above logic will print something similar to this:-
>>  
>>            <bean.....
>>                                <inner
>>  
>>  
>>  
>> IN the above output, indention is too much for second .
>> How can we have template as well generated output in proper xml
>> type format? i mean with proper indention
>
> You had to apply something on the output Writer that understands the
> output format and reformats it... Of course, that will have its cost
> in resource usage.
>


------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security
threats, fraudulent activity, and more. Splunk takes this data and makes
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2dcopy2
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: How to generate the output for a given java collection object

Rajvinder Pal
Ok.I think i want to see  a proffessionaly written  ftl sample file.Could you point me to any such  file from web or personally you might have written.I just want to know how a ftl file looks like in a real time application.
 
Thanks,
Rajvinder

From: Daniel Dekany <[hidden email]>
To: Rajvinder Pal <[hidden email]>
Cc: FreeMarker-user <[hidden email]>
Sent: Sunday, September 25, 2011 12:51 AM
Subject: Re: [FreeMarker-user] How to generate the output for a given java collection object

Saturday, September 24, 2011, 7:28:40 PM, Rajvinder Pal wrote:

> Thanks, in that case i will prefer to write template with proper
> formating.I will use compress directive in template and let browser format the html display for me.

I would not use compress, unless the bandwidth you save indeed
matters. It's a *runtime* compression, and is actually yet another
legacy, from FM 1.x I believe.

--
Best regards,
Daniel Dekany

> Regards,
> Rajvinder

>
>
> From: Daniel Dekany <[hidden email]>
> To: Rajvinder Pal <[hidden email]>
> Cc: FreeMarker-user <[hidden email]>
> Sent: Saturday, September 24, 2011 10:13 PM
> Subject: Re: [FreeMarker-user] How to generate the output for a given java collection object
>
> Saturday, September 24, 2011, 5:00:00 PM, Rajvinder Pal wrote:
>
>> Thanks Daniel! finally i am able to get the expected output  with your suggesstion:-
>> 
>> <#list itemMap?keys as key>
>> ${key}
>> <#list itemMap(key) as item>${item} </#list>
>> </#list>
>> 
>> I was trying to evaluate the FreeMarker for a requirement to
>> generate the html table dynamically.I felt FreeMarker is bit tricky
>> to solve the problem.
>
> Well, there are some old warts (legacies...) that users has to get
> over... the two main warts are that FTL has no map type (only a
> string-keys-only one) and that FTL doesn't have null (but treats
> null-s coming from Java as missing variables).
>
>> But the mailing list response is amazing.te
>> with respect to generating output i am facing one more challenge it
>> may be very silly to ask but since i am new to FreeMarker i want to
>> know, how can we write  template with proper indention but still
>> able to get the get the output html  in proper indention.
>
> Sadly, you can't... nor in any other template language that I know of.
> It's actually a rather tricky thing to do properly (much easier to do
> almost properly), unless the template language understands HTML/XML
> (FreeMarker doesn't), AND you are generating HTML/XML, and even then
> it would mean some runtime overhead. You may find some tricks here
> regarding white-space handling:
>
http://freemarker.org/docs/dgui_misc_whitespace.html
>
> but they won't give you proper indentation.
>
>> for example suppose my template logic looks like:-
>> 
>> for each bean
>>    print <beean.....
>>    <# if condition.....>
>>        <#if condition .... >
>>                <#if condition >
>>                        print <inner tag.......
>> 
>> 
>> 
>> The above logic will print something similar to this:-
>> 
>>            <bean.....
>>                                <inner
>> 
>> 
>> 
>> IN the above output, indention is too much for second .
>> How can we have template as well generated output in proper xml
>> type format? i mean with proper indention
>
> You had to apply something on the output Writer that understands the
> output format and reformats it... Of course, that will have its cost
> in resource usage.
>




------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security
threats, fraudulent activity, and more. Splunk takes this data and makes
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2dcopy2
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: How to generate the output for a given java collection object

Daniel Dekany
Just indent naturally so the source is easy to read... no tricks.
Don't worry about the extra white-space in the output. That's how
templates used to work.


Sunday, September 25, 2011, 1:46:07 PM, Rajvinder Pal wrote:

> Ok.I think i want to see  a proffessionaly written  ftl sample
> file.Could you point me to any such  file from web or personally you
> might have written.I just want to know how a ftl file looks like in a real time application.
>  
> Thanks,
> Rajvinder
>
>
> From: Daniel Dekany <[hidden email]>
> To: Rajvinder Pal <[hidden email]>
> Cc: FreeMarker-user <[hidden email]>
> Sent: Sunday, September 25, 2011 12:51 AM
> Subject: Re: [FreeMarker-user] How to generate the output for a given java collection object
>
> Saturday, September 24, 2011, 7:28:40 PM, Rajvinder Pal wrote:
>
>> Thanks, in that case i will prefer to write template with proper
>> formating.I will use compress directive in template and let browser format the html display for me.
>
> I would not use compress, unless the bandwidth you save indeed
> matters. It's a *runtime* compression, and is actually yet another
> legacy, from FM 1.x I believe.
>

--
Best regards,
 Daniel Dekany


------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security
threats, fraudulent activity, and more. Splunk takes this data and makes
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2dcopy2
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: How to generate the output for a given java collection object

Rajvinder Pal
Thanks for your valuable feedback.
 
Thanks,
Rajvinder

From: Daniel Dekany <[hidden email]>
To: Rajvinder Pal <[hidden email]>
Cc: FreeMarker-user <[hidden email]>
Sent: Sunday, September 25, 2011 6:42 PM
Subject: Re: [FreeMarker-user] How to generate the output for a given java collection object

Just indent naturally so the source is easy to read... no tricks.
Don't worry about the extra white-space in the output. That's how
templates used to work.


Sunday, September 25, 2011, 1:46:07 PM, Rajvinder Pal wrote:

> Ok.I think i want to see  a proffessionaly written  ftl sample
> file.Could you point me to any such  file from web or personally you
> might have written.I just want to know how a ftl file looks like in a real time application.

> Thanks,
> Rajvinder
>
>
> From: Daniel Dekany <[hidden email]>
> To: Rajvinder Pal <[hidden email]>
> Cc: FreeMarker-user <[hidden email]>
> Sent: Sunday, September 25, 2011 12:51 AM
> Subject: Re: [FreeMarker-user] How to generate the output for a given java collection object
>
> Saturday, September 24, 2011, 7:28:40 PM, Rajvinder Pal wrote:
>
>> Thanks, in that case i will prefer to write template with proper
>> formating.I will use compress directive in template and let browser format the html display for me.
>
> I would not use compress, unless the bandwidth you save indeed
> matters. It's a *runtime* compression, and is actually yet another
> legacy, from FM 1.x I believe.
>

--
Best regards,
Daniel Dekany




------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security
threats, fraudulent activity, and more. Splunk takes this data and makes
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2dcopy2
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: How to generate the output for a given java collection object

arthernan
This post has NOT been accepted by the mailing list yet.
In reply to this post by Daniel Dekany
Actually I have been researching on templating tools. And StringTemplate does cover this.
Reply | Threaded
Open this post in threaded view
|

Re: How to generate the output for a given java collection object

arthernan
In reply to this post by Rajvinder Pal
I actually am looking at different templating tools. And I was trying to see if in fact freemarker did support indentation or not. StringTemplate does. Rather than having a long templates, StringTemplate promotes many smaller templates. The templates are indented correctly in reference to the calling template.
Reply | Threaded
Open this post in threaded view
|

Re: How to generate the output for a given java collection object

Daniel Dekany
Unfortunately FreeMarker doesn't support automatic indentation.

--
Best regards,
 Daniel Dekany


Wednesday, January 23, 2013, 5:46:03 PM, arthernan wrote:

> I actually am looking at different templating tools. And I was trying to see
> if in fact freemarker did support indentation or not. StringTemplate does.
> Rather than having a long templates, StringTemplate promotes many smaller
> templates. The templates are indented correctly in reference to the calling
> template.


------------------------------------------------------------------------------
Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
MVPs and experts. ON SALE this month only -- learn more at:
http://p.sf.net/sfu/learnnow-d2d
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user