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.selection;
018:
019: import java.util.Map;
020:
021: import org.apache.avalon.framework.activity.Disposable;
022: import org.apache.avalon.framework.logger.AbstractLogEnabled;
023: import org.apache.avalon.framework.parameters.Parameters;
024: import org.apache.avalon.framework.service.ServiceException;
025: import org.apache.avalon.framework.service.ServiceManager;
026: import org.apache.avalon.framework.service.Serviceable;
027: import org.apache.avalon.framework.thread.ThreadSafe;
028: import org.apache.excalibur.source.Source;
029: import org.apache.excalibur.source.SourceNotFoundException;
030: import org.apache.excalibur.source.SourceResolver;
031:
032: /**
033: * Selects the first of a set of Resources (usually files) that exists.
034: *
035: * <p>
036: * A parameter 'prefix',
037: * <pre>
038: * <map:parameter src="prefix" value="<code>some/path</code>"/<
039: * </pre>
040: * may be supplied to the selector instance. This prefix is prepended to all
041: * test expressions before evaluation. The default prefix is '' (empty string),
042: * meaning that all expressions are relative to the current sitemap, unless
043: * explicitly overridden.
044: *
045: * <p><b>NOTE:</b>
046: * Provided resource URI is resolved as Source, relative to the current
047: * sitemap, which differs from behavior of selector in previous versions.
048: * To resolve resource paths relative to the context root, provide prefix
049: * parameter:
050: * <pre>
051: * <map:parameter name="prefix" value="context://"/<
052: * </pre>
053: *
054: * <p>
055: * For example, we could define a ResourceExistsSelector with:
056: * <pre>
057: * <map:selector name="resource-exists"
058: * logger="sitemap.selector.resource-exists"
059: * src="org.apache.cocoon.selection.ResourceExistsSelector" /<
060: * </pre>
061: * And use it to build a PDF from XSL:FO or a higher-level XML format with:
062: *
063: * <pre>
064: * <map:match pattern="**.pdf"<
065: * <map:select type="resource-exists"<
066: * <map:when test="context/xdocs/{1}.fo"<
067: * <map:generate src="content/xdocs/{1}.fo" /<
068: * </map:when<
069: * <map:otherwise<
070: * <map:generate src="content/xdocs/{1}.xml" /<
071: * <map:transform src="stylesheets/document2fo.xsl" /<
072: * </map:otherwise<
073: * </map:select<
074: * <map:serialize type="fo2pdf" /<
075: * </pre>
076: *
077: * @author <a href="mailto:jefft@apache.org">Jeff Turner</a>
078: * @author <a href="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
079: * @version CVS $Id: ResourceExistsSelector.java 433543 2006-08-22 06:22:54Z crossley $
080: */
081: public class ResourceExistsSelector extends AbstractLogEnabled
082: implements ThreadSafe, Serviceable, Disposable, Selector {
083:
084: private ServiceManager manager;
085: private SourceResolver resolver;
086:
087: public void service(ServiceManager manager) throws ServiceException {
088: this .manager = manager;
089: this .resolver = (SourceResolver) manager
090: .lookup(SourceResolver.ROLE);
091: }
092:
093: public void dispose() {
094: this .manager.release(this .resolver);
095: this .resolver = null;
096: this .manager = null;
097: }
098:
099: public boolean select(String expression, Map objectModel,
100: Parameters parameters) {
101: String resourceURI = parameters.getParameter("prefix", "")
102: + expression;
103: Source source = null;
104: try {
105: source = resolver.resolveURI(resourceURI);
106: return source.exists();
107: } catch (SourceNotFoundException e) {
108: return false;
109: } catch (Exception e) {
110: getLogger().warn(
111: "Exception resolving resource " + resourceURI, e);
112: return false;
113: } finally {
114: if (source != null) {
115: resolver.release(source);
116: }
117: }
118: }
119: }
|