1 19 22 package org.tanesha.replacer; 23 24 import java.util.Map ; 25 import java.util.HashMap ; 26 27 33 public class ReplacerEnvironment { 34 35 private Map _macros = new HashMap (); 36 private ReplacerEnvironment _parent; 37 38 42 public ReplacerEnvironment() { 43 this(null); 44 } 45 46 51 public ReplacerEnvironment(ReplacerEnvironment parent) { 52 _parent = parent; 53 } 54 55 protected Object resolve(String key) { 56 if (_macros.containsKey(key)) 57 return _macros.get(key); 58 else if (_parent != null) 59 return _parent.resolve(key); 60 61 return null; 62 } 63 64 71 public ReplacerEnvironment add(String key, Object value) { 72 _macros.put(key, value); 73 74 return this; 75 } 76 77 83 public static ReplacerEnvironment chain(ReplacerEnvironment e1, ReplacerEnvironment e2) { 84 return new ChainedReplacerEnvironment(e1, e2); 85 } 86 87 private static class ChainedReplacerEnvironment extends ReplacerEnvironment { 88 89 ReplacerEnvironment _e2; 90 91 public ChainedReplacerEnvironment(ReplacerEnvironment e1, ReplacerEnvironment e2) { 92 super(e1); 93 94 _e2 = e2; 95 } 96 97 public Object resolve(String key) { 98 Object t = super.resolve(key); 99 if (t == null) { 100 t = _e2.resolve(key); 101 } 102 103 return t; 104 } 105 } 106 107 } 108 | Popular Tags |