One drawback of portletUnit is not being able to really see the rendered code. Using the ideas of the Bare Bones Browser Launch for Java, I am now able to display the rendered code.
Bare Bones Browser Launch for Java • • • Use Default Browser to Open a Web Page from a Swing Application: "Java is often touted as the programing language of the Internet, so you would think Java might include a standard platform-independent mechanism to launch the user's default web browser. Unfortunately, this commonly needed feature is left to the application developer to build, and it's not particularly easy."
The code I am using is:
/**
* Show the response in a browser.
*
* @param response
* the response
* @param class1
* not used but the intent is to add a system propery regex that
* will control whether this page is loaded or not. Default will be none.
* @param id
* another regex system property will match against this. Default will be all.
* @throws Exception
* on error
*/
public static void showResponseInBrowser(WebResponse response, Class class1, String id) throws Exception {
String text = response.getText();
File f = File.createTempFile("httpUnit", ".html");
f.deleteOnExit();
PrintWriter fod = new PrintWriter(new FileOutputStream(f));
fod.print("<head><base href="'http://localhost'/"> </head>");
fod.print(text);
fod.close();
URL url = f.toURL();
openURL(url);
}
/**
* Bare Bones Browser Launch Version 1.5 (December 10, 2005) By Dem
* Pilafian. Supports: Mac OS X, GNU/Linux, Unix, Windows XP
*
* Example Usage: String url = "http://www.centerkey.com/";
* BareBonesBrowserLaunch.openURL(url); Public Domain Software -- Free to
* Use as You Like
*
* @param url
* the url to open
* @throws ClassNotFoundException
* getting class
* @throws NoSuchMethodException
* yes
* @throws SecurityException
* well
* @throws InvocationTargetException
* trying to invloke
* @throws IllegalAccessException
* trying to access
* @throws IllegalArgumentException
* bad arguement
* @throws IOException
* opening window
* @throws InterruptedException waiting
*/
public static void openURL(URL url) throws ClassNotFoundException, NoSuchMethodException,
IllegalAccessException, InvocationTargetException, IOException, InterruptedException {
String osName = System.getProperty("os.name");
if (osName.startsWith("Mac OS")) {
Class fileMgr = Class.forName("com.apple.eio.FileManager");
Method openURL = fileMgr.getDeclaredMethod("openURL", new Class[] {String.class });
openURL.invoke(null, new Object[] {url.toString() });
} else if (osName.startsWith("Windows")) {
String cmdLine = "rundll32 url.dll,FileProtocolHandler " + url.toString();
Process exec = Runtime.getRuntime().exec(cmdLine);
exec.waitFor();
} else { // assume Unix or Linux
String[] browsers = {"firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape" };
String browser = null;
for (int count = 0; count < browsers.length && browser == null; count++) { if (Runtime.getRuntime().exec(new String[] {"which", browsers[count] }).waitFor() == 0) { browser = browsers[count]; } } if (browser == null) { throw new IllegalStateException("Could not find web browser"); } else { Runtime.getRuntime().exec(new String[] {browser, url.toString() });
}
}
}
I am creating a <base> for all the links so the images and CSS all get included as needed.