1 17 18 19 20 package org.apache.lenya.cms.cocoon.uriparameterizer; 21 22 import java.util.HashMap ; 23 import java.util.Map ; 24 25 import org.apache.avalon.framework.logger.AbstractLogEnabled; 26 import org.apache.avalon.framework.logger.Logger; 27 import org.apache.avalon.framework.parameters.Parameters; 28 import org.apache.avalon.framework.service.ServiceException; 29 import org.apache.avalon.framework.service.ServiceManager; 30 import org.apache.avalon.framework.service.Serviceable; 31 import org.apache.cocoon.components.source.SourceUtil; 32 import org.apache.cocoon.xml.AbstractXMLConsumer; 33 import org.apache.excalibur.source.Source; 34 import org.apache.excalibur.source.SourceResolver; 35 import org.apache.lenya.util.CacheMap; 36 import org.xml.sax.Attributes ; 37 38 41 public class URIParameterizerImpl 42 extends AbstractLogEnabled 43 implements URIParameterizer, Serviceable { 44 45 48 public class URIParametrizerConsumer extends AbstractXMLConsumer { 49 private boolean inParamElement = false; 50 private String parameterValue = null; 51 52 private Logger logger; 53 54 58 public URIParametrizerConsumer(Logger logger) { 59 this.logger = logger; 60 } 61 62 65 public void startElement(String uri, String loc, String raw, Attributes a) { 66 if (loc.equals("parameter")) { 67 if (logger.isDebugEnabled()) { 68 logger.debug("start Element " + uri + ":" + loc + ":" + raw); 69 } 70 inParamElement = true; 71 } 72 } 73 74 77 public void endElement(String uri, String loc, String raw, Attributes a) { 78 if (loc.equals("parameter")) { 79 if (logger.isDebugEnabled()) { 80 logger.debug("stop Element " + uri + ":" + loc + ":" + raw); 81 } 82 inParamElement = false; 83 } 84 85 if (logger.isDebugEnabled()) { 86 logger.debug("processing Element " + uri + ":" + loc + ":" + raw); 87 } 88 } 89 90 93 public void characters(char[] ch, int start, int len) { 94 if (inParamElement) { 95 parameterValue = new String (ch, start, len); 96 if (logger.isDebugEnabled()) { 97 logger.debug("grab Element " + parameterValue); 98 } 99 } 100 } 101 102 106 public String getParameter() { 107 return parameterValue; 108 } 109 110 } 111 112 115 public URIParameterizerImpl() { 116 } 117 118 126 public Map parameterize(String uri, String src, Parameters parameters) 127 throws URIParameterizerException { 128 129 Map uriParameters = new HashMap (); 130 SourceResolver resolver = null; 131 132 try { 133 resolver = (SourceResolver) manager.lookup(SourceResolver.ROLE); 134 URIParametrizerConsumer xmlConsumer = new URIParametrizerConsumer(getLogger()); 135 136 String [] parameterNames = parameters.getNames(); 137 138 for (int i = 0; i < parameterNames.length; i++) { 139 140 String key = uri + "_" + parameterNames[i]; 141 142 String value = (String ) cache.get(key); 143 144 if (value == null) { 145 String parameterSrc = parameters.getParameter(parameterNames[i]) + "/" + src; 146 147 Source inputSource = null; 148 try { 149 inputSource = resolver.resolveURI(parameterSrc); 150 151 if (getLogger().isDebugEnabled()) { 152 getLogger().debug("File resolved to " + inputSource.getURI()); 153 } 154 155 SourceUtil.toSAX(inputSource, xmlConsumer); 156 } finally { 157 if (inputSource != null) { 158 resolver.release(inputSource); 159 } 160 } 161 value = xmlConsumer.getParameter(); 162 cache.put(key, value); 163 } 164 165 uriParameters.put(parameterNames[i], value); 166 } 167 } catch (Exception e) { 168 getLogger().error("URI parameterizing failed: ", e); 169 throw new URIParameterizerException(e); 170 } finally { 171 if (resolver != null) { 172 manager.release(resolver); 173 } 174 } 175 return uriParameters; 176 } 177 178 protected static final int CACHE_CAPACITY = 1000; 179 180 private static Map cache = new CacheMap(CACHE_CAPACITY); 181 182 private ServiceManager manager; 183 184 187 public void service(ServiceManager manager) throws ServiceException { 188 this.manager = manager; 189 } 190 191 } 192 | Popular Tags |