KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > gargoylesoftware > htmlunit > javascript > host > ActiveXObject


1 /*
2  * Copyright (c) 2002, 2005 Gargoyle Software Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12  * 3. The end-user documentation included with the redistribution, if any, must
13  * include the following acknowledgment:
14  *
15  * "This product includes software developed by Gargoyle Software Inc.
16  * (http://www.GargoyleSoftware.com/)."
17  *
18  * Alternately, this acknowledgment may appear in the software itself, if
19  * and wherever such third-party acknowledgments normally appear.
20  * 4. The name "Gargoyle Software" must not be used to endorse or promote
21  * products derived from this software without prior written permission.
22  * For written permission, please contact info@GargoyleSoftware.com.
23  * 5. Products derived from this software may not be called "HtmlUnit", nor may
24  * "HtmlUnit" appear in their name, without prior written permission of
25  * Gargoyle Software Inc.
26  *
27  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
28  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
29  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
30  * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
33  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
36  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37  */

38 package com.gargoylesoftware.htmlunit.javascript.host;
39
40 import java.util.Map JavaDoc;
41
42 import org.mozilla.javascript.Context;
43 import org.mozilla.javascript.Function;
44 import org.mozilla.javascript.Scriptable;
45
46 import com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine;
47 import com.gargoylesoftware.htmlunit.javascript.SimpleScriptable;
48
49 /**
50  * This is the host object that allows javascript to instantiate java objects via the ActiveXObject
51  * constructor. This host object enables a person to emulate ActiveXObjects in javascript with java
52  * objects. See the <code>WebClient</code> class to see how ActiveXObject string parameter specifies
53  * which java class is instantiated.
54  * @see com.gargoylesoftware.htmlunit.WebClient
55  * @version $Revision: 100 $
56  * @author <a HREF="mailto:bcurren@esomnie.com">Ben Curren</a>
57  */

58 public class ActiveXObject extends SimpleScriptable {
59     private static final long serialVersionUID = 7327032075131452226L;
60
61     /**
62      * The default constructor.
63      */

64     public ActiveXObject() {
65     }
66
67     /**
68      * This method searches the map specified in the <code>WebClient</code> class for the java
69      * object to instantiate based on the ActiveXObject constructor String.
70      * @param cx The current context
71      * @param args The arguments to the ActiveXObject constructor
72      * @param ctorObj The function object
73      * @param inNewExpr Is new or not
74      * @return the java object to allow javascript to access
75      */

76     public static Scriptable jsConstructor(
77             final Context cx, final Object JavaDoc[] args, final Function ctorObj,
78             final boolean inNewExpr) {
79         if( args.length < 1 || args.length > 2 ) {
80             throw Context.reportRuntimeError(
81                     "ActiveXObject Error: constructor must have one or two String parameters." );
82         }
83         if( args[0] == Context.getUndefinedValue() ) {
84             throw Context.reportRuntimeError( "ActiveXObject Error: constructor parameter is undefined." );
85         }
86         if( !(args[0] instanceof String JavaDoc) ) {
87             throw Context.reportRuntimeError( "ActiveXObject Error: constructor parameter must be a String." );
88         }
89         final Map JavaDoc map = JavaScriptEngine.getWebClientForCurrentThread().getActiveXObjectMap();
90         if ( map == null ) {
91              throw Context.reportRuntimeError( "ActiveXObject Error: the map is null." );
92         }
93         final Object JavaDoc mapValue = map.get(args[0]);
94         if( mapValue == null ) {
95             throw Context.reportRuntimeError( "ActiveXObject Error: no map for " + args[0] + "." );
96         }
97         if( !(mapValue instanceof String JavaDoc) ) {
98             throw Context.reportRuntimeError( "ActiveXObject Error: value for " + args[0] + " is not a String." );
99         }
100
101         final String JavaDoc xClassString = (String JavaDoc)mapValue;
102         Object JavaDoc object = null;
103         try {
104             final Class JavaDoc xClass = Class.forName(xClassString);
105             object = xClass.newInstance();
106         }
107         catch( final Exception JavaDoc e ) {
108             throw Context.reportRuntimeError( "ActiveXObject Error: failed instantiating class " + xClassString +
109                     " because " + e.getMessage() + "." );
110         }
111         return Context.toObject( object, ctorObj );
112     }
113
114     /**
115      * Get the name of the host object class.
116      * @return the javascript class name
117      */

118     public String JavaDoc getClassName() {
119         return "ActiveXObject";
120     }
121 }
122
Popular Tags