View Javadoc

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