- /**
- * Copyright: Copyright (c) 2002-2004
- * Company: JavaResearch(http://www.javaresearch.org)
- */
-
- package org.jr.java2html;
-
- import java.util.*;
-
- /**
- * 模版属性映射的抽象类。
- * 此类定义模版文件中的属性关键字的格式:<BR>
- * <b>##UPPCASE_STRING@@</b><BR>
- * 前面两个'#',接着是全部大写的字符串,单词之间建议使用下划线进行分隔。另外在##和@@之间不能再包含##或者@@。<BR>
- * 模版文件中具有以上形式的内容都将作为模版属性进行替换。
- * <br>最后更新日期:2003年5月23日
- * @author cherami@javaresearch.org
- * @version 0.9
- */
- public class TemplatePropertyMap
- extends HashMap {
- /**
- * 属性前缀。
- */
- public static final String PROPERTY_KEY_PREFIX = "##";
- /**
- * 属性后缀。
- */
- public static final String PROPERTY_KEY_SUFFIX = "@@";
- static final int KEY_LEAST_LENGTH = PROPERTY_KEY_PREFIX.length() +
- PROPERTY_KEY_SUFFIX.length() + 1;
- /**
- * 构造一个空的TemplatePropertyMap。
- */
- public TemplatePropertyMap() {
- }
- /**
- * 以初始容量构造一个空的TemplatePropertyMap。
- * @param initialCapacity 初始容量
- */
- public TemplatePropertyMap(int initialCapacity) {
- super(initialCapacity);
- }
- /**
- * 以初始容量和装载因子构造一个空的TemplatePropertyMap。
- * @param initialCapacity 初始容量
- * @param loadFactor 装载因子
- */
- public TemplatePropertyMap(int initialCapacity,
- float loadFactor) {
- super(initialCapacity, loadFactor);
- }
- /**
- * 向映射中增加元素。
- * 关键字会被转换为一个字符串,如果关键字不是字符串实例对象会抛出IllegalArgumentException异常。
- * @param key 关键字
- * @param value 值
- * @return 关键字对应的上一个值,如果没有则返回null
- * @see #put(String key,Object value) put(String key,Object value)
- */
- public Object put(Object key,
- Object value) {
- if (!(key instanceof String)) {
- throw new IllegalArgumentException(
- "Your key must be a string object!");
- }
- return put((String)key,value);
- }
-
- /**
- * 向映射中增加元素。
- * 此方法会自动将普通的没有属性前缀和后缀的关键字转换为符合属性格式的关键字。
- * 如果关键字已经是符合的格式则不再进行处理。
- * <BR><b>因此关键字不能是null。</b>
- * 如果关键字包含小写字母、不符合规则但是又包含属性属性格式的前缀或者后缀、关键字和值相等将不能作为属性加入映射,并且会抛出IllegalArgumentException异常。<br>
- * <b>注意值可以是任何类对象的,在进行替换的时候将使用值的toString()方法得到的内容进行替换。</b>
- * @param key 关键字
- * @param value 值
- * @return 关键字对应的上一个值,如果没有则返回null
- */
- public Object put(String key,
- Object value) {
- if (!Utility.isWholeUppercase(key)) {
- throw new IllegalArgumentException(
- "Your property key must be all UPPERCASE!");
- }
- if (isPropertyKey(key)) {
- return directPut(key, value);
- }
- else {
- if (key.indexOf(PROPERTY_KEY_PREFIX)>=0||key.indexOf(PROPERTY_KEY_SUFFIX)>=0) {
- throw new IllegalArgumentException(
- "Your property key contain special string "+PROPERTY_KEY_PREFIX+" or "+PROPERTY_KEY_SUFFIX+",can not be converted to a property key!");
- }
- }
- return directPut(PROPERTY_KEY_PREFIX + key + PROPERTY_KEY_SUFFIX, value);
- }
- /**
- * <b>本方法被禁用。</b>由于本类的关键字有特殊的要求,因此屏蔽了父类中的此方法的使用。可以使用本类的另外一个重载的方法。
- * @param t 映射
- */
-
- public void putAll(Map t) {
- throw new UnsupportedOperationException("This method have been forbbiden by special Key define");
- }
-
- /**
- * 将另外一个TemplatePropertyMap中的内容合并到此映射中。
- * @param t 模版属性映射
- */
- public void putAll(TemplatePropertyMap t) {
- if (t!=null) {
- super.putAll(t);
- }
- }
-
- /**
- * 将关键字和值加入映射。
- * @param key 关键字
- * @param value 值
- * @return 关键字对应的上一个值,如果没有则返回null
- */
- private Object directPut(String key,Object value) {
- if (key.equals(value)) {
- throw new IllegalArgumentException(
- "Your converted property key must not be same as value!"+"converted key:"+key+",value:"+value);
- }
- return super.put(key, value);
- }
- /**
- * 判断给定的关键字是否是一个合法的属性关键字。
- * @param key 要判断的关键字
- * @return 严格符合属性关键字的定义时返回true,否则返回false。
- */
- public static boolean isPropertyKey(String key) {
- if (key==null||key.length()<KEY_LEAST_LENGTH) {
- return false;
- }
- if (key.startsWith(PROPERTY_KEY_PREFIX)&&key.endsWith(PROPERTY_KEY_SUFFIX)) {
- int prefixIndex, suffixIndex;
- prefixIndex = key.indexOf(PROPERTY_KEY_PREFIX);
- suffixIndex = key.lastIndexOf(PROPERTY_KEY_SUFFIX);
- if (key.indexOf(PROPERTY_KEY_PREFIX,prefixIndex+1)>0||key.lastIndexOf(PROPERTY_KEY_SUFFIX,suffixIndex-1)>0) {
- return false;
- }
- String k = key.substring(prefixIndex + 2, suffixIndex);
- if (Utility.isWholeUppercase(k)) {
- return true;
- }
- else {
- return false;
- }
- }
- return false;
- }
- /**
- * 判断指定的字符串中是否包含符合规则的属性关键字。
- * @param source 字符串
- * @return 包含至少一个符合属性关键字要求的值时返回true,否则返回false
- */
- public static boolean containPropertyKey(String source) {
- if (source == null || source.length() < KEY_LEAST_LENGTH) {
- return false;
- }
- int prefixIndex, suffixIndex;
- prefixIndex = source.indexOf(PROPERTY_KEY_PREFIX);
- while (prefixIndex >= 0) {
- suffixIndex = source.indexOf(PROPERTY_KEY_SUFFIX, prefixIndex + 1);
- if (suffixIndex < 0) {
- prefixIndex = source.indexOf(PROPERTY_KEY_PREFIX, prefixIndex + 1);
- continue;
- }
- String key = source.substring(prefixIndex, suffixIndex + 2);
- if (!isPropertyKey(key)) {
- prefixIndex = source.indexOf(PROPERTY_KEY_PREFIX, prefixIndex + 1);
- continue;
- }
- return true;
- }
- return false;
- }
- /**
- * 将字符串中的全部属性替换为映射中的内容。
- * @param string 原字符串
- * @return 替换以后的字符串
- */
- public String replaceAllProperties(String string) {
- if (size() == 0||!containPropertyKey(string)) {
- return string;
- }
- return completeMatchReplace(string);
- }
- /**
- * 将字符串中的全部属性替换为映射中的内容。
- * @param string 原字符串
- * @return 替换以后的字符串
- */
- private String completeMatchReplace(String string) {
- String result = string;
- Iterator keys = keySet().iterator();
- while (keys.hasNext()) {
- String key = (String) keys.next();
- if (result.indexOf(key) < 0) {
- continue;
- }
- else {
- result = replace(result, key);
- }
- }
- return result;
- }
- /**
- * 将字符串中包含的指定属性全部进行替换。
- * @param source 字符串
- * @param key 属性关键字
- * @return 替换以后的内容
- */
- public String replace(String source, String key) {
- return repalce(source,key,get(key).toString());
- }
- /**
- * 将字符串中包含的指定属性全部进行替换,属性对应的是一个列表,将指定索引的值进行替换。
- * @param source 字符串
- * @param key 属性关键字
- * @param arrayIndex 列表下标
- * @return 替换以后的内容
- */
- public String replaceByList(String source, String key, int arrayIndex) {
- return repalce(source,key,( (List) get(key)).get(arrayIndex).toString());
- }
- /**
- * 将字符串中包含的指定属性全部以指定的内容进行替换。
- * @param source 字符串
- * @param key 关键字
- * @param replacement 替换内容
- * @return 替换以后的内容
- */
- private String repalce(String source, String key,String replacement) {
- String result = source;
- int index = result.indexOf(key);
- while (index >= 0) {
- result = result.substring(0, index) + replacement +
- result.substring(index + key.length());
- index = result.indexOf(key,index+replacement.length());
- }
- return result;
- }
- }