1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.util.bridges.common;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.CharPool;
20  import com.liferay.portal.kernel.util.StringBundler;
21  import com.liferay.portal.kernel.util.StringPool;
22  
23  import javax.portlet.PortletURL;
24  
25  /**
26   * <a href="ScriptPostProcess.java.html"><b><i>View Source</i></b></a>
27   *
28   * @author Gavin Wan
29   * @author Brian Wing Shun Chan
30   * @see    org.apache.portals.bridges.common.ScriptPostProcess
31   */
32  public class ScriptPostProcess {
33  
34      public String getFinalizedPage() {
35          if (_sb != null) {
36              return _sb.toString();
37          }
38  
39          return StringPool.BLANK;
40      }
41  
42      public void postProcessPage(
43          PortletURL actionURL, String actionParameterName) {
44  
45          processPage(
46              "<a", StringPool.GREATER_THAN, "href=", actionURL,
47              actionParameterName);
48          processPage(
49              "<A", StringPool.GREATER_THAN, "HREF=", actionURL,
50              actionParameterName);
51          processPage(
52              "<area", StringPool.GREATER_THAN, "href=", actionURL,
53              actionParameterName);
54          processPage(
55              "<AREA", StringPool.GREATER_THAN, "HREF=", actionURL,
56              actionParameterName);
57          processPage(
58              "<FORM", StringPool.GREATER_THAN, "ACTION=", actionURL,
59              actionParameterName);
60          processPage(
61              "<form", StringPool.GREATER_THAN, "action=", actionURL,
62              actionParameterName);
63      }
64  
65      public void processPage(
66          String startTag, String endTag, String ref, PortletURL actionURL,
67          String actionParameterName) {
68  
69          try {
70              doProcessPage(
71              startTag, endTag, ref, actionURL, actionParameterName);
72          }
73          catch (Exception e) {
74              _log.error(e, e);
75          }
76      }
77  
78      public void setInitalPage(StringBundler initialPage) {
79          _sb = initialPage;
80      }
81  
82      protected void doProcessPage(
83          String startTag, String endTag, String ref, PortletURL actionURL,
84          String actionParameterName) {
85  
86          StringBundler sb = new StringBundler();
87  
88          String content = _sb.toString();
89  
90          int startTagPos = content.indexOf(startTag);
91          int endTagPos = 0;
92  
93          int startRefPos = 0;
94          int endRefPos = 0;
95  
96          while (startTagPos != -1) {
97              sb.append(content.substring(0, startTagPos));
98  
99              content = content.substring(startTagPos);
100 
101             endTagPos = content.indexOf(endTag);
102             startRefPos = content.indexOf(ref);
103 
104             if ((startRefPos == -1) || (startRefPos > endTagPos)) {
105                 sb.append(content.substring(0, endTagPos));
106 
107                 content = content.substring(endTagPos);
108             }
109             else {
110                 startRefPos = startRefPos + ref.length();
111 
112                 sb.append(content.substring(0, startRefPos));
113 
114                 content = content.substring(startRefPos);
115 
116                 String quote = StringPool.BLANK;
117 
118                 if (content.startsWith(StringPool.APOSTROPHE)) {
119                     quote = StringPool.APOSTROPHE;
120                 }
121                 else if (content.startsWith(StringPool.QUOTE)) {
122                     quote = StringPool.QUOTE;
123                 }
124 
125                 String url = StringPool.BLANK;
126 
127                 if (quote.length() > 0) {
128                     sb.append(quote);
129 
130                     content = content.substring(1);
131 
132                     endRefPos = content.indexOf(quote);
133 
134                     url = content.substring(0, endRefPos);
135                 }
136                 else {
137                     endTagPos = content.indexOf(endTag);
138 
139                     endRefPos = 0;
140 
141                     StringBundler unquotedURL = new StringBundler();
142 
143                     while (true) {
144                         char c = content.charAt(endRefPos);
145 
146                         if (!Character.isSpaceChar(c) &&
147                             (endRefPos < endTagPos)) {
148 
149                             endRefPos++;
150 
151                             unquotedURL.append(c);
152                         }
153                         else {
154                             endRefPos--;
155 
156                             break;
157                         }
158                     }
159 
160                     url = unquotedURL.toString();
161                 }
162 
163                 if ((url.charAt(0) == CharPool.POUND) ||
164                     url.startsWith("http")) {
165 
166                     sb.append(url);
167                     sb.append(quote);
168                 }
169                 else {
170                     actionURL.setParameter(actionParameterName, url);
171 
172                     sb.append(actionURL.toString());
173                     sb.append(quote);
174                 }
175 
176                 content = content.substring(endRefPos + 1);
177             }
178 
179             startTagPos = content.indexOf(startTag);
180         }
181 
182         sb.append(content);
183 
184         _sb = sb;
185     }
186 
187     private static Log _log = LogFactoryUtil.getLog(ScriptPostProcess.class);
188 
189     private StringBundler _sb;
190 
191 }