1 16 package org.apache.cocoon.components.treeprocessor.variables; 17 18 import org.apache.avalon.framework.component.ComponentManager; 19 import org.apache.avalon.framework.service.ServiceManager; 20 import org.apache.avalon.framework.service.WrapperServiceManager; 21 22 import org.apache.cocoon.sitemap.PatternException; 23 24 import java.util.List ; 25 26 30 public class VariableResolverFactory { 31 32 private static ThreadLocal disposableCollector = new ThreadLocal (); 33 34 41 public static void setDisposableCollector(List collector) { 42 disposableCollector.set(collector); 43 } 44 45 48 public static boolean needsResolve(String expression) { 49 if (expression == null || expression.length() == 0) { 50 return false; 51 } 52 53 if (expression.charAt(0) == '{') { 55 return true; 56 } 57 58 if (expression.length() < 2) { 59 return false; 60 } 61 62 int pos = 1; 64 while ( (pos = expression.indexOf('{', pos)) != -1) { 65 if (expression.charAt(pos - 1) != '\\') { 67 return true; 69 } 70 pos++; 71 } 72 return false; 74 } 75 76 83 public static String unescape(String expression) { 84 if (expression == null || expression.indexOf("\\{") == -1) { 86 return expression; 87 } 88 89 StringBuffer buf = new StringBuffer (); 90 for (int i = 0; i < expression.length(); i++) { 91 char ch = expression.charAt(i); 92 if (ch != '\\' || i >= (expression.length() - 1) || expression.charAt(i+1) != '{') { 93 buf.append(ch); 94 } 95 } 96 97 return buf.toString(); 98 } 99 100 106 public static VariableResolver getResolver(String expression, ComponentManager manager) throws PatternException { 107 return getResolver(expression, new WrapperServiceManager(manager)); 108 } 109 110 114 public static VariableResolver getResolver(String expression, ServiceManager manager) throws PatternException { 115 if (needsResolve(expression)) { 116 VariableResolver resolver = new PreparedVariableResolver(expression, manager); 117 List collector = (List )disposableCollector.get(); 118 if (collector != null) 119 collector.add(resolver); 120 121 return resolver; 122 123 } else { 124 return new NOPVariableResolver(expression); 125 } 126 } 127 } 128 | Popular Tags |