/*
* Copyright 2010 Alibaba Group Holding Limited.
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.citrus.service.form;
import static com.alibaba.citrus.util.CollectionUtil.*;
import java.lang.reflect.Array;
import java.util.List;
import java.util.Map;
import com.alibaba.citrus.expr.ExpressionContext;
import com.alibaba.citrus.util.ToStringBuilder;
import com.alibaba.citrus.util.ToStringBuilder.MapBuilder;
/**
* ��������validator������Ϣ��context��
*
* @author Michael Zhou
*/
public abstract class MessageContext implements ExpressionContext {
protected final Map<Object, Object> thisContext = createHashMap();
/**
* ȡ��ָ��ֵ��
*/
public Object get(String key) {
Object value = thisContext.get(key);
if (value == null) {
value = internalGet(key);
}
ExpressionContext parentContext = getParentContext();
if (value == null && parentContext != null) {
value = parentContext.get(key);
}
return decorate(value);
}
/**
* ���������飬ת���ɸ��ײ�����<code>List</code>��
*/
protected Object decorate(Object value) {
if (value != null && value.getClass().isArray()) {
int length = Array.getLength(value);
List<Object> list = createArrayList(length);
for (int i = 0; i < length; i++) {
list.add(Array.get(value, i));
}
value = list;
}
return value;
}
/**
* ���һ��ֵ��
*/
public void put(String key, Object value) {
if (value == null) {
thisContext.remove(key);
ExpressionContext parentContext = getParentContext();
if (parentContext != null) {
parentContext.put(key, null);
}
} else {
thisContext.put(key, value);
}
}
/**
* �������ֵ��
*/
public void putAll(Map<?, ?> values) {
if (values != null) {
thisContext.putAll(values);
}
}
/**
* ����һ��<code>MessageContext</code>�У��������б���context���������ǡ�
*/
public void copyLocalContext(MessageContext src) {
for (Map.Entry<Object, Object> entry : src.thisContext.entrySet()) {
Object key = entry.getKey();
if (!thisContext.containsKey(key)) {
thisContext.put(key, entry.getValue());
}
}
}
/**
* ȡ��ָ��ֵ��
*/
protected abstract Object internalGet(String key);
/**
* ȡ��parent context��
*/
public abstract ExpressionContext getParentContext();
@Override
public final String toString() {
ToStringBuilder sb = new ToStringBuilder();
MapBuilder mb = new MapBuilder();
buildToString(mb);
mb.append("context", new MapBuilder().setSortKeys(true).setPrintCount(true).appendAll(thisContext));
buildToString(sb);
sb.append(mb);
ExpressionContext parentContext = getParentContext();
if (parentContext != null) {
sb.append(parentContext);
}
return sb.toString();
}
protected abstract void buildToString(ToStringBuilder sb);
protected abstract void buildToString(MapBuilder mb);
}