001
014
015 package com.liferay.taglib.aui;
016
017 import com.liferay.alloy.util.ReservedAttributeUtil;
018 import com.liferay.portal.kernel.util.SetUtil;
019 import com.liferay.portal.kernel.util.StringUtil;
020 import com.liferay.taglib.aui.base.BaseComponentTag;
021
022 import java.util.HashMap;
023 import java.util.Map;
024 import java.util.Set;
025
026 import javax.servlet.http.HttpServletRequest;
027
028 import org.apache.commons.lang.StringUtils;
029
030
034 public class ComponentTag extends BaseComponentTag {
035
036 protected boolean isEventAttribute(String key) {
037 if (StringUtil.startsWith(key, "after") ||
038 StringUtil.startsWith(key, "on")) {
039
040 return true;
041 }
042
043 return false;
044 }
045
046 protected boolean isValidAttribute(String key) {
047 String excludeAttributes = getExcludeAttributes();
048
049 if (excludeAttributes == null) {
050 return true;
051 }
052
053 Set<String> excludeAttributesSet = SetUtil.fromArray(
054 StringUtil.split(excludeAttributes));
055
056 if (key.equals("dynamicAttributes") ||
057 excludeAttributesSet.contains(key)) {
058
059 return false;
060 }
061
062 return true;
063 }
064
065 protected void proccessAttributes(
066 Map<String, Object> options, Map<String, Object> jsonifiedOptions) {
067
068 Map<String, String> afterEventOptions = new HashMap<>();
069 Map<String, String> onEventOptions = new HashMap<>();
070
071 for (String key : options.keySet()) {
072 if (!isValidAttribute(key)) {
073 continue;
074 }
075
076 Object value = options.get(key);
077
078 String originalKey = ReservedAttributeUtil.getOriginalName(
079 getName(), key);
080
081 if (value instanceof Map) {
082 Map<String, Object> childOptions = new HashMap<>();
083
084 proccessAttributes((Map<String, Object>)value, childOptions);
085
086 jsonifiedOptions.put(originalKey, childOptions);
087
088 continue;
089 }
090
091 if (isEventAttribute(key)) {
092 processEventAttribute(
093 key, String.valueOf(value), afterEventOptions,
094 onEventOptions);
095 }
096 else {
097 jsonifiedOptions.put(originalKey, value);
098 }
099 }
100
101 if (!afterEventOptions.isEmpty()) {
102 jsonifiedOptions.put("after", afterEventOptions);
103 }
104
105 if (!onEventOptions.isEmpty()) {
106 jsonifiedOptions.put("on", onEventOptions);
107 }
108 }
109
110 protected void processEventAttribute(
111 String key, String value, Map<String, String> afterEventOptions,
112 Map<String, String> onEventsOptions) {
113
114 if (key.startsWith("after")) {
115 String eventName = StringUtils.uncapitalize(key.substring(5));
116
117 afterEventOptions.put(eventName, value);
118 }
119 else {
120 String eventName = StringUtils.uncapitalize(key.substring(2));
121
122 onEventsOptions.put(eventName, value);
123 }
124 }
125
126 @Override
127 protected void setAttributes(HttpServletRequest request) {
128 Map<String, Object> options = getOptions();
129
130 Map<String, Object> jsonifiedOptions = new HashMap<>();
131
132 proccessAttributes(options, jsonifiedOptions);
133
134 super.setAttributes(request);
135
136 setNamespacedAttribute(request, "jsonifiedOptions", jsonifiedOptions);
137 setNamespacedAttribute(request, "options", options);
138 }
139
140 }