001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.components.variables;
018:
019: import org.apache.avalon.framework.component.Component;
020: import org.apache.avalon.framework.container.ContainerUtil;
021: import org.apache.avalon.framework.context.Context;
022: import org.apache.avalon.framework.context.ContextException;
023: import org.apache.avalon.framework.context.Contextualizable;
024: import org.apache.avalon.framework.logger.AbstractLogEnabled;
025: import org.apache.avalon.framework.service.ServiceException;
026: import org.apache.avalon.framework.service.ServiceManager;
027: import org.apache.avalon.framework.service.Serviceable;
028: import org.apache.avalon.framework.thread.ThreadSafe;
029: import org.apache.cocoon.sitemap.PatternException;
030:
031: /**
032: * This factory component creates a {@link VariableResolver} for an expression.
033: * A {@link VariableResolver} can then be used at runtime to resolve
034: * a variable with the current value.
035: * A variable can contain dynamic parts that are contained in {...},
036: *
037: * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
038: *
039: * @version CVS $Id: DefaultVariableResolverFactory.java 433543 2006-08-22 06:22:54Z crossley $
040: */
041: public class DefaultVariableResolverFactory extends AbstractLogEnabled
042: implements ThreadSafe, VariableResolverFactory, Component,
043: Serviceable, Contextualizable {
044:
045: protected ServiceManager manager;
046: protected Context context;
047:
048: /* (non-Javadoc)
049: * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
050: */
051: public void service(ServiceManager manager) throws ServiceException {
052: this .manager = manager;
053: }
054:
055: /**
056: * Get a resolver for a given expression. Chooses the most efficient implementation
057: * depending on <code>expression</code>.
058: * Don't forget to release the resolver
059: */
060: public VariableResolver lookup(String expression)
061: throws PatternException {
062: if (this .needsResolve(expression)) {
063: return new PreparedVariableResolver(expression,
064: this .manager, this .context);
065: }
066: return new NOPVariableResolver(expression);
067: }
068:
069: public void release(VariableResolver resolver) {
070: ContainerUtil.dispose(resolver);
071: }
072:
073: /**
074: * Does an expression need resolving (i.e. contain {...} patterns) ?
075: */
076: protected boolean needsResolve(String expression) {
077: if (expression == null || expression.length() == 0) {
078: return false;
079: }
080:
081: // Is the first char a '{' ?
082: if (expression.charAt(0) == '{') {
083: return true;
084: }
085:
086: if (expression.length() < 2) {
087: return false;
088: }
089:
090: // Is there any unescaped '{' ?
091: int pos = 1;
092: while ((pos = expression.indexOf('{', pos)) != -1) {
093: // Found a '{' : is it escaped ?
094: if (expression.charAt(pos - 1) != '\\') {
095: // No : need to resolve
096: return true;
097: }
098: pos++;
099: }
100: // Nothing found...
101: return false;
102: }
103:
104: /* (non-Javadoc)
105: * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
106: */
107: public void contextualize(Context context) throws ContextException {
108: this.context = context;
109: }
110:
111: }
|