1 16 package org.apache.cocoon.components.modules.input; 17 18 import org.apache.avalon.framework.configuration.Configuration; 19 import org.apache.avalon.framework.configuration.ConfigurationException; 20 import org.apache.avalon.framework.thread.ThreadSafe; 21 22 import java.util.HashMap ; 23 import java.util.HashSet ; 24 import java.util.Iterator ; 25 import java.util.Map ; 26 import java.util.Set ; 27 28 53 public class SimpleMappingMetaModule extends AbstractMetaModule implements ThreadSafe { 54 55 String prefix = null; 56 String suffix = null; 57 String rmPrefix = null; 58 String rmSuffix = null; 59 Mapping mapping = null; 60 61 protected static class Mapping { 62 Map toMap = null; 63 Map fromMap = null; 64 65 public Mapping() { 66 } 67 68 public Mapping(Map to, Map from) { 69 this.toMap = to; 70 this.fromMap = from; 71 } 72 73 public Mapping(Configuration config) throws ConfigurationException { 74 Configuration[] mappings = config.getChildren("mapping"); 75 if (mappings!=null) { 76 if (this.toMap == null) this.toMap = new HashMap (); 77 if (this.fromMap == null) this.fromMap = new HashMap (); 78 for (int i=0; i < mappings.length; i++){ 79 String in = mappings[i].getAttribute("in",null); 80 String out = mappings[i].getAttribute("out",null); 81 if (in != null && out != null) { 82 this.toMap.put(in,out); 83 this.fromMap.put(out,in); 84 } 85 } 86 } 87 } 88 89 private String mapIt(Map map, String param) { 90 Object newParam = param; 91 if (map != null) { 92 newParam = map.get(param); 93 if (!map.containsKey(param) || newParam == null) 94 newParam = param; 95 } 96 return (String ) newParam; 97 } 98 99 public String mapFrom(String param) { 100 return this.mapIt(this.fromMap, param); 101 } 102 103 public String mapTo(String param) { 104 return this.mapIt(this.toMap, param); 105 } 106 } 107 108 109 public void configure(Configuration config) throws ConfigurationException { 110 111 this.inputConf = config.getChild("input-module"); 114 this.defaultInput = this.inputConf.getAttribute("name", this.defaultInput); 115 this.prefix = config.getChild("prefix").getValue(null); 116 this.suffix = config.getChild("suffix").getValue(null); 117 this.rmPrefix = config.getChild("rm-prefix").getValue(null); 118 this.rmSuffix = config.getChild("rm-suffix").getValue(null); 119 this.mapping = new Mapping(config); 120 } 121 122 123 public Object getAttribute( String name, Configuration modeConf, Map objectModel ) 124 throws ConfigurationException { 125 126 if (!this.initialized) { 127 this.lazy_initialize(); 128 } 129 if (this.defaultInput == null) { 130 if (getLogger().isWarnEnabled()) 131 getLogger().warn("No input module given. FAILING"); 132 return null; 133 } 134 135 Configuration inputConfig = null; 136 String inputName=null; 137 Mapping mapping = this.mapping; 138 String prefix = this.prefix; 139 String suffix = this.suffix; 140 String rmPrefix = this.rmPrefix; 141 String rmSuffix = this.rmSuffix; 142 143 if (modeConf!=null && modeConf.getChildren().length > 0) { 144 inputName = modeConf.getChild("input-module").getAttribute("name",null); 145 if (inputName != null) { 146 inputConfig = modeConf.getChild("input-module"); 147 } 148 mapping = new Mapping(modeConf); 149 prefix = modeConf.getChild("prefix").getValue(null); 150 suffix = modeConf.getChild("suffix").getValue(null); 151 rmPrefix = modeConf.getChild("rm-prefix").getValue(null); 152 rmSuffix = modeConf.getChild("rm-suffix").getValue(null); 153 } 154 155 if (rmPrefix != null && name.startsWith(rmPrefix)) { 157 name = name.substring(rmPrefix.length()); 158 } 159 if (rmSuffix != null && name.endsWith(rmSuffix)) { 160 name = name.substring(0,name.length() - rmSuffix.length()); 161 } 162 String param = mapping.mapTo(name); 164 if (prefix != null) param = prefix + param; 166 if (suffix != null) param = param + suffix; 167 if (getLogger().isDebugEnabled()) 168 getLogger().debug("mapping ['"+name+"'] to ['"+param+"']"); 169 170 Object res = getValue(param, objectModel, 171 this.input, this.defaultInput, this.inputConf, 172 null, inputName, inputConfig); 173 174 if (getLogger().isDebugEnabled()) 175 getLogger().debug("getting for real attribute ['"+param+"'] value: "+res); 176 177 return res; 178 } 179 180 181 182 183 184 public Object [] getAttributeValues( String name, Configuration modeConf, Map objectModel ) 185 throws ConfigurationException { 186 187 if (!this.initialized) { 188 this.lazy_initialize(); 189 } 190 if (this.defaultInput == null) { 191 if (getLogger().isWarnEnabled()) 192 getLogger().warn("No input module given. FAILING"); 193 return null; 194 } 195 196 Configuration inputConfig = null; 197 String inputName=null; 198 Mapping mapping = this.mapping; 199 String prefix = this.prefix; 200 String suffix = this.suffix; 201 String rmPrefix = this.rmPrefix; 202 String rmSuffix = this.rmSuffix; 203 204 if (modeConf!=null && modeConf.getChildren().length > 0) { 205 inputName = modeConf.getChild("input-module").getAttribute("name",null); 206 if (inputName != null) { 207 inputConfig = modeConf.getChild("input-module"); 208 } 209 mapping = new Mapping(modeConf); 210 prefix = modeConf.getChild("prefix").getValue(null); 211 suffix = modeConf.getChild("suffix").getValue(null); 212 rmPrefix = modeConf.getChild("rm-prefix").getValue(null); 213 rmSuffix = modeConf.getChild("rm-suffix").getValue(null); 214 } 215 216 if (rmPrefix != null && name.startsWith(rmPrefix)) { 218 name = name.substring(rmPrefix.length()); 219 } 220 if (rmSuffix != null && name.endsWith(rmSuffix)) { 221 name = name.substring(0,name.length() - rmSuffix.length()); 222 } 223 String param = mapping.mapTo(name); 225 if (prefix != null) param = prefix + param; 227 if (suffix != null) param = param + suffix; 228 if (getLogger().isDebugEnabled()) 229 getLogger().debug("mapping ['"+name+"'] to ['"+param+"']"); 230 231 Object [] res = getValues(param, objectModel, 232 this.input, this.defaultInput, this.inputConf, 233 null, inputName, inputConfig); 234 if (getLogger().isDebugEnabled()) 235 getLogger().debug("getting for real attribute ['"+param+"'] value: "+res); 236 237 return res; 238 } 239 240 241 242 public Iterator getAttributeNames( Configuration modeConf, Map objectModel ) 243 throws ConfigurationException { 244 245 if (!this.initialized) { 246 this.lazy_initialize(); 247 } 248 if (this.defaultInput == null) { 249 if (getLogger().isWarnEnabled()) 250 getLogger().warn("No input module given. FAILING"); 251 return null; 252 } 253 254 Configuration inputConfig = null; 255 String inputName=null; 256 Mapping mapping = this.mapping; 257 String prefix = this.prefix; 258 String suffix = this.suffix; 259 String rmPrefix = this.rmPrefix; 260 String rmSuffix = this.rmSuffix; 261 if (modeConf!=null && modeConf.getChildren().length > 0) { 262 inputName = modeConf.getChild("input-module").getAttribute("name",null); 263 if (inputName != null) { 264 inputConfig = modeConf.getChild("input-module"); 265 } 266 mapping = new Mapping(modeConf); 267 prefix = modeConf.getChild("prefix").getValue(null); 268 suffix = modeConf.getChild("suffix").getValue(null); 269 rmPrefix = modeConf.getChild("rm-prefix").getValue(null); 270 rmSuffix = modeConf.getChild("rm-suffix").getValue(null); 271 } 272 273 Iterator names = getNames(objectModel, 274 this.input, this.defaultInput, this.inputConf, 275 null, inputName, inputConfig); 276 277 Set set = new HashSet (); 278 while (names.hasNext()) { 279 String param = (String ) names.next(); 280 if (getLogger().isDebugEnabled()) 281 getLogger().debug("reverse mapping starts with ['"+param+"']"); 282 if (prefix != null) 283 if (param.startsWith(prefix)) 284 param = param.substring(prefix.length()); 285 else 286 continue; 288 291 if (suffix != null) 292 if (param.endsWith(suffix)) 293 param = param.substring(0,param.length() - suffix.length()); 294 else 295 continue; 297 300 if (param.length() < 1) 301 continue; 303 String newName = mapping.mapFrom(param); 304 305 if (rmPrefix != null) newName = rmPrefix + newName; 306 if (rmSuffix != null) newName = newName + rmSuffix; 307 308 if (getLogger().isDebugEnabled()) 309 getLogger().debug("reverse mapping results in ['"+newName+"']"); 310 311 set.add(newName); 312 } 313 314 return set.iterator(); 315 316 } 317 318 } 319 | Popular Tags |