001
014
015 package com.liferay.portal.kernel.servlet.taglib.aui;
016
017 import com.liferay.portal.kernel.util.Mergeable;
018 import com.liferay.portal.kernel.util.StringBundler;
019 import com.liferay.portal.kernel.util.StringPool;
020 import com.liferay.portal.kernel.util.StringUtil;
021 import com.liferay.portal.kernel.util.Validator;
022
023 import java.io.Serializable;
024
025 import java.util.HashMap;
026 import java.util.Map;
027 import java.util.Set;
028 import java.util.TreeSet;
029 import java.util.concurrent.ConcurrentHashMap;
030 import java.util.concurrent.ConcurrentMap;
031
032
036 public class ScriptData implements Mergeable<ScriptData>, Serializable {
037
038 public void append(String portletId, String content, String use) {
039 PortletData portletData = _getPortletData(portletId);
040
041 portletData.append(content, use);
042 }
043
044 public void append(String portletId, StringBundler contentSB, String use) {
045 PortletData portletData = _getPortletData(portletId);
046
047 portletData.append(contentSB, use);
048 }
049
050 public StringBundler getCallbackSB() {
051 StringBundler callbackSB = new StringBundler();
052
053 for (PortletData portletData : _portletDataMap.values()) {
054 callbackSB.append(portletData._callbackSB);
055 }
056
057 return callbackSB;
058 }
059
060 public StringBundler getRawSB() {
061 StringBundler rawSB = new StringBundler();
062
063 for (PortletData portletData : _portletDataMap.values()) {
064 rawSB.append(portletData._rawSB);
065 }
066
067 return rawSB;
068 }
069
070 public Set<String> getUseSet() {
071 Set<String> useSet = new TreeSet<String>();
072
073 for (PortletData portletData : _portletDataMap.values()) {
074 useSet.addAll(portletData._useSet);
075 }
076
077 return useSet;
078 }
079
080 public void mark() {
081 for (PortletData portletData : _portletDataMap.values()) {
082 StringBundler callbackSB = portletData._callbackSB;
083
084 _sbIndexMap.put(callbackSB, callbackSB.index());
085
086 StringBundler rawSB = portletData._rawSB;
087
088 _sbIndexMap.put(rawSB, rawSB.index());
089 }
090 }
091
092 public ScriptData merge(ScriptData scriptData) {
093 if ((scriptData != null) && (scriptData != this)) {
094 _portletDataMap.putAll(scriptData._portletDataMap);
095 }
096
097 return this;
098 }
099
100 public void reset() {
101 for (Map.Entry<StringBundler, Integer> entry : _sbIndexMap.entrySet()) {
102 StringBundler sb = entry.getKey();
103
104 sb.setIndex(entry.getValue());
105 }
106 }
107
108 private PortletData _getPortletData(String portletId) {
109 if (Validator.isNull(portletId)) {
110 portletId = StringPool.BLANK;
111 }
112
113 PortletData portletData = _portletDataMap.get(portletId);
114
115 if (portletData == null) {
116 portletData = new PortletData();
117
118 PortletData oldPortletData = _portletDataMap.putIfAbsent(
119 portletId, portletData);
120
121 if (oldPortletData != null) {
122 portletData = oldPortletData;
123 }
124 }
125
126 return portletData;
127 }
128
129 private static final long serialVersionUID = 1L;
130
131 private ConcurrentMap<String, PortletData> _portletDataMap =
132 new ConcurrentHashMap<String, PortletData>();
133 private Map<StringBundler, Integer> _sbIndexMap =
134 new HashMap<StringBundler, Integer>();
135
136 private class PortletData implements Serializable {
137
138 public void append(String content, String use) {
139 if (Validator.isNull(use)) {
140 _rawSB.append(content);
141 }
142 else {
143 _callbackSB.append("(function() {");
144 _callbackSB.append(content);
145 _callbackSB.append("})();");
146
147 String[] useArray = StringUtil.split(use);
148
149 for (int i = 0; i < useArray.length; i++) {
150 _useSet.add(useArray[i]);
151 }
152 }
153 }
154
155 public void append(StringBundler contentSB, String use) {
156 if (Validator.isNull(use)) {
157 _rawSB.append(contentSB);
158 }
159 else {
160 _callbackSB.append("(function() {");
161 _callbackSB.append(contentSB);
162 _callbackSB.append("})();");
163
164 String[] useArray = StringUtil.split(use);
165
166 for (int i = 0; i < useArray.length; i++) {
167 _useSet.add(useArray[i]);
168 }
169 }
170 }
171
172 private static final long serialVersionUID = 1L;
173
174 private StringBundler _callbackSB = new StringBundler();
175 private StringBundler _rawSB = new StringBundler();
176 private Set<String> _useSet = new TreeSet<String>();
177
178 }
179
180 }