1 /* |
|
2 * Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the License "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 package com.nokia.helium.core; |
|
20 |
|
21 import java.io.File; |
|
22 import java.util.HashMap; |
|
23 import java.util.Map; |
|
24 |
|
25 import freemarker.template.ObjectWrapper; |
|
26 import freemarker.template.SimpleCollection; |
|
27 import freemarker.template.SimpleScalar; |
|
28 import freemarker.template.TemplateCollectionModel; |
|
29 import freemarker.template.TemplateHashModelEx; |
|
30 import freemarker.template.TemplateModel; |
|
31 import fmpp.Engine; |
|
32 import fmpp.models.AddTransform; |
|
33 import fmpp.models.ClearTransform; |
|
34 import fmpp.models.CopyWritableVariableMethod; |
|
35 import fmpp.models.NewWritableHashMethod; |
|
36 import fmpp.models.NewWritableSequenceMethod; |
|
37 import fmpp.models.RemoveTransform; |
|
38 import fmpp.models.SetTransform; |
|
39 |
|
40 /** |
|
41 * Downgraded implementation which makes the PPHash public (from FMPP). |
|
42 */ |
|
43 public class PPHash implements TemplateHashModelEx { |
|
44 private Map<String, TemplateModel> map = new HashMap<String, TemplateModel>(); |
|
45 |
|
46 /** |
|
47 * Constructor |
|
48 * |
|
49 */ |
|
50 public PPHash() { |
|
51 // transforms |
|
52 put("set", new SetTransform()); |
|
53 put("add", new AddTransform()); |
|
54 put("remove", new RemoveTransform()); |
|
55 put("clear", new ClearTransform()); |
|
56 |
|
57 // methods |
|
58 put("newWritableSequence", new NewWritableSequenceMethod()); |
|
59 put("newWritableHash", new NewWritableHashMethod()); |
|
60 put("copyWritable", new CopyWritableVariableMethod()); |
|
61 |
|
62 // constants |
|
63 put("slash", File.separator); |
|
64 put("version", Engine.getVersionNumber()); |
|
65 put("freemarkerVersion", Engine.getFreeMarkerVersionNumber()); |
|
66 |
|
67 } |
|
68 /** |
|
69 * Get number of templates. |
|
70 * |
|
71 * @return |
|
72 * Number of templates. |
|
73 */ |
|
74 public int size() { |
|
75 return map.size(); |
|
76 } |
|
77 |
|
78 /** |
|
79 * Get templates. |
|
80 * |
|
81 * @return |
|
82 * templates. |
|
83 */ |
|
84 public TemplateCollectionModel keys() { |
|
85 return new SimpleCollection(map.keySet(), ObjectWrapper.SIMPLE_WRAPPER); |
|
86 } |
|
87 |
|
88 /** |
|
89 * Get template values. |
|
90 * |
|
91 * @return |
|
92 * template values. |
|
93 */ |
|
94 public TemplateCollectionModel values() { |
|
95 return new SimpleCollection(map.values(), ObjectWrapper.SIMPLE_WRAPPER); |
|
96 } |
|
97 |
|
98 /** |
|
99 * Get a template. |
|
100 * |
|
101 * @param String |
|
102 * key for template |
|
103 * @return |
|
104 * a template. |
|
105 */ |
|
106 public TemplateModel get(String key) { |
|
107 return map.get(key); |
|
108 } |
|
109 |
|
110 /** |
|
111 * Check if there are any template. |
|
112 * |
|
113 * @return |
|
114 * True if no template. |
|
115 */ |
|
116 public boolean isEmpty() { |
|
117 return map.isEmpty(); |
|
118 } |
|
119 /** |
|
120 * Add template |
|
121 * |
|
122 * @param String |
|
123 * name of template |
|
124 * @param TemplateModel |
|
125 * value |
|
126 */ |
|
127 public void put(String name, TemplateModel value) { |
|
128 map.put(name, value); |
|
129 } |
|
130 /** |
|
131 * Add template |
|
132 * |
|
133 * @param String |
|
134 * name of template |
|
135 * @param String |
|
136 * value |
|
137 */ |
|
138 public void put(String name, String value) { |
|
139 map.put(name, new SimpleScalar(value)); |
|
140 } |
|
141 |
|
142 /** |
|
143 * Delete template |
|
144 * |
|
145 * @param String |
|
146 * name of template |
|
147 */ |
|
148 public void remove(String name) { |
|
149 map.remove(name); |
|
150 } |
|
151 } |
|