KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > variables > DefaultVariableResolverFactory


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.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 /**
31  * This factory component creates a {@link VariableResolver} for an expression.
32  * A {@link VariableResolver} can then be used at runtime to resolve
33  * a variable with the current value.
34  * A variable can contain dynamic parts that are contained in {...},
35  *
36  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
37  *
38  * @version CVS $Id: DefaultVariableResolverFactory.java 314870 2005-10-12 12:57:16Z cziegeler $
39  */

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     /* (non-Javadoc)
48      * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
49      */

50     public void service(ServiceManager manager) throws ServiceException {
51         this.manager = manager;
52     }
53
54     /**
55      * Get a resolver for a given expression. Chooses the most efficient implementation
56      * depending on <code>expression</code>.
57      * Don't forget to release the resolver
58      */

59     public VariableResolver lookup(String JavaDoc 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     /**
72      * Does an expression need resolving (i.e. contain {...} patterns) ?
73      */

74     protected boolean needsResolve(String JavaDoc expression) {
75         if (expression == null || expression.length() == 0) {
76             return false;
77         }
78
79         // Is the first char a '{' ?
80
if (expression.charAt(0) == '{') {
81             return true;
82         }
83
84         if (expression.length() < 2) {
85             return false;
86         }
87
88         // Is there any unescaped '{' ?
89
int pos = 1;
90         while ( (pos = expression.indexOf('{', pos)) != -1) {
91             // Found a '{' : is it escaped ?
92
if (expression.charAt(pos - 1) != '\\') {
93                 // No : need to resolve
94
return true;
95             }
96             pos++;
97         }
98         // Nothing found...
99
return false;
100     }
101
102     /* (non-Javadoc)
103      * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
104      */

105     public void contextualize(Context context) throws ContextException {
106         this.context = context;
107     }
108
109 }
110
111
112
Popular Tags