001: package com.meterware.httpunit;
002:
003: /********************************************************************************************************************
004: * $Id: WebApplet.java,v 1.7 2003/03/12 15:40:44 russgold Exp $
005: *
006: * Copyright (c) 2002, Russell Gold
007: *
008: * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
009: * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
010: * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
011: * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included in all copies or substantial portions
014: * of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
017: * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
018: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
019: * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
020: * DEALINGS IN THE SOFTWARE.
021: *
022: *******************************************************************************************************************/
023: import java.net.URL;
024: import java.net.MalformedURLException;
025: import java.net.URLClassLoader;
026: import java.applet.Applet;
027: import java.io.IOException;
028: import java.util.HashMap;
029: import java.util.ArrayList;
030: import java.util.Map;
031: import java.util.StringTokenizer;
032: import java.util.List;
033:
034: import org.w3c.dom.Node;
035: import org.w3c.dom.Element;
036: import org.w3c.dom.NodeList;
037: import org.xml.sax.SAXException;
038: import com.meterware.httpunit.scripting.ScriptableDelegate;
039:
040: /**
041: * This class represents the embedding of an applet in a web page.
042: *
043: * @author <a href="mailto:Oliver.Imbusch.extern@HVBInfo.com">Oliver Imbusch</a>
044: * @author <a href="mailto:russgold@httpunit.org">Russell Gold</a>
045: **/
046: public class WebApplet extends HTMLElementBase {
047:
048: private WebResponse _response;
049: private String _baseTarget;
050:
051: private URL _codeBase;
052: private String _className;
053: private Applet _applet;
054: private HashMap _parameters;
055: private String[] _parameterNames;
056:
057: final private String CLASS_EXTENSION = ".class";
058:
059: public WebApplet(WebResponse response, Node rootNode,
060: String baseTarget) {
061: super (rootNode);
062: _response = response;
063: _baseTarget = baseTarget;
064: }
065:
066: /**
067: * Returns the URL of the codebase used to find the applet classes
068: */
069: public URL getCodeBaseURL() throws MalformedURLException {
070: if (_codeBase == null) {
071: _codeBase = new URL(_response.getURL(), getCodeBase());
072: }
073: return _codeBase;
074: }
075:
076: private String getCodeBase() {
077: final String codeBaseAttribute = getAttribute("codebase", "/");
078: return codeBaseAttribute.endsWith("/") ? codeBaseAttribute
079: : (codeBaseAttribute + "/");
080: }
081:
082: /**
083: * Returns the name of the applet main class.
084: */
085: public String getMainClassName() {
086: if (_className == null) {
087: _className = getAttribute("code");
088: if (_className.endsWith(CLASS_EXTENSION)) {
089: _className = _className.substring(0, _className
090: .lastIndexOf(CLASS_EXTENSION));
091: }
092: _className = _className.replace('/', '.')
093: .replace('\\', '.');
094: }
095: return _className;
096: }
097:
098: /**
099: * Returns the width of the panel in which the applet will be drawn.
100: */
101: public int getWidth() {
102: return Integer.parseInt(getAttribute("width"));
103: }
104:
105: /**
106: * Returns the height of the panel in which the applet will be drawn.
107: */
108: public int getHeight() {
109: return Integer.parseInt(getAttribute("height"));
110: }
111:
112: /**
113: * Returns the archive specification.
114: */
115: public String getArchiveSpecification() {
116: String specification = getParameter("archive");
117: if (specification == null)
118: specification = getAttribute("archive");
119: return specification;
120: }
121:
122: List getArchiveList() throws MalformedURLException {
123: ArrayList al = new ArrayList();
124: StringTokenizer st = new StringTokenizer(
125: getArchiveSpecification(), ",");
126: while (st.hasMoreTokens())
127: al.add(new URL(getCodeBaseURL(), st.nextToken()));
128: return al;
129: }
130:
131: /**
132: * Returns an array containing the names of the parameters defined for the applet.
133: */
134: public String[] getParameterNames() {
135: if (_parameterNames == null) {
136: ArrayList al = new ArrayList(getParameterMap().keySet());
137: _parameterNames = (String[]) al.toArray(new String[al
138: .size()]);
139: }
140: return _parameterNames;
141: }
142:
143: /**
144: * Returns the value of the specified applet parameter, or null if not defined.
145: */
146: public String getParameter(String name) {
147: return (String) getParameterMap().get(name);
148: }
149:
150: private Map getParameterMap() {
151: if (_parameters == null) {
152: _parameters = new HashMap();
153: NodeList nl = ((Element) getNode())
154: .getElementsByTagName("param");
155: for (int i = 0; i < nl.getLength(); i++) {
156: Node n = nl.item(i);
157: _parameters
158: .put(NodeUtils.getNodeAttribute(n, "name", ""),
159: NodeUtils.getNodeAttribute(n, "value",
160: ""));
161: }
162: }
163: return _parameters;
164: }
165:
166: public Applet getApplet() throws MalformedURLException,
167: ClassNotFoundException, InstantiationException,
168: IllegalAccessException {
169: if (_applet == null) {
170: ClassLoader cl = new URLClassLoader(getClassPath(), null);
171: Object o = cl.loadClass(getMainClassName()).newInstance();
172: if (!(o instanceof Applet))
173: throw new RuntimeException(getMainClassName()
174: + " is not an Applet");
175: _applet = (Applet) o;
176: _applet.setStub(new AppletStubImpl(this ));
177: }
178: return _applet;
179: }
180:
181: private URL[] getClassPath() throws MalformedURLException {
182: List classPath = getArchiveList();
183: classPath.add(getCodeBaseURL());
184: return (URL[]) classPath.toArray(new URL[classPath.size()]);
185: }
186:
187: String getBaseTarget() {
188: return _baseTarget;
189: }
190:
191: WebApplet[] getAppletsInPage() {
192: try {
193: return _response.getApplets();
194: } catch (SAXException e) {
195: e.printStackTrace(); // should never happen.
196: return null;
197: }
198: }
199:
200: void sendRequest(URL url, String target) {
201: WebRequest wr = new GetMethodWebRequest(null, url
202: .toExternalForm(), target);
203: try {
204: _response.getWindow().getResponse(wr);
205: } catch (IOException e) {
206: e.printStackTrace(); //To change body of catch statement use Options | File Templates.
207: throw new RuntimeException(e.toString());
208: } catch (SAXException e) {
209: }
210: }
211:
212: protected ScriptableDelegate newScriptable() {
213: return new HTMLElementScriptable(this );
214: }
215:
216: protected ScriptableDelegate getParentDelegate() {
217: return _response.getScriptableObject().getDocument();
218: }
219:
220: }
|