All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.tinygroup.placeholder.impl.PlaceholderContextFormater Maven / Gradle / Ivy

The newest version!
/**
 *  Copyright (c) 1997-2013, tinygroup.org ([email protected]).
 *
 *  Licensed under the GPL, Version 3.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.gnu.org/licenses/gpl.html
 *
 *  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.
 * --------------------------------------------------------------------------
 *  版权 (c) 1997-2013, tinygroup.org ([email protected]).
 *
 *  本开源软件遵循 GPL 3.0 协议;
 *  如果您不遵循此协议,则不被允许使用此文件。
 *  你可以从下面的地址获取完整的协议文本
 *
 *       http://www.gnu.org/licenses/gpl.html
 */
package org.tinygroup.placeholder.impl;

import java.util.HashMap;
import java.util.Map;

import org.tinygroup.context.Context;
import org.tinygroup.format.FormatProvider;
import org.tinygroup.format.exception.FormatException;
import org.tinygroup.logger.LogLevel;
import org.tinygroup.logger.Logger;
import org.tinygroup.logger.LoggerFactory;
import org.tinygroup.placeholder.Validator;
import org.tinygroup.placeholder.exception.PlaceholderException;

/**
 * 占位符格式化器
 * @author luoguo
 *
 */
public class PlaceholderContextFormater implements FormatProvider {
	private static final String DEFAULT_SUFFIX = ".default";
	private String defaultSuffix = DEFAULT_SUFFIX;
	
	private static final Logger logger = LoggerFactory
	.getLogger(PlaceholderContextFormater.class);

	private Map nameValidator = new HashMap();

	public String getDefaultSuffix() {
		return defaultSuffix;
	}

	public void setDefaultSuffix(String defaultSuffix) {
		this.defaultSuffix = defaultSuffix;
	}

	public String format(Context context, String string) throws FormatException {
		
		Object obj = context.get(string);
		if (obj == null) {
			obj = context.get(string + DEFAULT_SUFFIX);
			if (obj == null) {
				throw new FormatException(new PlaceholderException(
						"property_name_not_found", string));
			}
		}
		if (validateValue(string, obj.toString())) {
			return obj.toString();
		} else {
			throw new FormatException(new PlaceholderException(
					"property_value_validator_failure", string));
		}

	}

	/**
	 * 如果没有校验器,则返回true
	 * @param string
	 * @param string2
	 * @return
	 */
	private boolean validateValue(String string, String value) {
		Validator validator=nameValidator.get(string);
		if(validator!=null){
			logger.logMessage(LogLevel.DEBUG, "占位符[{0}]找到对应的校验器[{1}]",string,validator.getName());
			boolean result= validator.validate(value);
			if(!result){
			logger.logMessage(LogLevel.DEBUG, "占位符[{0}]找到对应的校验器[{1}],但是校验失败",string,validator.getName());
			}
			return result;
		}
		return true;
	}
	
	void addValidator(String name,Validator validator){
		nameValidator.put(name, validator);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy