KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > treeprocessor > variables > VariableResolverFactory


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
25
26 /**
27  *
28  * @version CVS $Id: VariableResolverFactory.java 47013 2004-09-22 01:22:52Z vgritsenko $
29  */

30 public class VariableResolverFactory {
31
32     private static ThreadLocal JavaDoc disposableCollector = new ThreadLocal JavaDoc();
33
34     /**
35      * Set the thread-local list where all created resolvers that need to be
36      * disposed will be collected.
37      * <p>
38      * The purpose of collecting resolvers is to avoid manual release (or lack thereof)
39      * that requires most <code>ProcessingNodes</code> to implement <code>Disposable</code>.
40      */

41     public static void setDisposableCollector(List JavaDoc collector) {
42         disposableCollector.set(collector);
43     }
44
45     /**
46      * Does an expression need resolving (i.e. contain {...} patterns) ?
47      */

48     public static boolean needsResolve(String JavaDoc expression) {
49         if (expression == null || expression.length() == 0) {
50             return false;
51         }
52
53         // Is the first char a '{' ?
54
if (expression.charAt(0) == '{') {
55             return true;
56         }
57
58         if (expression.length() < 2) {
59             return false;
60         }
61
62         // Is there any unescaped '{' ?
63
int pos = 1;
64         while ( (pos = expression.indexOf('{', pos)) != -1) {
65             // Found a '{' : is it escaped ?
66
if (expression.charAt(pos - 1) != '\\') {
67                 // No : need to resolve
68
return true;
69             }
70             pos++;
71         }
72         // Nothing found...
73
return false;
74     }
75
76     /**
77      * Unescape an expression that doesn't need to be resolved, but may contain
78      * escaped '{' characters.
79      *
80      * @param expression the expression to unescape.
81      * @return the unescaped result, or <code>expression</code> if unescaping isn't necessary.
82      */

83     public static String JavaDoc unescape(String JavaDoc expression) {
84         // Does it need escaping ?
85
if (expression == null || expression.indexOf("\\{") == -1) {
86             return expression;
87         }
88
89         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
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     /**
101      * Get a resolver for a given expression. Chooses the most efficient implementation
102      * depending on <code>expression</code>.
103      *
104      * @deprecated use the version with <code>ServiceManager</service>
105      */

106     public static VariableResolver getResolver(String JavaDoc expression, ComponentManager manager) throws PatternException {
107         return getResolver(expression, new WrapperServiceManager(manager));
108     }
109
110     /**
111      * Get a resolver for a given expression. Chooses the most efficient implementation
112      * depending on <code>expression</code>.
113      */

114     public static VariableResolver getResolver(String JavaDoc expression, ServiceManager manager) throws PatternException {
115         if (needsResolve(expression)) {
116             VariableResolver resolver = new PreparedVariableResolver(expression, manager);
117             List JavaDoc collector = (List JavaDoc)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