01: /**
02: * MVEL (The MVFLEX Expression Language)
03: *
04: * Copyright (C) 2007 Christopher Brock, MVFLEX/Valhalla Project and the Codehaus
05: *
06: * Licensed under the Apache License, Version 2.0 (the "License");
07: * you may not use this file except in compliance with the License.
08: * You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: *
18: */package org.mvel.ast;
19:
20: import org.mvel.CompileException;
21: import org.mvel.integration.VariableResolverFactory;
22: import static org.mvel.util.ParseTools.findClassImportResolverFactory;
23: import org.mvel.util.ParseTools;
24:
25: /**
26: * @author Christopher Brock
27: */
28: public class ImportNode extends ASTNode {
29: private Class importClass;
30: private boolean packageImport;
31: private short offset;
32:
33: public ImportNode(char[] expr) {
34:
35: String name = new String(this .name = expr);
36:
37: if (name.endsWith(".*")) {
38: packageImport = true;
39: offset = (short) name.lastIndexOf('.');
40: } else {
41: try {
42: this .importClass = Thread.currentThread()
43: .getContextClassLoader().loadClass(
44: new String(expr));
45: } catch (ClassNotFoundException e) {
46: throw new CompileException("class not found: "
47: + new String(expr));
48: }
49: }
50: }
51:
52: public Object getReducedValueAccelerated(Object ctx,
53: Object this Value, VariableResolverFactory factory) {
54: if (!packageImport) {
55: return findClassImportResolverFactory(factory).addClass(
56: importClass);
57: } else {
58: findClassImportResolverFactory(factory).addPackageImport(
59: new String(name, 0, (int) offset));
60: return null;
61: }
62: }
63:
64: public Object getReducedValue(Object ctx, Object this Value,
65: VariableResolverFactory factory) {
66: return getReducedValueAccelerated(ctx, this Value, factory);
67: }
68:
69: public Class getImportClass() {
70: return importClass;
71: }
72:
73: public boolean isPackageImport() {
74: return packageImport;
75: }
76:
77: public void setPackageImport(boolean packageImport) {
78: this .packageImport = packageImport;
79: }
80:
81: public String getPackageImport() {
82: return new String(name, 0, offset);
83: }
84: }
|