KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > selection > ResourceExistsSelector


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.selection;
17
18 import java.util.Map JavaDoc;
19
20 import org.apache.avalon.framework.activity.Disposable;
21 import org.apache.avalon.framework.logger.AbstractLogEnabled;
22 import org.apache.avalon.framework.parameters.Parameters;
23 import org.apache.avalon.framework.service.ServiceException;
24 import org.apache.avalon.framework.service.ServiceManager;
25 import org.apache.avalon.framework.service.Serviceable;
26 import org.apache.avalon.framework.thread.ThreadSafe;
27 import org.apache.excalibur.source.Source;
28 import org.apache.excalibur.source.SourceNotFoundException;
29 import org.apache.excalibur.source.SourceResolver;
30
31 /**
32  * Selects the first of a set of Resources (usually files) that exists.
33  *
34  * <p>
35  * A parameter 'prefix',
36  * <pre>
37  * &lt;map:parameter SRC="prefix" value="<code>some/path</code>"/&lt;
38  * </pre>
39  * may be supplied to the selector instance. This prefix is prepended to all
40  * test expressions before evaluation. The default prefix is '' (empty string),
41  * meaning that all expressions are relative to the current sitemap, unless
42  * explicitly overridden.
43  *
44  * <p><b>NOTE:</b>
45  * Provided resource URI is resolved as Source, relative to the current
46  * sitemap, which differs from behavior of selector in previous versions.
47  * To resolve resource paths relative to the context root, provide prefix
48  * parameter:
49  * <pre>
50  * &lt;map:parameter name="prefix" value="context://"/&lt;
51  * </pre>
52  *
53  * <p>
54  * For example, we could define a ResourceExistsSelector with:
55  * <pre>
56  * &lt;map:selector name="resource-exists"
57  * logger="sitemap.selector.resource-exists"
58  * SRC="org.apache.cocoon.selection.ResourceExistsSelector" /&lt;
59  * </pre>
60  * And use it to build a PDF from XSL:FO or a higher-level XML format with:
61  *
62  * <pre>
63  * &lt;map:match pattern="**.pdf"&lt;
64  * &lt;map:select type="resource-exists"&lt;
65  * &lt;map:when test="context/xdocs/{1}.fo"&lt;
66  * &lt;map:generate SRC="content/xdocs/{1}.fo" /&lt;
67  * &lt;/map:when&lt;
68  * &lt;map:otherwise&lt;
69  * &lt;map:generate SRC="content/xdocs/{1}.xml" /&lt;
70  * &lt;map:transform SRC="stylesheets/document2fo.xsl" /&lt;
71  * &lt;/map:otherwise&lt;
72  * &lt;/map:select&lt;
73  * &lt;map:serialize type="fo2pdf" /&lt;
74  * </pre>
75  *
76  * @author <a HREF="mailto:jefft@apache.org">Jeff Turner</a>
77  * @author <a HREF="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
78  * @version CVS $Id: ResourceExistsSelector.java 30932 2004-07-29 17:35:38Z vgritsenko $
79  */

80 public class ResourceExistsSelector extends AbstractLogEnabled
81                                     implements ThreadSafe, Serviceable, Disposable, Selector {
82
83     private ServiceManager manager;
84     private SourceResolver resolver;
85     
86     public void service(ServiceManager manager) throws ServiceException {
87         this.manager = manager;
88         this.resolver = (SourceResolver)manager.lookup(SourceResolver.ROLE);
89     }
90
91     public void dispose() {
92         this.manager.release(this.resolver);
93         this.resolver = null;
94         this.manager = null;
95     }
96     
97     public boolean select(String JavaDoc expression, Map JavaDoc objectModel, Parameters parameters) {
98         String JavaDoc resourceURI = parameters.getParameter("prefix", "") + expression;
99         Source source = null;
100         try {
101             source = resolver.resolveURI(resourceURI);
102             return source.exists();
103         } catch (SourceNotFoundException e) {
104             return false;
105         } catch (Exception JavaDoc e) {
106             getLogger().warn("Exception resolving resource " + resourceURI, e);
107             return false;
108         } finally {
109             if (source != null) {
110                 resolver.release(source);
111             }
112         }
113     }
114 }
115
Popular Tags