1 17 package org.apache.forrest.locationmap.lm; 18 19 import java.util.ArrayList ; 20 import java.util.List ; 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.service.ServiceManager; 26 import org.apache.cocoon.components.treeprocessor.InvokeContext; 27 import org.apache.cocoon.components.treeprocessor.variables.VariableResolver; 28 import org.apache.cocoon.components.treeprocessor.variables.VariableResolverFactory; 29 import org.apache.cocoon.sitemap.PatternException; 30 import org.apache.excalibur.source.SourceUtil; 31 32 33 46 public final class LocatorNode extends AbstractNode { 47 48 private final LocationMap m_lm; 50 51 private VariableResolver m_baseLocation; 53 54 private AbstractNode[] m_nodes; 56 57 public LocatorNode(final LocationMap lm, final ServiceManager manager) { 58 super(manager); 59 m_lm = lm; 60 } 61 62 public void build(final Configuration configuration) throws ConfigurationException { 63 64 super.build(configuration); 65 66 String base = configuration.getAttribute("base", null); 68 if (base != null) { 69 try { 70 m_baseLocation = VariableResolverFactory.getResolver(base, super.m_manager); 71 } catch (PatternException e) { 72 final String message = "Illegal pattern syntax for locator attribute 'base'" + 73 " at " + configuration.getLocation(); 74 throw new ConfigurationException(message); 75 } 76 } 77 78 final Configuration[] children = configuration.getChildren(); 80 final List nodes = new ArrayList (children.length); 81 for (int i = 0; i < children.length; i++) { 82 AbstractNode node = null; 83 if (children[i].getName().equals("match")) { 84 node = new MatchNode(this, super.m_manager); 85 } 86 else if (children[i].getName().equals("select")) { 87 node = new SelectNode(this, super.m_manager); 88 } 89 else { 90 final String message = "Illegal locator node child: " 91 + children[i].getName(); 92 throw new ConfigurationException(message); 93 } 94 node.enableLogging(getLogger()); 95 node.build(children[i]); 96 nodes.add(node); 97 } 98 m_nodes = (AbstractNode[]) nodes.toArray(new AbstractNode[nodes.size()]); 99 } 100 101 106 public String locate(Map om, InvokeContext context) throws Exception { 107 108 String base = null; 110 if (m_baseLocation != null) { 111 base = m_baseLocation.resolve(context, om); 112 if (base != null && base.charAt(base.length()-1) != '/') { 113 base = base + "/"; 114 } 115 } 116 117 for (int i = 0; i < m_nodes.length; i++) { 118 String location = m_nodes[i].locate(om, context); 119 if (location != null) { 120 if (base != null && base.length() != 0) { 121 if (location.charAt(0) != '/' && SourceUtil.indexOfSchemeColon(location) == -1) { 122 location = base + location; 123 } 124 } 125 if (getLogger().isDebugEnabled()) { 126 getLogger().debug("located: " + location); 127 } 128 return location; 129 } 130 } 131 return null; 132 } 133 134 String getDefaultMatcher() { 135 return m_lm.getDefaultMatcher(); 136 } 137 138 String getDefaultSelector() { 139 return m_lm.getDefaultSelector(); 140 } 141 142 } 143 | Popular Tags |