1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.dam.asset.model;
35
36 import freemarker.template.AdapterTemplateModel;
37 import freemarker.template.ObjectWrapper;
38 import freemarker.template.SimpleCollection;
39 import freemarker.template.TemplateCollectionModel;
40 import freemarker.template.TemplateHashModelEx;
41 import freemarker.template.TemplateModel;
42 import freemarker.template.TemplateModelException;
43
44 import info.magnolia.freemarker.models.MagnoliaModelFactory;
45 import info.magnolia.freemarker.models.MagnoliaObjectWrapper;
46
47
48
49
50
51
52 public class AssetMapModel implements TemplateHashModelEx, AdapterTemplateModel {
53
54
55
56
57 public static final class Factory implements MagnoliaModelFactory {
58 @SuppressWarnings("rawtypes")
59 @Override
60 public Class factoryFor() {
61 return AssetMap.class;
62 }
63
64 @Override
65 public AdapterTemplateModel create(Object object, ObjectWrapper wrapper) {
66 return new AssetMapModel((AssetMap) object, (MagnoliaObjectWrapper) wrapper);
67 }
68 };
69
70 private AssetMap assetMap;
71 private MagnoliaObjectWrapper wrapper;
72
73 public AssetMapModel() {
74 }
75
76 public AssetMapModel(AssetMap assetMap, MagnoliaObjectWrapper wrapper) {
77 this.assetMap = assetMap;
78 this.wrapper = wrapper;
79 }
80
81 @Override
82 public TemplateModel get(String key) throws TemplateModelException {
83 return wrapper.wrap(assetMap.get(key));
84 }
85
86 @Override
87 public boolean isEmpty() throws TemplateModelException {
88 return (size() == 0);
89 }
90
91 @Override
92 public int size() throws TemplateModelException {
93 return assetMap.size();
94 }
95
96 @Override
97 public TemplateCollectionModel keys() throws TemplateModelException {
98 return new SimpleCollection(assetMap.keySet().iterator());
99 }
100
101 @Override
102 public TemplateCollectionModel values() throws TemplateModelException {
103 return (TemplateCollectionModel) wrapper.wrap(assetMap.values().iterator());
104 }
105
106 @SuppressWarnings("rawtypes")
107 @Override
108 public Object getAdaptedObject(Class hint) {
109 return this.assetMap;
110 }
111
112 }