KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > forrest > sourceexists > SourceExistsSelector


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation or its licensors,
3  * as applicable.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * 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.forrest.sourceexists;
18
19 import java.net.MalformedURLException JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import java.io.IOException JavaDoc;
23
24 import org.apache.cocoon.selection.Selector;
25
26 import org.apache.avalon.framework.service.ServiceException;
27 import org.apache.avalon.framework.service.ServiceManager;
28 import org.apache.avalon.framework.service.Serviceable;
29
30 import org.apache.avalon.framework.logger.AbstractLogEnabled;
31 import org.apache.avalon.framework.parameters.Parameters;
32 import org.apache.avalon.framework.thread.ThreadSafe;
33
34 import org.apache.excalibur.source.Source;
35 import org.apache.excalibur.source.SourceResolver;
36
37 /**
38  * Selects the first of a set of Sources that exists in the context.
39  * <p>
40  * For example, we could define a SourceExistsSelector with:
41  * <pre>
42  * &lt;map:selector name="exists"
43  * logger="sitemap.selector.source-exists"
44  * SRC="org.apache.cocoon.selection.SourceExistsSelector" />
45  * </pre>
46  * And use it to build a PDF from XSL:FO or a higher-level XML format with:
47  *
48  * <pre>
49  * &lt;map:match pattern="**.pdf">
50  * &lt;map:select type="exists">
51  * &lt;map:when test="context/xdocs/{1}.fo">
52  * &lt;map:generate SRC="content/xdocs/{1}.fo" />
53  * &lt;/map:when>
54  * &lt;map:otherwise>
55  * &lt;map:generate SRC="content/xdocs/{1}.xml" />
56  * &lt;map:transform SRC="stylesheets/document2fo.xsl" />
57  * &lt;/map:otherwise>
58  * &lt;/map:select>
59  * &lt;map:serialize type="fo2pdf" />
60  * </pre>
61  */

62 public class SourceExistsSelector extends AbstractLogEnabled
63   implements ThreadSafe, Selector, Serviceable {
64
65     SourceResolver resolver = null;
66
67     /**
68      * Set the current <code>ComponentManager</code> instance used by this
69      * <code>Composable</code>.
70      */

71     public void service(ServiceManager manager) throws ServiceException {
72         this.resolver = (SourceResolver)manager.lookup(SourceResolver.ROLE);
73     }
74
75     /** Return true if Source 'uri' resolves and exists. */
76     public boolean select(String JavaDoc uri, Map JavaDoc objectModel, Parameters parameters) {
77         Source src = null;
78         try {
79             src = resolver.resolveURI(uri);
80             if (src.exists()) {
81                 return true;
82             } else {
83                 return false;
84             }
85         } catch (MalformedURLException JavaDoc e) {
86             getLogger().warn("Selector URL '"+uri+"' is not a valid Source URL");
87             return false;
88          } catch (IOException JavaDoc e) {
89             getLogger().warn("Error reading from source '"+uri+"': "+e.getMessage());
90             return false;
91         } finally {
92             resolver.release(src);
93         }
94     }
95 }
96
Popular Tags