1 package com.atlassian.plugin.web.model;
2
3 import com.atlassian.plugin.PluginParseException;
4 import com.atlassian.plugin.web.ContextProvider;
5 import com.atlassian.plugin.web.WebFragmentHelper;
6 import com.atlassian.plugin.web.descriptors.WebFragmentModuleDescriptor;
7 import org.dom4j.Element;
8
9 import javax.servlet.http.HttpServletRequest;
10 import java.util.ArrayList;
11 import java.util.HashMap;
12 import java.util.List;
13 import java.util.Map;
14
15
16
17
18 public class DefaultWebLabel extends DefaultWebParam implements WebLabel {
19 private final String key;
20 private final String noKeyValue;
21
22 public DefaultWebLabel(Element labelEl, WebFragmentHelper webFragmentHelper, ContextProvider contextProvider, WebFragmentModuleDescriptor descriptor) throws PluginParseException {
23 super(labelEl, webFragmentHelper, contextProvider, descriptor);
24 if (labelEl == null) {
25 throw new PluginParseException("You must specify a label for the section.");
26 } else {
27 this.key = labelEl.attributeValue("key");
28
29 if (this.key == null) {
30 this.noKeyValue = labelEl.getTextTrim();
31 } else {
32 this.noKeyValue = null;
33 }
34 }
35 }
36
37 public String getKey() {
38 return key;
39 }
40
41 public String getNoKeyValue() {
42 return noKeyValue;
43 }
44
45 public String getDisplayableLabel(HttpServletRequest req, Map<String, Object> origContext) {
46 final Map<String, Object> tmpContext = new HashMap<>(origContext);
47 tmpContext.putAll(getContextMap(tmpContext));
48 if (key != null) {
49 if (params == null || params.isEmpty()) {
50 return getWebFragmentHelper().getI18nValue(key, null, tmpContext);
51 } else {
52 final List<String> arguments = new ArrayList<>();
53
54
55 for (Map.Entry<String, String> entry : params.entrySet()) {
56 if (entry.getKey().startsWith("param"))
57 arguments.add(getWebFragmentHelper().renderVelocityFragment(entry.getValue(), tmpContext));
58 }
59
60 return getWebFragmentHelper().getI18nValue(key, arguments, tmpContext);
61 }
62 } else {
63 return getWebFragmentHelper().renderVelocityFragment(noKeyValue, tmpContext);
64 }
65 }
66 }