001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.servlet.taglib.aui;
016    
017    import com.liferay.portal.kernel.servlet.BrowserSnifferUtil;
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.IOException;
024    import java.io.Serializable;
025    import java.io.Writer;
026    
027    import java.util.Set;
028    import java.util.TreeSet;
029    import java.util.concurrent.ConcurrentHashMap;
030    import java.util.concurrent.ConcurrentMap;
031    
032    import javax.servlet.http.HttpServletRequest;
033    
034    /**
035     * @author Brian Wing Shun Chan
036     */
037    public class ScriptData {
038    
039            public void append(String portletId, String content, String use) {
040                    PortletData portletData = _getPortletData(portletId);
041    
042                    portletData.append(content, use);
043            }
044    
045            public void append(String portletId, StringBundler contentSB, String use) {
046                    PortletData portletData = _getPortletData(portletId);
047    
048                    portletData.append(contentSB, use);
049            }
050    
051            public void writeTo(HttpServletRequest request, Writer writer)
052                    throws IOException {
053    
054                    writer.write("<script type=\"text/javascript\">\n// <![CDATA[\n");
055    
056                    StringBundler callbackSB = new StringBundler();
057    
058                    for (PortletData portletData : _portletDataMap.values()) {
059                            portletData._rawSB.writeTo(writer);
060    
061                            callbackSB.append(portletData._callbackSB);
062                    }
063    
064                    if (callbackSB.index() == 0) {
065                            writer.write("\n// ]]>\n</script>");
066    
067                            return;
068                    }
069    
070                    String loadMethod = "use";
071    
072                    if (BrowserSnifferUtil.isIe(request) &&
073                            (BrowserSnifferUtil.getMajorVersion(request) < 8)) {
074    
075                            loadMethod = "ready";
076                    }
077    
078                    writer.write("AUI().");
079                    writer.write(loadMethod);
080                    writer.write("(");
081    
082                    Set<String> useSet = new TreeSet<String>();
083    
084                    for (PortletData portletData : _portletDataMap.values()) {
085                            useSet.addAll(portletData._useSet);
086                    }
087    
088                    for (String use : useSet) {
089                            writer.write(StringPool.APOSTROPHE);
090                            writer.write(use);
091                            writer.write(StringPool.APOSTROPHE);
092                            writer.write(StringPool.COMMA_AND_SPACE);
093                    }
094    
095                    writer.write("function(A) {");
096    
097                    callbackSB.writeTo(writer);
098    
099                    writer.write("});");
100    
101                    writer.write("\n// ]]>\n</script>");
102            }
103    
104            private PortletData _getPortletData(String portletId) {
105                    if (Validator.isNull(portletId)) {
106                            portletId = StringPool.BLANK;
107                    }
108    
109                    PortletData portletData = _portletDataMap.get(portletId);
110    
111                    if (portletData == null) {
112                            portletData = new PortletData();
113    
114                            PortletData oldPortletData = _portletDataMap.putIfAbsent(
115                                    portletId, portletData);
116    
117                            if (oldPortletData != null) {
118                                    portletData = oldPortletData;
119                            }
120                    }
121    
122                    return portletData;
123            }
124    
125            private ConcurrentMap<String, PortletData> _portletDataMap =
126                    new ConcurrentHashMap<String, PortletData>();
127    
128            private class PortletData implements Serializable {
129    
130                    public void append(String content, String use) {
131                            if (Validator.isNull(use)) {
132                                    _rawSB.append(content);
133                            }
134                            else {
135                                    _callbackSB.append("(function() {");
136                                    _callbackSB.append(content);
137                                    _callbackSB.append("})();");
138    
139                                    String[] useArray = StringUtil.split(use);
140    
141                                    for (int i = 0; i < useArray.length; i++) {
142                                            _useSet.add(useArray[i]);
143                                    }
144                            }
145                    }
146    
147                    public void append(StringBundler contentSB, String use) {
148                            if (Validator.isNull(use)) {
149                                    _rawSB.append(contentSB);
150                            }
151                            else {
152                                    _callbackSB.append("(function() {");
153                                    _callbackSB.append(contentSB);
154                                    _callbackSB.append("})();");
155    
156                                    String[] useArray = StringUtil.split(use);
157    
158                                    for (int i = 0; i < useArray.length; i++) {
159                                            _useSet.add(useArray[i]);
160                                    }
161                            }
162                    }
163    
164                    private static final long serialVersionUID = 1L;
165    
166                    private StringBundler _callbackSB = new StringBundler();
167                    private StringBundler _rawSB = new StringBundler();
168                    private Set<String> _useSet = new TreeSet<String>();
169    
170            }
171    
172    }