001: /*=============================================================================
002: * Copyright Texas Instruments 2000-2004. All Rights Reserved.
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: * $ProjectHeader: OSCRIPT 0.155 Fri, 20 Dec 2002 18:34:22 -0800 rclark $
019: */
020:
021: package oscript.data;
022:
023: import oscript.exceptions.*;
024:
025: import java.util.*;
026:
027: /**
028: * A wrapper for the JAWIN COM library, for accessing COM/ActiveX objects
029: * on a windows system. For example:
030: * <pre>
031: * var excel = new ComObject("Excel.Application");
032: * ... XXXX
033: * </pre>
034: *
035: * @author Rob Clark (rob@ti.com)
036: */
037: public class ComObject extends OObject {
038: /**
039: * The type object for an instance of ComObject.
040: */
041: public final static Value TYPE = BuiltinType
042: .makeBuiltinType("oscript.data.ComObject");
043: public final static String PARENT_TYPE_NAME = "oscript.data.OObject";
044: public final static String TYPE_NAME = "ComObject";
045: public final static String[] MEMBER_NAMES = new String[] {
046: "getType", "castToJavaObject", "castToString",
047: "bopInstanceOf", "bopEquals", "bopNotEquals", "getMember",
048: "_getDptr" };
049:
050: /**
051: * The dispatch-pointer object is our handle to the COM object.
052: */
053: private org.jawin.DispatchPtr dptr;
054:
055: public static void init() {
056: oscript.OscriptInterpreter.getGlobalScope().createMember(
057: "ComObject", 0).opAssign(TYPE);
058: }
059:
060: /*=======================================================================*/
061: /**
062: * Class Constructor.
063: *
064: * @param dptr the dispatch-ptr object
065: */
066: ComObject(org.jawin.DispatchPtr dptr) {
067: super ();
068:
069: this .dptr = dptr;
070: }
071:
072: /*=======================================================================*/
073: /**
074: * Class Constructor.
075: *
076: * @param str the name of the COM object to access
077: * @throws org.jawin.COMException if COM error
078: */
079: public ComObject(String str) throws org.jawin.COMException {
080: this (initComAndGetDispatchPtr(str));
081: }
082:
083: private final static org.jawin.DispatchPtr initComAndGetDispatchPtr(
084: String str) throws org.jawin.COMException {
085: org.jawin.win32.Ole32.CoInitialize();
086: return new org.jawin.DispatchPtr(str);
087: }
088:
089: /*=======================================================================*/
090: /**
091: * Class Constructor. This is the constructor that gets called via an
092: * BuiltinType instance.
093: *
094: * @param args arguments to this constructor
095: * @throws PackagedScriptObjectException(Exception) if wrong number of args
096: * @throws org.jawin.COMException if COM error
097: */
098: public ComObject(oscript.util.MemberTable args)
099: throws org.jawin.COMException {
100: this (argsToStr(args));
101: }
102:
103: private final static String argsToStr(oscript.util.MemberTable args) {
104: if (args.length() != 1)
105: throw PackagedScriptObjectException
106: .makeExceptionWrapper(new OIllegalArgumentException(
107: "wrong number of args!"));
108:
109: return args.referenceAt(0).castToString();
110: }
111:
112: /*=======================================================================*/
113: /**
114: * Get the type of this object. The returned type doesn't have to take
115: * into account the possibility of a script type extending a built-in
116: * type, since that is handled by {@link #getType}.
117: *
118: * @return the object's type
119: */
120: protected Value getTypeImpl() {
121: return TYPE;
122: }
123:
124: /*=======================================================================*/
125: /**
126: * Get a member of this object.
127: *
128: * @param id the id of the symbol that maps to the member
129: * @param exception whether an exception should be thrown if the
130: * member object is not resolved
131: * @return a reference to the member
132: * @throws PackagedScriptObjectException(NoSuchMemberException)
133: */
134: public Value getMember(int id, boolean exception)
135: throws PackagedScriptObjectException {
136: Value member = super .getMember(id, false);
137:
138: if (member != null)
139: return member;
140:
141: String name = Symbol.getSymbol(id).castToString();
142:
143: try {
144: Object obj = dptr.get(name);
145: if (obj instanceof org.jawin.DispatchPtr)
146: return new ComSubObject((org.jawin.DispatchPtr) obj,
147: name);
148: return new ComProperty(dptr, name, obj);
149: } catch (org.jawin.COMException e) {
150: // possibly a method?
151: return new ComMethod(dptr, name);
152: }
153: // XXX hack during com.develop.jarwin -> org.jawin transition, because
154: // com.develop.jawin.COMException seems to be getting thrown
155: catch (Throwable e) {
156: if (DEBUG)
157: e.printStackTrace();
158:
159: // possibly a method?
160: return new ComMethod(dptr, name);
161: }
162: }
163:
164: // for debug purposes only:
165: public org.jawin.DispatchPtr _getDptr() {
166: return dptr;
167: }
168:
169: // XXX hack for accessing array elements
170: class ComSubObject extends ComObject {
171: private String name;
172: private Object[] indexes;
173:
174: public ComSubObject(org.jawin.DispatchPtr dptr, String name) {
175: this (dptr, name, new Object[0]);
176: }
177:
178: public ComSubObject(org.jawin.DispatchPtr dptr, String name,
179: Object[] indexes) {
180: super (dptr);
181:
182: this .name = name;
183: this .indexes = indexes;
184: }
185:
186: public Value elementAt(Value idx)
187: throws PackagedScriptObjectException {
188: try {
189: Object[] indexes = new Object[this .indexes.length + 1];
190: System.arraycopy(this .indexes, 0, indexes, 0,
191: this .indexes.length);
192:
193: Object[] javaArgs = new Object[1];
194:
195: OArray args = new OArray(1);
196: args.push1(idx);
197: if (JavaBridge.convertArgs(
198: new Class[] { Object.class }, javaArgs, args) < 0)
199: throw PackagedScriptObjectException
200: .makeExceptionWrapper(new OIllegalArgumentException(
201: "wrong arg types!"));
202:
203: indexes[indexes.length - 1] = javaArgs[0];
204:
205: Object obj = ComObject.this .dptr.getN(name, indexes);
206:
207: if (obj instanceof org.jawin.DispatchPtr)
208: return new ComSubObject(
209: (org.jawin.DispatchPtr) obj, name, indexes);
210:
211: return new ComProperty(dptr, name, obj);
212: } catch (org.jawin.COMException e) {
213: throw OJavaException.makeJavaExceptionWrapper(e);
214: }
215: }
216:
217: public void opAssign(Value val)
218: throws PackagedScriptObjectException {
219: Object[] javaArgs = new Object[1];
220:
221: OArray args = new OArray(1);
222: args.push1(val);
223: if (JavaBridge.convertArgs(new Class[] { Object.class },
224: javaArgs, args) < 0)
225: throw PackagedScriptObjectException
226: .makeExceptionWrapper(new OIllegalArgumentException(
227: "wrong arg types!"));
228:
229: try {
230: ComObject.this .dptr.putN(name, indexes, javaArgs[0]);
231: } catch (org.jawin.COMException e) {
232: throw OJavaException.makeJavaExceptionWrapper(e);
233: }
234: }
235: }
236:
237: static final Class[] GETN_PARAMETER_TYPES = ComMethod.INVOKEN_PARAMETER_TYPES;
238: }
239:
240: /*
241: * Local Variables:
242: * tab-width: 2
243: * indent-tabs-mode: nil
244: * mode: java
245: * c-indentation-style: java
246: * c-basic-offset: 2
247: * eval: (c-set-offset 'substatement-open '0)
248: * eval: (c-set-offset 'case-label '+)
249: * eval: (c-set-offset 'inclass '+)
250: * eval: (c-set-offset 'inline-open '0)
251: * End:
252: */
|