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.portal.util;
18:
19: import java.util.HashMap;
20: import java.util.Iterator;
21: import java.util.Map;
22:
23: import org.apache.avalon.framework.service.ServiceException;
24: import org.apache.avalon.framework.service.ServiceManager;
25: import org.apache.cocoon.ProcessingException;
26: import org.apache.cocoon.components.variables.VariableResolver;
27: import org.apache.cocoon.components.variables.VariableResolverFactory;
28: import org.apache.cocoon.sitemap.PatternException;
29:
30: /**
31: * @version $Id: InputModuleHelper.java 433543 2006-08-22 06:22:54Z crossley $
32: */
33: public class InputModuleHelper {
34:
35: /** Service manager. */
36: protected final ServiceManager manager;
37:
38: protected final VariableResolverFactory variableFactory;
39:
40: protected final Map processedPatterns = new HashMap();
41:
42: public InputModuleHelper(ServiceManager serviceManager)
43: throws ServiceException {
44: this .manager = serviceManager;
45: this .variableFactory = (VariableResolverFactory) this .manager
46: .lookup(VariableResolverFactory.ROLE);
47: }
48:
49: public void dispose() {
50: final Iterator i = this .processedPatterns.values().iterator();
51: while (i.hasNext()) {
52: final VariableResolver resolver = (VariableResolver) i
53: .next();
54: this .variableFactory.release(resolver);
55: }
56: this .processedPatterns.clear();
57: this .manager.release(this .variableFactory);
58: }
59:
60: public String resolve(String value) throws ProcessingException {
61: VariableResolver resolver = null;
62: try {
63: resolver = this .variableFactory.lookup(value);
64: return resolver.resolve();
65: } catch (PatternException e) {
66: throw new ProcessingException("Error parsing pattern: "
67: + value, e);
68: } finally {
69: this .variableFactory.release(resolver);
70: }
71: }
72:
73: public VariableResolver getVariableResolver(String value)
74: throws ProcessingException {
75: VariableResolver resolver = (VariableResolver) this .processedPatterns
76: .get(value);
77: if (resolver == null) {
78: try {
79: resolver = this .variableFactory.lookup(value);
80: this .processedPatterns.put(value, resolver);
81: } catch (PatternException e) {
82: throw new ProcessingException("Error parsing pattern: "
83: + value, e);
84: }
85: }
86: return resolver;
87: }
88: }
|