/* this belongs to the WebRaveler package */ package webraveler.plugin; import java.util.*; import webraveler.http.HTTPResponse; import webraveler.html.HTMLPage2; import webraveler.ravel.PluginAPI; import webraveler.ravel.Raveler; import webraveler.vurl.HelpVURL; import webraveler.vurl.PluginChangePref; import webraveler.vurl.PluginConfig; import webraveler.vurl.TextReplaceVURL; /** * A Web Raveler plugin to replace text (by matching a regular * expression) in the entity of any HTTPMessage with content-type * text/* (that is, any content-type which starts with "text/", not * the content-type that equals "text/*") * * @author The Web Raveler Development Squad * @version 1.0 */ public class TextReplacePlugin extends webraveler.ravel.RavelerPlugin { /* variables */ String fullURL; boolean DEBUG = false; /* constructors */ /** * The default constructor, called from WebRaveler2.Raveler */ public TextReplacePlugin() { name = "TextReplacePlugin"; mimeTypes.add("text/html"); } /* methods */ /** * Start executing the plugin when given a response * * @param response an HTTPResponse with the response to be modified * @return an HTTPResponse object, the result of running the plugin on the input response */ public HTTPResponse execute(HTTPResponse response, Hashtable attributes) { if (DEBUG) System.out.println("\nTextReplacePlugin: executing..."); String pattern = (String) attributes.get("pattern"); if (DEBUG) System.out.println("TextReplacePlugin: pattern: "+pattern); String replacement = (String) attributes.get("replacement"); if (DEBUG) System.out.println("TextReplacePlugin: replacement: "+replacement); if (DEBUG) System.out.println("TextReplacePlugin: getting page html"); HTMLPage2 html = response.getHtmlPage(); html.replaceText("Debian","Red Hat"); if (DEBUG) System.out.println("TextReplacePlugin: page html:"); if (DEBUG) System.out.println(""+html+"\n"); if (DEBUG) System.out.println("TextReplacePlugin: replacing text"); //response.getHtmlPage().replaceText(pattern,replacement); if (DEBUG) System.out.println("TextReplacePlugin: done replacing text...correcting content length"); response.correctContentLength(); if (DEBUG) System.out.println("TextReplacePlugin: finished\n"); return response; } /** * Do the setup stuff with Raveler - banner setup, etc. * Put the plugin on Raveler's "plugin vector" */ public boolean initialize() { /* Add virtual urls for configuration */ String fullURL1; Vector attributes = new Vector(); attributes.add("pattern"); attributes.add("replacement"); PluginChangePref changePref = new webraveler.vurl.PluginChangePref("TextReplacePlugin"); fullURL1 = PluginAPI.addPermanentVirtualURL(ravel,name,"changePref",changePref); PluginConfig config = new PluginConfig("TextReplacePlugin",attributes,fullURL1); fullURL = PluginAPI.addConfigVirtualURL(ravel,name,config); /* some helpful HTML to display to the user */ String helpfulHTML = new String(); helpfulHTML += ""+Raveler.ravelerCSS+"WebRaveler2 :: TextReplacePlugin Help Page"; helpfulHTML += "

TextReplacePlugin :: Help

"; helpfulHTML += "

Thank you for using TextReplacePlugin.

"; helpfulHTML += "

For more information on allowable regular expressions, please visit Sun's Java API "; helpfulHTML += "at http://java.sun.com.

"; HelpVURL help = new HelpVURL(helpfulHTML); fullURL = PluginAPI.addHelpVirtualURL(ravel,name,help); TextReplaceVURL textReplace = new TextReplaceVURL(); fullURL = PluginAPI.addPluginVirtualURL(ravel,name,textReplace); return true; } /** * Terminate the Plugin - Hasta la vista, BABY! */ public void terminate() { } /** * Return the default configuration for this plugin as a hash table. * */ public void setDefaultAttributes() { attributes = new Hashtable(); attributes.put("pattern","[Gg]oogle"); attributes.put("replacement","Poop"); } } //class RavelerPlugin