1 16 package org.apache.cocoon.components.variables; 17 18 import org.apache.avalon.framework.component.Component; 19 import org.apache.avalon.framework.container.ContainerUtil; 20 import org.apache.avalon.framework.context.Context; 21 import org.apache.avalon.framework.context.ContextException; 22 import org.apache.avalon.framework.context.Contextualizable; 23 import org.apache.avalon.framework.logger.AbstractLogEnabled; 24 import org.apache.avalon.framework.service.ServiceException; 25 import org.apache.avalon.framework.service.ServiceManager; 26 import org.apache.avalon.framework.service.Serviceable; 27 import org.apache.avalon.framework.thread.ThreadSafe; 28 import org.apache.cocoon.sitemap.PatternException; 29 30 40 public class DefaultVariableResolverFactory 41 extends AbstractLogEnabled 42 implements ThreadSafe, VariableResolverFactory, Component, Serviceable, Contextualizable { 43 44 protected ServiceManager manager; 45 protected Context context; 46 47 50 public void service(ServiceManager manager) throws ServiceException { 51 this.manager = manager; 52 } 53 54 59 public VariableResolver lookup(String expression) 60 throws PatternException { 61 if ( this.needsResolve( expression ) ) { 62 return new PreparedVariableResolver( expression, this.manager, this.context); 63 } 64 return new NOPVariableResolver( expression ); 65 } 66 67 public void release(VariableResolver resolver) { 68 ContainerUtil.dispose(resolver); 69 } 70 71 74 protected boolean needsResolve(String expression) { 75 if (expression == null || expression.length() == 0) { 76 return false; 77 } 78 79 if (expression.charAt(0) == '{') { 81 return true; 82 } 83 84 if (expression.length() < 2) { 85 return false; 86 } 87 88 int pos = 1; 90 while ( (pos = expression.indexOf('{', pos)) != -1) { 91 if (expression.charAt(pos - 1) != '\\') { 93 return true; 95 } 96 pos++; 97 } 98 return false; 100 } 101 102 105 public void contextualize(Context context) throws ContextException { 106 this.context = context; 107 } 108 109 } 110 111 112 | Popular Tags |