001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.el;
019:
020: import java.io.Externalizable;
021: import java.io.IOException;
022: import javax.el.ELContext;
023: import javax.el.PropertyNotWritableException;
024:
025: import java.io.ObjectInput;
026: import java.io.ObjectOutput;
027:
028: import javax.el.ValueExpression;
029:
030: import org.apache.el.lang.ELSupport;
031: import org.apache.el.util.MessageFactory;
032: import org.apache.el.util.ReflectionUtil;
033:
034: public final class ValueExpressionLiteral extends ValueExpression
035: implements Externalizable {
036:
037: private static final long serialVersionUID = 1L;
038:
039: private Object value;
040:
041: private Class expectedType;
042:
043: public ValueExpressionLiteral() {
044: super ();
045: }
046:
047: public ValueExpressionLiteral(Object value, Class expectedType) {
048: this .value = value;
049: this .expectedType = expectedType;
050: }
051:
052: public Object getValue(ELContext context) {
053: if (this .expectedType != null) {
054: return ELSupport
055: .coerceToType(this .value, this .expectedType);
056: }
057: return this .value;
058: }
059:
060: public void setValue(ELContext context, Object value) {
061: throw new PropertyNotWritableException(MessageFactory.get(
062: "error.value.literal.write", this .value));
063: }
064:
065: public boolean isReadOnly(ELContext context) {
066: return true;
067: }
068:
069: public Class getType(ELContext context) {
070: return (this .value != null) ? this .value.getClass() : null;
071: }
072:
073: public Class getExpectedType() {
074: return this .expectedType;
075: }
076:
077: public String getExpressionString() {
078: return (this .value != null) ? this .value.toString() : null;
079: }
080:
081: public boolean equals(Object obj) {
082: return (obj instanceof ValueExpressionLiteral && this
083: .equals((ValueExpressionLiteral) obj));
084: }
085:
086: public boolean equals(ValueExpressionLiteral ve) {
087: return (ve != null && (this .value != null && ve.value != null && (this .value == ve.value || this .value
088: .equals(ve.value))));
089: }
090:
091: public int hashCode() {
092: return (this .value != null) ? this .value.hashCode() : 0;
093: }
094:
095: public boolean isLiteralText() {
096: return true;
097: }
098:
099: public void writeExternal(ObjectOutput out) throws IOException {
100: out.writeObject(this .value);
101: out.writeUTF((this .expectedType != null) ? this .expectedType
102: .getName() : "");
103: }
104:
105: public void readExternal(ObjectInput in) throws IOException,
106: ClassNotFoundException {
107: this .value = in.readObject();
108: String type = in.readUTF();
109: if (!"".equals(type)) {
110: this.expectedType = ReflectionUtil.forName(type);
111: }
112: }
113: }
|