KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > el > ValueExpressionLiteral


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.el;
19
20 import java.io.Externalizable JavaDoc;
21 import java.io.IOException JavaDoc;
22 import javax.el.ELContext;
23 import javax.el.PropertyNotWritableException;
24
25 import java.io.ObjectInput JavaDoc;
26 import java.io.ObjectOutput JavaDoc;
27
28 import javax.el.ValueExpression;
29
30 import org.apache.el.lang.ELSupport;
31 import org.apache.el.util.MessageFactory;
32 import org.apache.el.util.ReflectionUtil;
33
34
35 public final class ValueExpressionLiteral extends ValueExpression implements
36         Externalizable JavaDoc {
37
38     private static final long serialVersionUID = 1L;
39
40     private Object JavaDoc value;
41
42     private Class JavaDoc expectedType;
43
44     public ValueExpressionLiteral() {
45         super();
46     }
47     
48     public ValueExpressionLiteral(Object JavaDoc value, Class JavaDoc expectedType) {
49         this.value = value;
50         this.expectedType = expectedType;
51     }
52
53     public Object JavaDoc getValue(ELContext context) {
54         if (this.expectedType != null) {
55             return ELSupport.coerceToType(this.value, this.expectedType);
56         }
57         return this.value;
58     }
59
60     public void setValue(ELContext context, Object JavaDoc value) {
61         throw new PropertyNotWritableException(MessageFactory.get(
62                 "error.value.literal.write", this.value));
63     }
64
65     public boolean isReadOnly(ELContext context) {
66         return true;
67     }
68
69     public Class JavaDoc getType(ELContext context) {
70         return (this.value != null) ? this.value.getClass() : null;
71     }
72
73     public Class JavaDoc getExpectedType() {
74         return this.expectedType;
75     }
76
77     public String JavaDoc getExpressionString() {
78         return (this.value != null) ? this.value.toString() : null;
79     }
80
81     public boolean equals(Object JavaDoc obj) {
82         return (obj instanceof ValueExpressionLiteral && this
83                 .equals((ValueExpressionLiteral) obj));
84     }
85
86     public boolean equals(ValueExpressionLiteral ve) {
87         return (ve != null && (this.value != null && ve.value != null && (this.value == ve.value || this.value
88                 .equals(ve.value))));
89     }
90
91     public int hashCode() {
92         return (this.value != null) ? this.value.hashCode() : 0;
93     }
94
95     public boolean isLiteralText() {
96         return true;
97     }
98
99     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
100         out.writeObject(this.value);
101         out.writeUTF((this.expectedType != null) ? this.expectedType.getName()
102                 : "");
103     }
104
105     public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc,
106             ClassNotFoundException JavaDoc {
107         this.value = in.readObject();
108         String JavaDoc type = in.readUTF();
109         if (!"".equals(type)) {
110             this.expectedType = ReflectionUtil.forName(type);
111         }
112     }
113 }
114
Popular Tags