KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > execution > ScriptType


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide.execution;
21
22 import java.lang.reflect.InvocationTargetException JavaDoc;
23 import java.util.Collections JavaDoc;
24 import org.openide.ServiceType;
25 import org.openide.filesystems.FileObject;
26 import org.openide.util.Lookup;
27
28 /**
29  * Base class for scripting interpreters.
30  * @author dstrupl
31  * @deprecated Was used only in obsolete modules.
32  */

33 public abstract class ScriptType extends org.openide.ServiceType {
34
35     /** generated Serialized Version UID */
36     private static final long serialVersionUID = 4893207884933024341L;
37
38     /**
39      * The script type can decide whether it will be able to execute
40      * the given file object.
41      * @param fo a file to test
42      * @return true if the script can operate on this file
43      */

44     public abstract boolean acceptFileObject(FileObject fo);
45     
46     /**
47      * Evaluate the script given in the form of a Reader.
48      * @param r
49      * @param context
50      * @return whatever is the result of the script. It can be null.
51      */

52     public abstract Object JavaDoc eval(java.io.Reader JavaDoc r, Context context) throws InvocationTargetException JavaDoc;
53     
54     /** Calls eval(Reader, Context) with getDefaultContext() as
55      * the second argument.
56      */

57     public final Object JavaDoc eval(java.io.Reader JavaDoc r) throws InvocationTargetException JavaDoc {
58         return eval(r, getDefaultContext());
59     }
60     
61     /**
62      * Evaluate the script given in the form of a string.
63      * @param script
64      * @param context
65      * @return whatever is the result of the script. It can be null.
66      */

67     public abstract Object JavaDoc eval(String JavaDoc script, Context context) throws InvocationTargetException JavaDoc;
68     
69     /** Calls eval(String, Context) with getDefaultContext() as
70      * the second argument.
71      */

72     public final Object JavaDoc eval(String JavaDoc script) throws InvocationTargetException JavaDoc {
73         return eval(script, getDefaultContext());
74     }
75     
76     /**
77      * Execute the script given in the form of a Reader.
78      * @param r the contents of the script
79      * @param context the context in which to evaluate it
80      */

81     public abstract void exec(java.io.Reader JavaDoc r, Context context) throws InvocationTargetException JavaDoc;
82     
83     /** Calls exec(Reader, Context) with getDefaultContext() as
84      * the second argument.
85      */

86     public final void exec(java.io.Reader JavaDoc r) throws InvocationTargetException JavaDoc {
87         exec(r, getDefaultContext());
88     }
89     
90     /**
91      * Execute the script given in the form of a string.
92      * @param script
93      * @param context
94      * @return whatever is the result of the script. It can be null.
95      */

96     public abstract void exec(String JavaDoc script, Context context) throws InvocationTargetException JavaDoc;
97     
98     /** Calls exec(String, Context) with getDefaultContext() as
99      * the second argument.
100      */

101     public final void exec(String JavaDoc script) throws InvocationTargetException JavaDoc {
102         exec(script, getDefaultContext());
103     }
104
105     /**
106      * Adds variable with name to the variables known by the script type.
107      * @param name the name for the newly created variable
108      * @param value initial value variable value (can be null).
109      */

110     public abstract void addVariable(String JavaDoc name, Object JavaDoc value);
111     
112     /** Get all registered script types.
113     * @return enumeration of <code>ScriptType</code>s
114     * @deprecated Please use {@link org.openide.util.Lookup} instead.
115     */

116     public static java.util.Enumeration JavaDoc scriptTypes () {
117         return Collections.enumeration(Lookup.getDefault().lookup(new Lookup.Template(ScriptType.class)).allInstances());
118     }
119
120     /**
121      * Find the script type implemented as a given class.
122      * @param clazz the class of the script type looked for
123      * @return the desired script type or <code>null</code> if it does not exist
124     * @deprecated Please use {@link org.openide.util.Lookup} instead.
125      */

126     public static ScriptType find (Class JavaDoc clazz) {
127         return (ScriptType)Lookup.getDefault().lookup(clazz);
128     }
129
130     /**
131      * Find the script type with requested name.
132      * @param name (display) name of script type to find
133      * @return the desired script type or <code>null</code> if it does not exist
134      */

135     public static ScriptType find (String JavaDoc name) {
136         ServiceType t = ((ServiceType.Registry)Lookup.getDefault().lookup(ServiceType.Registry.class)).find (name);
137         if (t instanceof ScriptType) {
138             return (ScriptType)t;
139         } else {
140             return null;
141         }
142     }
143
144     /** Get the default script type.
145     * @return the default script type
146     * @deprecated Probably meaningless, find all available types and filter with {@link #acceptFileObject} instead.
147     */

148     public static ScriptType getDefault () {
149         java.util.Enumeration JavaDoc en = scriptTypes ();
150         if (en.hasMoreElements()) {
151             return (ScriptType)en.nextElement ();
152         } else {
153             throw new RuntimeException JavaDoc("No script type registered."); // NOI18N
154
}
155     }
156     
157     static Context getDefaultContext() {
158         return new Context();
159     }
160
161     /** Scripting context.
162      * Using instances of this class you can add additional parameters
163      * to the script execution using methods eval or exec.
164      */

165     public static class Context {
166     }
167 }
168
Popular Tags