KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > expressions > Expressions


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.internal.expressions;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Collection JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.w3c.dom.Element JavaDoc;
18
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IAdapterManager;
21 import org.eclipse.core.runtime.IConfigurationElement;
22 import org.eclipse.core.runtime.Platform;
23
24 import org.eclipse.core.expressions.Expression;
25 import org.eclipse.core.expressions.ICountable;
26 import org.eclipse.core.expressions.IIterable;
27
28 public class Expressions {
29     
30     /* debugging flag to enable tracing */
31     public static final boolean TRACING;
32     static {
33         String JavaDoc value= Platform.getDebugOption("org.eclipse.core.expressions/tracePropertyResolving"); //$NON-NLS-1$
34
TRACING= value != null && value.equalsIgnoreCase("true"); //$NON-NLS-1$
35
}
36     
37     private Expressions() {
38         // no instance
39
}
40     
41     public static boolean isInstanceOf(Object JavaDoc element, String JavaDoc type) {
42         // null isn't an instanceof of anything.
43
if (element == null)
44             return false;
45         return isSubtype(element.getClass(), type);
46     }
47     
48     private static boolean isSubtype(Class JavaDoc clazz, String JavaDoc type) {
49         if (clazz.getName().equals(type))
50             return true;
51         Class JavaDoc superClass= clazz.getSuperclass();
52         if (superClass != null && isSubtype(superClass, type))
53             return true;
54         Class JavaDoc[] interfaces= clazz.getInterfaces();
55         for (int i= 0; i < interfaces.length; i++) {
56             if (isSubtype(interfaces[i], type))
57                 return true;
58         }
59         return false;
60     }
61     
62     public static void checkAttribute(String JavaDoc name, String JavaDoc value) throws CoreException {
63         if (value == null) {
64             throw new CoreException(new ExpressionStatus(
65                 ExpressionStatus.MISSING_ATTRIBUTE,
66                 Messages.format(ExpressionMessages.Expression_attribute_missing, name)));
67         }
68     }
69     
70     public static void checkAttribute(String JavaDoc name, String JavaDoc value, String JavaDoc[] validValues) throws CoreException {
71         checkAttribute(name, value);
72         for (int i= 0; i < validValues.length; i++) {
73             if (value.equals(validValues[i]))
74                 return;
75         }
76         throw new CoreException(new ExpressionStatus(
77             ExpressionStatus.WRONG_ATTRIBUTE_VALUE,
78             Messages.format(ExpressionMessages.Expression_attribute_invalid_value, value)));
79     }
80     
81     public static void checkCollection(Object JavaDoc var, Expression expression) throws CoreException {
82         if (var instanceof Collection JavaDoc)
83             return;
84         throw new CoreException(new ExpressionStatus(
85             ExpressionStatus.VARIABLE_IS_NOT_A_COLLECTION,
86             Messages.format(ExpressionMessages.Expression_variable_not_a_collection, expression.toString())));
87     }
88     
89     public static void checkList(Object JavaDoc var, Expression expression) throws CoreException {
90         if (var instanceof List JavaDoc)
91             return;
92         throw new CoreException(new ExpressionStatus(
93             ExpressionStatus.VARIABLE_IS_NOT_A_LIST,
94             Messages.format(ExpressionMessages.Expression_variable_not_a_list, expression.toString())));
95     }
96     
97     /**
98      * Converts the given variable into an <code>IIterable</code>. If a corresponding adapter can't be found an
99      * exception is thrown. If the corresponding adapter isn't loaded yet, <code>null</code> is returned.
100      *
101      * @param var the variable to turn into an <code>IIterable</code>
102      * @param expression the expression referring to the variable
103      *
104      * @return the <code>IIterable</code> or <code>null<code> if a corresponding adapter isn't loaded yet
105      *
106      * @throws CoreException if the var can't be adapted to an <code>IIterable</code>
107      */

108     public static IIterable getAsIIterable(Object JavaDoc var, Expression expression) throws CoreException {
109         if (var instanceof IIterable) {
110             return (IIterable)var;
111         } else {
112             IAdapterManager manager= Platform.getAdapterManager();
113             IIterable result= (IIterable)manager.getAdapter(var, IIterable.class);
114             if (result != null)
115                 return result;
116             
117             if (manager.queryAdapter(var, IIterable.class.getName()) == IAdapterManager.NOT_LOADED)
118                 return null;
119             
120             throw new CoreException(new ExpressionStatus(
121                 ExpressionStatus.VARIABLE_IS_NOT_A_COLLECTION,
122                 Messages.format(ExpressionMessages.Expression_variable_not_iterable, expression.toString())));
123         }
124     }
125     
126     /**
127      * Converts the given variable into an <code>ICountable</code>. If a corresponding adapter can't be found an
128      * exception is thrown. If the corresponding adapter isn't loaded yet, <code>null</code> is returned.
129      *
130      * @param var the variable to turn into an <code>ICountable</code>
131      * @param expression the expression referring to the variable
132      *
133      * @return the <code>ICountable</code> or <code>null<code> if a corresponding adapter isn't loaded yet
134      *
135      * @throws CoreException if the var can't be adapted to an <code>ICountable</code>
136      */

137     public static ICountable getAsICountable(Object JavaDoc var, Expression expression) throws CoreException {
138         if (var instanceof ICountable) {
139             return (ICountable)var;
140         } else {
141             IAdapterManager manager= Platform.getAdapterManager();
142             ICountable result= (ICountable)manager.getAdapter(var, ICountable.class);
143             if (result != null)
144                 return result;
145             
146             if (manager.queryAdapter(var, ICountable.class.getName()) == IAdapterManager.NOT_LOADED)
147                 return null;
148             
149             throw new CoreException(new ExpressionStatus(
150                 ExpressionStatus.VARIABLE_IS_NOT_A_COLLECTION,
151                 Messages.format(ExpressionMessages.Expression_variable_not_countable, expression.toString())));
152         }
153     }
154     
155     public static boolean getOptionalBooleanAttribute(IConfigurationElement element, String JavaDoc attributeName) {
156         String JavaDoc value= element.getAttribute(attributeName);
157         if (value == null)
158             return false;
159         return Boolean.valueOf(value).booleanValue();
160     }
161
162     public static boolean getOptionalBooleanAttribute(Element JavaDoc element, String JavaDoc attributeName) {
163         String JavaDoc value= element.getAttribute(attributeName);
164         if (value.length() == 0)
165             return false;
166         return Boolean.valueOf(value).booleanValue();
167     }
168
169     //---- Argument parsing --------------------------------------------
170

171     public static final Object JavaDoc[] EMPTY_ARGS= new Object JavaDoc[0];
172     
173     public static Object JavaDoc[] getArguments(IConfigurationElement element, String JavaDoc attributeName) throws CoreException {
174         String JavaDoc args= element.getAttribute(attributeName);
175         if (args != null) {
176             return parseArguments(args);
177         } else {
178             return EMPTY_ARGS;
179         }
180     }
181
182     public static Object JavaDoc[] getArguments(Element JavaDoc element, String JavaDoc attributeName) throws CoreException {
183         String JavaDoc args= element.getAttribute(attributeName);
184         if (args.length() > 0) {
185             return parseArguments(args);
186         } else {
187             return EMPTY_ARGS;
188         }
189     }
190
191     public static Object JavaDoc[] parseArguments(String JavaDoc args) throws CoreException {
192         List JavaDoc result= new ArrayList JavaDoc();
193         int start= 0;
194         int comma;
195         while ((comma= findNextComma(args, start)) != -1) {
196             result.add(convertArgument(args.substring(start, comma).trim()));
197             start= comma + 1;
198         }
199         result.add(convertArgument(args.substring(start).trim()));
200         return result.toArray();
201     }
202     
203     private static int findNextComma(String JavaDoc str, int start) throws CoreException {
204         boolean inString= false;
205         for (int i= start; i < str.length(); i++) {
206             char ch= str.charAt(i);
207             if (ch == ',' && ! inString)
208                 return i;
209             if (ch == '\'') {
210                 if (!inString) {
211                     inString= true;
212                 } else {
213                     if (i + 1 < str.length() && str.charAt(i + 1) == '\'') {
214                         i++;
215                     } else {
216                         inString= false;
217                     }
218                 }
219             } else if (ch == ',' && !inString) {
220                 return i;
221             }
222         }
223         if (inString)
224             throw new CoreException(new ExpressionStatus(
225                 ExpressionStatus.STRING_NOT_TERMINATED,
226                 Messages.format(ExpressionMessages.Expression_string_not_terminated, str)));
227             
228         return -1;
229     }
230         
231     public static Object JavaDoc convertArgument(String JavaDoc arg) throws CoreException {
232         if (arg == null) {
233             return null;
234         } else if (arg.length() == 0) {
235             return arg;
236         } else if (arg.charAt(0) == '\'' && arg.charAt(arg.length() - 1) == '\'') {
237             return unEscapeString(arg.substring(1, arg.length() - 1));
238         } else if ("true".equals(arg)) { //$NON-NLS-1$
239
return Boolean.TRUE;
240         } else if ("false".equals(arg)) { //$NON-NLS-1$
241
return Boolean.FALSE;
242         } else if (arg.indexOf('.') != -1) {
243             try {
244                 return Float.valueOf(arg);
245             } catch (NumberFormatException JavaDoc e) {
246                 return arg;
247             }
248         } else {
249             try {
250                 return Integer.valueOf(arg);
251             } catch (NumberFormatException JavaDoc e) {
252                 return arg;
253             }
254         }
255     }
256
257     public static String JavaDoc unEscapeString(String JavaDoc str) throws CoreException {
258         StringBuffer JavaDoc result= new StringBuffer JavaDoc();
259         for (int i= 0; i < str.length(); i++) {
260             char ch= str.charAt(i);
261             if (ch == '\'') {
262                 if (i == str.length() - 1 || str.charAt(i + 1) != '\'')
263                     throw new CoreException(new ExpressionStatus(
264                         ExpressionStatus.STRING_NOT_CORRECT_ESCAPED,
265                         Messages.format(ExpressionMessages.Expression_string_not_correctly_escaped, str)));
266                 result.append('\'');
267                 i++;
268             } else {
269                 result.append(ch);
270             }
271         }
272         return result.toString();
273     }
274 }
275
Popular Tags