1 17 package org.apache.forrest.locationmap.lm; 18 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.Map ; 22 23 import org.apache.avalon.framework.configuration.Configuration; 24 import org.apache.avalon.framework.configuration.ConfigurationException; 25 import org.apache.avalon.framework.logger.AbstractLogEnabled; 26 import org.apache.avalon.framework.parameters.Parameters; 27 import org.apache.avalon.framework.service.ServiceManager; 28 import org.apache.cocoon.components.treeprocessor.InvokeContext; 29 import org.apache.cocoon.components.treeprocessor.variables.VariableResolver; 30 import org.apache.cocoon.components.treeprocessor.variables.VariableResolverFactory; 31 import org.apache.cocoon.sitemap.PatternException; 32 33 37 public abstract class AbstractNode extends AbstractLogEnabled { 38 39 40 protected final ServiceManager m_manager; 41 42 private Map m_parameters; 44 45 46 public AbstractNode(final ServiceManager manager) { 47 m_manager = manager; 48 } 49 50 public void build(final Configuration configuration) throws ConfigurationException { 51 m_parameters = getParameters(configuration); 52 } 53 54 62 private final Map getParameters(final Configuration configuration) 63 throws ConfigurationException { 64 65 final Configuration[] children = configuration.getChildren("parameter"); 66 if (children.length == 0) { 67 return null; 68 } 69 final Map parameters = new HashMap (); 70 for (int i = 0; i < children.length; i++) { 71 final String name = children[i].getAttribute("name"); 72 final String value = children[i].getAttribute("value"); 73 try { 74 parameters.put( 75 VariableResolverFactory.getResolver(name, m_manager), 76 VariableResolverFactory.getResolver(value, m_manager)); 77 } catch(PatternException pe) { 78 String msg = "Invalid pattern '" + value + "' at " 79 + children[i].getLocation(); 80 throw new ConfigurationException(msg, pe); 81 } 82 } 83 84 return parameters; 85 } 86 87 96 protected final Parameters resolveParameters( 97 final InvokeContext context, 98 final Map om) throws PatternException { 99 100 Parameters parameters = null; 101 if (m_parameters != null) { 102 parameters = VariableResolver.buildParameters(m_parameters,context,om); 103 } 104 else { 105 parameters = new Parameters(); 106 } 107 Map anchorMap = context.getMapByAnchor(LocationMap.ANCHOR_NAME); 109 Iterator entries = anchorMap.entrySet().iterator(); 110 while (entries.hasNext()) { 111 Map.Entry entry = (Map.Entry ) entries.next(); 112 parameters.setParameter( 113 "#"+LocationMap.ANCHOR_NAME+":"+entry.getKey(), 114 entry.getValue().toString()); 115 } 116 return parameters; 117 } 118 119 public abstract String locate(Map objectModel, InvokeContext context) throws Exception ; 120 121 } 122 | Popular Tags |