View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.portals.bridges.common;
19  
20  import javax.portlet.PortletURL;
21  
22  /***
23   * ScriptPostProcess
24   * 
25   * Utility class for post processing perl or php created pages.
26   * 
27   * @author <a href="mailto:rogerrut@apache.org">Roger Ruttimann</a>
28   * @version $Id: ScriptPostProcess.java 517068 2007-03-12 01:44:37Z ate $
29   */
30  
31  
32  public class ScriptPostProcess {
33  
34  	// Private members
35  	StringBuffer internalPage = null;
36  	
37  	/***
38  	 * 
39  	 */
40  	public ScriptPostProcess() {
41  		super();
42  		// TODO Auto-generated constructor stub
43  	}
44  	
45  	/***
46  	 * getFinalizedPage
47  	 * @return String processed page
48  	 */
49  	public String getFinalizedPage()
50  	{
51  		if (internalPage != null)
52  		{
53  			return internalPage.toString();
54  		}
55  		else
56  		{
57  			return "";	
58  		}
59  	}
60  	
61  	/***
62  	 * setInitialPage() 
63  	 *  Sets the internal page that will be processed by invoking the different methods
64  	 * @param page
65  	 */
66  	public void setInitalPage(StringBuffer page)
67  	{
68  		this.internalPage = page;
69  	}
70  	
71  	/***
72  	 * postProcessPage()
73  	 * Applies default rules for processing HREFS and actions in an HTML page
74  	 * @param actionURL
75  	 * @param actionParameterName
76  	 */
77  	public void postProcessPage(PortletURL actionURL, String actionParameterName)
78  	{
79  		// Anchor tags
80  		processPage("<a", ">", "href=",  actionURL, actionParameterName);
81  		processPage("<A", ">", "HREF=",  actionURL, actionParameterName);
82  		processPage("<AREA", ">", "href=",  actionURL, actionParameterName);
83  		
84  		// Forms
85  		processPage("<FORM", ">", "action=",  actionURL, actionParameterName);
86  		processPage("<form", ">", "action=",  actionURL, actionParameterName);
87  	}
88  	
89  	/***
90  	 * processPage()
91  	 * Apply one rule to the page
92  	 * @param startTag
93  	 * @param endTag
94  	 * @param ref
95  	 * @param actionURL
96  	 * @param actionParameterName
97  	 */
98  	public void processPage(String startTag, String endTag, String ref,  PortletURL actionURL, String actionParameterName)
99  	{
100 		final String SINGLE_QUOTE = "\'";
101     	final String DOUBLE_QUOTE = "\"";
102     	
103     	StringBuffer finalPage = new StringBuffer();
104 		String page = internalPage.toString();
105 		
106 		int ixTagOpen, ixTagEnd, ixRefStart, ixRefEnd;
107 		//ref = ref + quote;
108 		
109 		// Start search
110 		ixTagOpen = page.indexOf(startTag);
111 		
112 		try
113 		{
114 			while (ixTagOpen != -1 )
115 			{
116 				finalPage.append(page.substring(0, ixTagOpen));
117 				page = page.substring(ixTagOpen);
118 				
119 				ixTagEnd = page.indexOf(endTag);
120 				ixRefStart = page.indexOf(ref);
121 				
122 				//If reference start tag is after endTag it means that the Tag doesn't include any source links
123 				// just continue...
124 				if ( ixRefStart == -1 || ixRefStart > ixTagEnd )
125 				{
126 					finalPage.append(page.substring(0, ixTagEnd));
127 					page = page.substring(ixTagEnd);
128 				}
129 				else
130 				{
131 					String strQuote = "";
132 					String url = "";
133 					
134 					ixRefStart = ixRefStart + ref.length();
135 					finalPage.append(page.substring(0, ixRefStart));
136 					page = page.substring(ixRefStart);
137 					
138 					// Check if the argument starts with a single or double quote or no quote
139 					if ( page.startsWith(SINGLE_QUOTE))
140 						strQuote = SINGLE_QUOTE;
141 					else if (page.startsWith(DOUBLE_QUOTE))
142 							strQuote = DOUBLE_QUOTE;
143 					
144 					if ( strQuote.length() > 0)
145 					{
146 						finalPage.append(strQuote);
147 						page = page.substring(1);
148 						ixRefEnd = page.indexOf(strQuote);
149 						
150 						// Extract the URL
151 						url = page.substring(0, ixRefEnd);
152 					}
153 					else
154 					{
155 						// Make sure that we don't parse over the tag end
156 						ixTagEnd = page.indexOf(endTag);
157 						
158 						// No quote just the first space or tagEnd index
159 						ixRefEnd = 0;
160 						StringBuffer nqurl = new StringBuffer();
161 						boolean  bEnd = false;
162 						
163 						while ( bEnd == false)
164 						{
165 							char c = page.charAt(ixRefEnd);
166 							
167 							if ( (Character.isSpaceChar(c) == false) && (ixRefEnd < ixTagEnd) )
168 							{
169 								ixRefEnd++;
170 								nqurl.append(c);
171 							}
172 							else
173 							{
174 								bEnd = true;
175 								ixRefEnd--;
176 							}
177 						}
178 						// Get the string
179 						url = nqurl.toString();
180 						
181 					}
182 					/*
183 					 * If the URL is an anchor don't replace it
184 					 * with a portlet action
185 					 */
186 					if (url.charAt(0) == '#')
187 					{
188 						finalPage.append(url).append(strQuote);
189 					}
190 					else
191 					{
192 						// Prepend the Action URL
193 						actionURL.setParameter(actionParameterName, url);
194 						
195 						finalPage.append(actionURL.toString()).append(strQuote);
196 					}
197 					//Remainder
198 					page = page.substring(ixRefEnd+1);
199 				}
200 				
201 				// Continue scan
202 				ixTagOpen = page.indexOf(startTag);
203 			}
204 			
205 			finalPage.append(page);
206 		}
207 		catch(Exception e)
208 		{
209 			System.out.println("ERROR: Exception in processHREFS " + e.getMessage() );
210 		}
211 	
212 		internalPage = finalPage;
213 	}
214 }