[Httpunit-commit] CVS: httpunit/src/com/meterware/httpunit AppletStubImpl.java,1.1,1.2 WebApplet.jav
Brought to you by:
russgold
|
From: Russell G. <rus...@us...> - 2002-11-22 17:19:38
|
Update of /cvsroot/httpunit/httpunit/src/com/meterware/httpunit
In directory sc8-pr-cvs1:/tmp/cvs-serv22673/src/com/meterware/httpunit
Modified Files:
AppletStubImpl.java WebApplet.java
Log Message:
Added support for applet parameters and archives
Index: AppletStubImpl.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/AppletStubImpl.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- AppletStubImpl.java 15 Nov 2002 00:10:22 -0000 1.1
+++ AppletStubImpl.java 22 Nov 2002 17:19:35 -0000 1.2
@@ -94,7 +94,7 @@
* or <tt>null</tt> if not set.
*/
public String getParameter( String name ) {
- return null;
+ return _webApplet.getParameter( name );
}
Index: WebApplet.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/WebApplet.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- WebApplet.java 15 Nov 2002 00:07:33 -0000 1.2
+++ WebApplet.java 22 Nov 2002 17:19:35 -0000 1.3
@@ -24,8 +24,15 @@
import java.net.URLClassLoader;
import java.applet.Applet;
import java.io.IOException;
+import java.util.HashMap;
+import java.util.ArrayList;
+import java.util.Map;
+import java.util.StringTokenizer;
+import java.util.List;
import org.w3c.dom.Node;
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
@@ -40,9 +47,12 @@
private WebResponse _response;
private Node _node;
private String _baseTarget;
+
private URL _codeBase;
private String _className;
private Applet _applet;
+ private HashMap _parameters;
+ private String[] _parameterNames;
final private String CLASS_EXTENSION = ".class";
@@ -57,14 +67,20 @@
/**
* Returns the URL of the codebase used to find the applet classes
*/
- public URL getCodeBase() throws MalformedURLException {
+ public URL getCodeBaseURL() throws MalformedURLException {
if (_codeBase == null) {
- _codeBase = new URL( _response.getURL(), getAttribute( "codebase", "/" ) );
+ _codeBase = new URL( _response.getURL(), getCodeBase() );
}
return _codeBase;
}
+ private String getCodeBase() {
+ final String codeBaseAttribute = getAttribute( "codebase", "/" );
+ return codeBaseAttribute.endsWith( "/" ) ? codeBaseAttribute : (codeBaseAttribute + "/");
+ }
+
+
/**
* Returns the name of the applet main class.
*/
@@ -114,9 +130,60 @@
}
+ /**
+ * Returns the archive specification.
+ */
+ public String getArchiveSpecification() {
+ String specification = getParameter( "archive" );
+ if (specification == null) specification = getAttribute( "archive" );
+ return specification;
+ }
+
+
+ List getArchiveList() throws MalformedURLException {
+ ArrayList al = new ArrayList();
+ StringTokenizer st = new StringTokenizer( getArchiveSpecification(), "," );
+ while (st.hasMoreTokens()) al.add( new URL( _response.getURL(), st.nextToken() ) );
+ return al;
+ }
+
+
+ /**
+ * Returns an array containing the names of the parameters defined for the applet.
+ */
+ public String[] getParameterNames() {
+ if (_parameterNames == null) {
+ ArrayList al = new ArrayList( getParameterMap().keySet() );
+ _parameterNames = (String[]) al.toArray( new String[ al.size() ] );
+ }
+ return _parameterNames;
+ }
+
+
+ /**
+ * Returns the value of the specified applet parameter, or null if not defined.
+ */
+ public String getParameter( String name ) {
+ return (String) getParameterMap().get( name );
+ }
+
+
+ private Map getParameterMap() {
+ if (_parameters == null) {
+ _parameters = new HashMap();
+ NodeList nl = ((Element) _node).getElementsByTagName( "param" );
+ for (int i = 0; i < nl.getLength(); i++) {
+ Node n = nl.item(i);
+ _parameters.put( NodeUtils.getNodeAttribute( n, "name", "" ), NodeUtils.getNodeAttribute( n, "value", "" ) );
+ }
+ }
+ return _parameters;
+ }
+
+
public Applet getApplet() throws MalformedURLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
if (_applet == null) {
- ClassLoader cl = new URLClassLoader( getClassPath() );
+ ClassLoader cl = new URLClassLoader( getClassPath(), null );
Object o = cl.loadClass( getMainClassName() ).newInstance();
if (!(o instanceof Applet)) throw new RuntimeException( getMainClassName() + " is not an Applet" );
_applet = (Applet) o;
@@ -127,7 +194,9 @@
private URL[] getClassPath() throws MalformedURLException {
- return new URL[] { getCodeBase() };
+ List classPath = getArchiveList();
+ classPath.add( getCodeBaseURL() );
+ return (URL[]) classPath.toArray( new URL[ classPath.size() ] );
}
@@ -156,6 +225,5 @@
} catch (SAXException e) {
}
}
-
}
|