KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > el > ValueExpressionLiteral


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  *
21  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
22  */
package com.sun.el;
23
24 import java.io.Externalizable JavaDoc;
25 import java.io.IOException JavaDoc;
26 import javax.el.ELContext;
27 import javax.el.PropertyNotWritableException;
28
29 import java.io.ObjectInput JavaDoc;
30 import java.io.ObjectOutput JavaDoc;
31
32 import javax.el.ValueExpression;
33
34 import com.sun.el.lang.ELSupport;
35 import com.sun.el.util.MessageFactory;
36 import com.sun.el.util.ReflectionUtil;
37
38 public final class ValueExpressionLiteral extends ValueExpression implements
39         Externalizable JavaDoc {
40
41     private static final long serialVersionUID = 1L;
42
43     private Object JavaDoc value;
44
45     private Class JavaDoc expectedType;
46
47     public ValueExpressionLiteral() {
48         super();
49     }
50     
51     public ValueExpressionLiteral(Object JavaDoc value, Class JavaDoc expectedType) {
52         this.value = value;
53         this.expectedType = expectedType;
54     }
55
56     public Object JavaDoc getValue(ELContext context) {
57         if (this.expectedType != null) {
58             return ELSupport.coerceToType(this.value, this.expectedType);
59         }
60         return this.value;
61     }
62
63     public void setValue(ELContext context, Object JavaDoc value) {
64         throw new PropertyNotWritableException(MessageFactory.get(
65                 "error.value.literal.write", this.value));
66     }
67
68     public boolean isReadOnly(ELContext context) {
69         return true;
70     }
71
72     public Class JavaDoc getType(ELContext context) {
73         return (this.value != null) ? this.value.getClass() : null;
74     }
75
76     public Class JavaDoc getExpectedType() {
77         return this.expectedType;
78     }
79
80     public String JavaDoc getExpressionString() {
81         return (this.value != null) ? this.value.toString() : null;
82     }
83
84     public boolean equals(Object JavaDoc obj) {
85         return (obj instanceof ValueExpressionLiteral && this
86                 .equals((ValueExpressionLiteral) obj));
87     }
88
89     public boolean equals(ValueExpressionLiteral ve) {
90         return (ve != null && (this.value != null && ve.value != null && (this.value == ve.value || this.value
91                 .equals(ve.value))));
92     }
93
94     public int hashCode() {
95         return (this.value != null) ? this.value.hashCode() : 0;
96     }
97
98     public boolean isLiteralText() {
99         return true;
100     }
101
102     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
103         out.writeObject(this.value);
104         out.writeUTF((this.expectedType != null) ? this.expectedType.getName()
105                 : "");
106     }
107
108     public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc,
109             ClassNotFoundException JavaDoc {
110         this.value = in.readObject();
111         String JavaDoc type = in.readUTF();
112         if (!"".equals(type)) {
113             this.expectedType = ReflectionUtil.forName(type);
114         }
115     }
116 }
117
Popular Tags