01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.components.expression;
18:
19: import org.apache.avalon.framework.activity.Disposable;
20: import org.apache.avalon.framework.logger.AbstractLogEnabled;
21: import org.apache.avalon.framework.service.ServiceException;
22: import org.apache.avalon.framework.service.ServiceManager;
23: import org.apache.avalon.framework.service.ServiceSelector;
24: import org.apache.avalon.framework.service.Serviceable;
25: import org.apache.avalon.framework.thread.ThreadSafe;
26:
27: /**
28: * @version $Id: DefaultExpressionFactory.java 449189 2006-09-23 06:52:29Z crossley $
29: */
30: public class DefaultExpressionFactory extends AbstractLogEnabled
31: implements Disposable, Serviceable, ThreadSafe,
32: ExpressionFactory {
33:
34: public static final String DEFAULT_EXPRESSION_LANGUAGE = "default";
35:
36: /** The component manager */
37: protected ServiceManager manager;
38:
39: /** The Expression compiler selector */
40: protected ServiceSelector compilerSelector;
41:
42: /**
43: * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
44: */
45: public void service(final ServiceManager manager)
46: throws ServiceException {
47: this .manager = manager;
48: this .compilerSelector = (ServiceSelector) this .manager
49: .lookup(ExpressionCompiler.ROLE + "Selector");
50: }
51:
52: /**
53: * @see org.apache.avalon.framework.activity.Disposable#dispose()
54: */
55: public void dispose() {
56: if (null != this .manager) {
57: this .manager.release(this .compilerSelector);
58: this .compilerSelector = null;
59: this .manager = null;
60: }
61: }
62:
63: public Expression getExpression(String language, String expression)
64: throws ExpressionException {
65:
66: Expression expressionImpl = null;
67: ExpressionCompiler compiler = null;
68: try {
69: compiler = (ExpressionCompiler) this .compilerSelector
70: .select(language);
71: expressionImpl = compiler.compile(language, expression);
72: } catch (final ServiceException ce) {
73: throw new ExpressionException("Can't find a compiler for "
74: + language);
75: } finally {
76: this .compilerSelector.release(compiler);
77: }
78: return expressionImpl;
79: }
80:
81: public Expression getExpression(String expression)
82: throws ExpressionException {
83: String language = DEFAULT_EXPRESSION_LANGUAGE;
84: int end = expression.indexOf(':');
85: if (end != -1) {
86: language = expression.substring(0, end);
87: expression = expression.substring(end + 1);
88: }
89: return getExpression(language, expression);
90: }
91: }
|