1 16 package org.apache.cocoon.components.treeprocessor.variables; 17 18 import java.util.Collections ; 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.Map ; 22 23 import org.apache.avalon.framework.parameters.Parameters; 24 import org.apache.cocoon.components.treeprocessor.InvokeContext; 25 import org.apache.cocoon.sitemap.PatternException; 26 import org.apache.cocoon.sitemap.SitemapParameters; 27 import org.apache.cocoon.util.location.Locatable; 28 import org.apache.cocoon.util.location.Location; 29 30 36 public abstract class VariableResolver { 37 38 public static final Map EMPTY_MAP = Collections.unmodifiableMap(new java.util.HashMap (0)); 39 40 protected final String originalExpr; 41 42 protected VariableResolver(String expr) { 43 this.originalExpr = expr; 44 } 45 46 public final String toString() { 47 return this.originalExpr; 48 } 49 50 53 public boolean equals(Object object) { 54 if (object instanceof VariableResolver) { 55 VariableResolver other = (VariableResolver)object; 56 return (this.originalExpr == null && other.originalExpr == null) || 57 (this.originalExpr.equals(other.originalExpr)); 58 } else { 59 return false; 60 } 61 } 62 63 67 public int hashCode() { 68 return this.originalExpr == null ? 0 : this.originalExpr.hashCode(); 69 } 70 71 74 public String resolve(Map objectModel) throws PatternException { 75 return resolve(null, objectModel); 76 } 77 78 81 public abstract String resolve(InvokeContext context, Map objectModel) throws PatternException; 82 83 89 public static Parameters buildParameters(Map expressions, InvokeContext context, Map objectModel) throws PatternException { 90 Location location; 91 if (expressions instanceof Locatable) { 92 location = ((Locatable)expressions).getLocation(); 93 } else { 94 location = Location.UNKNOWN; 95 } 96 97 if (expressions == null || expressions.size() == 0 && location.equals(Location.UNKNOWN)) { 98 return Parameters.EMPTY_PARAMETERS; 99 } 100 101 SitemapParameters result = new SitemapParameters(location); 102 103 Iterator iter = expressions.entrySet().iterator(); 104 while (iter.hasNext()) { 105 Map.Entry entry = (Map.Entry )iter.next(); 106 result.setParameter( 107 ((VariableResolver)entry.getKey()).resolve(context, objectModel), 108 ((VariableResolver)entry.getValue()).resolve(context, objectModel) 109 ); 110 } 111 112 return result; 113 } 114 115 121 public static Map buildMap(Map expressions, InvokeContext context, Map objectModel) throws PatternException { 122 int size; 123 if (expressions == null || (size = expressions.size()) == 0) { 124 return EMPTY_MAP; 125 } 126 127 Map result; 128 if ( expressions instanceof Locatable ) { 129 result = new SitemapParameters.LocatedHashMap(((Locatable)expressions).getLocation(), size); 130 } else { 131 result = new HashMap (size); 132 } 133 134 Iterator iter = expressions.entrySet().iterator(); 135 while (iter.hasNext()) { 136 Map.Entry entry = (Map.Entry )iter.next(); 137 result.put( 138 ((VariableResolver)entry.getKey()).resolve(context, objectModel), 139 ((VariableResolver)entry.getValue()).resolve(context, objectModel) 140 ); 141 } 142 143 return result; 144 } 145 146 } 158 | Popular Tags |