1 package com.atlassian.plugin.webresource;
2
3 import org.apache.commons.lang.StringUtils;
4
5 import java.util.Map;
6 import java.util.Iterator;
7 import java.util.List;
8 import java.util.ArrayList;
9
10 abstract class AbstractWebResourceFormatter implements WebResourceFormatter
11 {
12 /**
13 * Should return a List of parameter name {@link String}s, which the WebResourceFormatter will write out as
14 * HTML attributes.
15 *
16 * @return a {@link List} of parameter names
17 */
18 protected abstract List/*<String>*/ getAttributeParameters();
19
20 /**
21 * A convenient method to convert the given parameter map into a List of HTML {@link String} attributes.
22 * For example, an entry with key 'foo' and value 'bar' is converted to the attribute string, foo="bar".
23 * <p>
24 * Only parameters that are supported by the WebResourceFormatter are formatted (See {@link #getAttributeParameters()}).
25 * @param params a {@link Map} of parameters
26 * @return a list of HTML {@link String} attributes
27 */
28 protected List getParametersAsAttributes(Map params)
29 {
30 final List attributes = new ArrayList();
31 for (Iterator iterator = params.entrySet().iterator(); iterator.hasNext();)
32 {
33 Map.Entry entry = (Map.Entry) iterator.next();
34 String key = (String) entry.getKey();
35 String value = (String) entry.getValue();
36 if(StringUtils.isNotBlank(key) && getAttributeParameters().contains(key.toLowerCase()))
37 {
38 attributes.add(key + "=\"" + value + "\"");
39 }
40 }
41 return attributes;
42 }
43 }