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.util.bridges.common;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.CharPool;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.StringPool;
022    
023    import javax.portlet.PortletURL;
024    
025    /**
026     * @author Gavin Wan
027     * @author Brian Wing Shun Chan
028     * @see    org.apache.portals.bridges.common.ScriptPostProcess
029     */
030    public class ScriptPostProcess {
031    
032            public String getFinalizedPage() {
033                    if (_sb != null) {
034                            return _sb.toString();
035                    }
036    
037                    return StringPool.BLANK;
038            }
039    
040            public void postProcessPage(
041                    PortletURL actionURL, String actionParameterName) {
042    
043                    processPage(
044                            "<a", StringPool.GREATER_THAN, "href=", actionURL,
045                            actionParameterName);
046                    processPage(
047                            "<A", StringPool.GREATER_THAN, "HREF=", actionURL,
048                            actionParameterName);
049                    processPage(
050                            "<area", StringPool.GREATER_THAN, "href=", actionURL,
051                            actionParameterName);
052                    processPage(
053                            "<AREA", StringPool.GREATER_THAN, "HREF=", actionURL,
054                            actionParameterName);
055                    processPage(
056                            "<FORM", StringPool.GREATER_THAN, "ACTION=", actionURL,
057                            actionParameterName);
058                    processPage(
059                            "<form", StringPool.GREATER_THAN, "action=", actionURL,
060                            actionParameterName);
061            }
062    
063            public void processPage(
064                    String startTag, String endTag, String ref, PortletURL actionURL,
065                    String actionParameterName) {
066    
067                    try {
068                            doProcessPage(
069                                    startTag, endTag, ref, actionURL, actionParameterName);
070                    }
071                    catch (Exception e) {
072                            _log.error(e, e);
073                    }
074            }
075    
076            public void setInitalPage(StringBundler initialPage) {
077                    _sb = initialPage;
078            }
079    
080            protected void doProcessPage(
081                    String startTag, String endTag, String ref, PortletURL actionURL,
082                    String actionParameterName) {
083    
084                    StringBundler sb = new StringBundler();
085    
086                    String content = _sb.toString();
087    
088                    int startTagPos = content.indexOf(startTag);
089                    int endTagPos = 0;
090    
091                    int startRefPos = 0;
092                    int endRefPos = 0;
093    
094                    while (startTagPos != -1) {
095                            sb.append(content.substring(0, startTagPos));
096    
097                            content = content.substring(startTagPos);
098    
099                            endTagPos = content.indexOf(endTag);
100                            startRefPos = content.indexOf(ref);
101    
102                            if ((startRefPos == -1) || (startRefPos > endTagPos)) {
103                                    sb.append(content.substring(0, endTagPos));
104    
105                                    content = content.substring(endTagPos);
106                            }
107                            else {
108                                    startRefPos = startRefPos + ref.length();
109    
110                                    sb.append(content.substring(0, startRefPos));
111    
112                                    content = content.substring(startRefPos);
113    
114                                    String quote = StringPool.BLANK;
115    
116                                    if (content.startsWith(StringPool.APOSTROPHE)) {
117                                            quote = StringPool.APOSTROPHE;
118                                    }
119                                    else if (content.startsWith(StringPool.QUOTE)) {
120                                            quote = StringPool.QUOTE;
121                                    }
122    
123                                    String url = StringPool.BLANK;
124    
125                                    if (quote.length() > 0) {
126                                            sb.append(quote);
127    
128                                            content = content.substring(1);
129    
130                                            endRefPos = content.indexOf(quote);
131    
132                                            url = content.substring(0, endRefPos);
133                                    }
134                                    else {
135                                            endTagPos = content.indexOf(endTag);
136    
137                                            endRefPos = 0;
138    
139                                            StringBundler unquotedURL = new StringBundler();
140    
141                                            while (true) {
142                                                    char c = content.charAt(endRefPos);
143    
144                                                    if (!Character.isSpaceChar(c) &&
145                                                            (endRefPos < endTagPos)) {
146    
147                                                            endRefPos++;
148    
149                                                            unquotedURL.append(c);
150                                                    }
151                                                    else {
152                                                            endRefPos--;
153    
154                                                            break;
155                                                    }
156                                            }
157    
158                                            url = unquotedURL.toString();
159                                    }
160    
161                                    if ((url.charAt(0) == CharPool.POUND) ||
162                                            url.startsWith("http")) {
163    
164                                            sb.append(url);
165                                            sb.append(quote);
166                                    }
167                                    else {
168                                            actionURL.setParameter(actionParameterName, url);
169    
170                                            sb.append(actionURL.toString());
171                                            sb.append(quote);
172                                    }
173    
174                                    content = content.substring(endRefPos + 1);
175                            }
176    
177                            startTagPos = content.indexOf(startTag);
178                    }
179    
180                    sb.append(content);
181    
182                    _sb = sb;
183            }
184    
185            private static Log _log = LogFactoryUtil.getLog(ScriptPostProcess.class);
186    
187            private StringBundler _sb;
188    
189    }