1 8 package org.apache.avalon.excalibur.i18n; 9 10 import java.util.Map ; 11 import java.util.Locale ; 12 13 import org.apache.avalon.framework.logger.AbstractLogEnabled; 14 import org.apache.avalon.framework.component.Component; 15 16 public abstract class AbstractBundle extends AbstractLogEnabled implements Bundle, Component { 17 18 19 private BundleInfo bundleInfo; 20 21 22 private BundleInfoMapper mapper; 23 24 25 private Bundle parent = null; 26 27 28 private long lastModified = -1; 29 30 33 public BundleInfo getBundleInfo() { 34 return this.bundleInfo; 35 } 36 37 40 public void setBundleInfo(BundleInfo bundleInfo) { 41 if (this.bundleInfo == null) this.bundleInfo = bundleInfo; 42 } 43 44 public BundleInfoMapper getMapper() { 45 return mapper; 46 } 47 48 public void setMapper(BundleInfoMapper mapper) { 49 if (this.mapper == null) this.mapper = mapper; 50 } 51 52 57 public Bundle getParent() { 58 return this.parent; 59 } 60 61 66 public void setParent(Bundle parent) { 67 if (this.parent == null) this.parent = parent; 68 } 69 70 75 public long getLastModified() { 76 return this.lastModified; 77 } 78 79 84 public void setLastModified(long lastModified) { 85 this.lastModified = lastModified; 86 } 87 88 96 public String getString(String key, Map variables) { 97 return substitute(getString(key), variables); 98 } 99 100 110 public String convertKey(String userKey) { 111 return userKey; 112 } 113 114 122 public String substitute(String value, Map values) { 123 if (value == null || values == null) return value; 124 if (getLogger().isDebugEnabled()) getLogger().debug("Substituting value: " + value); 125 126 StringBuffer result = new StringBuffer (value.length()); 127 int startPos = 0; 128 int endPos = 0; 129 int lastPos = value.length(); 130 Object varValue = ""; 131 String varKey = "", oldKey = ""; 132 while (endPos < lastPos) { 133 startPos = endPos; 134 endPos = value.indexOf('{', startPos); 135 if (endPos == -1) 136 endPos = lastPos; 137 result.append(value.substring(startPos, endPos)); 138 if (endPos < lastPos) 139 endPos++; 140 if (endPos < lastPos) { 141 startPos = endPos; 142 endPos = value.indexOf('}', startPos); 143 if (endPos == -1) 144 endPos = lastPos; 145 oldKey = varKey; 146 varKey = value.substring(startPos, endPos); 147 if (!oldKey.equals(varKey)) 148 varValue = values.get(varKey); 149 if (varValue != null) { 150 if (getLogger().isDebugEnabled()) getLogger().debug("Substituting var: " + varKey + " --> " + varValue); 151 result.append(varValue); 152 } 153 else { 154 if (getLogger().isWarnEnabled()) getLogger().warn(bundleInfo + ": var not found: " + varKey); 155 result.append('{').append(varKey).append('}'); 156 } 157 if (endPos < lastPos) 158 endPos++; 159 } 160 } 161 if (getLogger().isDebugEnabled()) getLogger().debug("Returning substituted value: " + result); 162 return result.toString(); 163 } 164 165 } 166 | Popular Tags |